Search in sources :

Example 91 with Context

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())));
}
Also used : Context(com.enonic.xp.context.Context) Content(com.enonic.xp.content.Content) PropertyTree(com.enonic.xp.data.PropertyTree) Test(org.junit.jupiter.api.Test)

Example 92 with Context

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());
    }
}
Also used : Context(com.enonic.xp.context.Context) Content(com.enonic.xp.content.Content) ContentAlreadyExistsException(com.enonic.xp.content.ContentAlreadyExistsException) ContentId(com.enonic.xp.content.ContentId) ContentPath(com.enonic.xp.content.ContentPath)

Example 93 with Context

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"));
}
Also used : Context(com.enonic.xp.context.Context) Test(org.junit.jupiter.api.Test)

Example 94 with Context

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());
        }
    });
}
Also used : Context(com.enonic.xp.context.Context) ContentService(com.enonic.xp.content.ContentService) ContentConstants(com.enonic.xp.content.ContentConstants) FindContentByParentResult(com.enonic.xp.content.FindContentByParentResult) CONTENT_ROOT_PATH_ATTRIBUTE(com.enonic.xp.content.ContentConstants.CONTENT_ROOT_PATH_ATTRIBUTE) Function(java.util.function.Function) LinkedHashMap(java.util.LinkedHashMap) ContentId(com.enonic.xp.content.ContentId) Component(org.osgi.service.component.annotations.Component) FindContentByParentParams(com.enonic.xp.content.FindContentByParentParams) ContextAccessor(com.enonic.xp.context.ContextAccessor) MediaInfoService(com.enonic.xp.media.MediaInfoService) Map(java.util.Map) ProjectName(com.enonic.xp.project.ProjectName) Activate(org.osgi.service.component.annotations.Activate) ContextBuilder(com.enonic.xp.context.ContextBuilder) User(com.enonic.xp.security.User) ContentPath(com.enonic.xp.content.ContentPath) ArchiveConstants(com.enonic.xp.archive.ArchiveConstants) Collection(java.util.Collection) NodePath(com.enonic.xp.node.NodePath) Set(java.util.Set) Content(com.enonic.xp.content.Content) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) PrincipalKey(com.enonic.xp.security.PrincipalKey) BRANCH_DRAFT(com.enonic.xp.content.ContentConstants.BRANCH_DRAFT) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Queue(java.util.Queue) ArrayDeque(java.util.ArrayDeque) Reference(org.osgi.service.component.annotations.Reference) FindContentByParentResult(com.enonic.xp.content.FindContentByParentResult) Content(com.enonic.xp.content.Content) ArrayDeque(java.util.ArrayDeque) NodePath(com.enonic.xp.node.NodePath)

Example 95 with Context

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);
}
Also used : Context(com.enonic.xp.context.Context)

Aggregations

Context (com.enonic.xp.context.Context)101 Test (org.junit.jupiter.api.Test)35 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)21 Node (com.enonic.xp.node.Node)16 InternalContext (com.enonic.xp.repo.impl.InternalContext)16 User (com.enonic.xp.security.User)13 Content (com.enonic.xp.content.Content)11 PropertyTree (com.enonic.xp.data.PropertyTree)11 PrincipalKey (com.enonic.xp.security.PrincipalKey)10 Branch (com.enonic.xp.branch.Branch)9 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)9 ContextAccessor (com.enonic.xp.context.ContextAccessor)8 NodePath (com.enonic.xp.node.NodePath)8 ContextBuilder (com.enonic.xp.context.ContextBuilder)7 ContentId (com.enonic.xp.content.ContentId)6 CreateNodeParams (com.enonic.xp.node.CreateNodeParams)6 Repository (com.enonic.xp.repository.Repository)6 LogAuditLogParams (com.enonic.xp.audit.LogAuditLogParams)5 ProcessUpdateParams (com.enonic.xp.content.processor.ProcessUpdateParams)5 NodeComparison (com.enonic.xp.node.NodeComparison)5