Search in sources :

Example 1 with LookupIncludeBehavior

use of io.crnk.core.resource.annotations.LookupIncludeBehavior in project crnk-framework by crnk-project.

the class IncludeLookupSetter method populateField.

private void populateField(ResourceField resourceField, QueryAdapter queryAdapter, List<ResourceField> fieldPath, PopulatedCache populatedCache, Collection<Resource> resourceList, Set<String> fieldsWithEnforceIdSerialization, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, Set<ResourceIdentifier> inclusions, ResourceMappingConfig resourceMappingConfig) {
    ResourceInformation resourceInformation = resourceField.getParentResourceInformation();
    boolean includeRequested = util.isInclusionRequested(queryAdapter, fieldPath);
    boolean includeResources = includeRequested || resourceField.getSerializeType() == SerializeType.EAGER;
    boolean includeRelationId = resourceField.getSerializeType() != SerializeType.LAZY || fieldsWithEnforceIdSerialization.contains(resourceField.getJsonName());
    boolean includeRelationshipData = includeRelationId || includeResources;
    if (includeRelationshipData) {
        Collection<Resource> unpopulatedResourceList = populatedCache.filterProcessed(resourceList, resourceField);
        if (!unpopulatedResourceList.isEmpty()) {
            // only handle resources from the proper subtype where the
            // relationship is desired to be loaded
            List<Resource> resourcesByType = util.filterByType(unpopulatedResourceList, resourceInformation);
            List<Resource> resourcesWithField = util.filterByLoadedRelationship(resourcesByType, resourceField);
            // lookup resources by inspecting the POJOs in entityMap
            LookupIncludeBehavior fieldLookupIncludeBehavior = resourceField.getLookupIncludeAutomatically();
            Set<Resource> populatedResources;
            if (!includeResources && resourceField.hasIdField()) {
                // fill in @JsonApiRelationId into Relationship where possible
                fetchRelationFromEntity(resourcesWithField, resourceField, queryAdapter, resourceMap, entityMap, false, false, includeResources, resourceMappingConfig);
                // only ID is required and no lookup must take place
                // nothing to do
                populatedResources = Collections.emptySet();
            } else if (fieldLookupIncludeBehavior == LookupIncludeBehavior.AUTOMATICALLY_ALWAYS) {
                // fill in @JsonApiRelationId into Relationship where possible
                fetchRelationFromEntity(resourcesWithField, resourceField, queryAdapter, resourceMap, entityMap, false, false, includeResources, resourceMappingConfig);
                // lookup resources by making repository calls
                populatedResources = lookupRelatedResource(resourcesWithField, resourceField, queryAdapter, parameterProvider, resourceMap, entityMap, resourceMappingConfig);
            } else if (fieldLookupIncludeBehavior == LookupIncludeBehavior.AUTOMATICALLY_WHEN_NULL) {
                // try to populate from entities
                Set<Resource> extractedResources = fetchRelationFromEntity(resourcesWithField, resourceField, queryAdapter, resourceMap, entityMap, true, true, includeResources, resourceMappingConfig);
                // do lookups where relationship data is null
                Collection<Resource> resourcesForLookup = util.findResourcesWithoutRelationshipToLoad(resourcesWithField, resourceField, resourceMap);
                Collection<Resource> lookedupResources = lookupRelatedResource(resourcesForLookup, resourceField, queryAdapter, parameterProvider, resourceMap, entityMap, resourceMappingConfig);
                populatedResources = util.union(lookedupResources, extractedResources);
            } else {
                // do not do any lookups
                populatedResources = fetchRelationFromEntity(resourcesWithField, resourceField, queryAdapter, resourceMap, entityMap, false, true, includeResources, resourceMappingConfig);
                // and be sure.
                if (!Iterable.class.isAssignableFrom(resourceField.getType())) {
                    Nullable<Object> emptyData = Nullable.nullValue();
                    for (Resource resourceWithField : resourcesWithField) {
                        Relationship relationship = resourceWithField.getRelationships().get(resourceField.getJsonName());
                        if (!relationship.getData().isPresent()) {
                            relationship.setData(emptyData);
                        }
                    }
                }
            }
            // such
            if (includeResources && !populatedResources.isEmpty()) {
                inclusions.addAll(util.toIds(populatedResources));
                Set<String> additionalEagerLoadedNestedRelations = Collections.emptySet();
                populate(populatedResources, inclusions, resourceMap, entityMap, fieldPath, queryAdapter, parameterProvider, additionalEagerLoadedNestedRelations, populatedCache, resourceMappingConfig);
            }
        }
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Resource(io.crnk.core.engine.document.Resource) Relationship(io.crnk.core.engine.document.Relationship) LookupIncludeBehavior(io.crnk.core.resource.annotations.LookupIncludeBehavior)

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 LookupIncludeBehavior (io.crnk.core.resource.annotations.LookupIncludeBehavior)1