Search in sources :

Example 16 with QueryAdapter

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

the class IncludeLookupSetterBaseTest method setup.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
    super.setup();
    // get repositories
    ResourceRepositoryAdapter taskRepository = resourceRegistry.findEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryTaskToProject = resourceRegistry.findEntry(Task.class).getRelationshipRepository("projects", null);
    RelationshipRepositoryAdapter relRepositoryProjectToTask = resourceRegistry.findEntry(Project.class).getRelationshipRepository("tasks", null);
    ResourceRepositoryAdapter projectRepository = resourceRegistry.findEntry(Project.class).getResourceRepository(null);
    ResourceRepositoryAdapter hierarchicalTaskRepository = resourceRegistry.findEntry(HierarchicalTask.class).getResourceRepository(null);
    // setup test data
    ResourceInformation taskInfo = resourceRegistry.findEntry(Task.class).getResourceInformation();
    ResourceInformation projectInfo = resourceRegistry.findEntry(Project.class).getResourceInformation();
    ResourceField includedTaskField = projectInfo.findRelationshipFieldByName("includedTask");
    ResourceField includedProjectField = taskInfo.findRelationshipFieldByName("includedProject");
    ResourceField includedProjectsField = taskInfo.findRelationshipFieldByName("includedProjects");
    ResourceField projectField = taskInfo.findRelationshipFieldByName("project");
    QueryAdapter projectQuey = emptyQueryAdapter(Project.class);
    QueryAdapter taskQuery = emptyQueryAdapter(Task.class);
    QueryAdapter hierarchicalTaskQuery = emptyQueryAdapter(HierarchicalTask.class);
    Project project = new Project();
    project.setId(2L);
    projectRepository.create(project, projectQuey);
    Task task = new Task();
    task.setId(1L);
    taskRepository.create(task, taskQuery);
    relRepositoryTaskToProject.setRelation(task, project.getId(), includedProjectField, projectQuey);
    relRepositoryTaskToProject.setRelation(task, project.getId(), projectField, projectQuey);
    relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project.getId()), includedProjectsField, projectQuey);
    // setup deep nested relationship
    Task includedTask = new Task();
    includedTask.setId(3L);
    taskRepository.create(includedTask, taskQuery);
    relRepositoryProjectToTask.setRelation(project, includedTask.getId(), includedTaskField, taskQuery);
    Project deepIncludedProject = new Project();
    deepIncludedProject.setId(2L);
    projectRepository.create(project, projectQuey);
    relRepositoryTaskToProject.setRelation(includedTask, deepIncludedProject.getId(), includedProjectField, projectQuey);
    relRepositoryTaskToProject.addRelations(includedTask, Collections.singletonList(project.getId()), includedProjectsField, projectQuey);
    // setup hierarchy of resources
    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>());
    hierarchicalTaskRepository.create(h, hierarchicalTaskQuery);
    hierarchicalTaskRepository.create(h0, hierarchicalTaskQuery);
    hierarchicalTaskRepository.create(h1, hierarchicalTaskQuery);
    hierarchicalTaskRepository.create(h11, hierarchicalTaskQuery);
}
Also used : Project(io.crnk.core.mock.models.Project) ResourceField(io.crnk.core.engine.information.resource.ResourceField) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) HierarchicalTask(io.crnk.core.mock.models.HierarchicalTask) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Before(org.junit.Before)

Example 17 with QueryAdapter

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

the class IncludeLookupSetterInheritanceTest method setup.

