Search in sources :

Example 21 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method setRelation.

@SuppressWarnings("rawtypes")
public JsonApiResponse setRelation(T source, J targetId, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Serializable targetId = request.getId();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).setRelation(source, targetId, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.PATCH, source, queryAdapter, Arrays.asList(targetId), field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) Serializable(java.io.Serializable) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 22 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class RelationshipRepositoryAdapter method addRelations.

@SuppressWarnings("rawtypes")
public JsonApiResponse addRelations(T source, Iterable<J> targetIds, ResourceField field, QueryAdapter queryAdapter) {
    RepositoryRequestFilterChainImpl chain = new RepositoryRequestFilterChainImpl() {

        @Override
        protected JsonApiResponse invoke(RepositoryFilterContext context) {
            RepositoryRequestSpec request = context.getRequest();
            Object source = request.getEntity();
            Iterable<?> targetIds = request.getIds();
            ResourceField field = request.getRelationshipField();
            QueryAdapter queryAdapter = request.getQueryAdapter();
            if (isAnnotated) {
                ((AnnotatedRelationshipRepositoryAdapter) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName(), queryAdapter);
            } else if (relationshipRepository instanceof RelationshipRepositoryV2) {
                ((RelationshipRepositoryV2) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            } else {
                ((RelationshipRepository) relationshipRepository).addRelations(source, targetIds, field.getUnderlyingName());
            }
            return new JsonApiResponse();
        }
    };
    RepositoryRequestSpec requestSpec = RepositoryRequestSpecImpl.forRelation(moduleRegistry, HttpMethod.POST, source, queryAdapter, targetIds, field);
    return chain.doFilter(newRepositoryFilterContext(requestSpec));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) RepositoryRequestSpec(io.crnk.core.engine.dispatcher.RepositoryRequestSpec) AnnotatedRelationshipRepositoryAdapter(io.crnk.legacy.internal.AnnotatedRelationshipRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) RepositoryFilterContext(io.crnk.core.engine.filter.RepositoryFilterContext) RelationshipRepositoryV2(io.crnk.core.repository.RelationshipRepositoryV2) BulkRelationshipRepositoryV2(io.crnk.core.repository.BulkRelationshipRepositoryV2)

Example 23 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class IncludeLookupSetter method setIncludedElements.

public void setIncludedElements(Document document, Object entity, QueryAdapter queryAdapter, DocumentMappingConfig mappingConfig) {
    QueryAdapter inclusionQueryAdapter = queryAdapter;
    if (!allowPagination && !(queryAdapter instanceof QueryParamsAdapter) && queryAdapter != null) {
        // offset/limit cannot properly work for nested inclusions if becomes cyclic
        inclusionQueryAdapter = queryAdapter.duplicate();
        if (queryAdapter.getResourceInformation().getPagingBehavior() != null) {
            inclusionQueryAdapter.setPagingSpec(queryAdapter.getResourceInformation().getPagingBehavior().createEmptyPagingSpec());
        }
    }
    List<Object> entityList = DocumentMapperUtil.toList(entity);
    List<Resource> dataList = DocumentMapperUtil.toList(document.getData().get());
    Map<ResourceIdentifier, Resource> dataMap = new HashMap<>();
    Map<ResourceIdentifier, Object> entityMap = new HashMap<>();
    for (int i = 0; i < dataList.size(); i++) {
        Resource dataElement = dataList.get(i);
        ResourceIdentifier id = dataElement.toIdentifier();
        entityMap.put(id, entityList.get(i));
        dataMap.put(id, dataElement);
    }
    Map<ResourceIdentifier, Resource> resourceMap = new HashMap<>();
    resourceMap.putAll(dataMap);
    Set<ResourceIdentifier> inclusions = new HashSet<>();
    PopulatedCache populatedCache = new PopulatedCache();
    RepositoryMethodParameterProvider parameterProvider = mappingConfig.getParameterProvider();
    Set<String> fieldsWithEnforcedIdSerialization = mappingConfig.getFieldsWithEnforcedIdSerialization();
    ResourceMappingConfig resourceMappingConfig = mappingConfig.getResourceMapping();
    ArrayList<ResourceField> stack = new ArrayList<>();
    populate(dataList, inclusions, resourceMap, entityMap, stack, inclusionQueryAdapter, parameterProvider, fieldsWithEnforcedIdSerialization, populatedCache, resourceMappingConfig);
    // no need to include resources included in the data section
    inclusions.removeAll(dataMap.keySet());
    // setup included section
    ArrayList<Resource> included = new ArrayList<>();
    for (ResourceIdentifier inclusionId : inclusions) {
        Resource includedResource = resourceMap.get(inclusionId);
        PreconditionUtil.assertNotNull("resource not found", includedResource);
        included.add(includedResource);
    }
    Collections.sort(included);
    LOGGER.debug("Extracted included resources {}", included.toString());
    document.setIncluded(included);
}
Also used : HashMap(java.util.HashMap) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) Resource(io.crnk.core.engine.document.Resource) ArrayList(java.util.ArrayList) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) ResourceField(io.crnk.core.engine.information.resource.ResourceField) QueryParamsAdapter(io.crnk.legacy.internal.QueryParamsAdapter) RepositoryMethodParameterProvider(io.crnk.legacy.internal.RepositoryMethodParameterProvider) HashSet(java.util.HashSet)

