public Guid Save(byte[] data, PXBlobStorageContext saveContext)
        {
            FileHandler graph       = PXGraph.CreateInstance <FileHandler>();
            Guid        blobHandler = graph.SaveFileToBoxAndUpdateFileCache(data, saveContext);

            return(blobHandler);
        }
        private void CheckForMissingNoteRecord(PXBlobStorageContext saveContext)
        {
            EntityHelper entityHelper = new EntityHelper(this);
            Note         note         = entityHelper.SelectNote(saveContext.NoteID);

            if (note == null)
            {
                PXNoteAttribute.InsertNoteRecord(saveContext.Graph.Views[saveContext.ViewName].Cache, saveContext.NoteID.Value);
                this.Caches[typeof(Note)].ClearQueryCache();
            }
        }
        public Guid SaveFileToBoxAndUpdateFileCache(byte[] data, PXBlobStorageContext saveContext)
        {
            BoxUtils.FileFolderInfo boxFile = null;
            Guid blobHandlerGuid            = Guid.NewGuid();

            var tokenHandler = PXGraph.CreateInstance <UserTokenHandler>();

            if (saveContext == null || saveContext.FileInfo == null || !saveContext.NoteID.HasValue)
            {
                var fileName = string.Empty;
                if (saveContext?.FileInfo?.Name == null)
                {
                    fileName = blobHandlerGuid.ToString();
                }
                else
                {
                    fileName = BoxUtils.CleanFileOrFolderName(saveContext.FileInfo.Name);
                    fileName = $"{Path.GetFileNameWithoutExtension(fileName)} ({blobHandlerGuid.ToString()}){Path.GetExtension(fileName)}";
                }

                //We don't know on which screen this file belongs. We'll have to save it in miscellaneous files folder.
                BoxUtils.FileFolderInfo boxFolder = GetMiscellaneousFolder();
                boxFile = BoxUtils.UploadFile(tokenHandler, boxFolder.ID, fileName, data).Result;
            }
            else
            {
                CheckForMissingNoteRecord(saveContext);
                var    fileName    = BoxUtils.CleanFileOrFolderName(Path.GetFileName(saveContext.FileInfo.Name));
                string boxFolderID = GetOrCreateBoxFolderForNoteID(saveContext.NoteID.Value);
                try
                {
                    boxFile = BoxUtils.UploadFile(tokenHandler, boxFolderID, fileName, data).Result;
                }
                catch (AggregateException ae)
                {
                    ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, exception =>
                    {
                        using (new PXConnectionScope())
                        {
                            BoxFolderCache folderCacheToDelete = FoldersByFolderID.Cache.CreateInstance() as BoxFolderCache;
                            folderCacheToDelete.FolderID       = boxFolderID;
                            var deletedFolderInfo = FoldersByFolderID.Delete(folderCacheToDelete);
                            Actions.PressSave();
                        }

                        ScreenUtils.TraceAndThrowException(Messages.BoxFolderNotFoundRunSynchAgain, boxFolderID);
                    });
                }

                if (!string.IsNullOrEmpty(saveContext.FileInfo.Comment))
                {
                    BoxUtils.SetFileDescription(tokenHandler, boxFile.ID, saveContext.FileInfo.Comment).Wait();
                }
            }

            var bfc = (BoxFileCache)FilesByBlobHandler.Cache.CreateInstance();

            bfc.BlobHandler    = blobHandlerGuid;
            bfc.FileID         = boxFile.ID;
            bfc.ParentFolderID = boxFile.ParentFolderID;
            bfc = FilesByBlobHandler.Insert(bfc);
            Actions.PressSave();

            return(blobHandlerGuid);
        }