Merge branch 'release/5.18.0'
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / view / search / facet / occurrence / OccurrenceSearchController.java
1 /**
2 * Copyright (C) 2019 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.taxeditor.view.search.facet.occurrence;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Comparator;
14 import java.util.List;
15
16 import org.eclipse.swt.SWT;
17 import org.eclipse.swt.widgets.Composite;
18
19 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
20 import eu.etaxonomy.cdm.api.service.config.FindOccurrencesConfigurator;
21 import eu.etaxonomy.cdm.api.service.dto.DerivedUnitDTO;
22 import eu.etaxonomy.cdm.persistence.query.MatchMode;
23 import eu.etaxonomy.taxeditor.store.CdmStore;
24 import eu.etaxonomy.taxeditor.store.StoreUtil;
25 import eu.etaxonomy.taxeditor.view.search.facet.SearchComposite;
26 import eu.etaxonomy.taxeditor.view.search.facet.SearchController;
27
28 /**
29 * @author pplitzner
30 * @since Jan 22, 2019
31 *
32 */
33 public class OccurrenceSearchController extends SearchController<OccurrenceSearchResult, DerivedUnitDTO> {
34
35 public OccurrenceSearchController(SearchComposite composite) {
36 super(composite);
37 }
38
39 @Override
40 protected Comparator<OccurrenceSearchResult> getResultComparator(String searchString) {
41 return new Comparator<OccurrenceSearchResult>() {
42 @Override
43 public int compare(OccurrenceSearchResult o1, OccurrenceSearchResult o2) {
44 String label1 = o1.getContent().getLabel();
45 String label2 = o2.getContent().getLabel();
46 return StoreUtil.compareBySearchString(searchString, label1, label2);
47 }
48
49 };
50 }
51
52 @Override
53 protected List<OccurrenceSearchResult> searchResults(String searchString){
54 List<OccurrenceSearchResult> searchResults = new ArrayList<>();
55 FindOccurrencesConfigurator config = new FindOccurrencesConfigurator();
56 config.setTitleSearchString(searchString);
57 config.setMatchMode(MatchMode.ANYWHERE);
58 Collection<DerivedUnitDTO> dtos = CdmStore.getService(IOccurrenceService.class).findByTitleDerivedUnitDTO(config);
59 dtos.stream().forEach(dto->searchResults.add(new OccurrenceSearchResult(dto)));
60 return searchResults;
61 }
62
63 @Override
64 protected OccurrenceSearchResultComposite createResultComposite(OccurrenceSearchResult result, Composite parent) {
65 return new OccurrenceSearchResultComposite(result, parent, SWT.NONE);
66 }
67
68 @Override
69 protected List<OccurrenceSearchResult> getSelectedResults() {
70 return null;
71 }
72
73 }