public BoxUtils.FileFolderInfo GetMiscellaneousFolder()
        {
            var bfc = (BoxFolderCache)FoldersByScreen.Select(MiscellaneousFolderScreenId);

            if (bfc == null)
            {
                throw new PXException(Messages.MiscFolderNotFoundRunSynchAgain);
            }
            else
            {
                try
                {
                    // Folder was found in BoxFolderCache, retrieve it by ID
                    var tokenHandler = PXGraph.CreateInstance <UserTokenHandler>();
                    BoxUtils.FileFolderInfo folderInfo = BoxUtils.GetFolderInfo(tokenHandler, bfc.FolderID).Result;
                    return(folderInfo);
                }
                catch (AggregateException ae)
                {
                    ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, (exception) =>
                    {
                        using (new PXConnectionScope())
                        {
                            // Delete entry from BoxFolderCache so that it gets created again.
                            FoldersByScreen.Delete(bfc);
                            Actions.PressSave();
                        }

                        throw new PXException(Messages.MiscFolderNotFoundRunSynchAgain, bfc.FolderID, exception);
                    });

                    return(null);
                }
            }
        }
        public void SynchronizeScreen(Screen screen, BoxUtils.FileFolderInfo rootFolder, bool forceSync)
        {
            string folderName = string.Format("{0} ({1})", (object)BoxUtils.CleanFileOrFolderName(screen.Name), (object)screen.ScreenID);

            BoxFolderCache screenFolderInfo = FoldersByScreen.Select(screen.ScreenID);

            BoxUtils.FileFolderInfo folderInfo = null;
            var tokenHandler = PXGraph.CreateInstance <UserTokenHandler>();

            if (screenFolderInfo != null)
            {
                try
                {
                    folderInfo = BoxUtils.GetFolderInfo(tokenHandler, screenFolderInfo.FolderID).Result;
                }
                catch (AggregateException ae)
                {
                    ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, (exception) =>
                    {
                        // Folder no longer exist on Box - it may have been deleted on purpose by the user. Remove it from cache so it is recreated on the next run.
                        screenFolderInfo = FoldersByScreen.Delete(screenFolderInfo);
                        Actions.PressSave();
                        throw new PXException(Messages.BoxFolderNotFoundRunSynchAgain, screenFolderInfo.ScreenID, exception);
                    });
                }
            }

            if (folderInfo == null)
            {
                // Folder wasn't found, try finding it by name in the root folder.
                folderInfo = BoxUtils.FindFolder(tokenHandler, rootFolder.ID, folderName).Result;
            }

            if (folderInfo == null)
            {
                // Folder doesn't exist at all - create it
                folderInfo = BoxUtils.CreateFolder(tokenHandler, folderName, rootFolder.ID).Result;
            }

            if (screenFolderInfo == null)
            {
                screenFolderInfo                      = (BoxFolderCache)FoldersByScreen.Cache.CreateInstance();
                screenFolderInfo.FolderID             = folderInfo.ID;
                screenFolderInfo.ParentFolderID       = folderInfo.ParentFolderID;
                screenFolderInfo.ScreenID             = screen.ScreenID;
                screenFolderInfo.LastModifiedDateTime = null; //To force initial sync
                screenFolderInfo                      = FoldersByScreen.Insert(screenFolderInfo);
                Actions.PressSave();
            }

            // We don't synchronize the miscellaneous files folder, since we can't easily identify the corresponding NoteID from folder
            if (screen.ScreenID != FileHandler.MiscellaneousFolderScreenId && (forceSync || screenFolderInfo.LastModifiedDateTime != folderInfo.ModifiedAt))
            {
                SynchronizeFolderContentsWithScreen(screenFolderInfo.ScreenID, screenFolderInfo.FolderID, forceSync);
                screenFolderInfo.LastModifiedDateTime = folderInfo.ModifiedAt;
                FoldersByScreen.Update(screenFolderInfo);
                Actions.PressSave();
            }
        }
        public string GetOrCreateSublevelFolder(UserTokenHandler tokenHandler, string screenID, string parentFolderID, Guid?refNoteID, bool throwOnError)
        {
            BoxFolderSublevelCache sublevelFolder;
            var subLevelGrouping = GetSublevelName(refNoteID);

            if (string.IsNullOrEmpty(subLevelGrouping))
            {
                if (throwOnError)
                {
                    ScreenUtils.TraceAndThrowException(Messages.SubLevelConfigurationInvalid);
                }
                else
                {
                    PXTrace.WriteWarning(string.Format("No sublevel grouping found for note ID {0} (screen {1}, parent folder {2})", refNoteID, screenID, parentFolderID));
                    return(null);
                }
            }

            sublevelFolder = (BoxFolderSublevelCache)SubLevelByScreenAndGrouping.Select(screenID, subLevelGrouping);
            if (sublevelFolder == null)
            {
                var subLevelFolderInfo = GetOrCreateFolder(tokenHandler, parentFolderID, null, null, subLevelGrouping);

                sublevelFolder          = (BoxFolderSublevelCache)SubLevelByScreenAndGrouping.Cache.CreateInstance();
                sublevelFolder.FolderID = subLevelFolderInfo.ID;
                sublevelFolder.Grouping = subLevelGrouping;
                sublevelFolder.ScreenID = screenID;
                SubLevelByScreenAndGrouping.Insert(sublevelFolder);
            }
            else
            {
                try
                {
                    // Folder was found in BoxFolderCache, retrieve it by ID
                    var sublevelFolderInfo = BoxUtils.GetFolderInfo(tokenHandler, sublevelFolder.FolderID).Result;
                }
                catch (AggregateException ae)
                {
                    ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, (exception) =>
                    {
                        using (new PXConnectionScope())
                        {
                            // Delete entry from BoxFolderCache so that it gets created again.
                            SubLevelByScreenAndGrouping.Delete(sublevelFolder);
                            Actions.PressSave();
                        }

                        throw new PXException(Messages.BoxFolderNotFoundTryAgain, sublevelFolder.FolderID, exception);
                    });
                }
            }

            return(sublevelFolder.FolderID);
        }
        private BoxUtils.FileFolderInfo GetOrCreateActivityFolder(Guid?refNoteID, UserTokenHandler tokenHandler, object entityRow, object activityRefNoteID)
        {
            //Save an activity related file into the record's activity folder
            var activityRefNoteGuid = Guid.Parse(activityRefNoteID.ToString());

            //Get or create record folder
            string         folderID          = GetOrCreateBoxFolderForNoteID(activityRefNoteGuid);
            BoxFolderCache recordFolderCache = FoldersByFolderID.Select(folderID);

            //Get or create Activities folder
            BoxUtils.FileFolderInfo activityFolderInfo = null;
            if (string.IsNullOrEmpty(recordFolderCache.ActivityFolderID))
            {
                // Create Activities folder and update cache for future reference.
                activityFolderInfo = GetOrCreateFolder(tokenHandler, recordFolderCache.FolderID, null, null, Messages.ActivitiesFolderName);
                recordFolderCache.ActivityFolderID = activityFolderInfo.ID;
                FoldersByFolderID.Update(recordFolderCache);
                Actions.PressSave();
            }
            else
            {
                try
                {
                    // Folder was found in BoxFolderCache, retrieve it by ID
                    activityFolderInfo = BoxUtils.GetFolderInfo(tokenHandler, recordFolderCache.ActivityFolderID).Result;
                }
                catch (AggregateException ae)
                {
                    ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, (exception) =>
                    {
                        using (new PXConnectionScope())
                        {
                            // Delete entry from BoxFolderCache so that it gets created again.
                            recordFolderCache.ActivityFolderID = null;
                            FoldersByNote.Update(recordFolderCache);
                            Actions.PressSave();
                        }

                        throw new PXException(Messages.BoxFolderNotFoundTryAgain, recordFolderCache.FolderID, exception);
                    });
                }
            }

            //Get/Create activityRecord folder
            string folderName = GetFolderNameForActivityRow(entityRow);

            return(GetOrCreateFolder(tokenHandler, activityFolderInfo.ID, entityRow, refNoteID, folderName));
        }