Search in sources :

Example 6 with RelationshipRepositoryAdapter

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

the class FieldResourceGet method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    PathIds resourceIds = jsonPath.getIds();
    String resourceName = jsonPath.getResourceType();
    String elementName = jsonPath.getElementName();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
    verifyFieldNotNull(relationshipField, elementName);
    // TODO remove Class usage and replace by resourceId
    Class<?> baseRelationshipFieldClass = relationshipField.getType();
    RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    JsonApiResponse entities;
    if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
        entities = relationshipRepositoryForClass.findManyTargets(castedResourceId, relationshipField, queryAdapter);
    } else {
        entities = relationshipRepositoryForClass.findOneTarget(castedResourceId, relationshipField, queryAdapter);
    }
    Document responseDocument = documentMapper.toDocument(entities, queryAdapter, parameterProvider);
    // 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) ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) 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) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)

Example 7 with RelationshipRepositoryAdapter

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

the class RelationshipsResourceUpsert method handle.

@Override
public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = getRegistryEntry(resourceName);
    assertRequestDocument(requestBody, HttpMethod.POST, resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    @SuppressWarnings("unchecked") Object resource = resourceRepository.findOne(castedResourceId, queryAdapter).getEntity();
    ResourceInformation targetInformation = getRegistryEntry(relationshipField.getOppositeResourceType()).getResourceInformation();
    @SuppressWarnings("unchecked") RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    if (Iterable.class.isAssignableFrom(relationshipField.getType())) {
        Iterable<ResourceIdentifier> dataBodies = (Iterable<ResourceIdentifier>) (requestBody.isMultiple() ? requestBody.getData().get() : Collections.singletonList(requestBody.getData().get()));
        processToManyRelationship(resource, targetInformation, relationshipField, dataBodies, queryAdapter, relationshipRepositoryForClass);
    } else {
        if (requestBody.isMultiple()) {
            throw new RequestBodyException(HttpMethod.POST, resourceName, "Multiple data in body");
        }
        ResourceIdentifier dataBody = (ResourceIdentifier) requestBody.getData().get();
        processToOneRelationship(resource, targetInformation, relationshipField, dataBody, queryAdapter, relationshipRepositoryForClass);
    }
    return new Response(new Document(), HttpStatus.NO_CONTENT_204);
}
Also used : Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) RequestBodyException(io.crnk.core.exception.RequestBodyException) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds)

Example 8 with RelationshipRepositoryAdapter

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

the class FieldResourcePost method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
    RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
    Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.POST);
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    Serializable castedResourceId = getResourceId(resourceIds, endpointRegistryEntry);
    ResourceField relationshipField = endpointRegistryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    Class<?> baseRelationshipFieldClass = relationshipField.getType();
    RegistryEntry relationshipRegistryEntry = resourceRegistry.getEntry(relationshipField.getOppositeResourceType());
    String relationshipResourceType = relationshipField.getOppositeResourceType();
    verifyTypes(HttpMethod.POST, relationshipRegistryEntry, bodyRegistryEntry);
    Object newResource = buildNewResource(relationshipRegistryEntry, resourceBody, relationshipResourceType);
    setAttributes(resourceBody, newResource, relationshipRegistryEntry.getResourceInformation());
    ResourceRepositoryAdapter resourceRepository = relationshipRegistryEntry.getResourceRepository(parameterProvider);
    Document savedResourceResponse = documentMapper.toDocument(resourceRepository.create(newResource, queryAdapter), queryAdapter, parameterProvider);
    setRelations(newResource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
    ResourceIdentifier resourceId1 = savedResourceResponse.getSingleData().get();
    RelationshipRepositoryAdapter relationshipRepositoryForClass = endpointRegistryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    @SuppressWarnings("unchecked") JsonApiResponse parent = endpointRegistryEntry.getResourceRepository(parameterProvider).findOne(castedResourceId, queryAdapter);
    if (Iterable.class.isAssignableFrom(baseRelationshipFieldClass)) {
        List<ResourceIdentifier> resourceIdList = Collections.singletonList(resourceId1);
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceIdList = filter.modifyManyRelationship(parent.getEntity(), relationshipField, ResourceRelationshipModificationType.ADD, resourceIdList);
        }
        List<Serializable> parsedIds = new ArrayList<>();
        for (ResourceIdentifier resourceId : resourceIdList) {
            parsedIds.add(relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId.getId()));
        }
        // noinspection unchecked
        relationshipRepositoryForClass.addRelations(parent.getEntity(), parsedIds, relationshipField, queryAdapter);
    } else {
        // noinspection unchecked
        for (ResourceModificationFilter filter : modificationFilters) {
            resourceId1 = filter.modifyOneRelationship(parent.getEntity(), relationshipField, resourceId1);
        }
        Serializable parseId = relationshipRegistryEntry.getResourceInformation().parseIdString(resourceId1.getId());
        relationshipRepositoryForClass.setRelation(parent.getEntity(), parseId, relationshipField, queryAdapter);
    }
    return new Response(savedResourceResponse, HttpStatus.CREATED_201);
}
Also used : Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 9 with RelationshipRepositoryAdapter

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

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

Aggregations

RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)12 ResourceField (io.crnk.core.engine.information.resource.ResourceField)7 Response (io.crnk.core.engine.dispatcher.Response)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)5 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 Serializable (java.io.Serializable)5 Document (io.crnk.core.engine.document.Document)4 PathIds (io.crnk.core.engine.internal.dispatcher.path.PathIds)4 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)4 Resource (io.crnk.core.engine.document.Resource)3 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)3 Project (io.crnk.core.mock.models.Project)3 Task (io.crnk.core.mock.models.Task)3 Relationship (io.crnk.core.engine.document.Relationship)2 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)2 DirectResponseRelationshipEntry (io.crnk.legacy.internal.DirectResponseRelationshipEntry)2 Before (org.junit.Before)2 RelationshipRepositoryStubImpl (io.crnk.client.internal.RelationshipRepositoryStubImpl)1 RelationshipRepositoryStub (io.crnk.client.legacy.RelationshipRepositoryStub)1