Search in sources :

Example 31 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class IncludeLookupSetter method lookupRelatedResourceWithRelationship.

private Set<Resource> lookupRelatedResourceWithRelationship(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
    ResourceInformation resourceInformation = relationshipField.getParentResourceInformation();
    RegistryEntry registyEntry = resourceRegistry.getEntry(resourceInformation.getResourceType());
    List<Serializable> resourceIds = getIds(sourceResources, resourceInformation);
    boolean isMany = Iterable.class.isAssignableFrom(relationshipField.getType());
    Set<Resource> loadedTargets = new HashSet<>();
    @SuppressWarnings("rawtypes") RelationshipRepositoryAdapter relationshipRepository = registyEntry.getRelationshipRepository(relationshipField, parameterProvider);
    if (relationshipRepository != null) {
        Map<Object, JsonApiResponse> responseMap;
        if (isMany) {
            responseMap = relationshipRepository.findBulkManyTargets(resourceIds, relationshipField, queryAdapter);
        } else {
            responseMap = relationshipRepository.findBulkOneTargets(resourceIds, relationshipField, queryAdapter);
        }
        for (Resource sourceResource : sourceResources) {
            Serializable sourceId = resourceInformation.parseIdString(sourceResource.getId());
            JsonApiResponse targetResponse = responseMap.get(sourceId);
            if (targetResponse != null && targetResponse.getEntity() != null) {
                Object targetEntity = targetResponse.getEntity();
                List<Resource> targets = setupRelation(sourceResource, relationshipField, targetEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
                loadedTargets.addAll(targets);
            } else {
                Nullable<Object> emptyData = (Nullable) Nullable.of(Iterable.class.isAssignableFrom(relationshipField.getType()) ? Collections.emptyList() : null);
                Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
                relationship.setData(emptyData);
            }
        }
    } else {
        throw new RepositoryNotFoundException("no relationship repository found for " + resourceInformation.getResourceType() + "." + relationshipField.getUnderlyingName());
    }
    return loadedTargets;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Relationship(io.crnk.core.engine.document.Relationship) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Nullable(io.crnk.core.utils.Nullable) HashSet(java.util.HashSet)

Example 32 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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 33 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class RelationshipsResourceGet method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = resourceRegistry.getEntry(resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    String elementName = jsonPath.getElementName();
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(elementName);
    verifyFieldNotNull(relationshipField, elementName);
    boolean isCollection = Iterable.class.isAssignableFrom(relationshipField.getType());
    RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    JsonApiResponse entities;
    if (isCollection) {
        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 34 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse 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)

Example 35 with JsonApiResponse

use of io.crnk.core.repository.response.JsonApiResponse in project crnk-framework by crnk-project.

the class AbstractDocumentMapperTest method toResponse.

protected JsonApiResponse toResponse(Object entity) {
    JsonApiResponse response = new JsonApiResponse();
    response.setEntity(entity);
    return response;
}
Also used : JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse)

Aggregations

JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)49 Document (io.crnk.core.engine.document.Document)16 Test (org.junit.Test)16 ResourceField (io.crnk.core.engine.information.resource.ResourceField)15 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)14 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)12 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)11 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)10 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)10 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)10 Serializable (java.io.Serializable)10 Response (io.crnk.core.engine.dispatcher.Response)9 Resource (io.crnk.core.engine.document.Resource)9 Task (io.crnk.core.mock.models.Task)7 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)6 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)6 PagedLinksInformation (io.crnk.core.resource.links.PagedLinksInformation)6 HashSet (java.util.HashSet)6