use of io.crnk.core.exception.RepositoryNotFoundException in project crnk-framework by crnk-project.
the class DefaultQuerySpecSerializer method serialize.
private void serialize(QuerySpec querySpec, Map<String, Set<String>> map, QuerySpec parentQuerySpec) {
String resourceType = querySpec.getResourceType();
if (resourceType == null) {
RegistryEntry entry = resourceRegistry.getEntry(querySpec.getResourceClass());
if (entry == null) {
throw new RepositoryNotFoundException(querySpec.getResourceClass());
}
resourceType = entry.getResourceInformation().getResourceType();
}
serializeFilters(querySpec, resourceType, map);
serializeSorting(querySpec, resourceType, map);
serializeIncludedFields(querySpec, resourceType, map);
serializeIncludedRelations(querySpec, resourceType, map);
RegistryEntry entry = resourceRegistry.getEntry(parentQuerySpec.getResourceClass());
if (entry != null && entry.getResourceInformation() != null && entry.getResourceInformation().getPagingBehavior() != null) {
map.putAll(entry.getResourceInformation().getPagingBehavior().serialize(querySpec.getPagingSpec(), resourceType));
}
for (QuerySpec relatedSpec : querySpec.getRelatedSpecs().values()) {
serialize(relatedSpec, map, querySpec);
}
}
use of io.crnk.core.exception.RepositoryNotFoundException in project crnk-framework by crnk-project.
the class IncludeLookupSetter method lookupRelatedResourcesWithId.
private Set<Resource> lookupRelatedResourcesWithId(Collection<Resource> sourceResources, ResourceField relationshipField, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
String oppositeResourceType = relationshipField.getOppositeResourceType();
RegistryEntry oppositeEntry = resourceRegistry.getEntry(oppositeResourceType);
if (oppositeEntry == null) {
throw new RepositoryNotFoundException("no resource with type " + oppositeResourceType + " found");
}
ResourceInformation oppositeResourceInformation = oppositeEntry.getResourceInformation();
ResourceRepositoryAdapter oppositeResourceRepository = oppositeEntry.getResourceRepository(parameterProvider);
if (oppositeResourceRepository == null) {
throw new RepositoryNotFoundException("no relationship repository found for " + oppositeResourceInformation.getResourceType());
}
Set<Resource> related = new HashSet<>();
Set<Object> relatedIdsToLoad = new HashSet<>();
for (Resource sourceResource : sourceResources) {
Relationship relationship = sourceResource.getRelationships().get(relationshipField.getJsonName());
PreconditionUtil.assertTrue("expected relationship data to be loaded for @JsonApiResourceId annotated field", relationship.getData().isPresent());
if (relationship.getData().get() != null) {
for (ResourceIdentifier id : relationship.getCollectionData().get()) {
if (resourceMap.containsKey(id)) {
// load from cache
related.add(resourceMap.get(id));
} else {
relatedIdsToLoad.add(oppositeResourceInformation.parseIdString(id.getId()));
}
}
}
}
if (!relatedIdsToLoad.isEmpty()) {
JsonApiResponse response = oppositeResourceRepository.findAll(relatedIdsToLoad, queryAdapter);
Collection responseList = (Collection) response.getEntity();
for (Object responseEntity : responseList) {
Resource relatedResource = mergeResource(responseEntity, queryAdapter, resourceMap, entityMap, resourceMappingConfig);
related.add(relatedResource);
Object responseEntityId = oppositeResourceInformation.getId(responseEntity);
relatedIdsToLoad.remove(responseEntityId);
}
if (!relatedIdsToLoad.isEmpty()) {
throw new ResourceNotFoundException("type=" + relationshipField.getOppositeResourceType() + ", " + "ids=" + relatedIdsToLoad);
}
}
return related;
}
use of io.crnk.core.exception.RepositoryNotFoundException in project crnk-framework by crnk-project.
the class ActivitiModule method getRepository.
private <T> ResourceRepositoryV2<T, String> getRepository(Class<T> resourceClass) {
ResourceRegistry resourceRegistry = moduleContext.getResourceRegistry();
RegistryEntry entry = resourceRegistry.getEntry(resourceClass);
if (entry == null) {
throw new RepositoryNotFoundException(resourceClass.getName() + " not registered");
}
return entry.getResourceRepositoryFacade();
}
use of io.crnk.core.exception.RepositoryNotFoundException in project crnk-framework by crnk-project.
the class ResourceUpsert method getRequestBody.
protected Resource getRequestBody(Document requestDocument, JsonPath path, HttpMethod method) {
String resourceType = path.getResourceType();
assertRequestDocument(requestDocument, method, resourceType);
if (!requestDocument.getData().isPresent() || requestDocument.getData().get() == null) {
throw new RequestBodyException(method, resourceType, "No data field in the body.");
}
if (requestDocument.getData().get() instanceof Collection) {
throw new RequestBodyException(method, resourceType, "Multiple data in body");
}
Resource resourceBody = (Resource) requestDocument.getData().get();
RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
if (bodyRegistryEntry == null) {
throw new RepositoryNotFoundException(resourceBody.getType());
}
return resourceBody;
}
use of io.crnk.core.exception.RepositoryNotFoundException 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;
}
Aggregations