use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.
the class EditingContextEPackageService method loadDomainDefinitions.
private void loadDomainDefinitions(ResourceSet resourceSet, DocumentEntity domainDocument) {
URI uri = URI.createURI(domainDocument.getId().toString());
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
try (var inputStream = new ByteArrayInputStream(domainDocument.getContent().getBytes())) {
resourceSet.getResources().add(resource);
resource.load(inputStream, null);
} catch (IOException | IllegalArgumentException exception) {
this.logger.warn(exception.getMessage(), exception);
}
}
use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.
the class EditingContextSearchService method findById.
@Override
public Optional<IEditingContext> findById(String editingContextId) {
long start = System.currentTimeMillis();
// $NON-NLS-1$
this.logger.debug("Loading the editing context {}", editingContextId);
AdapterFactoryEditingDomain editingDomain = new AdapterFactoryEditingDomain(this.composedAdapterFactory, new BasicCommandStack());
ResourceSet resourceSet = editingDomain.getResourceSet();
resourceSet.eAdapters().add(new ECrossReferenceAdapter());
EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
this.globalEPackageRegistry.forEach(ePackageRegistry::put);
List<EPackage> additionalEPackages = this.editingContextEPackageService.getEPackages(editingContextId);
additionalEPackages.forEach(ePackage -> ePackageRegistry.put(ePackage.getNsURI(), ePackage));
resourceSet.setPackageRegistry(ePackageRegistry);
List<DocumentEntity> documentEntities = new IDParser().parse(editingContextId).map(this.documentRepository::findAllByProjectId).orElseGet(List::of);
for (DocumentEntity documentEntity : documentEntities) {
URI uri = URI.createURI(documentEntity.getId().toString());
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
try (var inputStream = new ByteArrayInputStream(documentEntity.getContent().getBytes())) {
resourceSet.getResources().add(resource);
resource.load(inputStream, null);
resource.eAdapters().add(new DocumentMetadataAdapter(documentEntity.getName()));
} catch (IOException | IllegalArgumentException exception) {
// $NON-NLS-1$
this.logger.warn("An error occured while loading document {}: {}.", documentEntity.getId(), exception.getMessage());
resourceSet.getResources().remove(resource);
}
}
// $NON-NLS-1$
this.logger.debug("{} documents loaded for the editing context {}", resourceSet.getResources().size(), editingContextId);
long end = System.currentTimeMillis();
this.timer.record(end - start, TimeUnit.MILLISECONDS);
return Optional.of(new EditingContext(editingContextId, editingDomain));
}
use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.
the class RenameDocumentEventHandlerTests method testRenameDocument.
@Test
public void testRenameDocument() {
IDocumentService noOpDocumentService = new IDocumentService.NoOp() {
@Override
public Optional<Document> rename(UUID documentId, String newName) {
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
return Optional.of(new Document(documentId, new Project(UUID.randomUUID(), "", new Profile(UUID.randomUUID(), "username"), Visibility.PUBLIC), newName, "noContent"));
}
};
RenameDocumentEventHandler handler = new RenameDocumentEventHandler(noOpDocumentService, new NoOpServicesMessageService(), new SimpleMeterRegistry());
UUID documentId = UUID.randomUUID();
IInput input = new RenameDocumentInput(UUID.randomUUID(), documentId, NEW_NAME);
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
DocumentMetadataAdapter adapter = new DocumentMetadataAdapter(OLD_NAME);
Resource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(documentId.toString()));
resource.eAdapters().add(adapter);
editingDomain.getResourceSet().getResources().add(resource);
assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
assertThat(adapter.getName()).isEqualTo(OLD_NAME);
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);
assertThat(editingDomain.getResourceSet().getResources().size()).isEqualTo(1);
assertThat(adapter.getName()).isEqualTo(NEW_NAME);
ChangeDescription changeDescription = changeDescriptionSink.asFlux().blockFirst();
assertThat(changeDescription.getKind()).isEqualTo(ChangeKind.SEMANTIC_CHANGE);
IPayload payload = payloadSink.asMono().block();
assertThat(payload).isInstanceOf(RenameDocumentSuccessPayload.class);
}
use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.
the class StereotypeBuilder method getStereotypeBody.
public String getStereotypeBody(EObject rootEObject) {
// $NON-NLS-1$
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("inmemory"));
if (rootEObject != null) {
resource.getContents().add(rootEObject);
}
// $NON-NLS-1$
String content = "";
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
Map<String, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ENCODING, JsonResource.ENCODING_UTF_8);
options.put(JsonResource.OPTION_SCHEMA_LOCATION, Boolean.TRUE);
resource.save(outputStream, options);
content = outputStream.toString();
} catch (IOException exception) {
this.logger.error(exception.getMessage(), exception);
}
return content;
}
use of org.eclipse.sirius.components.emf.services.SiriusWebJSONResourceFactoryImpl in project sirius-web by eclipse-sirius.
the class CreateDocumentEventHandler method createDocument.
private void createDocument(One<IPayload> payloadSink, Many<ChangeDescription> changeDescriptionSink, IInput input, IEditingContext editingContext, String editingContextId, String name, StereotypeDescription stereotypeDescription) {
IPayload payload = new ErrorPayload(input.getId(), this.messageService.unexpectedError());
ChangeDescription changeDescription = new ChangeDescription(ChangeKind.NOTHING, editingContext.getId(), input);
// @formatter:off
Optional<AdapterFactoryEditingDomain> optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
if (optionalEditingDomain.isPresent()) {
AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
var optionalDocument = this.documentService.createDocument(editingContextId, name, stereotypeDescription.getContent());
if (optionalDocument.isPresent()) {
Document document = optionalDocument.get();
URI uri = URI.createURI(document.getId().toString());
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(uri);
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 CreateDocumentSuccessPayload(input.getId());
changeDescription = new ChangeDescription(ChangeKind.SEMANTIC_CHANGE, editingContext.getId(), input);
}
}
payloadSink.tryEmitValue(payload);
changeDescriptionSink.tryEmitNext(changeDescription);
}
Aggregations