Search in sources :

Example 1 with Nullable

use of io.crnk.core.utils.Nullable 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)

Aggregations

Relationship (io.crnk.core.engine.document.Relationship)1 Resource (io.crnk.core.engine.document.Resource)1 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)1 RepositoryNotFoundException (io.crnk.core.exception.RepositoryNotFoundException)1 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)1 Nullable (io.crnk.core.utils.Nullable)1 Serializable (java.io.Serializable)1 HashSet (java.util.HashSet)1