use of org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter 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.documents.DocumentMetadataAdapter in project sirius-web by eclipse-sirius.
the class ExplorerDescriptionProvider method getLabel.
private String getLabel(VariableManager variableManager) {
Object self = variableManager.getVariables().get(VariableManager.SELF);
// $NON-NLS-1$
String label = "";
if (self instanceof RepresentationMetadata) {
label = ((RepresentationMetadata) self).getLabel();
} else if (self instanceof Resource) {
Resource resource = (Resource) self;
// @formatter:off
label = resource.eAdapters().stream().filter(DocumentMetadataAdapter.class::isInstance).map(DocumentMetadataAdapter.class::cast).findFirst().map(DocumentMetadataAdapter::getName).orElse(resource.getURI().lastSegment());
// @formatter:on
} else if (self instanceof EObject) {
label = this.objectService.getLabel(self);
if (label.isBlank()) {
var kind = this.objectService.getKind(self);
label = this.kindParser.getParameterValues(kind).get(SemanticKindConstants.ENTITY_ARGUMENT).get(0);
}
}
return label;
}
use of org.eclipse.sirius.web.services.documents.DocumentMetadataAdapter in project sirius-web by eclipse-sirius.
the class EditingContextPersistenceServiceTests method testDocumentPersistence.
@Test
public void testDocumentPersistence() {
UUID projectId = UUID.randomUUID();
// $NON-NLS-1$
String name = "New Document";
UUID id = UUID.randomUUID();
JsonResource resource = new SiriusWebJSONResourceFactoryImpl().createResource(URI.createURI(id.toString()));
resource.eAdapters().add(new DocumentMetadataAdapter(name));
EClass eClass = EcoreFactory.eINSTANCE.createEClass();
// $NON-NLS-1$
eClass.setName("Concept");
resource.getContents().add(eClass);
AdapterFactoryEditingDomain editingDomain = new EditingDomainFactory().create();
editingDomain.getResourceSet().getResources().add(resource);
ProjectEntity projectEntity = new ProjectEntity();
projectEntity.setId(projectId);
// $NON-NLS-1$
projectEntity.setName("");
AccountEntity owner = new AccountEntity();
owner.setId(UUID.randomUUID());
// $NON-NLS-1$
owner.setUsername("jdoe");
projectEntity.setOwner(owner);
DocumentEntity existingEntity = new DocumentEntity();
existingEntity.setId(id);
existingEntity.setProject(projectEntity);
existingEntity.setName(name);
// $NON-NLS-1$
existingEntity.setContent("");
List<DocumentEntity> entities = new ArrayList<>();
IDocumentRepository documentRepository = new NoOpDocumentRepository() {
@Override
public <S extends DocumentEntity> S save(S entity) {
entities.add(entity);
return entity;
}
@Override
public Optional<DocumentEntity> findById(UUID id) {
return Optional.of(existingEntity);
}
};
IEditingContextPersistenceService editingContextPersistenceService = new EditingContextPersistenceService(documentRepository, new NoOpApplicationEventPublisher(), new SimpleMeterRegistry());
assertThat(entities).hasSize(0);
IEditingContext editingContext = new EditingContext(UUID.randomUUID().toString(), editingDomain);
editingContextPersistenceService.persist(editingContext);
assertThat(entities).hasSize(1);
DocumentEntity documentEntity = entities.get(0);
assertThat(documentEntity.getId()).isEqualTo(id);
assertThat(documentEntity.getName()).isEqualTo(name);
assertThat(documentEntity.getProject().getId()).isEqualTo(projectId);
}
Aggregations