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