use of com.day.cq.replication.Replicator in project acs-aem-commons by Adobe-Consulting-Services.
the class PageRelocatorTest method getPageRelocatorTool.
private PageRelocator getPageRelocatorTool() {
PageManagerFactory mockPageManagerFactory = mock(PageManagerFactory.class);
when(mockPageManagerFactory.getPageManager(any())).then(invocation -> new MockPageManager(getMockResolver()));
slingContext.registerService(PageManagerFactory.class, mockPageManagerFactory);
Replicator replicator = mock(Replicator.class);
slingContext.registerService(Replicator.class, replicator);
PageRelocatorFactory t = new PageRelocatorFactory();
slingContext.registerInjectActivateService(t);
return t.createProcessDefinition();
}
use of com.day.cq.replication.Replicator in project acs-aem-commons by Adobe-Consulting-Services.
the class PageRelocator method movePage.
@SuppressWarnings("squid:S00112")
private void movePage(ResourceResolver rr, String sourcePage) throws Exception {
PageManager manager = pageManagerFactory.getPageManager(rr);
Field replicatorField = FieldUtils.getDeclaredField(manager.getClass(), "replicator", true);
FieldUtils.writeField(replicatorField, manager, replicatorQueue);
String destination = convertSourceToDestination(sourcePage);
String destinationParent = destination.substring(0, destination.lastIndexOf('/'));
note(sourcePage, Report.target, destination);
String beforeName = "";
final long start = System.currentTimeMillis();
String contentPath = sourcePage + "/jcr:content";
List<String> refs = new ArrayList<>();
List<String> publishRefs = new ArrayList<>();
if (maxReferences != 0 && resourceExists(rr, contentPath)) {
ReferenceSearch refSearch = new ReferenceSearch();
refSearch.setExact(true);
refSearch.setHollow(true);
refSearch.setMaxReferencesPerPage(maxReferences);
refSearch.setSearchRoot(referenceSearchRoot);
refSearch.search(rr, sourcePath).values().stream().peek(p -> refs.add(p.getPagePath())).filter(p -> isActivated(rr, p.getPagePath())).map(ReferenceSearch.Info::getPagePath).collect(Collectors.toCollection(() -> publishRefs));
}
note(sourcePage, Report.all_references, refs.size());
note(sourcePage, Report.published_references, publishRefs.size());
if (!dryRun) {
Actions.retry(10, 500, res -> {
waitUntilResourceFound(res, destinationParent);
Resource source = rr.getResource(sourcePage);
if (resourceExists(res, contentPath)) {
manager.move(source, destination, beforeName, true, true, listToStringArray(refs), listToStringArray(publishRefs));
} else {
Map<String, Object> props = new HashMap<>();
Resource parent = res.getResource(destinationParent);
res.create(parent, source.getName(), source.getValueMap());
}
res.commit();
res.refresh();
source = rr.getResource(sourcePage);
if (source != null && source.hasChildren()) {
for (Resource child : source.getChildren()) {
res.move(child.getPath(), destination);
}
res.commit();
}
}).accept(rr);
}
long end = System.currentTimeMillis();
note(sourcePage, Report.move_time, end - start);
}
Aggregations