protected virtual void moveFolders() { var fileHandlerGraph = PXGraph.CreateInstance <FileHandler>(); var tokenHandler = PXGraph.CreateInstance <UserTokenHandler>(); PXLongOperation.StartOperation(this, () => { EntityHelper entityHelper = new EntityHelper(this); var screenFolderCache = (BoxFolderCache)fileHandlerGraph.FoldersByScreen.Select(Screens.Current.ScreenID); var list = new List <BoxUtils.FileFolderInfo>(); //For each subfolders of a screen found on box server try { list = BoxUtils.GetFolderList(tokenHandler, screenFolderCache.FolderID, (int)BoxUtils.RecursiveDepth.FirstSubLevel).Result; } catch (AggregateException ae) { ScreenUtils.HandleAggregateException(ae, HttpStatusCode.NotFound, (exception) => { ScreenUtils.TraceAndThrowException(Messages.BoxFolderNotFoundRunSynchAgain, screenFolderCache.FolderID); }); } foreach (var folder in list) { //If folder has a RefNoteID, it contains files and might need to be moved BoxFolderCache bfc = (BoxFolderCache)fileHandlerGraph.FoldersByFolderID.Select(folder.ID); if (bfc != null && bfc.RefNoteID.HasValue) { string presumedParentFolderID = screenFolderCache.FolderID; if (fileHandlerGraph.FieldsGroupingByScreenID.Select(Screens.Current.ScreenID).Any()) { presumedParentFolderID = fileHandlerGraph.GetOrCreateSublevelFolder(tokenHandler, Screens.Current.ScreenID, screenFolderCache.FolderID, bfc.RefNoteID.Value, false); } //if nested under the wrong folder if (presumedParentFolderID != null && folder.ParentFolderID != presumedParentFolderID) { try { BoxUtils.MoveFolder(tokenHandler, folder.ID, presumedParentFolderID).Wait(); bfc.ParentFolderID = presumedParentFolderID; fileHandlerGraph.FoldersByFolderID.Update(bfc); fileHandlerGraph.Actions.PressSave(); } catch (AggregateException ae) { ScreenUtils.HandleAggregateException(ae, HttpStatusCode.Conflict, (exception) => { //Skip this folder if a name conflict happens }); continue; } } } } }); }
private void SynchronizeFolderContentsWithScreen(string screenID, string folderID, bool isForcingSync) { // Retrieve top-level folder list var tokenHandler = PXGraph.CreateInstance <UserTokenHandler>(); List <BoxUtils.FileFolderInfo> boxFolderList = new List <BoxUtils.FileFolderInfo>(); if (FieldsGroupingByScreenID.Select(screenID).Any()) { boxFolderList = BoxUtils.GetFolderList(tokenHandler, folderID, (int)BoxUtils.RecursiveDepth.FirstSubLevel).Result; boxFolderList = boxFolderList.Where(x => x.Name.Contains("\\")).ToList(); } else { boxFolderList = BoxUtils.GetFolderList(tokenHandler, folderID, (int)BoxUtils.RecursiveDepth.NoDepth).Result; } foreach (BoxUtils.FileFolderInfo boxFolderInfo in boxFolderList) { BoxFolderCache bfc = FoldersByFolderID.Select(boxFolderInfo.ID); if (bfc != null) { //Make sure NoteDoc still exists NoteDoc nd = NoteDocs.Select(bfc.RefNoteID); if (nd == null) { //NoteDoc can't be found; this is likely a file coming from a record that was deleted. //Since an existing folder may be renamed to something which exists, we want to force system to treat it as if it was a new folder FoldersByFolderID.Delete(bfc); bfc = null; } } if (bfc == null) { // We've never seen this folder; sync it Guid?refNoteID; if (screenID == ActivityMaintScreenId) { //Activity folders use custom naming defined in GetFolderNameForActivityRow() - just look for GUID inside it. refNoteID = ExtractGuidFromString(boxFolderInfo.Name); } else { refNoteID = FindMatchingNoteIDForFolder(screenID, boxFolderInfo.Name); } if (refNoteID == null) { // User may have created some folder manually with a name not matching to any record, or record // may have been deleted in Acumatica. We can safely ignore it, but let's write to trace. PXTrace.WriteWarning(string.Format("No record found for folder {0} (screen {1}, ID {2})", boxFolderInfo.Name, screenID, boxFolderInfo.ID)); continue; } bfc = (BoxFolderCache)FoldersByNote.Select(refNoteID); if (bfc != null) { // A folder existed before for this record; clear the previous entry for this refNoteID FoldersByNote.Delete(bfc); } // Store folder in cache for future reference bfc = (BoxFolderCache)FoldersByFolderID.Cache.CreateInstance(); bfc.FolderID = boxFolderInfo.ID; bfc.ParentFolderID = boxFolderInfo.ParentFolderID; bfc.RefNoteID = refNoteID; bfc.LastModifiedDateTime = null; //To force initial sync bfc = FoldersByFolderID.Insert(bfc); } if (isForcingSync || bfc.LastModifiedDateTime != boxFolderInfo.ModifiedAt) { RefreshRecordFileList(screenID, boxFolderInfo.Name, boxFolderInfo.ID, bfc.RefNoteID, isForcingSync); bfc.LastModifiedDateTime = boxFolderInfo.ModifiedAt; FoldersByFolderID.Update(bfc); PXContext.SetSlot <bool>("BoxDisableLoad", true); try { Actions.PressSave(); } finally { PXContext.SetSlot <bool>("BoxDisableLoad", false); } } } }