Search in sources :

Example 1 with Document

use of org.eclipse.sirius.web.services.api.document.Document 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 2 with Document

use of org.eclipse.sirius.web.services.api.document.Document in project sirius-web by eclipse-sirius.

the class CreateDocumentEventHandlerTests method testCreateTwoDocumentWithSameName.

@Test
public void testCreateTwoDocumentWithSameName() {
    IDocumentService documentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> createDocument(String projectId, String name, String content) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Optional.of(new Document(UUID.randomUUID(), new Project(UUID.fromString(projectId), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), name, content));
        }
    };
    IStereotypeDescriptionService stereotypeDescriptionService = new IStereotypeDescriptionService.NoOp() {

        @Override
        public Optional<StereotypeDescription> getStereotypeDescriptionById(String editingContextId, UUID stereotypeId) {
            // $NON-NLS-1$
            StereotypeDescription stereotypeDescription = new StereotypeDescription(stereotypeId, "label", () -> CONTENT);
            return Optional.of(stereotypeDescription);
        }
    };
    IServicesMessageService messageService = new NoOpServicesMessageService();
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    EditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    CreateDocumentEventHandler handler = new CreateDocumentEventHandler(documentService, stereotypeDescriptionService, messageService, new SimpleMeterRegistry());
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    changeDescriptionSink.asFlux().subscribe(changeDescription -> {
        assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    });
    var firstCreateInput = new CreateDocumentInput(UUID.randomUUID(), editingContext.getId(), DOCUMENT_NAME, STEREOTYPE_DESCRIPTION_ID);
    assertThat(handler.canHandle(editingContext, firstCreateInput)).isTrue();
    One<IPayload> firstPayloadSink = Sinks.one();
    handler.handle(firstPayloadSink, changeDescriptionSink, editingContext, firstCreateInput);
    IPayload firstPayload = firstPayloadSink.asMono().block();
    assertThat(firstPayload).isInstanceOf(CreateDocumentSuccessPayload.class);
    var secondCreatedInput = new CreateDocumentInput(UUID.randomUUID(), editingContext.getId(), DOCUMENT_NAME, STEREOTYPE_DESCRIPTION_ID);
    assertThat(handler.canHandle(editingContext, secondCreatedInput)).isTrue();
    One<IPayload> secondPayloadSink = Sinks.one();
    handler.handle(secondPayloadSink, changeDescriptionSink, editingContext, secondCreatedInput);
    IPayload secondPayload = firstPayloadSink.asMono().block();
    assertThat(secondPayload).isInstanceOf(CreateDocumentSuccessPayload.class);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) StereotypeDescription(org.eclipse.sirius.components.core.configuration.StereotypeDescription) IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) IStereotypeDescriptionService(org.eclipse.sirius.web.services.api.stereotypes.IStereotypeDescriptionService) Document(org.eclipse.sirius.web.services.api.document.Document) Profile(org.eclipse.sirius.web.services.api.accounts.Profile) IPayload(org.eclipse.sirius.components.core.api.IPayload) Project(org.eclipse.sirius.web.services.api.projects.Project) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) UUID(java.util.UUID) CreateDocumentInput(org.eclipse.sirius.components.collaborative.dto.CreateDocumentInput) Test(org.junit.jupiter.api.Test)

Example 3 with Document

use of org.eclipse.sirius.web.services.api.document.Document in project sirius-web by eclipse-sirius.

the class CreateDocumentEventHandlerTests method testCreateDocument.

