use of org.eclipse.sirius.components.emf.services.EObjectIDManager in project sirius-web by eclipse-sirius.
the class EditingContextPersistenceService method save.
private Optional<DocumentEntity> save(Resource resource) {
Optional<DocumentEntity> result = Optional.empty();
HashMap<Object, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
resource.save(outputStream, options);
for (Resource.Diagnostic warning : resource.getWarnings()) {
this.logger.warn(warning.getMessage());
}
for (Resource.Diagnostic error : resource.getErrors()) {
this.logger.warn(error.getMessage());
}
byte[] bytes = outputStream.toByteArray();
String content = new String(bytes);
// @formatter:off
result = new IDParser().parse(resource.getURI().toString()).flatMap(this.documentRepository::findById).map(entity -> {
entity.setContent(content);
return this.documentRepository.save(entity);
});
// @formatter:on
} catch (IllegalArgumentException | IOException exception) {
this.logger.warn(exception.getMessage(), exception);
}
return result;
}
use of org.eclipse.sirius.components.emf.services.EObjectIDManager in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandlerTests method getEObjectUriToId.
/**
* Loads a json resource with the {@link #JSON_CONTENT} to map the uriFragment of eObjects to the id supplied by
* {@link EObjectIDManager}, then returns the map.
*/
private Map<String, String> getEObjectUriToId(EditingDomain editingDomain, byte[] resourceBytes) {
Map<String, String> eObjectUriToId = new HashMap<>();
// $NON-NLS-1$
JsonResource jsonResource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI("json.flow"));
editingDomain.getResourceSet().getResources().add(jsonResource);
Map<String, Object> options = new HashMap<>();
options.put(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager());
try {
// Load the resource to attach the contained element to the resource.
ByteArrayInputStream inputStream = new ByteArrayInputStream(resourceBytes);
jsonResource.load(inputStream, options);
/*
* Once the resource is loaded we cannot load it anymore, so we use the save to associate, in a map, the uri
* fragment of eObject to theirs IDs that have been computed by the IDSupplier during the previous load.
*/
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
jsonResource.save(outputStream, Collections.singletonMap(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager() {
@Override
public String getOrCreateId(EObject eObject) {
String id = super.getOrCreateId(eObject);
String uriFragment = jsonResource.getURIFragment(eObject);
eObjectUriToId.put(uriFragment, id);
return id;
}
}));
} catch (IOException exception) {
fail(exception.getMessage());
}
return eObjectUriToId;
}
use of org.eclipse.sirius.components.emf.services.EObjectIDManager in project sirius-web by eclipse-sirius.
the class UploadDocumentEventHandlerTests method testEObjectIDGenerationForUpload.
/**
* This method test the {@link UploadDocumentEventHandler} generates new IDs for objects loaded from
* {@link UploadFile} to ensure there will be no ID conflict with existing elements.
*/
@Test
public void testEObjectIDGenerationForUpload() {
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
byte[] resourceBytes = JSON_CONTENT.getBytes();
Map<String, String> eObjectUriToId = this.getEObjectUriToId(editingDomain, resourceBytes);
final UUID documentId = UUID.randomUUID();
this.simulatesDocumentUpload(editingDomain, documentId, resourceBytes);
Resource resource = editingDomain.getResourceSet().getResource(URI.createURI(documentId.toString()), false);
// Use an output stream to prevent the resource to be written on file system.
try (var outputStream = new ByteArrayOutputStream()) {
resource.save(outputStream, Collections.singletonMap(JsonResource.OPTION_ID_MANAGER, new EObjectIDManager() {
@Override
public String getOrCreateId(EObject eObject) {
// Should get the Id produced by the EObjectRandomIDSupplier during upload
String id = super.getOrCreateId(eObject);
String uriFragment = resource.getURIFragment(eObject);
// Use the map to get and check if, the id generate from the first load and the id generated during
// upload are different
String idBeforeUpload = eObjectUriToId.get(uriFragment);
assertThat(idBeforeUpload).isNotEqualTo(id);
return id;
}
}));
} catch (IOException exception) {
fail(exception.getMessage());
}
}
Aggregations