Search in sources :

Example 1 with RepositoryNotFoundException

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);
    }
}
Also used : RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 2 with RepositoryNotFoundException

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;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) Relationship(io.crnk.core.engine.document.Relationship) Collection(java.util.Collection) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) HashSet(java.util.HashSet)

Example 3 with RepositoryNotFoundException

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();
}
Also used : ResourceRegistry(io.crnk.core.engine.registry.ResourceRegistry) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 4 with RepositoryNotFoundException

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;
}
Also used : RequestBodyException(io.crnk.core.exception.RequestBodyException) Resource(io.crnk.core.engine.document.Resource) RepositoryNotFoundException(io.crnk.core.exception.RepositoryNotFoundException) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 5 with RepositoryNotFoundException

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;
}
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

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)5 RepositoryNotFoundException (io.crnk.core.exception.RepositoryNotFoundException)5 Resource (io.crnk.core.engine.document.Resource)3 Relationship (io.crnk.core.engine.document.Relationship)2 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2 HashSet (java.util.HashSet)2 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)1 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)1 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)1 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)1 RequestBodyException (io.crnk.core.exception.RequestBodyException)1 ResourceNotFoundException (io.crnk.core.exception.ResourceNotFoundException)1 Nullable (io.crnk.core.utils.Nullable)1 Serializable (java.io.Serializable)1 Collection (java.util.Collection)1