Search in sources :

Example 1 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = getTargetEntry(field);
    String oppositeName = getOppositeName(fieldName);
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
    idQuerySpec.includeRelation(Arrays.asList(oppositeName));
    ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
    JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
    List<D> results = (List<D>) response.getEntity();
    MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {

        @Override
        protected List<D> newList() {
            return new DefaultResourceList<>();
        }
    };
    Set<I> sourceIdSet = new HashSet<>();
    for (I sourceId : sourceIds) {
        sourceIdSet.add(sourceId);
    }
    for (D result : results) {
        handleTarget(bulkResult, result, sourceIdSet, oppositeName, sourceInformation);
    }
    return bulkResult;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceList(io.crnk.core.resource.list.ResourceList) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 2 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class GetFromOwnerStrategy method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = context.getTargetEntry(field);
    List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, context.createSaveQueryAdapter(fieldName)).getEntity();
    ResourceInformation targetInformation = targetEntry.getResourceInformation();
    List<D> targets;
    if (field.hasIdField()) {
        Set targetIds = new HashSet();
        for (Object source : sources) {
            Object targetId = field.getIdAccessor().getValue(source);
            if (field.isCollection()) {
                targetIds.addAll((Collection) targetId);
            } else {
                targetIds.add(targetId);
            }
        }
        QuerySpec idQuerySpec = new QuerySpec(targetInformation);
        ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
        JsonApiResponse response = targetAdapter.findAll(targetIds, context.createQueryAdapter(idQuerySpec));
        targets = (List<D>) response.getEntity();
        return toResult(field, targetInformation, sources, targets);
    } else {
        MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {

            @Override
            protected List<D> newList() {
                return new DefaultResourceList<>();
            }
        };
        for (Object source : sources) {
            Object sourceId = sourceInformation.getId(source);
            Object target = field.getAccessor().getValue(source);
            if (target != null) {
                if (field.isCollection()) {
                    bulkResult.addAll(sourceId, (Collection) target);
                } else {
                    bulkResult.add(sourceId, target);
                }
            }
        }
        return bulkResult;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) HashSet(java.util.HashSet) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 3 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class ImplicitOwnerBasedRelationshipRepositoryTest method checkSetRelations.

@Test
public void checkSetRelations() {
    taskProjectRepository.setRelations(task, Arrays.asList(42L), "projects");
    Assert.assertEquals(1, task.getProjects().size());
    Assert.assertEquals(42L, task.getProjects().iterator().next().getId().longValue());
    MultivaluedMap targets = taskProjectRepository.findTargets(Arrays.asList(task.getId()), "projects", new QuerySpec(Task.class));
    Assert.assertEquals(1, targets.keySet().size());
    Assert.assertEquals(task.getId(), targets.keySet().iterator().next());
    Project target = (Project) targets.getUnique(task.getId());
    Assert.assertEquals(42L, target.getId().longValue());
}
Also used : Project(io.crnk.core.mock.models.Project) Task(io.crnk.core.mock.models.Task) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) QuerySpec(io.crnk.core.queryspec.QuerySpec) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 4 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class OwnerFowardingRelationshipRepositoryTest method checkSetRelationIds.

@Test
public void checkSetRelationIds() {
    relRepository.setRelations(resource, Arrays.asList(3L, 4L), "testMultipleValues");
    Assert.assertEquals(Arrays.asList(3L, 4L), resource.getTestMultipleValueIds());
    List<Schedule> targets = relRepository.findManyTargets(resource.getId(), "testMultipleValues", new QuerySpec(Schedule.class));
    Assert.assertEquals(2, targets.size());
    Assert.assertSame(schedule3, targets.get(0));
    Assert.assertSame(4L, targets.get(1).getId().longValue());
    MultivaluedMap targetsMap = relRepository.findTargets(Arrays.asList(resource.getId()), "testMultipleValues", new QuerySpec(Schedule.class));
    Assert.assertEquals(1, targetsMap.keySet().size());
    targets = targetsMap.getList(resource.getId());
    Assert.assertEquals(2, targets.size());
    Assert.assertSame(3L, targets.get(0).getId().longValue());
    Assert.assertSame(4L, targets.get(1).getId().longValue());
}
Also used : Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Example 5 with MultivaluedMap

use of io.crnk.core.engine.internal.utils.MultivaluedMap in project crnk-framework by crnk-project.

the class OwnerFowardingRelationshipRepositoryTest method checkSetRelationId.

@Test
public void checkSetRelationId() {
    relRepository.setRelation(resource, 3L, "testSerializeEager");
    Assert.assertEquals(3L, resource.getTestSerializeEagerId().longValue());
    Assert.assertNull(resource.getTestSerializeEager());
    Assert.assertSame(schedule3, relRepository.findOneTarget(resource.getId(), "testSerializeEager", new QuerySpec(Schedule.class)));
    MultivaluedMap targets = relRepository.findTargets(Arrays.asList(resource.getId()), "testSerializeEager", new QuerySpec(Schedule.class));
    Assert.assertEquals(1, targets.keySet().size());
    Object target = targets.getUnique(resource.getId());
    Assert.assertEquals(schedule3, target);
    relRepository.setRelation(resource, null, "testSerializeEager");
    Assert.assertNull(resource.getTestSerializeEagerId());
    Assert.assertNull(resource.getTestSerializeEager());
}
Also used : Schedule(io.crnk.core.mock.models.Schedule) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Aggregations

MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)14 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)6 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)6 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)6 Test (org.junit.Test)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)5 Schedule (io.crnk.core.mock.models.Schedule)4 HashSet (java.util.HashSet)4 Collection (java.util.Collection)3 HashMap (java.util.HashMap)3 List (java.util.List)3 Project (io.crnk.core.mock.models.Project)2 Task (io.crnk.core.mock.models.Task)2 FilterSpec (io.crnk.core.queryspec.FilterSpec)2 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)2 Map (java.util.Map)2 Set (java.util.Set)2