use of org.eclipse.sirius.web.persistence.entities.DocumentEntity in project sirius-web by eclipse-sirius.
the class EditingContextEPackageService method findDynamicEPackages.
/**
* Returns all the EPackages defined by a Domain definition.
*/
private Stream<EPackage> findDynamicEPackages(Function<Domain, Optional<EPackage>> domainConverter) {
ResourceSet resourceSet = new ResourceSetImpl();
EPackageRegistryImpl ePackageRegistry = new EPackageRegistryImpl();
this.globalEPackageRegistry.forEach(ePackageRegistry::put);
resourceSet.setPackageRegistry(ePackageRegistry);
var domainDocumentEntities = this.documentRepository.findAllByType(DomainPackage.eNAME, DomainPackage.eNS_URI);
for (DocumentEntity domainDocumentEntity : domainDocumentEntities) {
this.loadDomainDefinitions(resourceSet, domainDocumentEntity);
}
return resourceSet.getResources().stream().flatMap(res -> this.convertDomains(res, domainConverter));
}
use of org.eclipse.sirius.web.persistence.entities.DocumentEntity 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.persistence.entities.DocumentEntity 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.persistence.entities.DocumentEntity 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.persistence.entities.DocumentEntity in project sirius-web by eclipse-sirius.
the class RenameDocumentTreeItemHandler method handle.
@Override
public IStatus handle(IEditingContext editingContext, TreeItem treeItem, String newLabel) {
// @formatter:off
Optional<AdapterFactoryEditingDomain> 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()) {
DocumentEntity documentEntity = optionalDocumentEntity.get();
documentEntity.setName(newLabel);
this.documentRepository.save(documentEntity);
AdapterFactoryEditingDomain adapterFactoryEditingDomain = optionalEditingDomain.get();
ResourceSet resourceSet = adapterFactoryEditingDomain.getResourceSet();
// @formatter:off
resourceSet.getResources().stream().filter(resource -> documentEntity.getId().toString().equals(resource.getURI().toString())).findFirst().ifPresent(resource -> {
resource.eAdapters().stream().filter(DocumentMetadataAdapter.class::isInstance).map(DocumentMetadataAdapter.class::cast).findFirst().ifPresent(adapter -> adapter.setName(documentEntity.getName()));
});
// @formatter:on
return new Success(ChangeKind.SEMANTIC_CHANGE, Map.of());
}
// $NON-NLS-1$
return new Failure("");
}
Aggregations