Example 24 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class IncludeLookupSetter method populate.

private void populate(Collection<Resource> resourceList, Set<ResourceIdentifier> inclusions, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, List<ResourceField> fieldPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Set<String> fieldsWithEnforceIdSerialization, PopulatedCache populatedCache, ResourceMappingConfig resourceMappingConfig) {
    if (resourceList.isEmpty()) {
        // nothing to do
        return;
    }
    checkNoRecursion(fieldPath);
    Set<ResourceField> relationshipFields = util.getRelationshipFields(resourceList);
    for (ResourceField resourceField : relationshipFields) {
        fieldPath.add(resourceField);
        populateField(resourceField, queryAdapter, fieldPath, populatedCache, resourceList, fieldsWithEnforceIdSerialization, parameterProvider, resourceMap, entityMap, inclusions, resourceMappingConfig);
        fieldPath.remove(fieldPath.size() - 1);
    }
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField)

Example 25 with ResourceField

use of io.crnk.core.engine.information.resource.ResourceField in project crnk-framework by crnk-project.

the class DefaultResourceInformationProvider method build.

public ResourceInformation build(Class<?> resourceClass, boolean allowNonResourceBaseClass) {
    List<ResourceField> resourceFields = getResourceFields(resourceClass);
    String resourceType = getResourceType(resourceClass, allowNonResourceBaseClass);
    Optional<JsonPropertyOrder> propertyOrder = ClassUtils.getAnnotation(resourceClass, JsonPropertyOrder.class);
    if (propertyOrder.isPresent()) {
        JsonPropertyOrder propertyOrderAnnotation = propertyOrder.get();
        Collections.sort(resourceFields, new FieldOrderedComparator(propertyOrderAnnotation.value(), propertyOrderAnnotation.alphabetic()));
    }
    DefaultResourceInstanceBuilder<?> instanceBuilder = new DefaultResourceInstanceBuilder(resourceClass);
    Class<?> superclass = resourceClass.getSuperclass();
    String superResourceType = superclass != Object.class && context.accept(superclass) ? context.getResourceType(superclass) : null;
    ResourceInformation information = new ResourceInformation(context.getTypeParser(), resourceClass, resourceType, superResourceType, instanceBuilder, resourceFields, pagingBehaviors.get(ClassUtils.getAnnotation(resourceClass, JsonApiResource.class).get().pagingBehavior()));
    if (!allowNonResourceBaseClass && information.getIdField() == null) {
        throw new ResourceIdNotFoundException(resourceClass.getCanonicalName());
    }
    return information;
}
Also used : FieldOrderedComparator(io.crnk.core.engine.internal.utils.FieldOrderedComparator) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) JsonPropertyOrder(com.fasterxml.jackson.annotation.JsonPropertyOrder) ResourceIdNotFoundException(io.crnk.core.exception.ResourceIdNotFoundException)

Aggregations

ResourceField (io.crnk.core.engine.information.resource.ResourceField)109 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)75 Test (org.junit.Test)41 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)36 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)14 QueryAdapter (io.crnk.core.engine.query.QueryAdapter)13 QuerySpec (io.crnk.core.queryspec.QuerySpec)11 ArrayList (java.util.ArrayList)9 RepositoryRequestSpec (io.crnk.core.engine.dispatcher.RepositoryRequestSpec)8 Response (io.crnk.core.engine.dispatcher.Response)8 Resource (io.crnk.core.engine.document.Resource)8 RepositoryFilterContext (io.crnk.core.engine.filter.RepositoryFilterContext)8 BulkRelationshipRepositoryV2 (io.crnk.core.repository.BulkRelationshipRepositoryV2)8 HashSet (java.util.HashSet)8 Document (io.crnk.core.engine.document.Document)7 Task (io.crnk.core.mock.models.Task)7 Serializable (java.io.Serializable)7 RelationshipRepositoryAdapter (io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter)6 ResourceRepositoryAdapter (io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter)6 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)6