use of org.eclipse.sirius.web.services.api.document.UploadDocumentSuccessPayload 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.UploadDocumentSuccessPayload 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);
}
Aggregations