Search in sources :

Example 11 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier 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 12 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class IncludeLookupSetter method mergeResource.

private Resource mergeResource(Object targetEntity, QueryAdapter queryAdapter, Map<ResourceIdentifier, Resource> resourceMap, Map<ResourceIdentifier, Object> entityMap, ResourceMappingConfig resourceMappingConfig) {
    Resource targetResource = resourceMapper.toData(targetEntity, queryAdapter, resourceMappingConfig);
    ResourceIdentifier targetId = targetResource.toIdentifier();
    if (!resourceMap.containsKey(targetId)) {
        resourceMap.put(targetId, targetResource);
    } else {
        // TODO consider merging
        targetResource = resourceMap.get(targetId);
    }
    if (!(targetEntity instanceof Resource)) {
        entityMap.put(targetId, targetEntity);
    }
    return targetResource;
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Resource(io.crnk.core.engine.document.Resource)

Example 13 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class RelationshipsResourceDelete method processToManyRelationship.

@Override
public void processToManyRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, Iterable<ResourceIdentifier> dataBodies, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    List<ResourceIdentifier> resourceIds = new ArrayList<>();
    for (ResourceIdentifier dataBody : dataBodies) {
        resourceIds.add(dataBody);
    }
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceIds = filter.modifyManyRelationship(resource, relationshipField, ResourceRelationshipModificationType.REMOVE, resourceIds);
    }
    List<Serializable> parsedIds = new LinkedList<>();
    for (ResourceIdentifier resourceId : resourceIds) {
        Serializable parsedId = targetResourceInformation.parseIdString(resourceId.getId());
        parsedIds.add(parsedId);
    }
    // noinspection unchecked
    relationshipRepositoryForClass.removeRelations(resource, parsedIds, relationshipField, queryAdapter);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ArrayList(java.util.ArrayList) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter) LinkedList(java.util.LinkedList)

Example 14 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class RelationshipsResourceDelete method processToOneRelationship.

@Override
protected void processToOneRelationship(Object resource, ResourceInformation targetResourceInformation, ResourceField relationshipField, ResourceIdentifier dataBody, QueryAdapter queryAdapter, RelationshipRepositoryAdapter relationshipRepositoryForClass) {
    ResourceIdentifier resourceId = null;
    for (ResourceModificationFilter filter : modificationFilters) {
        resourceId = filter.modifyOneRelationship(resource, relationshipField, resourceId);
    }
    Serializable parsedId = resourceId != null ? targetResourceInformation.parseIdString(resourceId.getId()) : null;
    // noinspection unchecked
    relationshipRepositoryForClass.setRelation(resource, parsedId, relationshipField, queryAdapter);
}
Also used : ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) Serializable(java.io.Serializable) ResourceModificationFilter(io.crnk.core.engine.filter.ResourceModificationFilter)

Example 15 with ResourceIdentifier

use of io.crnk.core.engine.document.ResourceIdentifier in project crnk-framework by crnk-project.

the class RelationshipsResourceUpsert method handle.

@Override
public final Response handle(JsonPath jsonPath, QueryAdapter queryAdapter, RepositoryMethodParameterProvider parameterProvider, Document requestBody) {
    String resourceName = jsonPath.getResourceType();
    PathIds resourceIds = jsonPath.getIds();
    RegistryEntry registryEntry = getRegistryEntry(resourceName);
    assertRequestDocument(requestBody, HttpMethod.POST, resourceName);
    Serializable castedResourceId = getResourceId(resourceIds, registryEntry);
    ResourceField relationshipField = registryEntry.getResourceInformation().findRelationshipFieldByName(jsonPath.getElementName());
    verifyFieldNotNull(relationshipField, jsonPath.getElementName());
    ResourceRepositoryAdapter resourceRepository = registryEntry.getResourceRepository(parameterProvider);
    @SuppressWarnings("unchecked") Object resource = resourceRepository.findOne(castedResourceId, queryAdapter).getEntity();
    ResourceInformation targetInformation = getRegistryEntry(relationshipField.getOppositeResourceType()).getResourceInformation();
    @SuppressWarnings("unchecked") RelationshipRepositoryAdapter relationshipRepositoryForClass = registryEntry.getRelationshipRepository(relationshipField, parameterProvider);
    if (Iterable.class.isAssignableFrom(relationshipField.getType())) {
        Iterable<ResourceIdentifier> dataBodies = (Iterable<ResourceIdentifier>) (requestBody.isMultiple() ? requestBody.getData().get() : Collections.singletonList(requestBody.getData().get()));
        processToManyRelationship(resource, targetInformation, relationshipField, dataBodies, queryAdapter, relationshipRepositoryForClass);
    } else {
        if (requestBody.isMultiple()) {
            throw new RequestBodyException(HttpMethod.POST, resourceName, "Multiple data in body");
        }
        ResourceIdentifier dataBody = (ResourceIdentifier) requestBody.getData().get();
        processToOneRelationship(resource, targetInformation, relationshipField, dataBody, queryAdapter, relationshipRepositoryForClass);
    }
    return new Response(new Document(), HttpStatus.NO_CONTENT_204);
}
Also used : Serializable(java.io.Serializable) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Document(io.crnk.core.engine.document.Document) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) RelationshipRepositoryAdapter(io.crnk.core.engine.internal.repository.RelationshipRepositoryAdapter) Response(io.crnk.core.engine.dispatcher.Response) ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceIdentifier(io.crnk.core.engine.document.ResourceIdentifier) RequestBodyException(io.crnk.core.exception.RequestBodyException) ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) PathIds(io.crnk.core.engine.internal.dispatcher.path.PathIds)

Aggregations

ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)60 Resource (io.crnk.core.engine.document.Resource)42 Document (io.crnk.core.engine.document.Document)40 Test (org.junit.Test)33 Relationship (io.crnk.core.engine.document.Relationship)31 QuerySpec (io.crnk.core.queryspec.QuerySpec)25 Response (io.crnk.core.engine.dispatcher.Response)14 Task (io.crnk.core.mock.models.Task)13 BaseControllerTest (io.crnk.core.engine.internal.dispatcher.controller.BaseControllerTest)11 ResourcePost (io.crnk.core.engine.internal.dispatcher.controller.ResourcePost)11 JsonPath (io.crnk.core.engine.internal.dispatcher.path.JsonPath)11 RelationIdTestResource (io.crnk.core.mock.models.RelationIdTestResource)9 Serializable (java.io.Serializable)9 ResourceModificationFilter (io.crnk.core.engine.filter.ResourceModificationFilter)8 Project (io.crnk.core.mock.models.Project)8 ArrayList (java.util.ArrayList)8 ResourceField (io.crnk.core.engine.information.resource.ResourceField)6 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)6 LazyTask (io.crnk.core.mock.models.LazyTask)6 AbstractDocumentMapperTest (io.crnk.core.engine.internal.document.mapper.AbstractDocumentMapperTest)4