Search in sources :

Example 31 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceMetaFilter method adjustFieldForRequest.

private MetaElement adjustFieldForRequest(MetaResourceField field) {
    MetaResource metaResource = (MetaResource) field.getParent();
    Module.ModuleContext moduleContext = context.getModuleContext();
    RegistryEntry entry = moduleContext.getResourceRegistry().getEntry(metaResource.getResourceType());
    ResourceInformation resourceInformation = entry.getResourceInformation();
    ResourceField fieldInformation = resourceInformation.findFieldByUnderlyingName(field.getName());
    ResourceFilterDirectory filterBehaviorProvider = moduleContext.getResourceFilterDirectory();
    boolean readable = metaResource.isReadable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.GET) == FilterBehavior.NONE;
    boolean insertable = metaResource.isInsertable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.POST) == FilterBehavior.NONE;
    boolean updatable = metaResource.isUpdatable() && filterBehaviorProvider.get(fieldInformation, HttpMethod.PATCH) == FilterBehavior.NONE;
    // hide element if no permission
    if (!readable && !insertable && !updatable) {
        return null;
    }
    if (field.isUpdatable() != updatable || field.isInsertable() != insertable) {
        MetaResourceField clone = (MetaResourceField) field.duplicate();
        clone.setInsertable(insertable);
        clone.setUpdatable(updatable);
        return clone;
    }
    return field;
}
Also used : MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceField(io.crnk.core.engine.information.resource.ResourceField) MetaResourceField(io.crnk.meta.model.resource.MetaResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) MetaResource(io.crnk.meta.model.resource.MetaResource) Module(io.crnk.core.module.Module) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceFilterDirectory(io.crnk.core.engine.filter.ResourceFilterDirectory)

Example 32 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourcePatch method handle.

@Override
public Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestDocument) {
    RegistryEntry endpointRegistryEntry = getRegistryEntry(jsonPath);
    final Resource resourceBody = getRequestBody(requestDocument, jsonPath, HttpMethod.PATCH);
    RegistryEntry bodyRegistryEntry = resourceRegistry.getEntry(resourceBody.getType());
    String idString = jsonPath.getIds().getIds().get(0);
    ResourceInformation resourceInformation = bodyRegistryEntry.getResourceInformation();
    Serializable resourceId = resourceInformation.parseIdString(idString);
    verifyTypes(HttpMethod.PATCH, endpointRegistryEntry, bodyRegistryEntry);
    ResourceRepositoryAdapter resourceRepository = endpointRegistryEntry.getResourceRepository(parameterProvider);
    JsonApiResponse resourceFindResponse = resourceRepository.findOne(resourceId, queryAdapter);
    Object resource = resourceFindResponse.getEntity();
    if (resource == null) {
        throw new ResourceNotFoundException(jsonPath.toString());
    }
    final Resource resourceFindData = documentMapper.toDocument(resourceFindResponse, queryAdapter, parameterProvider).getSingleData().get();
    resourceInformation.verify(resource, requestDocument);
    // extract current attributes from findOne without any manipulation by query params (such as sparse fieldsets)
    ExceptionUtil.wrapCatchedExceptions(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            String attributesFromFindOne = extractAttributesFromResourceAsJson(resourceFindData);
            Map<String, Object> attributesToUpdate = new HashMap<>(emptyIfNull(objectMapper.readValue(attributesFromFindOne, Map.class)));
            // deserialize the request JSON's attributes object into a map
            String attributesAsJson = objectMapper.writeValueAsString(resourceBody.getAttributes());
            Map<String, Object> attributesFromRequest = emptyIfNull(objectMapper.readValue(attributesAsJson, Map.class));
            // remove attributes that were omitted in the request
            Iterator<String> it = attributesToUpdate.keySet().iterator();
            while (it.hasNext()) {
                String key = it.next();
                if (!attributesFromRequest.containsKey(key)) {
                    it.remove();
                }
            }
            // walk the source map and apply target values from request
            updateValues(attributesToUpdate, attributesFromRequest);
            Map<String, JsonNode> upsertedAttributes = new HashMap<>();
            for (Map.Entry<String, Object> entry : attributesToUpdate.entrySet()) {
                JsonNode value = objectMapper.valueToTree(entry.getValue());
                upsertedAttributes.put(entry.getKey(), value);
            }
            resourceBody.setAttributes(upsertedAttributes);
            return null;
        }
    }, "failed to merge patched attributes");
    JsonApiResponse updatedResource;
    Set<String> loadedRelationshipNames;
    if (resourceInformation.getResourceClass() == Resource.class) {
        loadedRelationshipNames = getLoadedRelationshipNames(resourceBody);
        updatedResource = resourceRepository.update(resourceBody, queryAdapter);
    } else {
        setAttributes(resourceBody, resource, bodyRegistryEntry.getResourceInformation());
        setRelations(resource, bodyRegistryEntry, resourceBody, queryAdapter, parameterProvider, false);
        loadedRelationshipNames = getLoadedRelationshipNames(resourceBody);
        updatedResource = resourceRepository.update(resource, queryAdapter);
    }
    Document responseDocument = documentMapper.toDocument(updatedResource, queryAdapter, parameterProvider, loadedRelationshipNames);
    return new Response(responseDocument, 200);
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Serializable(java.io.Serializable) Resource(io.crnk.core.engine.document.Resource) JsonNode(com.fasterxml.jackson.databind.JsonNode) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException) IOException(java.io.IOException) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) Response(io.crnk.core.engine.dispatcher.Response) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceNotFoundException(io.crnk.core.exception.ResourceNotFoundException)

