Search in sources :

Example 1 with RepresentationDescriptor

use of org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor in project sirius-web by eclipse-sirius.

the class ProjectExportService method addRepresentation.

/**
 * Adds a {@link ZipEntry} for every representations in the project, in the given {@link ZipOutputStream}.
 *
 * <p>
 * The name of the {@link ZipEntry} is [projectName]/representations/[representationId], where '/' are used as path
 * separator in the zip.
 * </p>
 *
 * <p>
 * Returns a map of representation IDs to {@link RepresentationManifest}. This map will be stored in the manifest
 * file.
 * </p>
 *
 * @param projectId
 *            The id of the project we want to export
 * @param projectName
 *            The name of the project we want to export
 * @param zippedOut
 *            The {@link ZipOutputStream} used to build the zip
 * @return the mapping between representation IDs and their {@link RepresentationManifest}
 * @throws IOException
 *             if an I/O error occurred
 */
private Map<String, RepresentationManifest> addRepresentation(String projectId, String projectName, ZipOutputStream zippedout) throws IOException {
    List<RepresentationDescriptor> representationsDescriptor = this.representationService.getRepresentationDescriptorsForProjectId(projectId);
    Map<String, RepresentationManifest> representationManifests = new HashMap<>();
    ResourceSet resourceSet = this.loadAllDocuments(projectId);
    for (RepresentationDescriptor representationDescriptor : representationsDescriptor) {
        RepresentationManifest representationManifest = this.createRepresentationManifest(representationDescriptor, resourceSet);
        UUID representationId = representationDescriptor.getId();
        representationManifests.put(representationId.toString(), representationManifest);
        byte[] bytes = new ObjectMapper().writeValueAsBytes(representationDescriptor);
        // $NON-NLS-1$ //$NON-NLS-2$
        String name = projectName + "/representations/" + representationId + "." + JsonResourceFactoryImpl.EXTENSION;
        ZipEntry zipEntry = this.createZipEntry(name, bytes.length);
        zippedout.putNextEntry(zipEntry);
        zippedout.write(bytes);
        zippedout.closeEntry();
    }
    return representationManifests;
}
Also used : HashMap(java.util.HashMap) RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) ZipEntry(java.util.zip.ZipEntry) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) UUID(java.util.UUID) RepresentationManifest(org.eclipse.sirius.web.services.api.projects.RepresentationManifest) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with RepresentationDescriptor

use of org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor in project sirius-web by eclipse-sirius.

the class ProjectImportService method importProject.

/**
 * Returns {@link UploadProjectSuccessPayload} if the project import has been successful, {@link ErrorPayload}
 * otherwise.
 *
 * <p>
 * Unzip the given {@link UploadFile}, then creates a project with the name of the root directory in the zip file,
 * then use {@link ProjectImporter} to create documents and representations. If the project has not been imported,
 * it disposes the {@link IEditingContextEventProcessor} used to create documents and representations then delete
 * the created project in order to keep the server in the same state before the project upload attempt.
 * </p>
 *
 * @param inputId
 *            The identifier of the input which has triggered the upload
 * @param file
 *            the file to upload
 * @return {@link UploadProjectSuccessPayload} whether the project import has been successful, {@link ErrorPayload}
 *         otherwise
 */
