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