5aab63bc8e094599ba57b38e9a5f651e7c4686e7
[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
17 import org.eclipse.jface.dialogs.MessageDialog;
18 import org.eclipse.swt.widgets.Display;
19
20 import eu.etaxonomy.cdm.api.service.IAgentService;
21 import eu.etaxonomy.cdm.api.service.IGroupService;
22 import eu.etaxonomy.cdm.api.service.INameService;
23 import eu.etaxonomy.cdm.api.service.IOccurrenceService;
24 import eu.etaxonomy.cdm.api.service.IReferenceService;
25 import eu.etaxonomy.cdm.api.service.ITaxonService;
26 import eu.etaxonomy.cdm.api.service.IUserService;
27 import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
28 import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
29 import eu.etaxonomy.cdm.model.agent.AgentBase;
30 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
31 import eu.etaxonomy.cdm.model.common.Group;
32 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
33 import eu.etaxonomy.cdm.model.common.RelationshipBase;
34 import eu.etaxonomy.cdm.model.common.User;
35 import eu.etaxonomy.cdm.model.common.UuidAndTitleCache;
36 import eu.etaxonomy.cdm.model.name.NameRelationship;
37 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
38 import eu.etaxonomy.cdm.model.occurrence.DerivedUnit;
39 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationType;
41 import eu.etaxonomy.cdm.model.reference.Reference;
42 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
43 import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
44 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
45
46 /**
47 * @author n.hoffmann
48 * @created Dec 8, 2010
49 * @version 1.0
50 */
51 public class SearchManager {
52
53 public static final List NO_RESULTS = Arrays.asList(new Object[]{});
54
55 public static final String WILDCARD = "*";
56
57 public static int NO_COUNT = -1;
58
59 // TODO make this configurable via preferences
60 private static final int MAX_RESULTS_BEFORE_WARNING = 500;
61
62 /**
63 * <p>findNames</p>
64 *
65 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
66 * @return a {@link java.util.List} object.
67 */
68 public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
69 if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
70 return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
71 }
72 return NO_RESULTS;
73 }
74
75 public List<NameRelationship> findNameRelationships(
76 IIdentifiableEntityServiceConfigurator configurator) {
77 if(true){
78 return NO_RESULTS;
79 }
80
81 List<NameRelationship> relationships = new ArrayList<NameRelationship>();
82 List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
83
84 for (RelationshipBase relationship : all){
85 if(relationship instanceof NameRelationship){
86 relationships.add((NameRelationship) relationship);
87 }
88 }
89
90
91 return relationships;
92
93 }
94
95 /**
96 * <p>findTaxaAndNames</p>
97 *
98 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
99 * @return a {@link java.util.List} object.
100 */
101 public List<UuidAndTitleCache<IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
102 return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
103 }
104
105
106
107
108 /**
109 * <p>findReferences</p>
110 *
111 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
112 * @return a {@link java.util.List} object.
113 */
114 public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
115 if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
116 return CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
117 }
118 return NO_RESULTS;
119 }
120
121
122 /**
123 * <p>findAgents</p>
124 *
125 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
126 * @return a {@link java.util.List} object.
127 */
128 public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
129 if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
130 return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
131 }
132 return NO_RESULTS;
133 }
134
135 /**
136 * <p>findTeamOrPersons</p>
137 *
138 * @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
139 * @return a {@link java.util.List} object.
140 */
141 public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
142 configurator.setClazz(TeamOrPersonBase.class);
143 return findAgents(configurator);
144 }
145
146 /**
147 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
148 * {@link IIdentifiableEntityServiceConfigurator}<br>
149 * <br>
150 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
151 *
152 * @param configurator the configurator to use for the search
153 * @return a list of the SpecimenOrObservationBases matching the search parameters found
154 */
155 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
156 // by default we do not show field units. This may be configured via preferences
157 return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
158 }
159
160
161 /**
162 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
163 * {@link IIdentifiableEntityServiceConfigurator}
164 *
165 * @param configurator the configurator to use for the search
166 * @return a list of the SpecimenOrObservationBases found
167 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
168 * @return
169 */
170 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator, boolean showFieldUnits){
171 if(! showFieldUnits){
172 configurator.setClazz(DerivedUnit.class);
173 }
174 if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
175 return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
176 }
177 return NO_RESULTS;
178 }
179
180 /**
181 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the {@link IIdentifiableEntityServiceConfigurator}
182 * which match the given {@link SpecimenOrObservationType}
183 * @param configurator the configurator to use for the search
184 * @param type the type/record basis the specimens must have
185 * @return a list of the SpecimenOrObservationBases matching the search parameters found
186
187 * @deprecated method needs to be optimized with a direct SQL query instead of iterating over all specimens
188 */
189 @Deprecated
190 //TODO: method needs to be optimized with a direct SQL query instead of iterating over all specimens
191 public List<SpecimenOrObservationBase> findOccurrencesByType(IIdentifiableEntityServiceConfigurator configurator, SpecimenOrObservationType type){
192 List<SpecimenOrObservationBase> filteredOccurrences = new ArrayList<SpecimenOrObservationBase>();
193 List<SpecimenOrObservationBase> occurrences = findOccurrences(configurator, true);
194 if(type!=null){
195 for(SpecimenOrObservationBase occurrence:occurrences){
196 if(occurrence.getRecordBasis().equals(type)
197 || occurrence.getRecordBasis().isKindOf(type)
198 || type == SpecimenOrObservationType.Unknown){
199 filteredOccurrences.add(occurrence);
200 }
201 }
202 }
203 else{
204 filteredOccurrences = occurrences;
205 }
206 return filteredOccurrences;
207 }
208
209 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
210 String userNameSearchString = sqlizeTitleSearchString(configurator);
211 // TODO why are users not identifiable entities?
212 return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
213 }
214
215
216 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
217 String groupNameSearchString = sqlizeTitleSearchString(configurator);
218 // TODO why are groups not identifiable entities?
219 return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
220 }
221
222
223 /**
224 * @param count
225 * @return
226 */
227 private boolean checkLargeResult(int count) {
228 if(count > MAX_RESULTS_BEFORE_WARNING){
229 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
230 String.format("The current search will return %s objects. This will " +
231 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
232 }else{
233 return true;
234 }
235 }
236
237 /**
238 *
239 * @param configurator
240 * @return
241 */
242 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
243 return configurator.getTitleSearchString().replace(WILDCARD, "%");
244 }
245
246 public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
247 if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
248 return CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
249 }
250 return NO_RESULTS;
251 }
252
253
254 }