@Override
public IPayload importProject(UUID inputId, UploadFile file) {
    IPayload payload = new ErrorPayload(inputId, this.messageService.unexpectedError());
    ProjectUnzipper unzipper = new ProjectUnzipper(file.getInputStream(), this.objectMapper);
    Optional<UnzippedProject> optionalUnzippedProject = unzipper.unzipProject();
    if (optionalUnzippedProject.isEmpty()) {
        return new ErrorPayload(inputId, this.messageService.unexpectedError());
    }
    UnzippedProject unzippedProject = optionalUnzippedProject.get();
    ProjectManifest manifest = unzippedProject.getProjectManifest();
    String projectName = unzippedProject.getProjectName();
    CreateProjectInput createProjectInput = new CreateProjectInput(inputId, projectName, Visibility.PRIVATE);
    IPayload createProjectPayload = this.projectService.createProject(createProjectInput);
    if (createProjectPayload instanceof CreateProjectSuccessPayload) {
        Project project = ((CreateProjectSuccessPayload) createProjectPayload).getProject();
        Optional<IEditingContextEventProcessor> optionalEditingContextEventProcessor = this.editingContextEventProcessorRegistry.getOrCreateEditingContextEventProcessor(project.getId().toString());
        if (optionalEditingContextEventProcessor.isPresent()) {
            IEditingContextEventProcessor editingContextEventProcessor = optionalEditingContextEventProcessor.get();
            Map<String, UploadFile> documents = unzippedProject.getDocumentIdToUploadFile();
            List<RepresentationDescriptor> representations = unzippedProject.getRepresentationDescriptors();
            ProjectImporter projectImporter = new ProjectImporter(project.getId().toString(), editingContextEventProcessor, documents, representations, manifest, this.idMappingRepository);
            boolean hasBeenImported = projectImporter.importProject(inputId);
            if (!hasBeenImported) {
                this.editingContextEventProcessorRegistry.disposeEditingContextEventProcessor(project.getId().toString());
                this.projectService.delete(project.getId());
            } else {
                payload = new UploadProjectSuccessPayload(inputId, project);
            }
        }
    }
    return payload;
}
Also used : RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) CreateProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.CreateProjectSuccessPayload) CreateProjectInput(org.eclipse.sirius.web.services.api.projects.CreateProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) UnzippedProject(org.eclipse.sirius.web.services.api.projects.UnzippedProject) Project(org.eclipse.sirius.web.services.api.projects.Project) UnzippedProject(org.eclipse.sirius.web.services.api.projects.UnzippedProject) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ProjectManifest(org.eclipse.sirius.web.services.api.projects.ProjectManifest) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile) UploadProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.UploadProjectSuccessPayload) IEditingContextEventProcessor(org.eclipse.sirius.components.collaborative.api.IEditingContextEventProcessor)

Example 3 with RepresentationDescriptor

use of org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor in project sirius-web by eclipse-sirius.

the class ProjectImporter method createRepresentations.

/**
 * Creates all representations in the project thanks to the {@link IEditingContextEventProcessor} and the create
 * representation input. If at least one representation has not been created it will return <code>false</code>.
 *
 * @param inputId
 *            The identifier of the input which has triggered this import
 *
 * @return <code>true</code> whether all representations has been created, <code>false</code> otherwise
 */
private boolean createRepresentations(UUID inputId) {
    boolean allRepresentationCreated = true;
    for (RepresentationDescriptor representationDescriptor : this.representations) {
        RepresentationManifest representationManifest = this.projectManifest.getRepresentations().get(representationDescriptor.getId().toString());
        String targetObjectURI = representationManifest.getTargetObjectURI();
        String oldDocumentId = URI.create(targetObjectURI).getPath();
        Document newDocument = this.oldDocumentIdToNewDocument.get(oldDocumentId);
        final String objectId;
        if (newDocument != null) {
            objectId = targetObjectURI.replace(oldDocumentId, newDocument.getId().toString());
        } else {
            objectId = targetObjectURI;
        }
        boolean representationCreated = false;
        String descriptionURI = representationManifest.getDescriptionURI();
        // @formatter:off
        var inputHandle = this.idMappingRepository.findByExternalId(descriptionURI).map(IdMappingEntity::getId).or(() -> Optional.of(descriptionURI)).map(representationDescriptionId -> new CreateRepresentationInput(inputId, this.projectId.toString(), representationDescriptionId.toString(), objectId, representationDescriptor.getLabel())).map(this.editingContextEventProcessor::handle).orElseGet(Mono::empty);
        representationCreated = inputHandle.filter(CreateRepresentationSuccessPayload.class::isInstance).map(CreateRepresentationSuccessPayload.class::cast).map(CreateRepresentationSuccessPayload::getRepresentation).blockOptional().isPresent();
        if (!representationCreated) {
            // $NON-NLS-1$
            this.logger.warn("The representation {} has not been created", representationDescriptor.getLabel());
        }
        allRepresentationCreated = allRepresentationCreated && representationCreated;
    }
    return allRepresentationCreated;
}
Also used : CreateRepresentationInput(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput) IdMappingEntity(org.eclipse.sirius.web.persistence.entities.IdMappingEntity) RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) Mono(reactor.core.publisher.Mono) CreateRepresentationSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload) Document(org.eclipse.sirius.web.services.api.document.Document) RepresentationManifest(org.eclipse.sirius.web.services.api.projects.RepresentationManifest)

