Search in sources :

Example 96 with ResourceField

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

the class SetOwnerStrategy method removeRelations.

@Override
public void removeRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        Collection currentIds = (Collection) field.getIdAccessor().getValue(source);
        currentIds.removeAll((Collection) targetIds);
    } else {
        RegistryEntry targetEntry = context.getTargetEntry(field);
        Iterable<D> targets = context.findAll(targetEntry, targetIds);
        @SuppressWarnings("unchecked") Collection<D> currentTargets = getOrCreateCollection(source, field);
        for (D target : targets) {
            currentTargets.remove(target);
        }
    }
    sourceAdapter.update(source, context.createSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Collection(java.util.Collection) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 97 with ResourceField

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

the class ImplicitOwnerBasedRelationshipRepository method toResult.

private MultivaluedMap<I, D> toResult(String fieldName, ResourceInformation targetInformation, List sources, List<D> targets) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {

        @Override
        protected List<D> newList() {
            return new DefaultResourceList<>();
        }
    };
    Map targetMap = new HashMap();
    for (D target : targets) {
        Object targetId = targetInformation.getId(target);
        targetMap.put(targetId, target);
    }
    for (Object source : sources) {
        Object sourceId = sourceInformation.getId(source);
        Object targetId = field.getIdAccessor().getValue(source);
        if (field.isCollection()) {
            for (Object targetElementId : (Collection) targetId) {
                addResult(bulkResult, field, sourceId, targetElementId, targetMap);
            }
        } else if (targetId != null) {
            addResult(bulkResult, field, sourceId, targetId, targetMap);
        } else {
            bulkResult.add(sourceId, null);
        }
    }
    return bulkResult;
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) HashMap(java.util.HashMap) Collection(java.util.Collection) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) HashMap(java.util.HashMap) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) Map(java.util.Map)

Example 98 with ResourceField

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

the class ImplicitOwnerBasedRelationshipRepository method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    List sources = (List) sourceEntry.getResourceRepository().findAll(sourceIds, getSaveQueryAdapter(fieldName)).getEntity();
    ResourceInformation targetInformation = getTargetInformation(field);
    List<D> targets;
    if (field.hasIdField()) {
        Set targetIds = new HashSet();
        for (Object source : sources) {
            Object targetId = field.getIdAccessor().getValue(source);
            if (field.isCollection()) {
                targetIds.addAll((Collection) targetId);
            } else {
                targetIds.add(targetId);
            }
        }
        QuerySpec idQuerySpec = new QuerySpec(targetInformation);
        ResourceRepositoryAdapter<D, J> targetAdapter = getTargetEntry(field).getResourceRepository();
        JsonApiResponse response = targetAdapter.findAll(targetIds, new QuerySpecAdapter(idQuerySpec, resourceRegistry));
        targets = (List<D>) response.getEntity();
        return toResult(fieldName, targetInformation, sources, targets);
    } else {
        MultivaluedMap bulkResult = new MultivaluedMap<I, D>() {

            @Override
            protected List<D> newList() {
                return new DefaultResourceList<>();
            }
        };
        for (Object source : sources) {
            Object sourceId = sourceInformation.getId(source);
            Object target = field.getAccessor().getValue(source);
            if (target != null) {
                if (field.isCollection()) {
                    bulkResult.addAll(sourceId, (Collection) target);
                } else {
                    bulkResult.add(sourceId, target);
                }
            }
        }
        return bulkResult;
    }
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) Set(java.util.Set) HashSet(java.util.HashSet) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 99 with ResourceField

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

the class GetFromOppositeStrategy method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = context.getSourceEntry();
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = context.getTargetEntry(field);
    ResourceInformation targetInformation = targetEntry.getResourceInformation();
    ResourceField oppositeField = Objects.requireNonNull(targetInformation.findFieldByUnderlyingName(field.getOppositeName()));
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeField.getUnderlyingName(), sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
    idQuerySpec.includeRelation(Arrays.asList(oppositeField.getUnderlyingName()));
    ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
    JsonApiResponse response = targetAdapter.findAll(context.createQueryAdapter(idQuerySpec));
    Collection<D> results = (Collection<D>) response.getEntity();
    MultivaluedMap<I, D> bulkResult = new MultivaluedMap<I, D>() {

        @Override
        protected List<D> newList() {
            return new DefaultResourceList<>();
        }
    };
    Set<I> sourceIdSet = new HashSet<>();
    for (I sourceId : sourceIds) {
        sourceIdSet.add(sourceId);
    }
    for (D result : results) {
        handleTarget(bulkResult, result, sourceIdSet, oppositeField, sourceInformation);
    }
    return bulkResult;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry) ResourceField(io.crnk.core.engine.information.resource.ResourceField) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) Collection(java.util.Collection) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 100 with ResourceField

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

the class RelationshipRepositoryBase method setRelations.

@Override
public void setRelations(T source, Iterable<J> targetIds, String fieldName) {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceRepositoryAdapter<T, I> sourceAdapter = sourceEntry.getResourceRepository();
    ResourceInformation sourceInformation = getSourceEntry().getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    if (field.hasIdField()) {
        field.getIdAccessor().setValue(source, targetIds);
    } else {
        RegistryEntry targetEntry = getTargetEntry(field);
        Iterable<D> targets = getTargets(targetEntry, targetIds);
        field.getAccessor().setValue(source, targets);
    }
    sourceAdapter.update(source, getSaveQueryAdapter(fieldName));
}
Also used : ResourceField(io.crnk.core.engine.information.resource.ResourceField) ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

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