missed to commit changes in search manager
[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.conversation.ConversationHolder;
22 import eu.etaxonomy.cdm.api.service.IAgentService;
23 import eu.etaxonomy.cdm.api.service.IGroupService;
24 import eu.etaxonomy.cdm.api.service.IMediaService;
25 import eu.etaxonomy.cdm.api.service.INameService;
26 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
27 import eu.etaxonomy.cdm.api.service.IReferenceService;
28 import eu.etaxonomy.cdm.api.service.IService;
29 import eu.etaxonomy.cdm.api.service.ITaxonService;
30 import eu.etaxonomy.cdm.api.service.IUserService;
31 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
32 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
33 import eu.etaxonomy.cdm.model.agent.AgentBase;
34 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
35 import eu.etaxonomy.cdm.model.common.Group;
36 import eu.etaxonomy.cdm.model.common.ICdmBase;
37 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
38 import eu.etaxonomy.cdm.model.common.RelationshipBase;
39 import eu.etaxonomy.cdm.model.common.User;
40 import eu.etaxonomy.cdm.model.media.Media;
41 import eu.etaxonomy.cdm.model.name.NameRelationship;
42 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
43 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
44 import eu.etaxonomy.cdm.model.reference.Reference;
45 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
46 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
47 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
48 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
49
50 /**
51 * @author n.hoffmann
52 * @created Dec 8, 2010
53 * @version 1.0
54 */
55 public class SearchManager {
56
57 public static final List NO_RESULTS = Arrays.asList(new Object[]{});
58
59 public static final String WILDCARD = "*";
60
61 public static int NO_COUNT = -1;
62
63
64 // TODO make this configurable via preferences
65 private static final int MAX_RESULTS_BEFORE_WARNING = 500;
66
67 public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
68
69 if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
70 List<TaxonNameBase> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
71 addUuidSearchResults(records, configurator, INameService.class);
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 addUuidSearchResults(records, configurator, IReferenceService.class);
103 return records;
104 }
105 return NO_RESULTS;
106 }
107
108 public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
109 if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
110 List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
111 addUuidSearchResults(records, configurator, IAgentService.class);
112 return records;
113 }
114 return NO_RESULTS;
115 }
116
117 /**
118 * Check search string if it is a {@link UUID} and, if <code>true</code>, search for the corresponding entity.
119 * @param records the list to which the search results are added
120 * @param configurator the configurator holding the search string
121 * @param service the service to use for searching
122 */
123 private <T extends ICdmBase> void addUuidSearchResults(List<T> records, IIdentifiableEntityServiceConfigurator configurator, Class<? extends IService<T>> service) {
124 String titleSearchString = configurator.getTitleSearchString();
125 try {
126 UUID uuid = UUID.fromString(titleSearchString);
127 T foundRecord = CdmStore.getService(service).load(uuid, configurator.getPropertyPaths());
128 if(foundRecord!=null){
129 records.add(foundRecord);
130 }
131 } catch (IllegalArgumentException e) {
132 //search string was no UUID
133 }
134 }
135
136 @SuppressWarnings("unchecked")
137 public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
138 configurator.setClazz(TeamOrPersonBase.class);
139 return (List)findAgents(configurator);
140 }
141
142 /**
143 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
144 * {@link IIdentifiableEntityServiceConfigurator}<br>
145 * <br>
146 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
147 *
148 * @param configurator the configurator to use for the search
149 * @return a list of the SpecimenOrObservationBases matching the search parameters found
150 */
151 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
152 // by default we do not show field units. This may be configured via preferences
153 return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
154 }
155
156
157 /**
158 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
159 * {@link IIdentifiableEntityServiceConfigurator}
160 *
161 * @param configurator the configurator to use for the search
162 * @return a list of the SpecimenOrObservationBases found
163 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
164 * @return
165 */
166 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
167 List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
168 final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
169 "collection",
170 "descriptions",
171 "identifiers",
172 "derivationEvents.originals",
173 "derivedFrom.originals",
174 "gatheringEvent.country.representations",
175 "gatheringEvent.collector",
176 "gatheringEvent.locality",
177 "descriptions.descriptionElements",
178 "kindOfUnit",
179 "amplificationResults",
180 "sequences.singleReadAlignments",
181 "mediaSpecimen"
182 });
183
184 List<String> occurrencePropertyPaths = new ArrayList<String>();
185 occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
186 for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
187 occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath);
188 }
189 configurator.setPropertyPaths(occurrencePropertyPaths);
190
191 if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
192 records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
193 }
194 addUuidSearchResults(records, configurator, IOccurrenceService.class);
195 return records;
196 }
197
198 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
199 String userNameSearchString = sqlizeTitleSearchString(configurator);
200 // TODO why are users not identifiable entities?
201 List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
202 addUuidSearchResults(records, configurator, IUserService.class);
203 return records;
204 }
205
206
207 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
208 String groupNameSearchString = sqlizeTitleSearchString(configurator);
209 // TODO why are groups not identifiable entities?
210 List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
211 addUuidSearchResults(records, configurator, IGroupService.class);
212 return records;
213 }
214
215
216 private boolean checkLargeResult(int count) {
217 return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
218 }
219
220 private boolean checkLargeResult(int count, int maxBeforWarning) {
221 if(count > maxBeforWarning){
222 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
223 String.format("The current search will return %s objects. This will " +
224 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
225 }else{
226 return true;
227 }
228 }
229
230 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
231 return configurator.getTitleSearchString().replace(WILDCARD, "%");
232 }
233
234 public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
235 if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
236 List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
237 addUuidSearchResults(records, configurator, ITaxonService.class);
238 return records;
239 }
240 return NO_RESULTS;
241 }
242
243 public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
244 if(checkLargeResult(CdmStore.getService(IMediaService.class).countByTitle(configurator))){
245 List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
246 addUuidSearchResults(records, configurator, IMediaService.class);
247 return records;
248 }
249 return NO_RESULTS;
250 }
251
252
253 }