Search in sources :

Example 1 with IDAdapter

use of org.eclipse.sirius.components.emf.services.IDAdapter in project sirius-components by eclipse-sirius.

the class EMFQueryServiceTests method createResourceWith4Elements.

private Resource createResourceWith4Elements() {
    Map<String, EObject> cache = new HashMap<>();
    EPackage ePackage = EcoreFactory.eINSTANCE.createEPackage();
    // $NON-NLS-1$
    UUID ePackageUUID = UUID.nameUUIDFromBytes("ePackage".getBytes());
    ePackage.eAdapters().add(new IDAdapter(ePackageUUID));
    cache.put(ePackageUUID.toString(), ePackage);
    EClass class1 = EcoreFactory.eINSTANCE.createEClass();
    // $NON-NLS-1$
    UUID class1UUID = UUID.nameUUIDFromBytes("class1".getBytes());
    class1.eAdapters().add(new IDAdapter(class1UUID));
    cache.put(class1UUID.toString(), ePackage);
    EClass class2 = EcoreFactory.eINSTANCE.createEClass();
    // $NON-NLS-1$
    UUID class2UUID = UUID.nameUUIDFromBytes("class2".getBytes());
    class2.eAdapters().add(new IDAdapter(class2UUID));
    cache.put(class2UUID.toString(), ePackage);
    EClass class3 = EcoreFactory.eINSTANCE.createEClass();
    // $NON-NLS-1$
    UUID class3UUID = UUID.nameUUIDFromBytes("class3".getBytes());
    class3.eAdapters().add(new IDAdapter(class3UUID));
    cache.put(class3UUID.toString(), ePackage);
    ePackage.getEClassifiers().addAll(List.of(class1, class2, class3));
    XMIResource resource = new XMIResourceImpl() {

        @Override
        protected EObject getEObjectByID(String id) {
            return cache.get(id);
        }
    };
    resource.getContents().add(ePackage);
    return resource;
}
Also used : EClass(org.eclipse.emf.ecore.EClass) HashMap(java.util.HashMap) EObject(org.eclipse.emf.ecore.EObject) UUID(java.util.UUID) IDAdapter(org.eclipse.sirius.components.emf.services.IDAdapter) XMIResource(org.eclipse.emf.ecore.xmi.XMIResource) XMIResourceImpl(org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl) EPackage(org.eclipse.emf.ecore.EPackage)

Example 2 with IDAdapter

use of org.eclipse.sirius.components.emf.services.IDAdapter in project sirius-components by eclipse-sirius.

the class EMFQueryServiceTests method testEMFQueryServiceGetObjectById.

@Test
public void testEMFQueryServiceGetObjectById() {
    EditingContext editingContext = this.createEditingContext();
    IEditingContextEPackageService editingContextEPackageService = new IEditingContextEPackageService() {

        @Override
        public List<EPackage> getEPackages(String editingContextId) {
            return List.of(EcorePackage.eINSTANCE);
        }
    };
    IQueryService queryService = new EMFQueryService(editingContextEPackageService, List.of());
    // @formatter:off
    EObject eObjectToRetrieve = editingContext.getDomain().getResourceSet().getResources().get(0).getContents().get(0);
    Optional<IDAdapter> optionalIDAdapter = eObjectToRetrieve.eAdapters().stream().filter(IDAdapter.class::isInstance).map(IDAdapter.class::cast).findFirst();
    // $NON-NLS-1$
    String id = optionalIDAdapter.map(IDAdapter::getId).map(Object::toString).orElse("");
    // @formatter:on
    // $NON-NLS-1$ //$NON-NLS-2$
    QueryBasedObjectInput input = new QueryBasedObjectInput(UUID.randomUUID(), "aql:editingContext.getObjectById('" + id + "')", Map.of());
    IPayload payload = queryService.execute(editingContext, input);
    assertTrue(payload instanceof QueryBasedObjectSuccessPayload);
    assertEquals(eObjectToRetrieve, ((QueryBasedObjectSuccessPayload) payload).getResult());
    // $NON-NLS-1$ //$NON-NLS-2$
    input = new QueryBasedObjectInput(UUID.randomUUID(), "aql:editingContext.getObjectById('" + id + "wrong')", Map.of());
    payload = queryService.execute(editingContext, input);
    assertTrue(payload instanceof ErrorPayload);
}
Also used : IQueryService(org.eclipse.sirius.components.collaborative.api.IQueryService) QueryBasedObjectSuccessPayload(org.eclipse.sirius.components.collaborative.dto.QueryBasedObjectSuccessPayload) IEditingContextEPackageService(org.eclipse.sirius.components.emf.services.IEditingContextEPackageService) IDAdapter(org.eclipse.sirius.components.emf.services.IDAdapter) EPackage(org.eclipse.emf.ecore.EPackage) IPayload(org.eclipse.sirius.components.core.api.IPayload) ErrorPayload(org.eclipse.sirius.components.core.api.ErrorPayload) IEditingContext(org.eclipse.sirius.components.core.api.IEditingContext) EditingContext(org.eclipse.sirius.components.emf.services.EditingContext) EObject(org.eclipse.emf.ecore.EObject) QueryBasedObjectInput(org.eclipse.sirius.components.collaborative.dto.QueryBasedObjectInput) Test(org.junit.jupiter.api.Test)

Aggregations

EObject (org.eclipse.emf.ecore.EObject)2 EPackage (org.eclipse.emf.ecore.EPackage)2 IDAdapter (org.eclipse.sirius.components.emf.services.IDAdapter)2 HashMap (java.util.HashMap)1 UUID (java.util.UUID)1 EClass (org.eclipse.emf.ecore.EClass)1 XMIResource (org.eclipse.emf.ecore.xmi.XMIResource)1 XMIResourceImpl (org.eclipse.emf.ecore.xmi.impl.XMIResourceImpl)1 IQueryService (org.eclipse.sirius.components.collaborative.api.IQueryService)1 QueryBasedObjectInput (org.eclipse.sirius.components.collaborative.dto.QueryBasedObjectInput)1 QueryBasedObjectSuccessPayload (org.eclipse.sirius.components.collaborative.dto.QueryBasedObjectSuccessPayload)1 ErrorPayload (org.eclipse.sirius.components.core.api.ErrorPayload)1 IEditingContext (org.eclipse.sirius.components.core.api.IEditingContext)1 IPayload (org.eclipse.sirius.components.core.api.IPayload)1 EditingContext (org.eclipse.sirius.components.emf.services.EditingContext)1 IEditingContextEPackageService (org.eclipse.sirius.components.emf.services.IEditingContextEPackageService)1 Test (org.junit.jupiter.api.Test)1