Example 4 with RepresentationDescriptor

use of org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor in project sirius-web by eclipse-sirius.

the class EditingContextRepresentationsDataFetcher method get.

@Override
public Connection<RepresentationMetadata> get(DataFetchingEnvironment environment) throws Exception {
    String editingContextId = environment.getSource();
    // @formatter:off
    List<RepresentationMetadata> representations = this.representationService.getRepresentationDescriptorsForProjectId(editingContextId).stream().map(RepresentationDescriptor::getRepresentation).map(this::toRepresentationMetadata).collect(Collectors.toList());
    // @formatter:on
    // @formatter:off
    List<Edge<RepresentationMetadata>> representationEdges = representations.stream().map(representation -> {
        String value = Base64.getEncoder().encodeToString(representation.getId().getBytes());
        ConnectionCursor cursor = new DefaultConnectionCursor(value);
        return new DefaultEdge<>(representation, cursor);
    }).collect(Collectors.toList());
    // @formatter:on
    ConnectionCursor startCursor = representationEdges.stream().findFirst().map(Edge::getCursor).orElse(null);
    ConnectionCursor endCursor = null;
    if (!representationEdges.isEmpty()) {
        endCursor = representationEdges.get(representationEdges.size() - 1).getCursor();
    }
    PageInfo pageInfo = new DefaultPageInfo(startCursor, endCursor, false, false);
    return new DefaultConnection<>(representationEdges, pageInfo);
}
Also used : PageInfo(graphql.relay.PageInfo) DataFetchingEnvironment(graphql.schema.DataFetchingEnvironment) QueryDataFetcher(org.eclipse.sirius.components.annotations.spring.graphql.QueryDataFetcher) ISemanticRepresentation(org.eclipse.sirius.components.representations.ISemanticRepresentation) RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) DefaultEdge(graphql.relay.DefaultEdge) IDataFetcherWithFieldCoordinates(org.eclipse.sirius.components.graphql.api.IDataFetcherWithFieldCoordinates) Collectors(java.util.stream.Collectors) DefaultConnectionCursor(graphql.relay.DefaultConnectionCursor) IRepresentationService(org.eclipse.sirius.web.services.api.representations.IRepresentationService) Objects(java.util.Objects) Base64(java.util.Base64) List(java.util.List) DefaultPageInfo(graphql.relay.DefaultPageInfo) Connection(graphql.relay.Connection) DefaultConnection(graphql.relay.DefaultConnection) RepresentationMetadata(org.eclipse.sirius.components.core.RepresentationMetadata) IRepresentation(org.eclipse.sirius.components.representations.IRepresentation) Edge(graphql.relay.Edge) Optional(java.util.Optional) ConnectionCursor(graphql.relay.ConnectionCursor) EditingContextTypeProvider(org.eclipse.sirius.web.graphql.schema.EditingContextTypeProvider) DefaultConnectionCursor(graphql.relay.DefaultConnectionCursor) ConnectionCursor(graphql.relay.ConnectionCursor) RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) PageInfo(graphql.relay.PageInfo) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnection(graphql.relay.DefaultConnection) RepresentationMetadata(org.eclipse.sirius.components.core.RepresentationMetadata) DefaultEdge(graphql.relay.DefaultEdge) Edge(graphql.relay.Edge) DefaultPageInfo(graphql.relay.DefaultPageInfo) DefaultConnectionCursor(graphql.relay.DefaultConnectionCursor)

Example 5 with RepresentationDescriptor

use of org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor in project sirius-web by eclipse-sirius.

the class ProjectUnzipper method unzipProject.

