|
| 1 | +package life.qbic.projectmanagement.infrastructure.ontology; |
| 2 | + |
| 3 | +import java.util.List; |
| 4 | +import java.util.Objects; |
| 5 | +import life.qbic.projectmanagement.application.OntologyClassEntity; |
| 6 | +import life.qbic.projectmanagement.application.SortOrder; |
| 7 | +import life.qbic.projectmanagement.application.api.OntologyTermLookup; |
| 8 | +import life.qbic.projectmanagement.infrastructure.OffsetBasedRequest; |
| 9 | +import org.springframework.context.annotation.Scope; |
| 10 | +import org.springframework.data.domain.Sort; |
| 11 | +import org.springframework.data.domain.Sort.Order; |
| 12 | +import org.springframework.stereotype.Component; |
| 13 | + |
| 14 | +/** |
| 15 | + * Basic implementation to query ontology class information |
| 16 | + * |
| 17 | + * @since 1.0.0 |
| 18 | + */ |
| 19 | +@Component |
| 20 | +@Scope("singleton") |
| 21 | +public class OntologyTermJpaRepository implements OntologyTermLookup { |
| 22 | + |
| 23 | + private final OntologyTermRepository ontologyTermRepository; |
| 24 | + |
| 25 | + public OntologyTermJpaRepository(OntologyTermRepository ontologyTermRepository) { |
| 26 | + Objects.requireNonNull(ontologyTermRepository); |
| 27 | + this.ontologyTermRepository = ontologyTermRepository; |
| 28 | + } |
| 29 | + |
| 30 | + @Override |
| 31 | + public List<OntologyClassEntity> query(int offset, int limit) { |
| 32 | + return ontologyTermRepository.findAll(new OffsetBasedRequest(offset, limit)).getContent(); |
| 33 | + } |
| 34 | + |
| 35 | + /** |
| 36 | + * The way the MyISAM engine searches the fulltext index makes it necessary to use multiple |
| 37 | + * search terms that need to be found instead of one full search term. |
| 38 | + * The asterisk suffix is needed so incomplete words are found (mu for musculus). |
| 39 | + */ |
| 40 | + private String buildSearchTerm(String searchString) { |
| 41 | + StringBuilder searchTermBuilder = new StringBuilder(); |
| 42 | + for(String word : searchString.split(" ")) { |
| 43 | + searchTermBuilder.append(" +" + word); |
| 44 | + } |
| 45 | + searchTermBuilder.append("*"); |
| 46 | + return searchTermBuilder.toString().trim(); |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + public List<OntologyClassEntity> query(String searchString, List<String> ontologies, int offset, |
| 51 | + int limit, List<SortOrder> sortOrders) { |
| 52 | + List<Order> orders = sortOrders.stream().map(it -> { |
| 53 | + Order order; |
| 54 | + if (it.isDescending()) { |
| 55 | + order = Order.desc(it.propertyName()); |
| 56 | + } else { |
| 57 | + order = Order.asc(it.propertyName()); |
| 58 | + } |
| 59 | + return order; |
| 60 | + }).toList(); |
| 61 | + // provide a short list of initial values when no search string was provided |
| 62 | + if(searchString.isBlank()) { |
| 63 | + return ontologyTermRepository.findByLabelNotNullAndOntologyIn(ontologies, |
| 64 | + new OffsetBasedRequest(offset, limit, Sort.by(orders))).getContent(); |
| 65 | + } |
| 66 | + // if the search string is shorter than 2, normal matching is faster |
| 67 | + if(searchString.length()==1) { |
| 68 | + return ontologyTermRepository.findByLabelStartingWithIgnoreCaseAndOntologyIn(searchString, ontologies, |
| 69 | + new OffsetBasedRequest(offset, limit, Sort.by(orders))).getContent(); |
| 70 | + } |
| 71 | + // otherwise create a more complex search term for fulltext search |
| 72 | + String searchTerm = buildSearchTerm(searchString); |
| 73 | + return ontologyTermRepository.findByLabelFulltextMatching( |
| 74 | + searchTerm, ontologies, new OffsetBasedRequest(offset, limit, Sort.by(orders))).getContent(); |
| 75 | + } |
| 76 | +} |
0 commit comments