diff --git a/src/main/java/com/labsynch/labseer/api/ApiExperimentController.java b/src/main/java/com/labsynch/labseer/api/ApiExperimentController.java index d6bfd2857..6107a51ba 100755 --- a/src/main/java/com/labsynch/labseer/api/ApiExperimentController.java +++ b/src/main/java/com/labsynch/labseer/api/ApiExperimentController.java @@ -44,6 +44,7 @@ import com.labsynch.labseer.dto.TsvLoaderResponseDTO; import com.labsynch.labseer.exceptions.ErrorMessage; import com.labsynch.labseer.exceptions.NotFoundException; +import com.labsynch.labseer.exceptions.TooManyResultsException; import com.labsynch.labseer.exceptions.UniqueNameException; import com.labsynch.labseer.service.AnalysisGroupService; import com.labsynch.labseer.service.AnalysisGroupValueService; @@ -1634,20 +1635,19 @@ public ResponseEntity jsonFindExperimentByNameGet(@RequestPara @RequestMapping(value = "/protocol/{codeName}", method = RequestMethod.GET, headers = "Accept=application/json") @ResponseBody public ResponseEntity jsonFindExperimentsByProtocolCodeName( - @PathVariable("codeName") String codeName, @RequestParam(value = "with", required = false) String with) { + @PathVariable("codeName") String codeName, @RequestParam(value = "projects", required = false) List projects, @RequestParam(value = "with", required = false) String with) { HttpHeaders headers = new HttpHeaders(); headers.add("Content-Type", "application/json; charset=utf-8"); - List protocols = Protocol.findProtocolsByCodeNameEqualsAndIgnoredNot(codeName, true).getResultList(); - if (protocols.size() == 1) { + try { + Collection experiments = experimentService.findExperimentsByProtocolCodeName(codeName, projects); return new ResponseEntity( - Experiment.toJsonArrayStub(Experiment.findExperimentsByProtocol(protocols.get(0)).getResultList()), + Experiment.toJsonArrayStub(experiments), headers, HttpStatus.OK); - } else if (protocols.size() > 1) { - logger.error("ERROR: multiple protocols found with the same code name"); - return new ResponseEntity("[ ]", headers, HttpStatus.CONFLICT); - } else { - logger.warn("WARN: no protocols found with the query code name"); + } catch (NoResultException e) { return new ResponseEntity("[ ]", headers, HttpStatus.NOT_FOUND); + + } catch (TooManyResultsException e) { + return new ResponseEntity("[ ]", headers, HttpStatus.CONFLICT); } } diff --git a/src/main/java/com/labsynch/labseer/domain/SaltForm.java b/src/main/java/com/labsynch/labseer/domain/SaltForm.java index 4ba95bc97..a8f99a8f4 100755 --- a/src/main/java/com/labsynch/labseer/domain/SaltForm.java +++ b/src/main/java/com/labsynch/labseer/domain/SaltForm.java @@ -29,7 +29,9 @@ import javax.persistence.criteria.JoinType; import javax.persistence.criteria.ParameterExpression; import javax.persistence.criteria.Predicate; +import javax.persistence.criteria.Expression; import javax.persistence.criteria.Root; +import javax.persistence.criteria.Order; import javax.validation.constraints.Size; import com.labsynch.labseer.dto.SearchFormDTO; @@ -239,6 +241,7 @@ public static TypedQuery findSaltFormsByMeta(SearchFormDTO searchParam Predicate[] predicates = new Predicate[0]; List predicateList = new ArrayList(); + List orderList = new ArrayList(); if (searchParams.getChemist() != null && !searchParams.getChemist().equals("anyone")) { logger.debug("incoming chemist :" + searchParams.getChemist().toString()); @@ -282,12 +285,34 @@ public static TypedQuery findSaltFormsByMeta(SearchFormDTO searchParam List corpNames = searchParams.getFormattedCorpNameList(); logger.debug("incoming corpNameList :" + corpNames.toString()); ArrayList corpNameLikePredicates = new ArrayList(); + Expression maxSimilarity = null; for (String corpName : corpNames) { Predicate predicate = criteriaBuilder.like(saltFormParent.get("corpName"), '%' + corpName); corpNameLikePredicates.add(predicate); + // Using pg_trgm.similarity to order results based on how similar the value is to + // the formatted corporate names. + Expression similarity = criteriaBuilder.function( + "similarity", + Double.class, + saltFormParent.get("corpName"), + criteriaBuilder.literal(corpName) + ); + if (maxSimilarity == null) { + maxSimilarity = similarity; + } + else { + maxSimilarity = criteriaBuilder.selectCase() + .when(criteriaBuilder.greaterThan(similarity, maxSimilarity), similarity) + .otherwise(maxSimilarity) + .as(Double.class); + } } Predicate predicate = criteriaBuilder.or(corpNameLikePredicates.toArray(new Predicate[0])); predicateList.add(predicate); + if (maxSimilarity != null) { + // Rows with a higher similarity will be ordered first. + orderList.add(criteriaBuilder.desc(maxSimilarity)); + } } if (searchParams.getAlias() != null && !searchParams.getAlias().equals("")) { Predicate predicate = null; @@ -335,6 +360,7 @@ public static TypedQuery findSaltFormsByMeta(SearchFormDTO searchParam predicates = predicateList.toArray(predicates); criteria.where(criteriaBuilder.and(predicates)); + criteria.orderBy(orderList); TypedQuery q = em.createQuery(criteria); if (searchParams.getMaxSynthDate() != null) { diff --git a/src/main/java/com/labsynch/labseer/dto/SearchFormReturnDTO.java b/src/main/java/com/labsynch/labseer/dto/SearchFormReturnDTO.java index 6bdf8593b..605787b06 100755 --- a/src/main/java/com/labsynch/labseer/dto/SearchFormReturnDTO.java +++ b/src/main/java/com/labsynch/labseer/dto/SearchFormReturnDTO.java @@ -15,7 +15,7 @@ public class SearchFormReturnDTO { - private Collection foundCompounds = new HashSet(); + private Collection foundCompounds = new ArrayList(); private boolean lotsWithheld; diff --git a/src/main/java/com/labsynch/labseer/service/ExperimentService.java b/src/main/java/com/labsynch/labseer/service/ExperimentService.java index 4725ad0b7..bb7fc3bad 100755 --- a/src/main/java/com/labsynch/labseer/service/ExperimentService.java +++ b/src/main/java/com/labsynch/labseer/service/ExperimentService.java @@ -51,6 +51,10 @@ public Collection getExperimentNodesByProtocolTree( public Collection findExperimentsByMetadataJson( List metaDataList); + + public Collection findExperimentsByProtocolCodeName(String query, List projects) + throws TooManyResultsException; + public Collection findExperimentsByGenericMetaDataSearch(String query, String userName, Boolean includeDeleted) throws TooManyResultsException; diff --git a/src/main/java/com/labsynch/labseer/service/ExperimentServiceImpl.java b/src/main/java/com/labsynch/labseer/service/ExperimentServiceImpl.java index 87c4b5101..cc0ef0159 100755 --- a/src/main/java/com/labsynch/labseer/service/ExperimentServiceImpl.java +++ b/src/main/java/com/labsynch/labseer/service/ExperimentServiceImpl.java @@ -1118,6 +1118,45 @@ public Collection findExperimentsByGenericMetaDataSearch(String quer } + public Collection findExperimentsByProtocolCodeName(String codeName, List projects) + throws TooManyResultsException, NoResultException { + List protocols = Protocol.findProtocolsByCodeNameEqualsAndIgnoredNot(codeName, true).getResultList(); + + if (protocols.size() > 1) { + logger.error("ERROR: multiple protocols found with the same code name"); + throw new TooManyResultsException("ERROR: multiple protocols found with the same code name"); + } else if (protocols.size() < 1) { + logger.warn("WARN: no protocols found with the query code name"); + throw new NoResultException("WARN: no protocols found with the query code name"); + } + + Collection rawResults = Experiment.findExperimentsByProtocol(protocols.get(0)).getResultList(); + if (propertiesUtilService.getRestrictExperiments()) { + projects.add("unassigned"); + Collection results = new HashSet(); + for (Experiment rawResult : rawResults) { + String experimentProject = null; + for (ExperimentState state : rawResult.getLsStates()) { + if (!state.isIgnored() && !state.isDeleted()) { + for (ExperimentValue value : state.getLsValues()) { + if (value.getLsKind().equals("project") && !value.getDeleted() && !value.getIgnored()) { + experimentProject = value.getCodeValue(); + break; + } + } + } + } + if (projects.contains(experimentProject)) { + results.add(rawResult); + } + } + return results; + } else { + return rawResults; + } + + } + @Override public Collection findExperimentsByGenericMetaDataSearch(String queryString, List projects, Boolean includeDeleted) throws TooManyResultsException { diff --git a/src/main/java/com/labsynch/labseer/service/SearchFormServiceImpl.java b/src/main/java/com/labsynch/labseer/service/SearchFormServiceImpl.java index 7164c5c2a..4704b9d33 100755 --- a/src/main/java/com/labsynch/labseer/service/SearchFormServiceImpl.java +++ b/src/main/java/com/labsynch/labseer/service/SearchFormServiceImpl.java @@ -613,7 +613,7 @@ public SearchFormReturnDTO findQuerySaltForms(SearchFormDTO searchParams) throws private SearchFormReturnDTO filterSearchResultsByProject(SearchFormReturnDTO searchResults, List projects) { if (!searchResults.getFoundCompounds().isEmpty()) { - Collection filteredFoundCompounds = new HashSet(); + Collection filteredFoundCompounds = new ArrayList(); for (SearchCompoundReturnDTO foundCompound : searchResults.getFoundCompounds()) { List filteredFoundLots = new ArrayList(); for (SearchLotDTO foundLot : foundCompound.getLotIDs()) {