Example 33 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceUpsert method setRelationsField.

protected void setRelationsField(Object newResource, RegistryEntry registryEntry, Map.Entry<String, Relationship> property, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    Relationship relationship = property.getValue();
    if (relationship.getData().isPresent()) {
        String propertyName = property.getKey();
        ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(propertyName);
        List<ResourceIdentifier> relationshipIds = relationship.getCollectionData().get();
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipIds = filter.modifyManyRelationship(newResource, relationshipField, ResourceRelationshipModificationType.SET, relationshipIds);
        }
        List relationshipTypedIds = new LinkedList<>();
        for (ResourceIdentifier resourceId : relationshipIds) {
            RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(resourceId, idFieldType);
            relationshipTypedIds.add(typedRelationshipId);
        }
        if (relationshipField.hasIdField()) {
            relationshipField.getIdAccessor().setValue(newResource, relationshipTypedIds);
        }
        // TODO batch fetchRelatedObject
        if (decideSetRelationObjectsField(relationshipField)) {
            List relationshipEntities = new LinkedList<>();
            for (int i = 0; i < relationshipIds.size(); i++) {
                ResourceIdentifier resourceId = relationshipIds.get(i);
                Serializable typedRelationshipId = (Serializable) relationshipTypedIds.get(i);
                RegistryEntry entry = resourceRegistry.getEntry(resourceId.getType());
                Object relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                relationshipEntities.add(relationObject);
            }
            relationshipField.getAccessor().setValue(newResource, relationshipEntities);
        }
    }
}
Also used : Serializable(java.io.Serializable) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Relationship(io.crnk.core.engine.document.Relationship) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 34 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry in project crnk-framework by crnk-project.

the class ResourceUpsert method setRelationField.

protected void setRelationField(Object newResource, RegistryEntry registryEntry, String relationshipName, Relationship relationship, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider) {
    if (relationship.getData().isPresent()) {
        ResourceIdentifier relationshipId = (ResourceIdentifier) relationship.getData().get();
        ResourceField field = registryEntry.getResourceInformation().findRelationshipFieldByName(relationshipName);
        if (field == null) {
            throw new ResourceException(String.format("Invalid relationship name: %s", relationshipName));
        }
        for (ResourceModificationFilter filter : modificationFilters) {
            relationshipId = filter.modifyOneRelationship(newResource, field, relationshipId);
        }
        Object relationObject;
        if (relationshipId == null) {
            relationObject = null;
            field.getAccessor().setValue(newResource, relationObject);
        } else {
            RegistryEntry entry = resourceRegistry.getEntry(relationshipId.getType());
            Class idFieldType = entry.getResourceInformation().getIdField().getType();
            Serializable typedRelationshipId = parseId(relationshipId, idFieldType);
            if (field.hasIdField()) {
                field.getIdAccessor().setValue(newResource, typedRelationshipId);
            }
            if (decideSetRelationObjectField(entry, typedRelationshipId, field)) {
                relationObject = fetchRelatedObject(entry, typedRelationshipId, parameterProvider, queryAdapter);
                field.getAccessor().setValue(newResource, relationObject);
            }
        }
    }
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceException(io.crnk.core.exception.ResourceException) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 35 with RegistryEntry

use of io.crnk.core.engine.registry.RegistryEntry 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)

Aggregations

RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)119 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)60 Test (org.junit.Test)38 ResourceField (io.crnk.core.engine.information.resource.ResourceField)36 ResourceRegistry (io.crnk.core.engine.registry.ResourceRegistry)19 QuerySpec (io.crnk.core.queryspec.QuerySpec)18 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 Serializable (java.io.Serializable)14 Task (io.crnk.core.mock.models.Task)13 Response (io.crnk.core.engine.dispatcher.Response)12 Document (io.crnk.core.engine.document.Document)11 Resource (io.crnk.core.engine.document.Resource)11 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)10 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)9 FilterSpec (io.crnk.core.queryspec.FilterSpec)9 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)8 Before (org.junit.Before)8 Collection (java.util.Collection)7 HashSet (java.util.HashSet)7 HttpMethod (io.crnk.core.engine.http.HttpMethod)6