use of org.molgenis.ontology.sorta.job.SortaJobExecution in project molgenis by molgenis.
the class SortaController method download.
@GetMapping("/match/download/{sortaJobExecutionId}")
public void download(@PathVariable String sortaJobExecutionId, HttpServletResponse response) throws IOException {
try (CsvWriter csvWriter = new CsvWriter(response.getOutputStream(), SortaServiceImpl.DEFAULT_SEPARATOR)) {
SortaJobExecution sortaJobExecution = findSortaJobExecution(sortaJobExecutionId);
response.setContentType("text/csv");
response.addHeader("Content-Disposition", "attachment; filename=" + generateCsvFileName());
List<String> columnHeaders = new ArrayList<>();
EntityType targetMetadata = entityTypeFactory.create("SortaDownload" + sortaJobExecutionId);
EntityType sourceMetaData = dataService.getEntityType(sortaJobExecution.getSourceEntityName());
for (Attribute attribute : sourceMetaData.getAttributes()) {
if (!attribute.getName().equalsIgnoreCase(SortaCsvRepository.ALLOWED_IDENTIFIER)) {
columnHeaders.add(attribute.getName());
targetMetadata.addAttribute(attrMetaFactory.create().setName(attribute.getName()));
}
}
columnHeaders.addAll(Arrays.asList(OntologyTermMetaData.ONTOLOGY_TERM_NAME, OntologyTermMetaData.ONTOLOGY_TERM_IRI, MatchingTaskContentMetaData.SCORE, MatchingTaskContentMetaData.VALIDATED));
targetMetadata.addAttribute(ontologyTermMetaData.getAttribute(OntologyTermMetaData.ONTOLOGY_TERM_NAME));
targetMetadata.addAttribute(ontologyTermMetaData.getAttribute(OntologyTermMetaData.ONTOLOGY_TERM_IRI));
targetMetadata.addAttribute(Attribute.newInstance(matchingTaskContentMetaData.getAttribute(MatchingTaskContentMetaData.SCORE), EntityType.AttributeCopyMode.SHALLOW_COPY_ATTRS, attrMetaFactory).setDataType(AttributeType.STRING));
targetMetadata.addAttribute(matchingTaskContentMetaData.getAttribute(MatchingTaskContentMetaData.VALIDATED));
csvWriter.writeAttributeNames(columnHeaders);
dataService.findAll(sortaJobExecution.getResultEntityName(), new QueryImpl<>()).forEach(resultEntity -> csvWriter.add(toDownloadRow(sortaJobExecution, resultEntity, targetMetadata)));
}
}
use of org.molgenis.ontology.sorta.job.SortaJobExecution 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);
}
use of org.molgenis.ontology.sorta.job.SortaJobExecution in project molgenis by molgenis.
the class SortaController method getJobsForCurrentUser.
private List<SortaJobExecution> getJobsForCurrentUser() {
final List<SortaJobExecution> jobs = new ArrayList<>();
User currentUser = userAccountService.getCurrentUser();
Query<SortaJobExecution> query = dataService.query(SORTA_JOB_EXECUTION, SortaJobExecution.class).eq(JobExecutionMetaData.USER, currentUser.getUsername());
query.sort().on(JobExecutionMetaData.START_DATE, DESC);
RunAsSystemAspect.runAsSystem(() -> query.findAll().forEach(job -> {
// TODO: fetch the user as well
job.set(JobExecutionMetaData.USER, currentUser.getUsername());
jobs.add(job);
}));
return jobs;
}
use of org.molgenis.ontology.sorta.job.SortaJobExecution in project molgenis by molgenis.
the class SortaController method findMatchingOntologyTerms.
@PostMapping("/match/entity")
@ResponseBody
public SortaServiceResponse findMatchingOntologyTerms(@RequestBody Map<String, Object> request) {
// TODO: less obfuscated request object, let Spring do the matching
if (request.containsKey("sortaJobExecutionId") && !isEmpty(request.get("sortaJobExecutionId").toString()) && request.containsKey(IDENTIFIER) && !isEmpty(request.get(IDENTIFIER).toString())) {
String sortaJobExecutionId = request.get("sortaJobExecutionId").toString();
SortaJobExecution sortaJobExecution = findSortaJobExecution(sortaJobExecutionId);
if (sortaJobExecution == null)
return new SortaServiceResponse("sortaJobExecutionId is invalid!");
String inputTermIdentifier = request.get(IDENTIFIER).toString();
Entity inputEntity = dataService.findOneById(sortaJobExecution.getSourceEntityName(), inputTermIdentifier);
if (inputEntity == null)
return new SortaServiceResponse("inputTerm identifier is invalid!");
return new SortaServiceResponse(inputEntity, sortaService.findOntologyTermEntities(sortaJobExecution.getOntologyIri(), inputEntity));
}
return new SortaServiceResponse("Please check that sortaJobExecutionId and identifier keys exist in input and have nonempty value!");
}
Aggregations