Search in sources :

Example 1 with ProjectNotFoundException

use of com.enonic.xp.project.ProjectNotFoundException 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();
}
Also used : ArchiveInitializer(com.enonic.xp.core.impl.project.init.ArchiveInitializer) DeleteRepositoryParams(com.enonic.xp.repository.DeleteRepositoryParams) RepositoryService(com.enonic.xp.repository.RepositoryService) LoggerFactory(org.slf4j.LoggerFactory) AttachmentSerializer(com.enonic.xp.attachment.AttachmentSerializer) ImageHelper(com.enonic.xp.image.ImageHelper) IndexService(com.enonic.xp.index.IndexService) BigDecimal(java.math.BigDecimal) RepositoryId(com.enonic.xp.repository.RepositoryId) ContextAccessor(com.enonic.xp.context.ContextAccessor) NodeService(com.enonic.xp.node.NodeService) CreateAttachment(com.enonic.xp.attachment.CreateAttachment) ImageIO(javax.imageio.ImageIO) ContextBuilder(com.enonic.xp.context.ContextBuilder) EnumSet(java.util.EnumSet) SecurityService(com.enonic.xp.security.SecurityService) RoundingMode(java.math.RoundingMode) BinaryReference(com.enonic.xp.util.BinaryReference) ProjectService(com.enonic.xp.project.ProjectService) Projects(com.enonic.xp.project.Projects) BufferedImage(java.awt.image.BufferedImage) BinaryAttachment(com.enonic.xp.node.BinaryAttachment) Project(com.enonic.xp.project.Project) Collectors(java.util.stream.Collectors) CreateProjectParams(com.enonic.xp.project.CreateProjectParams) UpdateRepositoryParams(com.enonic.xp.repository.UpdateRepositoryParams) UncheckedIOException(java.io.UncheckedIOException) List(java.util.List) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) Optional(java.util.Optional) RoleKeys(com.enonic.xp.security.RoleKeys) Context(com.enonic.xp.context.Context) Queue(java.util.Queue) Repository(com.enonic.xp.repository.Repository) EventPublisher(com.enonic.xp.event.EventPublisher) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Callable(java.util.concurrent.Callable) Attachment(com.enonic.xp.attachment.Attachment) ProjectConstants(com.enonic.xp.project.ProjectConstants) ProjectGraphEntry(com.enonic.xp.project.ProjectGraphEntry) ArrayList(java.util.ArrayList) IssueInitializer(com.enonic.xp.core.impl.project.init.IssueInitializer) ProjectRole(com.enonic.xp.project.ProjectRole) CreateAttachments(com.enonic.xp.attachment.CreateAttachments) ProjectName(com.enonic.xp.project.ProjectName) ContentInitializer(com.enonic.xp.core.impl.project.init.ContentInitializer) ModifyProjectIconParams(com.enonic.xp.project.ModifyProjectIconParams) ByteSource(com.google.common.io.ByteSource) PropertyTree(com.enonic.xp.data.PropertyTree) Logger(org.slf4j.Logger) ProjectGraph(com.enonic.xp.project.ProjectGraph) ProjectPermissions(com.enonic.xp.project.ProjectPermissions) PropertySet(com.enonic.xp.data.PropertySet) IOException(java.io.IOException) AuthenticationInfo(com.enonic.xp.security.auth.AuthenticationInfo) ArrayDeque(java.util.ArrayDeque) Collections(java.util.Collections) ModifyProjectParams(com.enonic.xp.project.ModifyProjectParams) InputStream(java.io.InputStream) Project(com.enonic.xp.project.Project) ProjectNotFoundException(com.enonic.xp.project.ProjectNotFoundException) ArrayList(java.util.ArrayList) Projects(com.enonic.xp.project.Projects) ProjectGraph(com.enonic.xp.project.ProjectGraph) ArrayDeque(java.util.ArrayDeque)

Example 2 with ProjectNotFoundException

use of com.enonic.xp.project.ProjectNotFoundException 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)

Aggregations

Context (com.enonic.xp.context.Context)2 ContextAccessor (com.enonic.xp.context.ContextAccessor)2 ContextBuilder (com.enonic.xp.context.ContextBuilder)2 Project (com.enonic.xp.project.Project)2 ProjectName (com.enonic.xp.project.ProjectName)2 ProjectNotFoundException (com.enonic.xp.project.ProjectNotFoundException)2 ProjectService (com.enonic.xp.project.ProjectService)2 RepositoryId (com.enonic.xp.repository.RepositoryId)2 RoleKeys (com.enonic.xp.security.RoleKeys)2 AuthenticationInfo (com.enonic.xp.security.auth.AuthenticationInfo)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Attachment (com.enonic.xp.attachment.Attachment)1 AttachmentSerializer (com.enonic.xp.attachment.AttachmentSerializer)1 CreateAttachment (com.enonic.xp.attachment.CreateAttachment)1 CreateAttachments (com.enonic.xp.attachment.CreateAttachments)1 Branch (com.enonic.xp.branch.Branch)1 ContentConstants (com.enonic.xp.content.ContentConstants)1