use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.
the class MutationUploadDocumentDataFetcher method get.
@Override
public CompletableFuture<IPayload> get(DataFetchingEnvironment environment) throws Exception {
Map<Object, Object> inputArgument = environment.getArgument(MutationTypeProvider.INPUT_ARGUMENT);
// We cannot use directly UploadDocumentInput, the objectMapper cannot handle the file stream.
// @formatter:off
UUID id = Optional.of(inputArgument.get(ID)).filter(String.class::isInstance).map(String.class::cast).flatMap(new IDParser()::parse).orElse(null);
String editingContextId = Optional.of(inputArgument.get(EDITING_CONTEXT_ID)).filter(String.class::isInstance).map(String.class::cast).orElse(null);
UploadFile file = Optional.of(inputArgument.get(FILE)).filter(UploadFile.class::isInstance).map(UploadFile.class::cast).orElse(null);
// @formatter:on
UploadDocumentInput input = new UploadDocumentInput(id, editingContextId, file);
// @formatter:off
return this.editingContextEventProcessorRegistry.dispatchEvent(input.getEditingContextId(), input).defaultIfEmpty(new ErrorPayload(input.getId(), this.messageService.unexpectedError())).toFuture();
// @formatter:on
}
use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandler method handle.
@Override
public void handle(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IEditingContext editingContext, IInput input) {
this.counter.increment();
IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
if (input instanceof UploadDocumentInput) {
UploadDocumentInput uploadDocumentInput = (UploadDocumentInput) input;
String projectId = uploadDocumentInput.getEditingContextId();
UploadFile file = uploadDocumentInput.getFile();
// @formatter:off
Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
// @formatter:on
String name = file.getName().trim();
if (optionalEditingDomain.isPresent()) {
AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
String content = this.getContent(adapterFactoryEditingDomain.getResourceSet().getPackageRegistry(), file);
var optionalDocument = this.documentService.createDocument(projectId, name, content);
if (optionalDocument.isPresent()) {
Document document = optionalDocument.get();
ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
URI uri = URI.createURI(document.getId().toString());
if (resourceSet.getResource(uri, false) == null) {
ResourceSet loadingResourceSet = new ResourceSetImpl();
loadingResourceSet.setPackageRegistry(resourceSet.getPackageRegistry());
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
loadingResourceSet.getResources().add(resource);
try (var inputStream = new ByteArrayInputStream(document.getContent().getBytes())) {
resource.load(inputStream, null);
} catch (IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
resource.eAdapters().add(new DocumentMetadataAdapter(name));
resourceSet.getResources().add(resource);
payload = new UploadDocumentSuccessPayload(input.getId(), document);
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
}
}
}
}
payloadSink.tryEmitValue(payload);
changeDescriptionSink.tryEmitNext(changeDescription);
}
use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.
the class ProjectImporter method createDocuments.
/**
* Creates all documents in the project thanks to the {@link IEditingContextEventProcessor} and the
* {@link CreateDocumentFromUploadEvent}. If at least one document has not been created it will return
* <code>false</code>.
*
* @param inputId
*
* @return <code>true</code> whether all documents has been created, <code>false</code> otherwise
*/
private boolean createDocuments(UUID inputId) {
for (Entry<String, UploadFile> entry : this.documents.entrySet()) {
String oldDocumentId = entry.getKey();
UploadFile uploadFile = entry.getValue();
UploadDocumentInput input = new UploadDocumentInput(inputId, this.projectId, uploadFile);
// @formatter:off
Document document = this.editingContextEventProcessor.handle(input).filter(UploadDocumentSuccessPayload.class::isInstance).map(UploadDocumentSuccessPayload.class::cast).map(UploadDocumentSuccessPayload::getDocument).blockOptional().orElse(null);
// @formatter:on
if (document == null) {
// $NON-NLS-1$
this.logger.warn("The document {} has not been created", this.projectManifest.getDocumentIdsToName().get(oldDocumentId));
}
this.oldDocumentIdToNewDocument.put(oldDocumentId, document);
}
return this.oldDocumentIdToNewDocument.values().stream().allMatch(Objects::nonNull);
}
use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandlerTests method simulatesDocumentUpload.
/**
* Simulates a document upload with fake services. The uploaded resource will be put in the given editing domain
* with the documentId as URI.
*
* @param editingDomain
* The editing domain where the uploaded resource will be put
* @param documentId
* The id of the document to upload
* @param resourceBytes
* The content of the document to upload
*/
private void simulatesDocumentUpload(AdapterFactoryEditingDomain editingDomain, UUID documentId, byte[] resourceBytes) {
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(documentId, new Project(UUID.fromString(projectId), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), name, content));
}
};
IServicesMessageService messageService = new NoOpServicesMessageService();
UploadDocumentEventHandler handler = new UploadDocumentEventHandler(documentService, messageService, new SimpleMeterRegistry());
UploadFile file = new UploadFile(FILE_NAME, new ByteArrayInputStream(resourceBytes));
var input = new UploadDocumentInput(UUID.randomUUID(), UUID.randomUUID().toString(), file);
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);
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(UploadDocumentSuccessPayload.class);
}
use of org.eclipse.sirius.web.services.api.document.UploadDocumentInput in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandlerTests method uploadDocument.
private EditingDomain uploadDocument(InputStream inputstream) {
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));
}
};
IServicesMessageService messageService = new NoOpServicesMessageService();
UploadDocumentEventHandler handler = new UploadDocumentEventHandler(documentService, messageService, new SimpleMeterRegistry());
UploadFile file = new UploadFile(FILE_NAME, inputstream);
var input = new UploadDocumentInput(UUID.randomUUID(), UUID.randomUUID().toString(), file);
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
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);
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(UploadDocumentSuccessPayload.class);
return editingDomain;
}
Aggregations