Search in sources :

Example 6 with SortaJobExecution

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)));
    }
}
Also used : EntityType(org.molgenis.data.meta.model.EntityType) QueryImpl(org.molgenis.data.support.QueryImpl) CsvWriter(org.molgenis.data.csv.CsvWriter) Attribute(org.molgenis.data.meta.model.Attribute) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution)

Example 7 with SortaJobExecution

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);
}
Also used : PluginController(org.molgenis.web.PluginController) DEEP_COPY_ATTRS(org.molgenis.data.meta.model.EntityType.AttributeCopyMode.DEEP_COPY_ATTRS) MenuReaderService(org.molgenis.core.ui.menu.MenuReaderService) IdGenerator(org.molgenis.data.populate.IdGenerator) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) PermissionSystemService(org.molgenis.data.security.permission.PermissionSystemService) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) FileStore(org.molgenis.data.file.FileStore) Collections.singletonList(java.util.Collections.singletonList) JobExecutionMetaData(org.molgenis.jobs.model.JobExecutionMetaData) User(org.molgenis.data.security.auth.User) Model(org.springframework.ui.Model) ByteArrayInputStream(java.io.ByteArrayInputStream) SORTA_JOB_EXECUTION(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData.SORTA_JOB_EXECUTION) Arrays.asList(java.util.Arrays.asList) RunAsSystemAspect(org.molgenis.security.core.runas.RunAsSystemAspect) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) EntityCollectionResponse(org.molgenis.data.rest.EntityCollectionResponse) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) org.molgenis.data(org.molgenis.data) ImmutableMap(com.google.common.collect.ImmutableMap) XREF(org.molgenis.data.meta.AttributeType.XREF) UserAccountService(org.molgenis.security.user.UserAccountService) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) URI(org.molgenis.ontology.sorta.controller.SortaController.URI) CsvWriter(org.molgenis.data.csv.CsvWriter) SortaJobExecutionMetaData(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData) SortaService(org.molgenis.ontology.sorta.service.SortaService) EntityPager(org.molgenis.data.rest.EntityPager) java.util(java.util) SortaServiceImpl(org.molgenis.ontology.sorta.service.impl.SortaServiceImpl) SimpleDateFormat(java.text.SimpleDateFormat) QueryImpl(org.molgenis.data.support.QueryImpl) SortaCsvRepository(org.molgenis.ontology.sorta.repo.SortaCsvRepository) Controller(org.springframework.stereotype.Controller) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) OntologyTermMetaData(org.molgenis.ontology.core.meta.OntologyTermMetaData) NumberFormat(java.text.NumberFormat) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) HttpServletRequest(javax.servlet.http.HttpServletRequest) DynamicEntity(org.molgenis.data.support.DynamicEntity) SortaJobImpl(org.molgenis.ontology.sorta.job.SortaJobImpl) SortaServiceRequest(org.molgenis.ontology.sorta.request.SortaServiceRequest) Objects.requireNonNull(java.util.Objects.requireNonNull) EntityTypeFactory(org.molgenis.data.meta.model.EntityTypeFactory) StreamSupport(java.util.stream.StreamSupport) MatchingTaskContentMetaData(org.molgenis.ontology.sorta.meta.MatchingTaskContentMetaData) OntologyService(org.molgenis.ontology.core.service.OntologyService) ExecutorService(java.util.concurrent.ExecutorService) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) SortaJobFactory(org.molgenis.ontology.sorta.job.SortaJobFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SortaServiceUtil(org.molgenis.ontology.utils.SortaServiceUtil) File(java.io.File) SortaServiceResponse(org.molgenis.ontology.sorta.request.SortaServiceResponse) HttpStatus(org.springframework.http.HttpStatus) DESC(org.molgenis.data.Sort.Direction.DESC) SortaServiceUtil.getEntityAsMap(org.molgenis.ontology.utils.SortaServiceUtil.getEntityAsMap) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution) MultipartFile(org.springframework.web.multipart.MultipartFile) SortaJobExecutionFactory(org.molgenis.ontology.sorta.job.SortaJobExecutionFactory) InputStream(java.io.InputStream) DynamicEntity(org.molgenis.data.support.DynamicEntity) EntityCollectionResponse(org.molgenis.data.rest.EntityCollectionResponse) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution) EntityPager(org.molgenis.data.rest.EntityPager) QueryImpl(org.molgenis.data.support.QueryImpl) ImmutableMap(com.google.common.collect.ImmutableMap) SortaServiceUtil.getEntityAsMap(org.molgenis.ontology.utils.SortaServiceUtil.getEntityAsMap)

