Search in sources :

Example 16 with ResourceInformation

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

the class FieldResourceGetTest method onGivenIncludeRequestFieldResourcesGetShouldHandleIt.

@Test
@SuppressWarnings({ "rawtypes", "unchecked" })
public void onGivenIncludeRequestFieldResourcesGetShouldHandleIt() throws Exception {
    // get repositories
    ResourceRepositoryAdapter userRepo = resourceRegistry.getEntry(User.class).getResourceRepository(null);
    ResourceRepositoryAdapter projectRepo = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
    ResourceRepositoryAdapter taskRepo = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryUserToProject = resourceRegistry.getEntry(User.class).getRelationshipRepository("assignedProjects", null);
    RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.getEntry(Project.class).getRelationshipRepository("tasks", null);
    ResourceInformation userInfo = resourceRegistry.getEntry(User.class).getResourceInformation();
    ResourceInformation projectInfo = resourceRegistry.getEntry(Project.class).getResourceInformation();
    ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
    ResourceField assignedProjectsField = userInfo.findRelationshipFieldByName("assignedProjects");
    // setup test data
    User user = new User();
    user.setId(1L);
    userRepo.create(user, emptyUserQuery);
    Project project = new Project();
    project.setId(2L);
    projectRepo.create(project, emptyProjectQuery);
    Task task = new Task();
    task.setId(3L);
    taskRepo.create(task, emptyTaskQuery);
    relRepositoryUserToProject.setRelations(user, Collections.singletonList(project.getId()), assignedProjectsField, emptyProjectQuery);
    relRepositoryProjectToTask.setRelation(project, task.getId(), includedTaskField, emptyTaskQuery);
    Map<String, Set<String>> params = new HashMap<String, Set<String>>();
    addParams(params, "include[projects]", "includedTask");
    QueryParams queryParams = queryParamsBuilder.buildQueryParams(params);
    QueryAdapter queryAdapter = new QueryParamsAdapter(projectInfo, queryParams, moduleRegistry);
    JsonPath jsonPath = pathBuilder.build("/users/1/assignedProjects");
    FieldResourceGet sut = new FieldResourceGet(resourceRegistry, typeParser, documentMapper);
    Response response = sut.handle(jsonPath, queryAdapter, null, null);
    // THEN
    Assert.assertNotNull(response);
    Assert.assertNotNull(response.getDocument().getData());
    List<Resource> entityList = ((List<Resource>) response.getDocument().getData().get());
    Assert.assertEquals(true, entityList.size() > 0);
    Assert.assertEquals("projects", entityList.get(0).getType());
    Resource returnedProject = entityList.get(0);
    Assert.assertEquals(project.getId().toString(), returnedProject.getId());
    Relationship relationship = returnedProject.getRelationships().get("includedTask");
    Assert.assertNotNull(relationship);
    Assert.assertEquals(task.getId().toString(), relationship.getSingleData().get().getId());
}
Also used : Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) User(io.crnk.core.mock.models.User) FieldResourceGet(io.crnk.core.engine.internal.dispatcher.controller.FieldResourceGet) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) JsonPath(io.crnk.core.engine.internal.dispatcher.path.JsonPath) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) 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) Relationship(io.crnk.core.engine.document.Relationship) QueryParams(io.crnk.legacy.queryParams.QueryParams) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) BaseControllerTest(io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest) Test(org.junit.Test)

Example 17 with ResourceInformation

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

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

the class SetOwnerStrategy method addRelations.

@Override
public void addRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        Collection currentIds = (Collection) field.getIdAccessor().getValue(source);
        currentIds.addAll((Collection) targetIds);
    } else {
        RegistryEntry targetEntry = context.getTargetEntry(field);
        Iterable<D> targets = context.findAll(targetEntry, targetIds);
        @SuppressWarnings("unchecked") Collection<D> currentTargets = getOrCreateCollection(source, field);
        for (D target : targets) {
            currentTargets.add(target);
        }
    }
    sourceAdapter.update(source, context.createSaveQueryAdapter(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 19 with ResourceInformation

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

the class SetOwnerStrategy method setRelations.

@Override
public void setRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        field.getIdAccessor().setValue(source, targetIds);
    } else {
        RegistryEntry targetEntry = context.getTargetEntry(field);
        Iterable<D> targets = context.findAll(targetEntry, targetIds);
        field.getAccessor().setValue(source, targets);
    }
    sourceAdapter.update(source, context.createSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 20 with ResourceInformation

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

the class SetOwnerStrategy method setRelation.

@Override
public void setRelation(T source, J targetId, String fieldName) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        field.getIdAccessor().setValue(source, targetId);
    } else {
        RegistryEntry targetEntry = context.getTargetEntry(field);
        D target = context.findOne(targetEntry, targetId);
        field.getAccessor().setValue(source, target);
    }
    sourceAdapter.update(source, context.createSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

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