Search in sources :

Example 11 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method addRelations.

@Override
public void addRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = getSourceEntry().getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        Collection currentIds = (Collection) field.getIdAccessor().getValue(source);
        currentIds.addAll((Collection) targetIds);
    } else {
        RegistryEntry targetEntry = getTargetEntry(field);
        Iterable<D> targets = getTargets(targetEntry, targetIds);
        @SuppressWarnings("unchecked") Collection<D> currentTargets = getOrCreateCollection(source, fieldName);
        for (D target : targets) {
            currentTargets.add(target);
        }
    }
    sourceAdapter.update(source, getSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Collection(java.util.Collection) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 12 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation 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 13 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ForwardingStrategyContext method createSourceQuerySpec.

private QuerySpec createSourceQuerySpec() {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceInformation resourceInformation = sourceEntry.getResourceInformation();
    return new QuerySpec(resourceInformation.getResourceClass(), resourceInformation.getResourceType());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 14 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class RegistryEntry method getParentRegistryEntry.

public RegistryEntry getParentRegistryEntry() {
    ResourceInformation resourceInformation = getResourceInformation();
    String superResourceType = resourceInformation.getSuperResourceType();
    if (superResourceType != null) {
        ResourceRegistry resourceRegistry = moduleRegistry.getResourceRegistry();
        return resourceRegistry.getEntry(superResourceType);
    }
    return parentRegistryEntry;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation)

Example 15 with ResourceInformation

use of io.crnk.core.engine.information.resource.ResourceInformation in project crnk-framework by crnk-project.

the class ResourceFilterTest method checkFilterGetOnResourceField.

@Test
public void checkFilterGetOnResourceField() {
    // setup test data
    RegistryEntry entry = resourceRegistry.getEntry(Task.class);
    ResourceRepositoryAdapter resourceRepository = entry.getResourceRepository();
    Project project = new Project();
    project.setId(13L);
    project.setName("myProject");
    Task task = new Task();
    task.setId(12L);
    task.setName("myTask");
    task.setProject(project);
    resourceRepository.create(task, new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry));
    // get information
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField projectField = resourceInformation.findFieldByUnderlyingName("project");
    ResourceField nameField = resourceInformation.findFieldByUnderlyingName("name");
    String path = "/tasks/";
    String method = HttpMethod.GET.toString();
    Map<String, Set<String>> parameters = Collections.emptyMap();
    Document requestBody = null;
    // forbid field
    Mockito.when(filter.filterField(Mockito.eq(projectField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    Response response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    Resource taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertTrue(taskResource.getAttributes().containsKey("name"));
    // allow resource
    Mockito.when(filter.filterField(Mockito.eq(nameField), Mockito.any(HttpMethod.class))).thenReturn(FilterBehavior.FORBIDDEN);
    response = boot.getRequestDispatcher().dispatchRequest(path, method, parameters, null, requestBody);
    Assert.assertEquals(HttpStatus.OK_200, response.getHttpStatus().intValue());
    taskResource = response.getDocument().getCollectionData().get().get(0);
    Assert.assertTrue(taskResource.getRelationships().containsKey("projects"));
    Assert.assertFalse(taskResource.getRelationships().containsKey("project"));
    Assert.assertFalse(taskResource.getAttributes().containsKey("name"));
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) Resource(io.crnk.core.engine.document.Resource) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) Response(io.crnk.core.engine.dispatcher.Response) Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) HttpMethod(io.crnk.core.engine.http.HttpMethod) ResourceRegistryTest(io.crnk.core.resource.registry.ResourceRegistryTest) Test(org.junit.Test)

Aggregations

ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)167 Test (org.junit.Test)79 ResourceField (io.crnk.core.engine.information.resource.ResourceField)76 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)60 QuerySpec (io.crnk.core.queryspec.QuerySpec)16 Resource (io.crnk.core.engine.document.Resource)15 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)15 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)9 Task (io.crnk.core.mock.models.Task)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 HashSet (java.util.HashSet)9 Collection (java.util.Collection)8 Relationship (io.crnk.core.engine.document.Relationship)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)7 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 OffsetLimitPagingBehavior (io.crnk.core.queryspec.pagingspec.OffsetLimitPagingBehavior)7 DefaultResourceList (io.crnk.core.resource.list.DefaultResourceList)7 Set (java.util.Set)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6