use of com.enonic.xp.context.Context in project xp by enonic.
the class ContentServiceImplTest_unpublish method unpublish_with_children.
@Test
public void unpublish_with_children() throws Exception {
final Content content = this.contentService.create(CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").parent(ContentPath.ROOT).type(ContentTypeName.folder()).build());
final Content child = this.contentService.create(CreateContentParams.create().contentData(new PropertyTree()).displayName("This is my content").parent(content.getPath()).type(ContentTypeName.folder()).build());
this.contentService.publish(PushContentParams.create().target(ContentConstants.BRANCH_MASTER).contentIds(ContentIds.from(content.getId())).build());
final Context masterContext = ContextBuilder.from(ContextAccessor.current()).branch(ContentConstants.BRANCH_MASTER).build();
assertTrue(masterContext.callWith(() -> contentService.contentExists(content.getId())));
assertTrue(masterContext.callWith(() -> contentService.contentExists(child.getId())));
this.contentService.unpublishContent(UnpublishContentParams.create().contentIds(ContentIds.from(content.getId())).unpublishBranch(ContentConstants.BRANCH_MASTER).build());
assertNotNull(contentService.contentExists(content.getId()));
assertNotNull(contentService.contentExists(child.getId()));
assertFalse(masterContext.callWith(() -> contentService.contentExists(content.getId())));
assertFalse(masterContext.callWith(() -> contentService.contentExists(child.getId())));
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class MoveContentHandler method executeMove.
private Content executeMove() {
final ContentId sourceId;
final ContentPath sourcePath;
if (this.source.startsWith("/")) {
// source is path
sourcePath = ContentPath.from(this.source);
final Content sourceContent = contentService.getByPath(sourcePath);
sourceId = sourceContent.getId();
} else {
// source is key
sourceId = ContentId.from(this.source);
final Content sourceContent = contentService.getById(sourceId);
sourcePath = sourceContent.getPath();
}
if (target.endsWith("/")) {
// /a/b -> /c/d/ => /c/d/b
return move(sourceId, ContentPath.from(target).asAbsolute());
} else if (!target.startsWith("/")) {
// /a/b -> c => /a/c
return rename(sourceId, target);
} else {
// rename+move to target path
final ContentPath targetPath = ContentPath.from(target);
final ContentPath targetParent = targetPath.getParentPath();
if (targetParent.equals(sourcePath.getParentPath())) {
// /a/b -> /a/c => /a/c
return rename(sourceId, targetPath.getName());
}
// /a/b -> /c/d => /c/d
if (contentService.contentExists(targetPath)) {
final Context currentContext = ContextAccessor.current();
throw new ContentAlreadyExistsException(targetPath, currentContext.getRepositoryId(), currentContext.getBranch());
}
// needs to be first renamed to temporary unique name to avoid clashing with siblings with same target name in source parent or with siblings with source name in target parent
rename(sourceId, uniqueName());
move(sourceId, targetParent);
return rename(sourceId, targetPath.getName());
}
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class MultiRepoConnectTest method testExample.
@Test
public void testExample() {
final Context context = ContextBuilder.create().authInfo(AuthenticationInfo.create().user(User.create().key(PrincipalKey.ofUser(IdProviderKey.system(), "test-user")).login("test-user").build()).principals(RoleKeys.ADMIN).build()).build();
context.runWith(() -> runScript("/lib/xp/examples/node/multiRepoConnect.js"));
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class ParentContentSynchronizer method doSyncWithChildren.
private void doSyncWithChildren(final Collection<ContentToSync> sourceContents) {
final Queue<ContentToSync> queue = new ArrayDeque<>(sourceContents);
final Map<NodePath, Context> targetContexts = !sourceContents.isEmpty() ? initContexts(ProjectName.from(sourceContents.stream().findAny().get().getTargetContext().getRepositoryId())) : Map.of();
final List<ContentToSync> contentsToSync = sourceContents.stream().filter(sourceContent -> {
final Content root = sourceContent.getSourceContext().callWith(() -> contentService.getByPath(ContentPath.ROOT));
return !root.getId().equals(sourceContent.getSourceContent().getId());
}).collect(Collectors.toList());
if (!contentsToSync.isEmpty()) {
this.doSync(contentsToSync);
}
while (queue.size() > 0) {
final ContentToSync currentContentToSync = queue.poll();
final FindContentByParentResult result = currentContentToSync.getSourceContext().callWith(() -> contentService.findByParent(FindContentByParentParams.create().parentId(currentContentToSync.getId()).recursive(false).childOrder(currentContentToSync.getSourceContent().getChildOrder()).size(-1).build()));
if (result.getContents().isNotEmpty()) {
final List<ContentToSync> childrenToSync = result.getContents().stream().map(content -> {
final Context actualTargetContext = getActualContext(content.getId(), targetContexts.values());
return ContentToSync.create().sourceContent(content).targetContent(actualTargetContext != null ? actualTargetContext.callWith(() -> contentService.getById(content.getId())) : null).sourceContext(currentContentToSync.getSourceContext()).targetContext(actualTargetContext != null ? actualTargetContext : currentContentToSync.getTargetContext()).build();
}).collect(Collectors.toList());
this.doSync(childrenToSync);
for (final ContentToSync content : childrenToSync) {
if (content.getSourceContent().hasChildren()) {
queue.offer(content);
}
}
}
}
sourceContents.forEach(sourceContent -> {
if (sourceContent.getTargetContent() != null) {
cleanDeletedContents(sourceContent);
return;
}
final Content root = sourceContent.getSourceContext().callWith(() -> contentService.getByPath(ContentPath.ROOT));
if (root.getId().equals(sourceContent.getSourceContent().getId())) {
cleanDeletedContents(ContentToSync.create(sourceContent).targetContent(sourceContent.getTargetContext().callWith(() -> contentService.getByPath(ContentPath.ROOT))).build());
}
});
}
use of com.enonic.xp.context.Context in project xp by enonic.
the class ParentContentSynchronizer method initContexts.
private Map<NodePath, Context> initContexts(final ProjectName projectName) {
final Context contentContext = initContext(projectName, ContentConstants.CONTENT_ROOT_PATH);
final Context archiveContext = initContext(projectName, ArchiveConstants.ARCHIVE_ROOT_PATH);
return Map.of((NodePath) contentContext.getAttribute(CONTENT_ROOT_PATH_ATTRIBUTE), contentContext, (NodePath) archiveContext.getAttribute(CONTENT_ROOT_PATH_ATTRIBUTE), archiveContext);
}
Aggregations