use of org.molgenis.core.ui.data.rsql.MolgenisRSQL in project molgenis by molgenis.
the class RestController method retrieveEntityCollection.
/**
* Does a rsql/fiql query, returns the result as csv
* <p>
* Parameters:
* <p>
* q: the query
* <p>
* attributes: the attributes to return, if not specified returns all attributes
* <p>
* start: the index of the first row, default 0
* <p>
* num: the number of results to return, default 100, max 10000
* <p>
* <p>
* Example: /api/v1/csv/person?q=firstName==Piet&attributes=firstName,lastName&start=10&num=100
*/
@GetMapping(value = "/csv/{entityTypeId}", produces = "text/csv")
@ResponseBody
public EntityCollection retrieveEntityCollection(@PathVariable("entityTypeId") String entityTypeId, @RequestParam(value = "attributes", required = false) String[] attributes, HttpServletRequest req, HttpServletResponse resp) throws IOException {
final Set<String> attributesSet = toAttributeSet(attributes);
EntityType meta;
Iterable<Entity> entities;
try {
meta = dataService.getEntityType(entityTypeId);
Query<Entity> q = new QueryStringParser(meta, molgenisRSQL).parseQueryString(req.getParameterMap());
String[] sortAttributeArray = req.getParameterMap().get("sortColumn");
if (sortAttributeArray != null && sortAttributeArray.length == 1 && StringUtils.isNotEmpty(sortAttributeArray[0])) {
String sortAttribute = sortAttributeArray[0];
String[] sortOrderArray = req.getParameterMap().get("sortOrder");
Sort.Direction order = Sort.Direction.ASC;
if (sortOrderArray != null && sortOrderArray.length == 1 && StringUtils.isNotEmpty(sortOrderArray[0])) {
String sortOrder = sortOrderArray[0];
switch(sortOrder) {
case "ASC":
order = Sort.Direction.ASC;
break;
case "DESC":
order = Sort.Direction.DESC;
break;
default:
throw new RuntimeException("unknown sort order");
}
}
q.sort().on(sortAttribute, order);
}
if (q.getPageSize() == 0) {
q.pageSize(EntityCollectionRequest.DEFAULT_ROW_COUNT);
}
if (q.getPageSize() > EntityCollectionRequest.MAX_ROWS) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Num exceeded the maximum of " + EntityCollectionRequest.MAX_ROWS + " rows");
return null;
}
entities = () -> dataService.findAll(entityTypeId, q).iterator();
} catch (ConversionFailedException | RSQLParserException | UnknownAttributeException | IllegalArgumentException | UnsupportedOperationException | UnknownEntityException e) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, e.getMessage());
return null;
} catch (MolgenisDataAccessException e) {
resp.sendError(HttpServletResponse.SC_UNAUTHORIZED);
return null;
}
// Check attribute names
Iterable<String> attributesIterable = Iterables.transform(meta.getAtomicAttributes(), attribute -> attribute.getName().toLowerCase());
if (attributesSet != null) {
SetView<String> diff = Sets.difference(attributesSet, Sets.newHashSet(attributesIterable));
if (!diff.isEmpty()) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown attributes " + diff);
return null;
}
}
attributesIterable = Iterables.transform(meta.getAtomicAttributes(), Attribute::getName);
if (attributesSet != null) {
attributesIterable = Iterables.filter(attributesIterable, attribute -> attributesSet.contains(attribute.toLowerCase()));
}
return new DefaultEntityCollection(entities, attributesIterable);
}
Aggregations