Search in sources :

Example 1 with CreateProjectInput

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

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

the class ProjectServiceTests method testProjectCreationSuccess.

@Test
public void testProjectCreationSuccess() {
    // $NON-NLS-1$
    Object principal = new User(OWNER_NAME, "", List.of());
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, new Object()));
    CreateProjectInput input = new CreateProjectInput(UUID.randomUUID(), NEW_PROJECT, Visibility.PRIVATE);
    IPayload payload = this.projectService.createProject(input);
    assertThat(payload).isInstanceOf(CreateProjectSuccessPayload.class);
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) CreateProjectInput(org.eclipse.sirius.web.services.api.projects.CreateProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) Test(org.junit.jupiter.api.Test)

Example 3 with CreateProjectInput

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

the class ProjectServiceTests method testCreateProjectsWithSameName.

@Test
public void testCreateProjectsWithSameName() {
    // $NON-NLS-1$
    Object principal = new User(OWNER_NAME, "", List.of());
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, new Object()));
    CreateProjectInput firstInput = new CreateProjectInput(UUID.randomUUID(), NEW_PROJECT, Visibility.PRIVATE);
    IPayload payload = this.projectService.createProject(firstInput);
    assertThat(payload).isInstanceOf(CreateProjectSuccessPayload.class);
    CreateProjectInput secondInput = new CreateProjectInput(UUID.randomUUID(), NEW_PROJECT, Visibility.PRIVATE);
    IPayload secondPayload = this.projectService.createProject(secondInput);
    assertThat(secondPayload).isInstanceOf(CreateProjectSuccessPayload.class);
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) CreateProjectInput(org.eclipse.sirius.web.services.api.projects.CreateProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) Test(org.junit.jupiter.api.Test)

Example 4 with CreateProjectInput

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

the class ProjectServiceTests method testProjectCreationWithInvalidName.

@Test
public void testProjectCreationWithInvalidName() {
    // $NON-NLS-1$
    Object principal = new User(OWNER_NAME, "", List.of());
    SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(principal, new Object()));
    // $NON-NLS-1$
    CreateProjectInput input = new CreateProjectInput(UUID.randomUUID(), "", Visibility.PRIVATE);
    IPayload payload = this.projectService.createProject(input);
    assertThat(payload).isInstanceOf(ErrorPayload.class);
    SecurityContextHolder.getContext().setAuthentication(null);
}
Also used : User(org.springframework.security.core.userdetails.User) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) CreateProjectInput(org.eclipse.sirius.web.services.api.projects.CreateProjectInput) IPayload(org.eclipse.sirius.components.core.api.IPayload) Test(org.junit.jupiter.api.Test)

Aggregations

IPayload (org.eclipse.sirius.components.core.api.IPayload)4 CreateProjectInput (org.eclipse.sirius.web.services.api.projects.CreateProjectInput)4 Test (org.junit.jupiter.api.Test)3 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)3 User (org.springframework.security.core.userdetails.User)3 IEditingContextEventProcessor (org.eclipse.sirius.components.collaborative.api.IEditingContextEventProcessor)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 UploadFile (org.eclipse.sirius.components.graphql.api.UploadFile)1 CreateProjectSuccessPayload (org.eclipse.sirius.web.services.api.projects.CreateProjectSuccessPayload)1 Project (org.eclipse.sirius.web.services.api.projects.Project)1 ProjectManifest (org.eclipse.sirius.web.services.api.projects.ProjectManifest)1 UnzippedProject (org.eclipse.sirius.web.services.api.projects.UnzippedProject)1 UploadProjectSuccessPayload (org.eclipse.sirius.web.services.api.projects.UploadProjectSuccessPayload)1 RepresentationDescriptor (org.eclipse.sirius.web.services.api.representations.RepresentationDescriptor)1