Search in sources :

Example 1 with CreateRepresentationInput

use of org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput 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 CreateRepresentationInput

use of org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput in project sirius-components by eclipse-sirius.

the class CreateDiagramEventHandlerTests method testDiagramCreation.

@Test
public void testDiagramCreation() {
    IRepresentationDescriptionSearchService representationDescriptionSearchService = new IRepresentationDescriptionSearchService.NoOp() {

        @Override
        public Optional<IRepresentationDescription> findById(IEditingContext editingContext, UUID id) {
            // @formatter:off
            DiagramDescription diagramDescription = DiagramDescription.newDiagramDescription(UUID.randomUUID()).label(// $NON-NLS-1$
            "label").canCreatePredicate(variableManager -> Boolean.TRUE).edgeDescriptions(new ArrayList<>()).labelProvider(// $NON-NLS-1$
            variableManager -> "label").toolSections(List.of()).nodeDescriptions(new ArrayList<>()).targetObjectIdProvider(// $NON-NLS-1$
            variableManager -> "targetObjectId").dropHandler(// $NON-NLS-1$
            variableManager -> new Failure("")).build();
            return Optional.of(diagramDescription);
        }
    };
    AtomicBoolean hasBeenCalled = new AtomicBoolean();
    IDiagramCreationService diagramCreationService = new IDiagramCreationService.NoOp() {

        @Override
        public Diagram create(String label, Object targetObject, DiagramDescription diagramDescription, IEditingContext editingContext) {
            hasBeenCalled.set(true);
            return new TestDiagramBuilder().getDiagram(UUID.randomUUID().toString());
        }
    };
    IObjectService objectService = new IObjectService.NoOp() {

        @Override
        public Optional<Object> getObject(IEditingContext editingContext, String objectId) {
            return Optional.of(new Object());
        }
    };
    CreateDiagramEventHandler handler = new CreateDiagramEventHandler(representationDescriptionSearchService, new IRepresentationPersistenceService.NoOp(), diagramCreationService, objectService, new ICollaborativeDiagramMessageService.NoOp(), new SimpleMeterRegistry());
    // $NON-NLS-1$//$NON-NLS-2$
    var input = new CreateRepresentationInput(UUID.randomUUID(), UUID.randomUUID().toString(), UUID.randomUUID(), "objectId", "representationName");
    assertThat(handler.canHandle(null, input)).isTrue();
    Many<ChangeDescription> changeDescriptionSink = Sinks.many().unicast().onBackpressureBuffer();
    One<IPayload> payloadSink = Sinks.one();
    IEditingContext editingContext = () -> UUID.randomUUID().toString();
    handler.handle(payloadSink, changeDescriptionSink, editingContext, input);
    assertThat(hasBeenCalled.get()).isTrue();
    ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
    assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.REPRESENTATION_CREATION);
    IPayload payload = payloadSink.asMono().block();
    assertThat(payload).isInstanceOf(CreateRepresentationSuccessPayload.class);
}
Also used : Sinks(reactor.core.publisher.Sinks) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) TestDiagramBuilder(org.eclipse.sirius.components.diagrams.tests.TestDiagramBuilder) IRepresentationPersistenceService(org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) One(reactor.core.publisher.Sinks.One) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DiagramDescription(org.eclipse.sirius.components.diagrams.description.DiagramDescription) CreateRepresentationSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload) ArrayList(java.util.ArrayList) Diagram(org.eclipse.sirius.components.diagrams.Diagram) ICollaborativeDiagramMessageService(org.eclipse.sirius.components.collaborative.diagrams.messages.ICollaborativeDiagramMessageService) ChangeKind(org.eclipse.sirius.components.collaborative.api.ChangeKind) Failure(org.eclipse.sirius.components.representations.Failure) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) IRepresentationDescriptionSearchService(org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService) UUID(java.util.UUID) IDiagramCreationService(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramCreationService) Test(org.junit.jupiter.api.Test) List(java.util.List) IPayload(org.eclipse.sirius.components.core.api.IPayload) CreateRepresentationInput(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput) Many(reactor.core.publisher.Sinks.Many) Optional(java.util.Optional) IRepresentationDescription(org.eclipse.sirius.components.representations.IRepresentationDescription) ArrayList(java.util.ArrayList) IRepresentationDescriptionSearchService(org.eclipse.sirius.components.core.api.IRepresentationDescriptionSearchService) IRepresentationDescription(org.eclipse.sirius.components.representations.IRepresentationDescription) IPayload(org.eclipse.sirius.components.core.api.IPayload) TestDiagramBuilder(org.eclipse.sirius.components.diagrams.tests.TestDiagramBuilder) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) ICollaborativeDiagramMessageService(org.eclipse.sirius.components.collaborative.diagrams.messages.ICollaborativeDiagramMessageService) UUID(java.util.UUID) Failure(org.eclipse.sirius.components.representations.Failure) IRepresentationPersistenceService(org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService) SimpleMeterRegistry(io.micrometer.core.instrument.simple.SimpleMeterRegistry) DiagramDescription(org.eclipse.sirius.components.diagrams.description.DiagramDescription) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CreateRepresentationInput(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) IObjectService(org.eclipse.sirius.components.core.api.IObjectService) IDiagramCreationService(org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramCreationService) Test(org.junit.jupiter.api.Test)

