use of org.molgenis.data.rest.EntityPager in project molgenis by molgenis.
the class RestControllerV2 method createEntityCollectionResponse.
private EntityCollectionResponseV2 createEntityCollectionResponse(String entityTypeId, EntityCollectionRequestV2 request, HttpServletRequest httpRequest, boolean includeCategories) {
EntityType meta = dataService.getEntityType(entityTypeId);
Query<Entity> q = request.getQ() != null ? request.getQ().createQuery(meta) : new QueryImpl<>();
q.pageSize(request.getNum()).offset(request.getStart()).sort(request.getSort());
Fetch fetch = AttributeFilterToFetchConverter.convert(request.getAttrs(), meta, LocaleContextHolder.getLocale().getLanguage());
if (fetch != null) {
q.fetch(fetch);
}
if (request.getAggs() != null) {
// return aggregates for aggregate query
AggregateQuery aggsQ = request.getAggs().createAggregateQuery(meta, q);
Attribute xAttr = aggsQ.getAttributeX();
Attribute yAttr = aggsQ.getAttributeY();
if (xAttr == null && yAttr == null) {
throw new MolgenisQueryException("Aggregate query is missing 'x' or 'y' attribute");
}
AggregateResult aggs = dataService.aggregate(entityTypeId, aggsQ);
AttributeResponseV2 xAttrResponse = xAttr != null ? new AttributeResponseV2(entityTypeId, meta, xAttr, fetch, permissionService, dataService) : null;
AttributeResponseV2 yAttrResponse = yAttr != null ? new AttributeResponseV2(entityTypeId, meta, yAttr, fetch, permissionService, dataService) : null;
return new EntityAggregatesResponse(aggs, xAttrResponse, yAttrResponse, BASE_URI + '/' + entityTypeId);
} else {
Long count = dataService.count(entityTypeId, new QueryImpl<>(q).setOffset(0).setPageSize(0));
Iterable<Entity> it;
if (count > 0 && q.getPageSize() > 0) {
it = () -> dataService.findAll(entityTypeId, q).iterator();
} else {
it = Collections.emptyList();
}
EntityPager pager = new EntityPager(request.getStart(), request.getNum(), count, it);
List<Map<String, Object>> entities = new ArrayList<>();
for (Entity entity : it) {
Map<String, Object> responseData = new LinkedHashMap<>();
createEntityValuesResponse(entity, fetch, responseData);
entities.add(responseData);
}
UriComponentsBuilder builder = UriComponentsBuilder.fromUriString(getFullURL(httpRequest));
String prevHref = null;
if (pager.getPrevStart() != null) {
builder.replaceQueryParam("start", pager.getPrevStart());
prevHref = builder.build(false).toUriString();
}
String nextHref = null;
if (pager.getNextStart() != null) {
builder.replaceQueryParam("start", pager.getNextStart());
nextHref = builder.build(false).toUriString();
}
return new EntityCollectionResponseV2(pager, entities, fetch, BASE_URI + '/' + entityTypeId, meta, permissionService, dataService, prevHref, nextHref, includeCategories);
}
}
use of org.molgenis.data.rest.EntityPager in project molgenis by molgenis.
the class SortaController method retrieveSortaJobResults.
@PostMapping("/match/retrieve")
@ResponseBody
public EntityCollectionResponse retrieveSortaJobResults(@RequestBody SortaServiceRequest sortaServiceRequest) {
List<Map<String, Object>> entityMaps = new ArrayList<>();
String sortaJobExecutionId = sortaServiceRequest.getSortaJobExecutionId();
String filterQuery = sortaServiceRequest.getFilterQuery();
String ontologyIri = sortaServiceRequest.getOntologyIri();
EntityPager entityPager = sortaServiceRequest.getEntityPager();
SortaJobExecution sortaJobExecution = findSortaJobExecution(sortaJobExecutionId);
String resultEntityName = sortaJobExecution.getResultEntityName();
double threshold = sortaJobExecution.getThreshold();
boolean isMatched = sortaServiceRequest.isMatched();
QueryRule queryRuleInputEntities = new QueryRule(Arrays.asList(new QueryRule(VALIDATED, EQUALS, isMatched), new QueryRule(isMatched ? OR : AND), new QueryRule(SCORE, isMatched ? GREATER_EQUAL : LESS, threshold)));
List<QueryRule> queryRuleInputEntitiesInOneMatchingTask = singletonList(queryRuleInputEntities);
// Add filter to the query if query string is not empty
if (isNotEmpty(filterQuery)) {
Iterable<String> filteredInputTermIds = dataService.findAll(sortaJobExecution.getSourceEntityName(), new QueryImpl<>().search(filterQuery)).map(inputEntity -> inputEntity.getString(SortaServiceImpl.DEFAULT_MATCHING_IDENTIFIER)).collect(Collectors.toList());
QueryRule previousQueryRule = new QueryRule(queryRuleInputEntitiesInOneMatchingTask);
QueryRule queryRuleFilterInput = new QueryRule(MatchingTaskContentMetaData.INPUT_TERM, Operator.IN, filteredInputTermIds);
queryRuleInputEntitiesInOneMatchingTask = Arrays.asList(previousQueryRule, new QueryRule(Operator.AND), queryRuleFilterInput);
}
Query<Entity> query = new QueryImpl<>(queryRuleInputEntitiesInOneMatchingTask);
long count = dataService.count(resultEntityName, query);
int start = entityPager.getStart();
int num = entityPager.getNum();
Stream<Entity> findAll = dataService.findAll(sortaJobExecution.getResultEntityName(), query.offset(start).pageSize(num).sort(new Sort().on(VALIDATED, DESC).on(SCORE, DESC)));
findAll.forEach(mappingEntity -> {
Map<String, Object> outputEntity = new HashMap<>();
outputEntity.put("inputTerm", getEntityAsMap(mappingEntity.getEntity(INPUT_TERM)));
outputEntity.put("matchedTerm", getEntityAsMap(mappingEntity));
Object matchedTerm = mappingEntity.get(MATCHED_TERM);
if (matchedTerm != null) {
outputEntity.put("ontologyTerm", SortaServiceUtil.getEntityAsMap(sortaService.getOntologyTermEntity(matchedTerm.toString(), ontologyIri)));
}
entityMaps.add(outputEntity);
});
EntityPager pager = new EntityPager(start, num, count, null);
return new EntityCollectionResponse(pager, entityMaps, "/match/retrieve", ontologyTermMetaData, permissionService, dataService);
}
Aggregations