@Test
public void testCreateDocument() {
    IDocumentService documentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> createDocument(String projectId, String name, String content) {
            // $NON-NLS-1$ //$NON-NLS-2$
            return Optional.of(new Document(UUID.randomUUID(), new Project(UUID.fromString(projectId), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), name, content));
        }
    };
    IStereotypeDescriptionService stereotypeDescriptionService = new IStereotypeDescriptionService.NoOp() {

        @Override
        public Optional<StereotypeDescription> getStereotypeDescriptionById(String editingContextId, UUID stereotypeId) {
            // $NON-NLS-1$
            StereotypeDescription stereotypeDescription = new StereotypeDescription(stereotypeId, "label", () -> CONTENT);
            return Optional.of(stereotypeDescription);
        }
    };
    IServicesMessageService messageService = new NoOpServicesMessageService();
    CreateDocumentEventHandler handler = new CreateDocumentEventHandler(documentService, stereotypeDescriptionService, messageService, new SimpleMeterRegistry());
    var input = new CreateDocumentInput(UUID.randomUUID(), UUID.randomUUID().toString(), DOCUMENT_NAME, STEREOTYPE_DESCRIPTION_ID);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    EditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    assertThat(handler.canHandle(editingContext, input)).isTrue();
    handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(CreateDocumentSuccessPayload.class);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    // $NON-NLS-1$
    Condition<Object> condition = new Condition<>(adapter -> adapter instanceof DocumentMetadataAdapter, "has an DocumentMetadataAdapter");
    assertThat(editingDomain.getResourceSet().getResources().get(0).eAdapters()).areAtLeastOne(condition);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) IStereotypeDescriptionService(org.eclipse.sirius.web.services.api.stereotypes.IStereotypeDescriptionService) Document(org.eclipse.sirius.web.services.api.document.Document) Profile(org.eclipse.sirius.web.services.api.accounts.Profile) IPayload(org.eclipse.sirius.components.core.api.IPayload) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) UUID(java.util.UUID) CreateDocumentInput(org.eclipse.sirius.components.collaborative.dto.CreateDocumentInput) Condition(org.assertj.core.api.Condition) StereotypeDescription(org.eclipse.sirius.components.core.configuration.StereotypeDescription) IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) Project(org.eclipse.sirius.web.services.api.projects.Project) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) Test(org.junit.jupiter.api.Test)

Example 4 with Document

use of org.eclipse.sirius.web.services.api.document.Document in project sirius-web by eclipse-sirius.

the class RenameDocumentEventHandlerTests method testRenameDocument.

@Test
public void testRenameDocument() {
    IDocumentService noOpDocumentService = new IDocumentService.NoOp() {

        @Override
        public Optional<Document> rename(UUID documentId, String newName) {
            // $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
            return Optional.of(new Document(documentId, new Project(UUID.randomUUID(), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), newName, "noContent"));
        }
    };
    RenameDocumentEventHandler handler = new RenameDocumentEventHandler(noOpDocumentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
    UUID documentId = UUID.randomUUID();
    IInput input = new RenameDocumentInput(UUID.randomUUID(), documentId, NEW_NAME);
    AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
    DocumentMetadataAdapter adapter = new DocumentMetadataAdapter(OLD_NAME);
    Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(documentId.toString()));
    resource.eAdapters().add(adapter);
    editingDomain.getResourceSet().getResources().add(resource);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    assertThat(adapter.getName()).isEqualTo(OLD_NAME);
    IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    assertThat(handler.canHandle(editingContext, input)).isTrue();
    handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
    assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
    assertThat(adapter.getName()).isEqualTo(NEW_NAME);
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(RenameDocumentSuccessPayload.class);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) NoOpServicesMessageService(org.eclipse.sirius.web.services.projects.NoOpServicesMessageService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) RenameDocumentInput(org.eclipse.sirius.components.collaborative.dto.RenameDocumentInput) Resource(org.eclipse.emf.ecore.resource.Resource) IInput(org.eclipse.sirius.components.core.api.IInput) Document(org.eclipse.sirius.web.services.api.document.Document) Profile(org.eclipse.sirius.web.services.api.accounts.Profile) IPayload(org.eclipse.sirius.components.core.api.IPayload) Project(org.eclipse.sirius.web.services.api.projects.Project) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) SiriusWebJSONResourceFactoryImpl(org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) UUID(java.util.UUID) Test(org.junit.jupiter.api.Test)

