Search in sources :

Example 6 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter 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 7 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class AbstractIncludeBehaviorTest method setup.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
    super.setup();
    ResourceRepositoryAdapter hierarchicalTaskRepository = resourceRegistry.findEntry(HierarchicalTask.class).getResourceRepository(null);
    h = new HierarchicalTask();
    h.setId(1L);
    h.setName("");
    h0 = new HierarchicalTask();
    h0.setId(2L);
    h0.setName("0");
    h0.setParent(h);
    h1 = new HierarchicalTask();
    h1.setId(3L);
    h1.setName("1");
    h1.setParent(h);
    h11 = new HierarchicalTask();
    h11.setId(4L);
    h11.setName("11");
    h11.setParent(h1);
    h.setChildren(Arrays.asList(h0, h1));
    h0.setChildren(new ArrayList<HierarchicalTask>());
    h1.setChildren(Arrays.asList(h11));
    h11.setChildren(new ArrayList<HierarchicalTask>());
    QueryAdapter emptyQueryAdapter = new QuerySpecAdapter(new QuerySpec(HierarchicalTask.class), resourceRegistry);
    hierarchicalTaskRepository.create(h, emptyQueryAdapter);
    hierarchicalTaskRepository.create(h0, emptyQueryAdapter);
    hierarchicalTaskRepository.create(h1, emptyQueryAdapter);
    hierarchicalTaskRepository.create(h11, emptyQueryAdapter);
}
Also used : HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Before(org.junit.Before)

Example 8 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method setRelation.

@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Serializable targetId = request.getId();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 9 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method addRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Iterable<?> targetIds = request.getIds();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 10 with QueryAdapter

use of io.crnk.core.engine.query.QueryAdapter in project crnk-framework by crnk-project.

the class ResourceRepositoryAdapter method delete.

public JsonApiResponse delete(I id, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @SuppressWarnings("rawtypes")
        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            Serializable id = request.getId();
            if (isAnnotated) {
                ((AnnotatedResourceRepositoryAdapter) resourceRepository).delete(id, queryAdapter);
            } else if (resourceRepository instanceof ResourceRepositoryV2) {
                ((ResourceRepositoryV2) resourceRepository).delete(id);
            } else {
                ((ResourceRepository) resourceRepository).delete(id);
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forDelete(moduleRegistry, resourceInformation, queryAdapter, id);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedResourceRepositoryAdapter(io.crnk.legacy.internal.AnnotatedResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext)

Aggregations

QueryAdapter (io.crnk.core.engine.query.QueryAdapter)35 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)15 QuerySpec (io.crnk.core.queryspec.QuerySpec)14 ResourceField (io.crnk.core.engine.information.resource.ResourceField)13 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)12 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)12 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)9 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)7 RelationshipRepositoryV2 (io.crnk.core.repository.RelationshipRepositoryV2)6 Resource (io.crnk.core.engine.document.Resource)5 Task (io.crnk.core.mock.models.Task)5 Serializable (java.io.Serializable)5 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)4 AnnotatedRelationshipRepositoryAdapter (io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter)4 Before (org.junit.Before)4 Test (org.junit.Test)4 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)3 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)3