Search in sources :

Example 11 with FilterSpec

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

the class ApprovalTestApplication method createApprovalModule.

// tag::approvalModule[]
public static SimpleModule createApprovalModule(ApprovalManager approvalManager) {
    FilterSpec approvalFilter = new FilterSpec(Arrays.asList("definitionKey"), FilterOperator.EQ, "scheduleChange");
    List<FilterSpec> approvalFilters = Arrays.asList(approvalFilter);
    SimpleModule module = new SimpleModule("approval");
    module.addRepositoryDecoratorFactory(ApprovalRepositoryDecorator.createFactory(approvalManager));
    module.addRepository(new ApprovalRelationshipRepository(Schedule.class, ScheduleApprovalProcessInstance.class, "approval", "approval/schedule", approvalFilters));
    return module;
}
Also used : Schedule(io.crnk.test.mock.models.Schedule) ScheduleApprovalProcessInstance(io.crnk.activiti.example.model.ScheduleApprovalProcessInstance) ApprovalRelationshipRepository(io.crnk.activiti.example.approval.ApprovalRelationshipRepository) FilterSpec(io.crnk.core.queryspec.FilterSpec) SimpleModule(io.crnk.core.module.SimpleModule)

Example 12 with FilterSpec

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

the class ApprovalRelationshipRepository method findOneTarget.

@Override
public P findOneTarget(Serializable sourceId, String fieldName, QuerySpec querySpec) {
    if (relationshipName.equals(fieldName)) {
        RegistryEntry resourceEntry = resourceRegistry.getEntry(resourceClass);
        RegistryEntry processEntry = resourceRegistry.getEntry(processInfoClass);
        String resourceType = resourceEntry.getResourceInformation().getResourceType();
        ResourceRepositoryV2 processRepository = (ResourceRepositoryV2) processEntry.getResourceRepository(null).getResourceRepository();
        QuerySpec processQuerySpec = querySpec.duplicate();
        processQuerySpec.addFilter(new FilterSpec(Arrays.asList(RESOURCE_ID_FIELD), FilterOperator.EQ, sourceId.toString()));
        processQuerySpec.addFilter(new FilterSpec(Arrays.asList(RESOURCE_TYPE_FIELD), FilterOperator.EQ, resourceType));
        baseFilters.forEach(processQuerySpec::addFilter);
        ResourceList list = processRepository.findAll(querySpec);
        PreconditionUtil.assertTrue("unique result expected", list.size() <= 1);
        return list.isEmpty() ? null : (P) list.get(0);
    } else {
        throw new UnsupportedOperationException("unknown fieldName '" + fieldName + "'");
    }
}
Also used : ResourceList(io.crnk.core.resource.list.ResourceList) ResourceRepositoryV2(io.crnk.core.repository.ResourceRepositoryV2) QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec) RegistryEntry(io.crnk.core.engine.registry.RegistryEntry)

Example 13 with FilterSpec

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

the class JpaEntityRepository method findOne.

@Override
public final T findOne(I id, QuerySpec querySpec) {
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(primaryKeyAttr.getName()), FilterOperator.EQ, id));
    List<T> results = findAll(idQuerySpec);
    return getUnique(results, id);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 14 with FilterSpec

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

the class JpaEntityRepository method findAll.

@Override
public final ResourceList<T> findAll(Iterable<I> ids, QuerySpec querySpec) {
    QuerySpec idQuerySpec = querySpec.duplicate();
    idQuerySpec.addFilter(new FilterSpec(Arrays.asList(primaryKeyAttr.getName()), FilterOperator.EQ, ids));
    return findAll(idQuerySpec);
}
Also used : QuerySpec(io.crnk.core.queryspec.QuerySpec) FilterSpec(io.crnk.core.queryspec.FilterSpec)

Example 15 with FilterSpec

use of io.crnk.core.queryspec.FilterSpec 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)

Aggregations

FilterSpec (io.crnk.core.queryspec.FilterSpec)56 QuerySpec (io.crnk.core.queryspec.QuerySpec)51 Test (org.junit.Test)40 HashSet (java.util.HashSet)14 Set (java.util.Set)13 AbstractQuerySpecTest (io.crnk.core.queryspec.AbstractQuerySpecTest)12 HashMap (java.util.HashMap)12 RegistryEntry (io.crnk.core.engine.registry.RegistryEntry)9 TaskResource (io.crnk.activiti.resource.TaskResource)8 MetaResource (io.crnk.meta.model.resource.MetaResource)8 ScheduleApprovalProcessInstance (io.crnk.activiti.example.model.ScheduleApprovalProcessInstance)5 ResourceInformation (io.crnk.core.engine.information.resource.ResourceInformation)5 MetaAttribute (io.crnk.meta.model.MetaAttribute)5 ResourceField (io.crnk.core.engine.information.resource.ResourceField)4 ResourceList (io.crnk.core.resource.list.ResourceList)3 RelatedEntity (io.crnk.jpa.model.RelatedEntity)3 ArrayList (java.util.ArrayList)3 MultivaluedMap (io.crnk.core.engine.internal.utils.MultivaluedMap)2 ResourceRepositoryV2 (io.crnk.core.repository.ResourceRepositoryV2)2 JsonApiResponse (io.crnk.core.repository.response.JsonApiResponse)2