Example 5 with Document

use of org.eclipse.sirius.web.services.api.document.Document in project sirius-web by eclipse-sirius.

the class DeleteDocumentEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
    this.counter.increment();
    String message = this.messageService.invalidInput(input.getClass().getSimpleName(), DeleteDocumentInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    // @formatter:off
    var optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
    if (input instanceof DeleteDocumentInput) {
        DeleteDocumentInput deleteDocumentInput = (DeleteDocumentInput) input;
        var optionalDocument = this.documentService.getDocument(deleteDocumentInput.getDocumentId());
        if (optionalEditingDomain.isPresent() && optionalDocument.isPresent()) {
            AdapterFactoryEditingDomain editingDomain = optionalEditingDomain.get();
            Document document = optionalDocument.get();
            ResourceSet resourceSet = editingDomain.getResourceSet();
            URI uri = URI.createURI(document.getId().toString());
            // @formatter:off
            List<Resource> resourcesToDelete = resourceSet.getResources().stream().filter(resource -> resource.getURI().equals(uri)).collect(Collectors.toUnmodifiableList());
            resourcesToDelete.stream().forEach(resourceSet.getResources()::remove);
            // @formatter:on
            this.documentService.delete(document.getId());
            payload = new DeleteDocumentSuccessPayload(input.getId());
            changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) URI(org.eclipse.emf.common.util.URI) One(reactor.core.publisher.Sinks.One) IInput(org.eclipse.sirius.components.core.api.IInput) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) Document(org.eclipse.sirius.web.services.api.document.Document) Monitoring(org.eclipse.sirius.components.collaborative.api.Monitoring) Service(org.springframework.stereotype.Service) DeleteDocumentInput(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput) Counter(io.micrometer.core.instrument.Counter) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) IDocumentService(org.eclipse.sirius.web.services.api.document.IDocumentService) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) Collectors(java.util.stream.Collectors) DeleteDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentSuccessPayload) Objects(java.util.Objects) List(java.util.List) IPayload(org.eclipse.sirius.components.core.api.IPayload) MeterRegistry(io.micrometer.core.instrument.MeterRegistry) Resource(org.eclipse.emf.ecore.resource.Resource) IServicesMessageService(org.eclipse.sirius.web.services.messages.IServicesMessageService) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) IEditingContextEventHandler(org.eclipse.sirius.components.collaborative.api.IEditingContextEventHandler) AdapterFactoryEditingDomain(org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain) Resource(org.eclipse.emf.ecore.resource.Resource) ResourceSet(org.eclipse.emf.ecore.resource.ResourceSet) Document(org.eclipse.sirius.web.services.api.document.Document) URI(org.eclipse.emf.common.util.URI) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) DeleteDocumentInput(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentInput) DeleteDocumentSuccessPayload(org.eclipse.sirius.components.collaborative.dto.DeleteDocumentSuccessPayload)

Aggregations

Document (org.eclipse.sirius.web.services.api.document.Document)19 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)11 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)10 IPayload (org.eclipse.sirius.components.core.api.IPayload)10 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)9 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)8 Profile (org.eclipse.sirius.web.services.api.accounts.Profile)8 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)8 Project (org.eclipse.sirius.web.services.api.projects.Project)8 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)6 UUID (java.util.UUID)6 NoOpServicesMessageService (org.eclipse.sirius.web.services.projects.NoOpServicesMessageService)6 ByteArrayInputStream (java.io.ByteArrayInputStream)5 ResourceSet (org.eclipse.emf.ecore.resource.ResourceSet)5 SiriusWebJSONResourceFactoryImpl (org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl)5 IServicesMessageService (org.eclipse.sirius.web.services.messages.IServicesMessageService)5 Test (org.junit.jupiter.api.Test)5 IOException (java.io.IOException)4 URI (org.eclipse.emf.common.util.URI)4 Resource (org.eclipse.emf.ecore.resource.Resource)4