Example 8 with SortaJobExecution

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;
}
Also used : PluginController(org.molgenis.web.PluginController) DEEP_COPY_ATTRS(org.molgenis.data.meta.model.EntityType.AttributeCopyMode.DEEP_COPY_ATTRS) MenuReaderService(org.molgenis.core.ui.menu.MenuReaderService) IdGenerator(org.molgenis.data.populate.IdGenerator) Operator(org.molgenis.data.QueryRule.Operator) LoggerFactory(org.slf4j.LoggerFactory) PermissionSystemService(org.molgenis.data.security.permission.PermissionSystemService) StringUtils(org.apache.commons.lang3.StringUtils) Attribute(org.molgenis.data.meta.model.Attribute) FileStore(org.molgenis.data.file.FileStore) Collections.singletonList(java.util.Collections.singletonList) JobExecutionMetaData(org.molgenis.jobs.model.JobExecutionMetaData) User(org.molgenis.data.security.auth.User) Model(org.springframework.ui.Model) ByteArrayInputStream(java.io.ByteArrayInputStream) SORTA_JOB_EXECUTION(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData.SORTA_JOB_EXECUTION) Arrays.asList(java.util.Arrays.asList) RunAsSystemAspect(org.molgenis.security.core.runas.RunAsSystemAspect) EntityTypeIdentity(org.molgenis.data.security.EntityTypeIdentity) StringUtils.isEmpty(org.apache.commons.lang3.StringUtils.isEmpty) EntityCollectionResponse(org.molgenis.data.rest.EntityCollectionResponse) AttributeFactory(org.molgenis.data.meta.model.AttributeFactory) org.molgenis.data(org.molgenis.data) ImmutableMap(com.google.common.collect.ImmutableMap) XREF(org.molgenis.data.meta.AttributeType.XREF) UserAccountService(org.molgenis.security.user.UserAccountService) EntityType(org.molgenis.data.meta.model.EntityType) Collectors(java.util.stream.Collectors) Stream(java.util.stream.Stream) UserPermissionEvaluator(org.molgenis.security.core.UserPermissionEvaluator) org.springframework.web.bind.annotation(org.springframework.web.bind.annotation) URI(org.molgenis.ontology.sorta.controller.SortaController.URI) CsvWriter(org.molgenis.data.csv.CsvWriter) SortaJobExecutionMetaData(org.molgenis.ontology.sorta.meta.SortaJobExecutionMetaData) SortaService(org.molgenis.ontology.sorta.service.SortaService) EntityPager(org.molgenis.data.rest.EntityPager) java.util(java.util) SortaServiceImpl(org.molgenis.ontology.sorta.service.impl.SortaServiceImpl) SimpleDateFormat(java.text.SimpleDateFormat) QueryImpl(org.molgenis.data.support.QueryImpl) SortaCsvRepository(org.molgenis.ontology.sorta.repo.SortaCsvRepository) Controller(org.springframework.stereotype.Controller) EntityTypePermission(org.molgenis.data.security.EntityTypePermission) OntologyTermMetaData(org.molgenis.ontology.core.meta.OntologyTermMetaData) NumberFormat(java.text.NumberFormat) StringUtils.isNotEmpty(org.apache.commons.lang3.StringUtils.isNotEmpty) HttpServletRequest(javax.servlet.http.HttpServletRequest) DynamicEntity(org.molgenis.data.support.DynamicEntity) SortaJobImpl(org.molgenis.ontology.sorta.job.SortaJobImpl) SortaServiceRequest(org.molgenis.ontology.sorta.request.SortaServiceRequest) Objects.requireNonNull(java.util.Objects.requireNonNull) EntityTypeFactory(org.molgenis.data.meta.model.EntityTypeFactory) StreamSupport(java.util.stream.StreamSupport) MatchingTaskContentMetaData(org.molgenis.ontology.sorta.meta.MatchingTaskContentMetaData) OntologyService(org.molgenis.ontology.core.service.OntologyService) ExecutorService(java.util.concurrent.ExecutorService) AttributeType(org.molgenis.data.meta.AttributeType) Logger(org.slf4j.Logger) SortaJobFactory(org.molgenis.ontology.sorta.job.SortaJobFactory) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) SortaServiceUtil(org.molgenis.ontology.utils.SortaServiceUtil) File(java.io.File) SortaServiceResponse(org.molgenis.ontology.sorta.request.SortaServiceResponse) HttpStatus(org.springframework.http.HttpStatus) DESC(org.molgenis.data.Sort.Direction.DESC) SortaServiceUtil.getEntityAsMap(org.molgenis.ontology.utils.SortaServiceUtil.getEntityAsMap) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution) MultipartFile(org.springframework.web.multipart.MultipartFile) SortaJobExecutionFactory(org.molgenis.ontology.sorta.job.SortaJobExecutionFactory) InputStream(java.io.InputStream) User(org.molgenis.data.security.auth.User) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution)

Example 9 with SortaJobExecution

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!");
}
Also used : DynamicEntity(org.molgenis.data.support.DynamicEntity) SortaServiceResponse(org.molgenis.ontology.sorta.request.SortaServiceResponse) SortaJobExecution(org.molgenis.ontology.sorta.job.SortaJobExecution)

Aggregations

SortaJobExecution (org.molgenis.ontology.sorta.job.SortaJobExecution)9 EntityType (org.molgenis.data.meta.model.EntityType)4 File (java.io.File)3 IOException (java.io.IOException)3 CsvWriter (org.molgenis.data.csv.CsvWriter)3 Attribute (org.molgenis.data.meta.model.Attribute)3 DynamicEntity (org.molgenis.data.support.DynamicEntity)3 QueryImpl (org.molgenis.data.support.QueryImpl)3 SortaJobImpl (org.molgenis.ontology.sorta.job.SortaJobImpl)3 SortaCsvRepository (org.molgenis.ontology.sorta.repo.SortaCsvRepository)3 SortaServiceResponse (org.molgenis.ontology.sorta.request.SortaServiceResponse)3 MultipartFile (org.springframework.web.multipart.MultipartFile)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 InputStream (java.io.InputStream)2 NumberFormat (java.text.NumberFormat)2 SimpleDateFormat (java.text.SimpleDateFormat)2 java.util (java.util)2 Arrays.asList (java.util.Arrays.asList)2 Collections.singletonList (java.util.Collections.singletonList)2