/**
 * Unzip the project.
 *
 * <p>
 * The zip must have the following structure:
 * <ul>
 * <li>Documents should be in [projectName]/documents/*</li>
 * <li>Representations should be in [projectName]/representations/*</li>
 * <li>The manifest.json should be in [projectName]/*</li>
 * </ul>
 * </p>
 *
 * @return <code>true</code> whether the unzip went well, <code>false</code> otherwise.
 */
public Optional<UnzippedProject> unzipProject() {
    Optional<UnzippedProject> optionalUnzippedProject = Optional.empty();
    Map<String, ByteArrayOutputStream> zipEntryNameToContent = this.readZipFile();
    Optional<String> optionalProjectName = this.handleProjectName(zipEntryNameToContent);
    if (optionalProjectName.isPresent()) {
        String projectName = optionalProjectName.get();
        String documentsFolderInZip = projectName + ZIP_FOLDER_SEPARATOR + DOCUMENTS_FOLDER + ZIP_FOLDER_SEPARATOR;
        String representationsFolderInZip = projectName + ZIP_FOLDER_SEPARATOR + REPRESENTATIONS_FOLDER + ZIP_FOLDER_SEPARATOR;
        String manifestPathInZip = projectName + ZIP_FOLDER_SEPARATOR + MANIFEST_JSON_FILE;
        Optional<ProjectManifest> optionalManifest = this.getProjectManifest(zipEntryNameToContent, manifestPathInZip);
        Map<String, ByteArrayOutputStream> documentIdToDocumentContent = this.selectAndTransformIntoDocumentIdToDocumentContent(zipEntryNameToContent, documentsFolderInZip);
        List<ByteArrayOutputStream> representationDescritorsContent = this.selectAndTransformIntoRepresentationDescriptorsContent(zipEntryNameToContent, representationsFolderInZip);
        if (!optionalManifest.isEmpty() && this.validateDocuments(documentIdToDocumentContent, optionalManifest.get())) {
            ProjectManifest manifest = optionalManifest.get();
            Map<String, UploadFile> documentIdToUploadFile = this.getUploadFiles(documentIdToDocumentContent, manifest);
            List<RepresentationDescriptor> representationDescriptors;
            try {
                representationDescriptors = this.getRepresentationDescriptors(representationDescritorsContent);
                // @formatter:off
                UnzippedProject unzippedProject = UnzippedProject.newUnzippedProject(projectName).projectManifest(manifest).documentIdToUploadFile(documentIdToUploadFile).representationDescriptors(representationDescriptors).build();
                // @formatter:on
                optionalUnzippedProject = Optional.of(unzippedProject);
            } catch (IOException exception) {
                this.logger.warn(exception.getMessage(), exception);
            }
        }
    }
    return optionalUnzippedProject;
}
Also used : RepresentationDescriptor(org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) UnzippedProject(org.eclipse.sirius.web.services.api.projects.UnzippedProject) ProjectManifest(org.eclipse.sirius.web.services.api.projects.ProjectManifest) UploadFile(org.eclipse.sirius.components.graphql.api.UploadFile)

Aggregations

RepresentationDescriptor (org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)2 ProjectManifest (org.eclipse.sirius.web.services.api.projects.ProjectManifest)2 RepresentationManifest (org.eclipse.sirius.web.services.api.projects.RepresentationManifest)2 UnzippedProject (org.eclipse.sirius.web.services.api.projects.UnzippedProject)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Connection (graphql.relay.Connection)1 ConnectionCursor (graphql.relay.ConnectionCursor)1 DefaultConnection (graphql.relay.DefaultConnection)1 DefaultConnectionCursor (graphql.relay.DefaultConnectionCursor)1 DefaultEdge (graphql.relay.DefaultEdge)1 DefaultPageInfo (graphql.relay.DefaultPageInfo)1 Edge (graphql.relay.Edge)1 PageInfo (graphql.relay.PageInfo)1 DataFetchingEnvironment (graphql.schema.DataFetchingEnvironment)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Base64 (java.util.Base64)1 HashMap (java.util.HashMap)1