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;
}
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;
}
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);
}
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);
}
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);
}
Aggregations