@SuppressWarnings({ "rawtypes", "unchecked" })
@Before
public void setup() {
    super.setup();
    // get repositories
    ResourceRepositoryAdapter taskRepository = resourceRegistry.getEntry(Task.class).getResourceRepository(null);
    RelationshipRepositoryAdapter relRepositoryTaskToProject = resourceRegistry.getEntry(Task.class).getRelationshipRepository("projects", null);
    ResourceRepositoryAdapter projectRepository = resourceRegistry.getEntry(Project.class).getResourceRepository(null);
    // setup test data
    ResourceInformation taskInfo = resourceRegistry.getEntry(Task.class).getResourceInformation();
    ResourceField includedProjectsField = taskInfo.findRelationshipFieldByName("includedProjects");
    QueryAdapter emptyProjectQuery = new QuerySpecAdapter(new QuerySpec(Project.class), resourceRegistry);
    QueryAdapter emptyTaskQuery = new QuerySpecAdapter(new QuerySpec(Task.class), resourceRegistry);
    Project project1 = new Project();
    project1.setId(3L);
    projectRepository.create(project1, emptyProjectQuery);
    FancyProject project2 = new FancyProject();
    project2.setId(ProjectRepository.FANCY_PROJECT_ID);
    projectRepository.create(project2, emptyProjectQuery);
    Task task = new Task();
    task.setId(1L);
    taskRepository.create(task, emptyTaskQuery);
    relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project1.getId()), includedProjectsField, emptyProjectQuery);
    relRepositoryTaskToProject.addRelations(task, Collections.singletonList(project2.getId()), includedProjectsField, emptyProjectQuery);
}
Also used : Project(io.crnk.core.mock.models.Project) FancyProject(io.crnk.core.mock.models.FancyProject) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Task(io.crnk.core.mock.models.Task) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) FancyProject(io.crnk.core.mock.models.FancyProject) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Before(org.junit.Before)

Example 18 with QueryAdapter

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

the class SleuthUtil method getQuery.

public static String getQuery(RepositoryRequestSpec request, ResourceRegistry resourceRegistry) {
    QueryAdapter queryAdapter = request.getQueryAdapter();
    StringBuilder builder = new StringBuilder();
    builder.append("?");
    if (queryAdapter instanceof QuerySpecAdapter) {
        QuerySpec querySpec = request.getQuerySpec(queryAdapter.getResourceInformation());
        DefaultQuerySpecSerializer serializer = new DefaultQuerySpecSerializer(resourceRegistry);
        Map<String, Set<String>> parameters = serializer.serialize(querySpec);
        for (Map.Entry<String, Set<String>> entry : parameters.entrySet()) {
            if (builder.length() > 1) {
                builder.append("&");
            }
            builder.append(entry.getKey());
            builder.append("=");
            builder.append(StringUtils.join(",", entry.getValue()));
        }
        return builder.toString();
    }
    return null;
}
Also used : Set(java.util.Set) DefaultQuerySpecSerializer(io.crnk.core.queryspec.DefaultQuerySpecSerializer) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec) Map(java.util.Map)

Example 19 with QueryAdapter

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

the class RelationshipRepositoryAdapter method removeRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse removeRelations(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).removeRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).removeRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.DELETE, 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 20 with QueryAdapter

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

the class RelationshipRepositoryAdapter method findBulkOneTargets.

@SuppressWarnings("rawtypes")
public Map<I, JsonApiResponse> findBulkOneTargets(List<I> sourceIds, ResourceField field, QueryAdapter queryAdapter) {
    if (relationshipRepository instanceof BulkRelationshipRepositoryV2) {
        RepositoryBulkRequestFilterChainImpl<I> chain = new RepositoryBulkRequestFilterChainImpl<I>() {

            @Override
            protected Map<I, JsonApiResponse> invoke(RepositoryFilterContext context) {
                RepositoryRequestSpec request = context.getRequest();
                Iterable<?> sourceIds = request.getIds();
                ResourceField field = request.getRelationshipField();
                QueryAdapter queryAdapter = request.getQueryAdapter();
                BulkRelationshipRepositoryV2 bulkRepository = (BulkRelationshipRepositoryV2) relationshipRepository;
                ResourceInformation targetResourceInformation = moduleRegistry.getResourceRegistry().getEntry(field.getOppositeResourceType()).getResourceInformation();
                MultivaluedMap<I, D> targetsMap = bulkRepository.findTargets(sourceIds, field.getUnderlyingName(), request.getQuerySpec(targetResourceInformation));
                return toResponses(targetsMap, false, queryAdapter, field, HttpMethod.GET);
            }
        };
        RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forFindTarget(moduleRegistry, queryAdapter, sourceIds, field);
        return chain.doFilter(newRepositoryFilterContext(requestSpec));
    } else {
        // fallback to non-bulk operation
        Map<I, JsonApiResponse> responseMap = new HashMap<>();
        for (I sourceId : sourceIds) {
            JsonApiResponse response = findOneTarget(sourceId, field, queryAdapter);
            responseMap.put(sourceId, response);
        }
        return responseMap;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) ResourceField(io.crnk.core.engine.information.resource.ResourceField) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

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