4025e4197ad193603deba7e8dc1218506301b9f6
[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.FieldUnit;
42 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
43 import eu.etaxonomy.cdm.model.reference.Reference;
44 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
45 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
46 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
47 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
48
49 /**
50 * @author n.hoffmann
51 * @created Dec 8, 2010
52 * @version 1.0
53 */
54 public class SearchManager {
55
56 public static final List NO_RESULTS = Arrays.asList(new Object[]{});
57
58 public static final String WILDCARD = "*";
59
60 public static int NO_COUNT = -1;
61
62 // TODO make this configurable via preferences
63 private static final int MAX_RESULTS_BEFORE_WARNING = 500;
64
65 public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
66 if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
67 List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
68 TaxonNameBase uuidSearchResult = addUuidSearchResults(configurator, INameService.class);
69 if(uuidSearchResult!=null){
70 records.add(uuidSearchResult);
71 }
72 return records;
73 }
74 return NO_RESULTS;
75 }
76
77 public List<NameRelationship> findNameRelationships(
78 IIdentifiableEntityServiceConfigurator configurator) {
79 if(true){
80 //if activated again remember to add uuid search results like in other searches
81 return NO_RESULTS;
82 }
83
84 List<NameRelationship> relationships = new ArrayList<NameRelationship>();
85 List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
86
87 for (RelationshipBase relationship : all){
88 if(relationship instanceof NameRelationship){
89 relationships.add((NameRelationship) relationship);
90 }
91 }
92 return relationships;
93 }
94
95 public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
96 return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
97 }
98
99 public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
100 if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
101 List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
102 Reference uuidSearchResult = addUuidSearchResults(configurator, IReferenceService.class);
103 if(uuidSearchResult!=null){
104 records.add(uuidSearchResult);
105 }
106 return records;
107 }
108 return NO_RESULTS;
109 }
110
111 public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
112 if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
113 List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
114 AgentBase uuidSearchResult = addUuidSearchResults(configurator, IAgentService.class);
115 if(uuidSearchResult!=null){
116 records.add(uuidSearchResult);
117 }
118 return records;
119 }
120 return NO_RESULTS;
121 }
122
123 /**
124 * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
125 * @param configurator the configurator holding the search string
126 * @param service the service to use for searching
127 */
128 private <T extends ICdmBase> T addUuidSearchResults(IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
129 String titleSearchString = configurator.getTitleSearchString();
130 try {
131 UUID uuid = UUID.fromString(titleSearchString);
132 return CdmStore.getService(service).find(uuid);
133 } catch (IllegalArgumentException e) {
134 //search string was no UUID
135 }
136 return null;
137 }
138
139 @SuppressWarnings("unchecked")
140 public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
141 configurator.setClazz(TeamOrPersonBase.class);
142 return (List)findAgents(configurator);
143 }
144
145 /**
146 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
147 * {@link IIdentifiableEntityServiceConfigurator}<br>
148 * <br>
149 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
150 *
151 * @param configurator the configurator to use for the search
152 * @return a list of the SpecimenOrObservationBases matching the search parameters found
153 */
154 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
155 // by default we do not show field units. This may be configured via preferences
156 return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
157 }
158
159
160 /**
161 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
162 * {@link IIdentifiableEntityServiceConfigurator}
163 *
164 * @param configurator the configurator to use for the search
165 * @return a list of the SpecimenOrObservationBases found
166 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
167 * @return
168 */
169 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
170 List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
171 if(configurator.getClazz()==null){
172 if(showFieldUnits){
173 configurator.setClazz(SpecimenOrObservationBase.class);
174 }
175 else{
176 configurator.setClazz(DerivedUnit.class);
177 }
178 }
179 if(configurator.getClazz().equals(SpecimenOrObservationBase.class)){
180 //get FieldUnits + DerivedUnits
181 configurator.setClazz(DerivedUnit.class);
182 int derivedUnitCount = CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator);
183 configurator.setClazz(FieldUnit.class);
184 int fieldUnitCount = CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator);
185
186 if(checkLargeResult(derivedUnitCount+fieldUnitCount)){
187 configurator.setClazz(DerivedUnit.class);
188 records.addAll(CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords());
189 configurator.setClazz(FieldUnit.class);
190 records.addAll(CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords());
191 }
192
193 }
194 else if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
195 records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
196 }
197 SpecimenOrObservationBase uuidSearchResult = addUuidSearchResults(configurator, IOccurrenceService.class);
198 if(uuidSearchResult!=null){
199 records.add(uuidSearchResult);
200 }
201 return records;
202 }
203
204 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
205 String userNameSearchString = sqlizeTitleSearchString(configurator);
206 // TODO why are users not identifiable entities?
207 List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
208 User uuidSearchResult = addUuidSearchResults(configurator, IUserService.class);
209 if(uuidSearchResult!=null){
210 records.add(uuidSearchResult);
211 }
212 return records;
213 }
214
215
216 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
217 String groupNameSearchString = sqlizeTitleSearchString(configurator);
218 // TODO why are groups not identifiable entities?
219 List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
220 Group uuidSearchResult = addUuidSearchResults(configurator, IGroupService.class);
221 if(uuidSearchResult!=null){
222 records.add(uuidSearchResult);
223 }
224 return records;
225 }
226
227
228 private boolean checkLargeResult(int count) {
229 if(count > MAX_RESULTS_BEFORE_WARNING){
230 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
231 String.format("The current search will return %s objects. This will " +
232 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
233 }else{
234 return true;
235 }
236 }
237
238 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
239 return configurator.getTitleSearchString().replace(WILDCARD, "%");
240 }
241
242 public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
243 if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
244 List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
245 TaxonBase uuidSearchResult = addUuidSearchResults(configurator, ITaxonService.class);
246 if(uuidSearchResult!=null){
247 records.add(uuidSearchResult);
248 }
249 return records;
250 }
251 return NO_RESULTS;
252 }
253
254
255 }