Search in sources :

Example 1 with Project

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

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

the class ProjectService method createProject.

@Override
public IPayload createProject(CreateProjectInput input) {
    IPayload payload = null;
    String name = input.getName().trim();
    if (!this.isValidProjectName(name)) {
        payload = new ErrorPayload(input.getId(), this.messageService.invalidProjectName());
    } else {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        var optionalOwner = this.accountRepository.findByUsername(authentication.getName());
        if (!optionalOwner.isEmpty()) {
            ProjectEntity projectEntity = this.createProjectEntity(name, optionalOwner.get(), input.getVisibility());
            projectEntity = this.projectRepository.save(projectEntity);
            Project project = this.projectMapper.toDTO(projectEntity);
            payload = new CreateProjectSuccessPayload(input.getId(), project);
        }
    }
    return payload;
}
Also used : Project(org.eclipse.sirius.web.services.api.projects.Project) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ProjectEntity(org.eclipse.sirius.web.persistence.entities.ProjectEntity) Authentication(org.springframework.security.core.Authentication) CreateProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.CreateProjectSuccessPayload) IPayload(org.eclipse.sirius.components.core.api.IPayload)

Example 3 with Project

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

the class RenameProjectEventHandler method handle.

@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
    String message = this.messageService.invalidInput(input.getClass().getSimpleName(), RenameProjectInput.class.getSimpleName());
    IPayload payload = new ErrorPayload(input.getId(), message);
    ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
    if (input instanceof RenameProjectInput) {
        RenameProjectInput renameProjectInput = (RenameProjectInput) input;
        Optional<Project> optionalProject = this.projectService.renameProject(renameProjectInput.getProjectId(), renameProjectInput.getNewName());
        if (optionalProject.isPresent()) {
            payload = new RenameProjectSuccessPayload(input.getId(), optionalProject.get());
            changeDescription = new ChangeDescription(ChangeKind.PROJECT_RENAMING, editingContext.getId(), input);
        }
    }
    payloadSink.tryEmitValue(payload);
    changeDescriptionSink.tryEmitNext(changeDescription);
}
Also used : Project(org.eclipse.sirius.web.services.api.projects.Project) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) ChangeDescription(org.eclipse.sirius.components.collaborative.api.ChangeDescription) RenameProjectSuccessPayload(org.eclipse.sirius.web.services.api.projects.RenameProjectSuccessPayload) RenameProjectInput(org.eclipse.sirius.web.services.api.projects.RenameProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload)

Example 4 with Project

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

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

Aggregations

Project (org.eclipse.sirius.web.services.api.projects.Project)16 IPayload (org.eclipse.sirius.components.core.api.IPayload)10 Profile (org.eclipse.sirius.web.services.api.accounts.Profile)10 ChangeDescription (org.eclipse.sirius.components.collaborative.api.ChangeDescription)8 Document (org.eclipse.sirius.web.services.api.document.Document)8 UUID (java.util.UUID)7 IDocumentService (org.eclipse.sirius.web.services.api.document.IDocumentService)7 Test (org.junit.jupiter.api.Test)7 SimpleMeterRegistry (io.micrometer.core.instrument.simple.SimpleMeterRegistry)6 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)6 NoOpServicesMessageService (org.eclipse.sirius.web.services.projects.NoOpServicesMessageService)6 AdapterFactoryEditingDomain (org.eclipse.emf.edit.domain.AdapterFactoryEditingDomain)5 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)4 IServicesMessageService (org.eclipse.sirius.web.services.messages.IServicesMessageService)4 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)3 UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)3 IProjectService (org.eclipse.sirius.web.services.api.projects.IProjectService)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 Resource (org.eclipse.emf.ecore.resource.Resource)2 CreateDocumentInput (org.eclipse.sirius.components.collaborative.dto.CreateDocumentInput)2