use of org.eclipse.sirius.web.services.api.id.IDParser 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.web.services.api.id.IDParser 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.web.services.api.id.IDParser in project sirius-web by eclipse-sirius.
the class DeleteDocumentTreeItemEventHandler method handle.
@Override
public IStatus handle(IEditingContext editingContext, TreeItem treeItem) {
// @formatter:off
var optionalEditingDomain = Optional.of(editingContext).filter(EditingContext.class::isInstance).map(EditingContext.class::cast).map(EditingContext::getDomain);
// @formatter:on
var optionalDocumentEntity = new IDParser().parse(treeItem.getId()).flatMap(this.documentRepository::findById);
if (optionalEditingDomain.isPresent() && optionalDocumentEntity.isPresent()) {
AdapterFactoryEditingDomain editingDomain = optionalEditingDomain.get();
DocumentEntity documentEntity = optionalDocumentEntity.get();
ResourceSet resourceSet = editingDomain.getResourceSet();
URI uri = URI.createURI(documentEntity.getId().toString());
// @formatter:off
List<Resource> resourcesToDelete = resourceSet.getResources().stream().filter(resource -> resource.getURI().equals(uri)).collect(Collectors.toUnmodifiableList());
resourcesToDelete.stream().forEach(resourceSet.getResources()::remove);
// @formatter:on
this.documentRepository.delete(documentEntity);
return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of());
}
// $NON-NLS-1$
return new Failure("");
}
use of org.eclipse.sirius.web.services.api.id.IDParser in project sirius-web by eclipse-sirius.
the class RepresentationService method save.
@Override
public void save(IEditingContext editingContext, ISemanticRepresentation representation) {
long start = System.currentTimeMillis();
var editingContextId = new IDParser().parse(editingContext.getId());
var representationId = new IDParser().parse(representation.getId());
if (editingContextId.isPresent() && representationId.isPresent()) {
UUID editingContextUUID = editingContextId.get();
UUID representationUUID = representationId.get();
var optionalProjectEntity = this.projectRepository.findById(editingContextUUID);
if (optionalProjectEntity.isPresent()) {
ProjectEntity projectEntity = optionalProjectEntity.get();
RepresentationEntity representationEntity = this.toEntity(projectEntity, representationUUID, representation);
this.representationRepository.save(representationEntity);
}
}
long end = System.currentTimeMillis();
this.timer.record(end - start, TimeUnit.MILLISECONDS);
}
use of org.eclipse.sirius.web.services.api.id.IDParser in project sirius-web by eclipse-sirius.
the class RepresentationService method getRepresentationDescriptorForProjectId.
@Override
public Optional<RepresentationDescriptor> getRepresentationDescriptorForProjectId(String projectId, String representationId) {
var projectUUID = new IDParser().parse(projectId);
var representationUUID = new IDParser().parse(representationId);
if (projectUUID.isPresent() && representationUUID.isPresent()) {
return this.representationRepository.findByIdAndProjectId(representationUUID.get(), projectUUID.get()).map(new RepresentationMapper(this.objectMapper)::toDTO);
}
return Optional.empty();
}
Aggregations