Search in sources :

Example 11 with Project

use of com.enonic.xp.project.Project in project xp by enonic.

the class ProjectContentEventListener method doHandleContentEvent.

private void doHandleContentEvent(final List<Map<String, String>> nodes, final String type) {
    createAdminContext().runWith(() -> {
        final Branch branch = Branch.from(nodes.stream().findAny().orElseThrow(() -> new IllegalArgumentException("nodes cannot be empty")).get("branch"));
        if (!ContentConstants.BRANCH_DRAFT.equals(branch)) {
            return;
        }
        final List<ContentId> contentIds = nodes.stream().map(map -> ContentId.from(map.get("id"))).collect(Collectors.toList());
        final List<ProjectName> projectNames = nodes.stream().map(map -> map.get("repo")).distinct().map(repo -> ProjectName.from(RepositoryId.from(repo))).collect(Collectors.toList());
        if (projectNames.size() != 1) {
            throw new IllegalArgumentException(projectNames.size() > 1 ? "An event cannot contain nodes from different repositories" : "An event must contain 'repo' property");
        }
        final ProjectName currentProjectName = projectNames.get(0);
        final Project sourceProject = this.projectService.list().stream().filter(project -> currentProjectName.equals(project.getName())).findAny().orElseThrow(() -> new ProjectNotFoundException(currentProjectName));
        this.projectService.list().stream().filter(project -> currentProjectName.equals(project.getParent())).forEach(targetProject -> {
            final ContentEventsSyncParams.Builder paramsBuilder = ContentEventsSyncParams.create().addContentIds(contentIds).sourceProject(sourceProject.getName()).targetProject(targetProject.getName());
            switch(type) {
                case "node.created":
                case "node.duplicated":
                    paramsBuilder.syncEventType(ContentSyncEventType.CREATED);
                    break;
                case "node.updated":
                case "node.pushed":
                    paramsBuilder.syncEventType(ContentSyncEventType.UPDATED);
                    break;
                case "node.manualOrderUpdated":
                    paramsBuilder.syncEventType(ContentSyncEventType.MANUAL_ORDER_UPDATED);
                    break;
                case "node.sorted":
                    paramsBuilder.syncEventType(ContentSyncEventType.SORTED);
                    break;
                case "node.renamed":
                    paramsBuilder.syncEventType(ContentSyncEventType.RENAMED);
                    break;
                case "node.moved":
                    paramsBuilder.syncEventType(ContentSyncEventType.MOVED);
                    break;
                case "node.deleted":
                    paramsBuilder.syncEventType(ContentSyncEventType.DELETED);
                    break;
                default:
                    LOG.debug("Ignoring node type: {}", type);
                    break;
            }
            final ContentEventsSyncParams params = paramsBuilder.build();
            if (params.getSyncType() != null) {
                contentSynchronizer.sync(params);
            }
        });
        if (sourceProject.getParent() != null && "node.deleted".equals(type)) {
            this.projectService.list().stream().filter(project -> project.getName().equals(sourceProject.getParent())).forEach(parentProject -> contentSynchronizer.sync(ContentSyncParams.create().addContentIds(contentIds).sourceProject(parentProject.getName()).targetProject(sourceProject.getName()).build()));
        }
    });
}
Also used : EventListener(com.enonic.xp.event.EventListener) ContentConstants(com.enonic.xp.content.ContentConstants) LoggerFactory(org.slf4j.LoggerFactory) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId) Component(org.osgi.service.component.annotations.Component) RepositoryId(com.enonic.xp.repository.RepositoryId) ContextAccessor(com.enonic.xp.context.ContextAccessor) Duration(java.time.Duration) Map(java.util.Map) ProjectName(com.enonic.xp.project.ProjectName) Activate(org.osgi.service.component.annotations.Activate) ContextBuilder(com.enonic.xp.context.ContextBuilder) Event(com.enonic.xp.event.Event) User(com.enonic.xp.security.User) ProjectService(com.enonic.xp.project.ProjectService) Logger(org.slf4j.Logger) Deactivate(org.osgi.service.component.annotations.Deactivate) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) Project(com.enonic.xp.project.Project) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) List(java.util.List) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) PrincipalKey(com.enonic.xp.security.PrincipalKey) SimpleExecutor(com.enonic.xp.core.internal.concurrent.SimpleExecutor) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Reference(org.osgi.service.component.annotations.Reference) Project(com.enonic.xp.project.Project) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) ProjectName(com.enonic.xp.project.ProjectName) Branch(com.enonic.xp.branch.Branch) ContentId(com.enonic.xp.content.ContentId)

Example 12 with Project

use of com.enonic.xp.project.Project in project xp by enonic.

the class ProjectEventListener method handleProjectCreated.

private void handleProjectCreated(final ProjectName projectName) {
    final Project project = this.projectService.get(projectName);
    if (project != null && project.getParent() != null) {
        final Project parentProject = this.projectService.get(project.getParent());
        if (parentProject != null) {
            final ContentSyncTask syncTask = ContentSyncTask.create().sourceProject(parentProject.getName()).targetProject(project.getName()).contentSynchronizer(contentSynchronizer).build();
            taskService.submitTask(syncTask, String.format("sync [%s] project from parent [%s]", project.getName(), parentProject.getName()));
        }
    }
}
Also used : Project(com.enonic.xp.project.Project)

