Search in sources :

Example 36 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class RelationshipRepositoryBase method findTargets.

@SuppressWarnings("unchecked")
public MultivaluedMap<I, D> findTargets(Iterable<I> sourceIds, String fieldName, QuerySpec querySpec) {
    RegistryEntry sourceEntry = resourceRegistry.findEntry(sourceResourceClass);
    ResourceInformation sourceInformation = sourceEntry.getResourceInformation();
    ResourceField field = sourceInformation.findFieldByUnderlyingName(fieldName);
    RegistryEntry targetEntry = getTargetEntry(field);
    String oppositeName = getOppositeName(fieldName);
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(oppositeName, sourceInformation.getIdField().getUnderlyingName()), FilterOperator.EQ, sourceIds));
    idQuerySpec.includeRelation(Arrays.asList(oppositeName));
    ResourceRepositoryAdapter<D, J> targetAdapter = targetEntry.getResourceRepository();
    JsonApiResponse response = targetAdapter.findAll(new QuerySpecAdapter(idQuerySpec, resourceRegistry));
    List<D> results = (List<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, oppositeName, sourceInformation);
    }
    return bulkResult;
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) FilterSpec(io.crnk.core.queryspec.FilterSpec) 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) JsonApiResponse(io.crnk.core.repository.response.JsonApiResponse) ResourceList(io.crnk.core.resource.list.ResourceList) ArrayList(java.util.ArrayList) DefaultResourceList(io.crnk.core.resource.list.DefaultResourceList) List(java.util.List) QuerySpec(io.crnk.core.queryspec.QuerySpec) MultivaluedMap(io.crnk.core.engine.internal.utils.MultivaluedMap) HashSet(java.util.HashSet)

Example 37 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class ForwardingStrategyContext method createSourceQuerySpec.

private QuerySpec createSourceQuerySpec() {
    RegistryEntry sourceEntry = getSourceEntry();
    ResourceInformation resourceInformation = sourceEntry.getResourceInformation();
    return new QuerySpec(resourceInformation.getResourceClass(), resourceInformation.getResourceType());
}
Also used : ResourceInformation(io.crnk.core.engine.information.resource.ResourceInformation) QuerySpec(io.crnk.core.queryspec.QuerySpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 38 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class ForwardingStrategyContext method findAll.

public <Q> Iterable<Q> findAll(RegistryEntry entry, Iterable<?> targetIds) {
    ResourceRepositoryAdapter targetAdapter = entry.getResourceRepository();
    QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec(entry.getResourceInformation()), resourceRegistry);
    return (Iterable) targetAdapter.findAll(targetIds, queryAdapter).getEntity();
}
Also used : ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 39 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class ForwardingStrategyContext method findOne.

public <Q> Q findOne(RegistryEntry entry, Serializable id) {
    ResourceRepositoryAdapter targetAdapter = entry.getResourceRepository();
    QueryAdapter queryAdapter = new QuerySpecAdapter(new QuerySpec(entry.getResourceInformation()), resourceRegistry);
    return (Q) targetAdapter.findOne(id, queryAdapter).getEntity();
}
Also used : ResourceRepositoryAdapter(io.crnk.core.engine.internal.repository.ResourceRepositoryAdapter) QueryAdapter(io.crnk.core.engine.query.QueryAdapter) QuerySpecAdapter(io.crnk.core.queryspec.internal.QuerySpecAdapter) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Example 40 with QuerySpec

use of io.crnk.core.queryspec.QuerySpec in project crnk-framework by crnk-project.

the class JsonApiUrlBuilder method buildUrlInternal.

private String buildUrlInternal(ResourceInformation resourceInformation, Object id, Object query, String relationshipName) {
    String url = resourceRegistry.getResourceUrl(resourceInformation);
    if (id instanceof Collection) {
        Collection<?> ids = (Collection<?>) id;
        Collection<String> strIds = new ArrayList<>();
        for (Object idElem : ids) {
            String strIdElem = resourceInformation.toIdString(idElem);
            strIds.add(strIdElem);
        }
        url += "/";
        url += StringUtils.join(",", strIds);
    } else if (id != null) {
        String strId = resourceInformation.toIdString(id);
        url += "/" + strId;
    }
    if (relationshipName != null) {
        url += "/relationships/" + relationshipName;
    }
    UrlParameterBuilder urlBuilder = new UrlParameterBuilder(url);
    if (query instanceof QuerySpec) {
        QuerySpec querySpec = (QuerySpec) query;
        urlBuilder.addQueryParameters(querySpecSerializer.serialize(querySpec));
    } else if (query instanceof QueryParams) {
        QueryParams queryParams = (QueryParams) query;
        urlBuilder.addQueryParameters(queryParamsSerializer.serializeFilters(queryParams));
        urlBuilder.addQueryParameters(queryParamsSerializer.serializeSorting(queryParams));
        urlBuilder.addQueryParameters(queryParamsSerializer.serializeGrouping(queryParams));
        urlBuilder.addQueryParameters(queryParamsSerializer.serializePagination(queryParams));
        urlBuilder.addQueryParameters(queryParamsSerializer.serializeIncludedFields(queryParams));
        urlBuilder.addQueryParameters(queryParamsSerializer.serializeIncludedRelations(queryParams));
    }
    return urlBuilder.toString();
}
Also used : ArrayList(java.util.ArrayList) Collection(java.util.Collection) QueryParams(io.crnk.legacy.queryParams.QueryParams) QuerySpec(io.crnk.core.queryspec.QuerySpec)

Aggregations

QuerySpec (io.crnk.core.queryspec.QuerySpec)306 Test (org.junit.Test)233 FilterSpec (io.crnk.core.queryspec.FilterSpec)51 Document (io.crnk.core.engine.document.Document)45 Resource (io.crnk.core.engine.document.Resource)43 Set (java.util.Set)39 HashMap (java.util.HashMap)37 HashSet (java.util.HashSet)36 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)34 QuerySpecAdapter (io.crnk.core.queryspec.internal.QuerySpecAdapter)32 AbstractJpaJerseyTest (io.crnk.jpa.AbstractJpaJerseyTest)32 Task (io.crnk.test.mock.models.Task)32 Project (io.crnk.core.mock.models.Project)28 Relationship (io.crnk.core.engine.document.Relationship)26 Task (io.crnk.core.mock.models.Task)26 TestEntity (io.crnk.jpa.model.TestEntity)26 ResourceIdentifier (io.crnk.core.engine.document.ResourceIdentifier)25 Serializable (java.io.Serializable)24 RelatedEntity (io.crnk.jpa.model.RelatedEntity)21 ResourceRegistryTest (io.crnk.core.resource.registry.ResourceRegistryTest)20