Search in sources :

Example 1 with RepresentationManifest

use of org.eclipse.sirius.web.services.api.projects.RepresentationManifest 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 RepresentationManifest

use of org.eclipse.sirius.web.services.api.projects.RepresentationManifest in project sirius-web by eclipse-sirius.

the class ProjectExportService method toZip.

private byte[] toZip(Project project) {
    byte[] zip = new byte[0];
    String projectId = project.getId().toString();
    String projectName = project.getName();
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    try (var zippedOut = new ZipOutputStream(outputStream)) {
        Map<String, String> id2DocumentName = this.addDocuments(projectId, projectName, zippedOut);
        Map<String, RepresentationManifest> representationsManifests = this.addRepresentation(projectId, projectName, zippedOut);
        this.addManifest(projectId, projectName, id2DocumentName, representationsManifests, zippedOut);
    } catch (IOException exception) {
        this.logger.warn(exception.getMessage(), exception);
        outputStream.reset();
    }
    if (outputStream.size() > 0) {
        zip = outputStream.toByteArray();
    }
    return zip;
}
Also used : ZipOutputStream(java.util.zip.ZipOutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) RepresentationManifest(org.eclipse.sirius.web.services.api.projects.RepresentationManifest)

Example 3 with RepresentationManifest

use of org.eclipse.sirius.web.services.api.projects.RepresentationManifest 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)

Aggregations

RepresentationManifest (org.eclipse.sirius.web.services.api.projects.RepresentationManifest)3 RepresentationDescriptor (org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 ZipEntry (java.util.zip.ZipEntry)1 ZipOutputStream (java.util.zip.ZipOutputStream)1 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)1 CreateRepresentationInput (org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput)1 CreateRepresentationSuccessPayload (org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload)1 IdMappingEntity (org.eclipse.sirius.web.persistence.entities.IdMappingEntity)1 Document (org.eclipse.sirius.web.services.api.document.Document)1 Mono (reactor.core.publisher.Mono)1