use of com.enonic.xp.project.ProjectGraph in project xp by enonic.
the class ProjectServiceImpl method graph.
@Override
public ProjectGraph graph(final ProjectName projectName) {
final ProjectGraph.Builder graph = ProjectGraph.create();
final Project targetProject;
try {
targetProject = this.get(projectName);
if (targetProject == null) {
throw new ProjectNotFoundException(projectName);
}
} catch (ProjectAccessException e) {
throw new ProjectNotFoundException(e.getProjectName());
}
final Projects projects = adminContext().callWith(this::doList);
Project project = targetProject;
final List<Project> parents = new ArrayList<>();
while (project.getParent() != null) {
project = getProject(projects, project.getParent());
parents.add(project);
}
Collections.reverse(parents);
parents.add(targetProject);
parents.forEach(p -> graph.add(ProjectGraphEntry.create().name(p.getName()).parent(p.getParent()).build()));
final Queue<Project> children = new ArrayDeque<>();
children.add(targetProject);
while (!children.isEmpty()) {
final Project current = children.poll();
projects.stream().filter(p -> current.getName().equals(p.getParent())).forEach(p -> {
children.offer(p);
graph.add(ProjectGraphEntry.create().name(p.getName()).parent(p.getParent()).build());
});
}
return graph.build();
}
use of com.enonic.xp.project.ProjectGraph 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()));
}
Aggregations