Example 13 with Project

use of com.enonic.xp.project.Project in project xp by enonic.

the class ProjectServiceImplTest method graph.

@Test
void graph() {
    final Project project1 = adminContext().callWith(() -> doCreateProject(ProjectName.from("project1"), null, true, null));
    final Project project2 = adminContext().callWith(() -> doCreateProject(ProjectName.from("project2"), null, true, project1.getName()));
    final Project project3 = adminContext().callWith(() -> doCreateProject(ProjectName.from("project3"), null, true, project2.getName()));
    final Project project4 = adminContext().callWith(() -> doCreateProject(ProjectName.from("project4"), null, true, project2.getName()));
    final Project project5 = adminContext().callWith(() -> doCreateProject(ProjectName.from("project5"), null, true, project4.getName()));
    final ProjectGraph graph1 = adminContext().callWith(() -> projectService.graph(project1.getName()));
    assertEquals(5, graph1.getSize());
    assertThat(graph1.getList()).extracting("name", "parent").containsExactly(tuple(project1.getName(), null), tuple(project2.getName(), project1.getName()), tuple(project4.getName(), project2.getName()), tuple(project3.getName(), project2.getName()), tuple(project5.getName(), project4.getName()));
    final ProjectGraph graph2 = adminContext().callWith(() -> projectService.graph(project4.getName()));
    assertEquals(4, graph2.getSize());
    assertThat(graph2.getList()).extracting("name", "parent").containsExactly(tuple(project1.getName(), null), tuple(project2.getName(), project1.getName()), tuple(project4.getName(), project2.getName()), tuple(project5.getName(), project4.getName()));
}
Also used : Project(com.enonic.xp.project.Project) ProjectGraph(com.enonic.xp.project.ProjectGraph) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 14 with Project

use of com.enonic.xp.project.Project in project xp by enonic.

the class ProjectServiceImplTest method create_in_non_master_node.

@Test
void create_in_non_master_node() {
    IndexServiceInternal indexServiceInternalMock = Mockito.mock(IndexServiceInternal.class);
    when(indexServiceInternalMock.waitForYellowStatus()).thenReturn(true);
    indexService.setIndexServiceInternal(indexServiceInternalMock);
    final RepositoryId projectRepoId = RepositoryId.from("com.enonic.cms.test-project");
    final Project project = adminContext().callWith(() -> doCreateProject(ProjectName.from(projectRepoId), null, true, null));
    assertNotNull(project);
    assertEquals("test-project", project.getName().toString());
    final NodeBranchEntry nodeBranchEntry = this.branchService.get(Node.ROOT_UUID, InternalContext.create(adminContext()).repositoryId(projectRepoId).build());
    assertNotNull(nodeBranchEntry);
    adminContext().runWith(() -> {
        final Repository pro = repositoryService.get(projectRepoId);
        assertNotNull(pro);
    });
}
Also used : Project(com.enonic.xp.project.Project) Repository(com.enonic.xp.repository.Repository) IndexServiceInternal(com.enonic.xp.repo.impl.index.IndexServiceInternal) NodeBranchEntry(com.enonic.xp.node.NodeBranchEntry) RepositoryId(com.enonic.xp.repository.RepositoryId) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Example 15 with Project

use of com.enonic.xp.project.Project in project xp by enonic.

the class ProjectServiceImplTest method create_with_content_admin_permissions.

@Test
void create_with_content_admin_permissions() {
    final RepositoryId projectRepoId = RepositoryId.from("com.enonic.cms.test-project");
    final Project project = contentAdminContext().callWith(() -> doCreateProject(ProjectName.from(projectRepoId)));
    assertNotNull(project);
}
Also used : Project(com.enonic.xp.project.Project) RepositoryId(com.enonic.xp.repository.RepositoryId) Test(org.junit.jupiter.api.Test) AbstractNodeTest(com.enonic.xp.repo.impl.node.AbstractNodeTest)

Aggregations

Project (com.enonic.xp.project.Project)31 Test (org.junit.jupiter.api.Test)19 AbstractNodeTest (com.enonic.xp.repo.impl.node.AbstractNodeTest)18 ProjectName (com.enonic.xp.project.ProjectName)9 ProjectPermissions (com.enonic.xp.project.ProjectPermissions)9 RepositoryId (com.enonic.xp.repository.RepositoryId)9 CreateProjectParams (com.enonic.xp.project.CreateProjectParams)7 ModifyProjectParams (com.enonic.xp.project.ModifyProjectParams)7 Repository (com.enonic.xp.repository.Repository)7 Context (com.enonic.xp.context.Context)6 ContextBuilder (com.enonic.xp.context.ContextBuilder)6 ProjectGraph (com.enonic.xp.project.ProjectGraph)6 RoleKeys (com.enonic.xp.security.RoleKeys)6 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)6 ByteSource (com.google.common.io.ByteSource)6 IOException (java.io.IOException)6 UncheckedIOException (java.io.UncheckedIOException)6 List (java.util.List)6 Collectors (java.util.stream.Collectors)6 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)5