Fix field unit filter for bulk editor #5572
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / store / SearchManager.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.store;
12
13 import java.util.ArrayList;
14 import java.util.Arrays;
15 import java.util.List;
16 import java.util.UUID;
17
18 import org.eclipse.jface.dialogs.MessageDialog;
19 import org.eclipse.swt.widgets.Display;
20
21 import eu.etaxonomy.cdm.api.service.IAgentService;
22 import eu.etaxonomy.cdm.api.service.IGroupService;
23 import eu.etaxonomy.cdm.api.service.INameService;
24 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
25 import eu.etaxonomy.cdm.api.service.IReferenceService;
26 import eu.etaxonomy.cdm.api.service.IService;
27 import eu.etaxonomy.cdm.api.service.ITaxonService;
28 import eu.etaxonomy.cdm.api.service.IUserService;
29 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
30 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
31 import eu.etaxonomy.cdm.model.agent.AgentBase;
32 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
33 import eu.etaxonomy.cdm.model.common.Group;
34 import eu.etaxonomy.cdm.model.common.ICdmBase;
35 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
36 import eu.etaxonomy.cdm.model.common.RelationshipBase;
37 import eu.etaxonomy.cdm.model.common.User;
38 import eu.etaxonomy.cdm.model.name.NameRelationship;
39 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
40 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
41 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
42 import eu.etaxonomy.cdm.model.reference.Reference;
43 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
44 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
45 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
46 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
47
48 /**
49 * @author n.hoffmann
50 * @created Dec 8, 2010
51 * @version 1.0
52 */
53 public class SearchManager {
54
55 public static final List NO_RESULTS = Arrays.asList(new Object[]{});
56
57 public static final String WILDCARD = "*";
58
59 public static int NO_COUNT = -1;
60
61 // TODO make this configurable via preferences
62 private static final int MAX_RESULTS_BEFORE_WARNING = 500;
63
64 public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
65 if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
66 List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
67 addUuidSearchResults(records, configurator, INameService.class);
68 return records;
69 }
70 return NO_RESULTS;
71 }
72
73 public List<NameRelationship> findNameRelationships(
74 IIdentifiableEntityServiceConfigurator configurator) {
75 if(true){
76 //if activated again remember to add uuid search results like in other searches
77 return NO_RESULTS;
78 }
79
80 List<NameRelationship> relationships = new ArrayList<NameRelationship>();
81 List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
82
83 for (RelationshipBase relationship : all){
84 if(relationship instanceof NameRelationship){
85 relationships.add((NameRelationship) relationship);
86 }
87 }
88 return relationships;
89 }
90
91 public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
92 return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
93 }
94
95 public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
96 if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
97 List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
98 addUuidSearchResults(records, configurator, IReferenceService.class);
99 return records;
100 }
101 return NO_RESULTS;
102 }
103
104 public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
105 if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
106 List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
107 addUuidSearchResults(records, configurator, IAgentService.class);
108 return records;
109 }
110 return NO_RESULTS;
111 }
112
113 /**
114 * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
115 * @param records the list to which the search results are added
116 * @param configurator the configurator holding the search string
117 * @param service the service to use for searching
118 */
119 private <T extends ICdmBase> void addUuidSearchResults(List<T> records, IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
120 String titleSearchString = configurator.getTitleSearchString();
121 try {
122 UUID uuid = UUID.fromString(titleSearchString);
123 T foundRecord = CdmStore.getService(service).find(uuid);
124 if(foundRecord!=null){
125 records.add(foundRecord);
126 }
127 } catch (IllegalArgumentException e) {
128 //search string was no UUID
129 }
130 }
131
132 @SuppressWarnings("unchecked")
133 public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
134 configurator.setClazz(TeamOrPersonBase.class);
135 return (List)findAgents(configurator);
136 }
137
138 /**
139 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
140 * {@link IIdentifiableEntityServiceConfigurator}<br>
141 * <br>
142 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
143 *
144 * @param configurator the configurator to use for the search
145 * @return a list of the SpecimenOrObservationBases matching the search parameters found
146 */
147 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
148 // by default we do not show field units. This may be configured via preferences
149 return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
150 }
151
152
153 /**
154 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
155 * {@link IIdentifiableEntityServiceConfigurator}
156 *
157 * @param configurator the configurator to use for the search
158 * @return a list of the SpecimenOrObservationBases found
159 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
160 * @return
161 */
162 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
163 List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
164 final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
165 "collection",
166 "descriptions",
167 "identifiers",
168 "derivationEvents.originals",
169 "derivedFrom.originals",
170 "gatheringEvent.country.representations",
171 "gatheringEvent.collector",
172 "gatheringEvent.locality",
173 "descriptions.descriptionElements",
174 "kindOfUnit",
175 "amplificationResults",
176 "sequences.singleReadAlignments",
177 "mediaSpecimen"
178 });
179
180 if(configurator.getClazz()==null){
181 if(showFieldUnits){
182 configurator.setClazz(SpecimenOrObservationBase.class);
183 }
184 else{
185 configurator.setClazz(DerivedUnit.class);
186 }
187 }
188 List<String> occurrencePropertyPaths = new ArrayList<String>();
189 occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
190 for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
191 occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
192 }
193 configurator.setPropertyPaths(occurrencePropertyPaths);
194
195 if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
196 records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
197 }
198 addUuidSearchResults(records, configurator, IOccurrenceService.class);
199 return records;
200 }
201
202 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
203 String userNameSearchString = sqlizeTitleSearchString(configurator);
204 // TODO why are users not identifiable entities?
205 List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
206 addUuidSearchResults(records, configurator, IUserService.class);
207 return records;
208 }
209
210
211 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
212 String groupNameSearchString = sqlizeTitleSearchString(configurator);
213 // TODO why are groups not identifiable entities?
214 List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
215 addUuidSearchResults(records, configurator, IGroupService.class);
216 return records;
217 }
218
219
220 private boolean checkLargeResult(int count) {
221 return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
222 }
223
224 private boolean checkLargeResult(int count, int maxBeforWarning) {
225 if(count > maxBeforWarning){
226 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
227 String.format("The current search will return %s objects. This will " +
228 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
229 }else{
230 return true;
231 }
232 }
233
234 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
235 return configurator.getTitleSearchString().replace(WILDCARD, "%");
236 }
237
238 public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
239 if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
240 List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
241 addUuidSearchResults(records, configurator, ITaxonService.class);
242 return records;
243 }
244 return NO_RESULTS;
245 }
246
247
248 }