Search in sources :

Example 16 with ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter 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 ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter 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 ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.

the class RegistryEntry method getResourceRepository.

@SuppressWarnings("unchecked")
public ResourceRepositoryAdapter getResourceRepository(RepositoryMethodParameterProvider parameterProvider) {
    Object repoInstance = null;
    if (resourceEntry instanceof DirectResponseResourceEntry) {
        repoInstance = ((DirectResponseResourceEntry) resourceEntry).getResourceRepository();
    } else if (resourceEntry instanceof AnnotatedResourceEntry) {
        repoInstance = ((AnnotatedResourceEntry) resourceEntry).build(parameterProvider);
    }
    if (repoInstance instanceof ResourceRegistryAware) {
        ((ResourceRegistryAware) repoInstance).setResourceRegistry(moduleRegistry.getResourceRegistry());
    }
    ResourceInformation resourceInformation = getResourceInformation();
    return new ResourceRepositoryAdapter(resourceInformation, moduleRegistry, repoInstance);
}
Also used : AnnotatedResourceEntry(io.crnk.legacy.registry.AnnotatedResourceEntry) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) DirectResponseResourceEntry(io.crnk.legacy.internal.DirectResponseResourceEntry) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)

Example 19 with ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.

the class CollectionGet method handle.

@Override
@SuppressWarnings("unchecked")
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getElementName();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    if (registryEntry == null) {
        throw new ResourceNotFoundException(resourceName);
    }
    Document responseDocument;
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse entities;
    if (jsonPath.getIds() == null || jsonPath.getIds().getIds().isEmpty()) {
        entities = resourceRepository.findAll(queryAdapter);
    } else {
        Class<? extends Serializable> idType = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
        Iterable<? extends Serializable> parsedIds = typeParser.parse((Iterable<String>) jsonPath.getIds().getIds(), idType);
        entities = resourceRepository.findAll(parsedIds, queryAdapter);
    }
    responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 20 with ResourceRepositoryAdapter

use of io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter in project crnk-framework by crnk-project.

the class ResourceGet method handle.

/**
 * {@inheritDoc}
 * <p>
 * Passes the request to controller method.
 */
@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceType = jsonPath.getElementName();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = getRegistryEntry(resourceType);
    String id = resourceIds.getIds().get(0);
    @SuppressWarnings("unchecked") Class<? extends Serializable> idClass = (Class<? extends Serializable>) registryEntry.getResourceInformation().getIdField().getType();
    Serializable castedId = typeParser.parse(id, idClass);
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse entities = resourceRepository.findOne(castedId, queryAdapter);
    Document responseDocument = documentMapper.toDocument(entities, queryAdapter);
    // return explicit { data : null } if values found
    if (!responseDocument.getData().isPresent()) {
        responseDocument.setData(Nullable.nullValue());
    }
    return new Response(responseDocument, 200);
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) Serializable(java.io.Serializable) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Aggregations

ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)22 Response (io.crnk.core.engine.dispatcher.Response)11 Document (io.crnk.core.engine.document.Document)10 Resource (io.crnk.core.engine.document.Resource)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)9 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)9 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)9 ResourceField (io.crnk.core.engine.information.resource.ResourceField)7 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)7 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)7 Task (io.crnk.core.mock.models.Task)6 QuerySpec (io.crnk.core.queryspec.QuerySpec)6 Test (org.junit.Test)6 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)5 Serializable (java.io.Serializable)5 Project (io.crnk.core.mock.models.Project)4 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)3 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)3 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)3 PathIds (io.crnk.core.engine.internal.dispatcher.path.PathIds)3