@Test void updateParentFields(MockitoComponentManager componentManager) throws Exception { DocumentReference oldParentReference = new DocumentReference("wiki", "Space", "Old"); DocumentReference newParentReference = new DocumentReference("wiki", "Space", "New"); XWikiDocument oldParentDocument = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(oldParentReference, this.xcontext)).thenReturn(oldParentDocument); DocumentReference child1Reference = new DocumentReference("wiki", "Space", "Child1"); DocumentReference child2Reference = new DocumentReference("wiki", "Space", "Child2"); when(oldParentDocument.getChildrenReferences(this.xcontext)) .thenReturn(Arrays.asList(child1Reference, child2Reference)); JobProgressManager mockProgressManager = componentManager.getInstance(JobProgressManager.class); XWikiDocument child1Document = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(child1Reference, this.xcontext)).thenReturn(child1Document); XWikiDocument child2Document = mock(XWikiDocument.class); when(this.xcontext.getWiki().getDocument(child2Reference, this.xcontext)).thenReturn(child2Document); this.modelBridge.updateParentField(oldParentReference, newParentReference); verify(mockProgressManager).pushLevelProgress(2, this.modelBridge); verify(child1Document).setParentReference(newParentReference); verify(this.xcontext.getWiki()).saveDocument(child1Document, "Updated parent field.", true, this.xcontext); verify(child2Document).setParentReference(newParentReference); verify(this.xcontext.getWiki()).saveDocument(child1Document, "Updated parent field.", true, this.xcontext); assertLog(Level.INFO, "Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, 2); }
@Override public boolean updateParentField(final DocumentReference oldParentReference, final DocumentReference newParentReference) { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); boolean popLevelProgress = false; try { XWikiDocument oldParentDocument = wiki.getDocument(oldParentReference, context); List<DocumentReference> childReferences = oldParentDocument.getChildrenReferences(context); if (childReferences.size() > 0) { this.progressManager.pushLevelProgress(childReferences.size(), this); popLevelProgress = true; } for (DocumentReference childReference : childReferences) { this.progressManager.startStep(this); XWikiDocument childDocument = wiki.getDocument(childReference, context); childDocument.setParentReference(newParentReference); wiki.saveDocument(childDocument, "Updated parent field.", true, context); this.progressManager.endStep(this); } if (childReferences.size() > 0) { this.logger.info("Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, childReferences.size()); } } catch (Exception e) { this.logger.error("Failed to update the document parent fields from [{}] to [{}].", oldParentReference, newParentReference, e); return false; } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } } return true; }
DefaultModelBridge implements ModelBridge { @Override public boolean updateParentField(final DocumentReference oldParentReference, final DocumentReference newParentReference) { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); boolean popLevelProgress = false; try { XWikiDocument oldParentDocument = wiki.getDocument(oldParentReference, context); List<DocumentReference> childReferences = oldParentDocument.getChildrenReferences(context); if (childReferences.size() > 0) { this.progressManager.pushLevelProgress(childReferences.size(), this); popLevelProgress = true; } for (DocumentReference childReference : childReferences) { this.progressManager.startStep(this); XWikiDocument childDocument = wiki.getDocument(childReference, context); childDocument.setParentReference(newParentReference); wiki.saveDocument(childDocument, "Updated parent field.", true, context); this.progressManager.endStep(this); } if (childReferences.size() > 0) { this.logger.info("Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, childReferences.size()); } } catch (Exception e) { this.logger.error("Failed to update the document parent fields from [{}] to [{}].", oldParentReference, newParentReference, e); return false; } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } } return true; } }
DefaultModelBridge implements ModelBridge { @Override public boolean updateParentField(final DocumentReference oldParentReference, final DocumentReference newParentReference) { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); boolean popLevelProgress = false; try { XWikiDocument oldParentDocument = wiki.getDocument(oldParentReference, context); List<DocumentReference> childReferences = oldParentDocument.getChildrenReferences(context); if (childReferences.size() > 0) { this.progressManager.pushLevelProgress(childReferences.size(), this); popLevelProgress = true; } for (DocumentReference childReference : childReferences) { this.progressManager.startStep(this); XWikiDocument childDocument = wiki.getDocument(childReference, context); childDocument.setParentReference(newParentReference); wiki.saveDocument(childDocument, "Updated parent field.", true, context); this.progressManager.endStep(this); } if (childReferences.size() > 0) { this.logger.info("Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, childReferences.size()); } } catch (Exception e) { this.logger.error("Failed to update the document parent fields from [{}] to [{}].", oldParentReference, newParentReference, e); return false; } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } } return true; }  }
DefaultModelBridge implements ModelBridge { @Override public boolean updateParentField(final DocumentReference oldParentReference, final DocumentReference newParentReference) { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); boolean popLevelProgress = false; try { XWikiDocument oldParentDocument = wiki.getDocument(oldParentReference, context); List<DocumentReference> childReferences = oldParentDocument.getChildrenReferences(context); if (childReferences.size() > 0) { this.progressManager.pushLevelProgress(childReferences.size(), this); popLevelProgress = true; } for (DocumentReference childReference : childReferences) { this.progressManager.startStep(this); XWikiDocument childDocument = wiki.getDocument(childReference, context); childDocument.setParentReference(newParentReference); wiki.saveDocument(childDocument, "Updated parent field.", true, context); this.progressManager.endStep(this); } if (childReferences.size() > 0) { this.logger.info("Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, childReferences.size()); } } catch (Exception e) { this.logger.error("Failed to update the document parent fields from [{}] to [{}].", oldParentReference, newParentReference, e); return false; } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } } return true; }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination); }
DefaultModelBridge implements ModelBridge { @Override public boolean updateParentField(final DocumentReference oldParentReference, final DocumentReference newParentReference) { XWikiContext context = xcontextProvider.get(); XWiki wiki = context.getWiki(); boolean popLevelProgress = false; try { XWikiDocument oldParentDocument = wiki.getDocument(oldParentReference, context); List<DocumentReference> childReferences = oldParentDocument.getChildrenReferences(context); if (childReferences.size() > 0) { this.progressManager.pushLevelProgress(childReferences.size(), this); popLevelProgress = true; } for (DocumentReference childReference : childReferences) { this.progressManager.startStep(this); XWikiDocument childDocument = wiki.getDocument(childReference, context); childDocument.setParentReference(newParentReference); wiki.saveDocument(childDocument, "Updated parent field.", true, context); this.progressManager.endStep(this); } if (childReferences.size() > 0) { this.logger.info("Document parent fields updated from [{}] to [{}] for [{}] documents.", oldParentReference, newParentReference, childReferences.size()); } } catch (Exception e) { this.logger.error("Failed to update the document parent fields from [{}] to [{}].", oldParentReference, newParentReference, e); return false; } finally { if (popLevelProgress) { this.progressManager.popLevelProgress(this); } } return true; }  @Override boolean create(DocumentReference documentReference); @Override boolean copy(DocumentReference source, DocumentReference destination); @Override boolean delete(DocumentReference reference); @Override boolean delete(DocumentReference reference, boolean skipRecycleBin); @Override boolean removeLock(DocumentReference reference); @Override void createRedirect(DocumentReference oldReference, DocumentReference newReference); @Override boolean canOverwriteSilently(DocumentReference documentReference); @Override boolean exists(DocumentReference reference); @Override List<DocumentReference> getBackLinkedReferences(DocumentReference documentReference, String wikiId); @Override List<DocumentReference> getDocumentReferences(SpaceReference spaceReference); @Override boolean updateParentField(final DocumentReference oldParentReference,
        final DocumentReference newParentReference); @Override DocumentReference setContextUserReference(DocumentReference userReference); @Override void update(DocumentReference documentReference, Map<String, String> parameters); @Override boolean restoreDeletedDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override List<Long> getDeletedDocumentIds(String batchId); @Override boolean permanentlyDeleteDocument(long deletedDocumentId, AbstractCheckRightsRequest request); @Override boolean permanentlyDeleteAllDocuments(PermanentlyDeleteJob deleteJob, AbstractCheckRightsRequest request); @Override boolean rename(DocumentReference source, DocumentReference destination);  }