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()));
}
});
}
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()));
}
}
}
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()));
}
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);
});
}
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);
}
Aggregations