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;
}
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 + "'");
}
}
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);
}
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);
}
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;
}
Aggregations