Example 3 with CreateRepresentationInput

use of org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput in project sirius-components by eclipse-sirius.

the class CreateDiagramEventHandler 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(), CreateRepresentationInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    if (input instanceof CreateRepresentationInput) {
        CreateRepresentationInput createRepresentationInput = (CreateRepresentationInput) input;
        // @formatter:off
        Optional<DiagramDescription> optionalDiagramDescription = this.representationDescriptionSearchService.findById(editingContext, createRepresentationInput.getRepresentationDescriptionId()).filter(DiagramDescription.class::isInstance).map(DiagramDescription.class::cast);
        // @formatter:on
        Optional<Object> optionalObject = this.objectService.getObject(editingContext, createRepresentationInput.getObjectId());
        if (optionalDiagramDescription.isPresent() && optionalObject.isPresent()) {
            DiagramDescription diagramDescription = optionalDiagramDescription.get();
            Object object = optionalObject.get();
            Diagram diagram = this.diagramCreationService.create(createRepresentationInput.getRepresentationName(), object, diagramDescription, editingContext);
            this.representationPersistenceService.save(editingContext, diagram);
            payload = new CreateRepresentationSuccessPayload(input.getId(), diagram);
            changeDescription = new ChangeDescription(ChangeKind.REPRESENTATION_CREATION, editingContext.getId(), input);
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : CreateRepresentationInput(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) CreateRepresentationSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload) DiagramDescription(org.eclipse.sirius.components.diagrams.description.DiagramDescription) IPayload(org.eclipse.sirius.components.core.api.IPayload) Diagram(org.eclipse.sirius.components.diagrams.Diagram)

Example 4 with CreateRepresentationInput

use of org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput in project sirius-components by eclipse-sirius.

the class CreateFormEventHandler 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(), CreateRepresentationInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    if (input instanceof CreateRepresentationInput) {
        CreateRepresentationInput createRepresentationInput = (CreateRepresentationInput) input;
        Optional<IRepresentationDescription> optionalRepresentationDescription = this.representationDescriptionSearchService.findById(editingContext, createRepresentationInput.getRepresentationDescriptionId());
        Optional<Object> optionalObject = this.objectService.getObject(editingContext, createRepresentationInput.getObjectId());
        if (optionalRepresentationDescription.isPresent() && optionalObject.isPresent()) {
            IRepresentationDescription representationDescription = optionalRepresentationDescription.get();
            String targetObjectId = this.objectService.getId(optionalObject.get());
            if (representationDescription instanceof FormDescription) {
                // @formatter:off
                Form form = Form.newForm(UUID.randomUUID().toString()).label(createRepresentationInput.getRepresentationName()).targetObjectId(targetObjectId).descriptionId(representationDescription.getId()).pages(// We don't store form pages, it will be re-render by the FormProcessor.
                List.of()).build();
                // @formatter:on
                this.representationPersistenceService.save(editingContext, form);
                payload = new CreateRepresentationSuccessPayload(input.getId(), form);
                changeDescription = new ChangeDescription(ChangeKind.REPRESENTATION_CREATION, editingContext.getId(), input);
            }
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : CreateRepresentationInput(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) Form(org.eclipse.sirius.components.forms.Form) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) CreateRepresentationSuccessPayload(org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload) FormDescription(org.eclipse.sirius.components.forms.description.FormDescription) IRepresentationDescription(org.eclipse.sirius.components.representations.IRepresentationDescription) IPayload(org.eclipse.sirius.components.core.api.IPayload)

Aggregations

CreateRepresentationInput (org.eclipse.sirius.components.collaborative.dto.CreateRepresentationInput)4 CreateRepresentationSuccessPayload (org.eclipse.sirius.components.collaborative.dto.CreateRepresentationSuccessPayload)4 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)3 IPayload (org.eclipse.sirius.components.core.api.IPayload)3 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)2 Diagram (org.eclipse.sirius.components.diagrams.Diagram)2 DiagramDescription (org.eclipse.sirius.components.diagrams.description.DiagramDescription)2 IRepresentationDescription (org.eclipse.sirius.components.representations.IRepresentationDescription)2 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Optional (java.util.Optional)1 UUID (java.util.UUID)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 ChangeKind (org.eclipse.sirius.components.collaborative.api.ChangeKind)1 IRepresentationPersistenceService (org.eclipse.sirius.components.collaborative.api.IRepresentationPersistenceService)1 IDiagramCreationService (org.eclipse.sirius.components.collaborative.diagrams.api.IDiagramCreationService)1 ICollaborativeDiagramMessageService (org.eclipse.sirius.components.collaborative.diagrams.messages.ICollaborativeDiagramMessageService)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1