moving UuidAndTitleCache from model to persistence (changes in Editor). #4799
[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.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.FieldUnit;
39 import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
40 import eu.etaxonomy.cdm.model.reference.Reference;
41 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
42 import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
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 @SuppressWarnings("unchecked")
142 public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
143 configurator.setClazz(TeamOrPersonBase.class);
144 return (List)findAgents(configurator);
145 }
146
147 /**
148 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
149 * {@link IIdentifiableEntityServiceConfigurator}<br>
150 * <br>
151 * Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
152 *
153 * @param configurator the configurator to use for the search
154 * @return a list of the SpecimenOrObservationBases matching the search parameters found
155 */
156 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
157 // by default we do not show field units. This may be configured via preferences
158 return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS));
159 }
160
161
162 /**
163 * Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
164 * {@link IIdentifiableEntityServiceConfigurator}
165 *
166 * @param configurator the configurator to use for the search
167 * @return a list of the SpecimenOrObservationBases found
168 * @param showFieldUnits if <code>true</code> then also FieldUnits are searched
169 * @return
170 */
171 public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits){
172 if(configurator.getClazz()==null){
173 if(showFieldUnits){
174 configurator.setClazz(SpecimenOrObservationBase.class);
175 }
176 else{
177 configurator.setClazz(DerivedUnit.class);
178 }
179 }
180 if(configurator.getClazz().equals(SpecimenOrObservationBase.class)){
181 //get FieldUnits + DerivedUnits
182 List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
183 configurator.setClazz(DerivedUnit.class);
184 int derivedUnitCount = CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator);
185 configurator.setClazz(FieldUnit.class);
186 int fieldUnitCount = CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator);
187
188 if(checkLargeResult(derivedUnitCount+fieldUnitCount)){
189 configurator.setClazz(DerivedUnit.class);
190 records.addAll(CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords());
191 configurator.setClazz(FieldUnit.class);
192 records.addAll(CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords());
193 }
194 return records;
195
196 }
197 if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
198 return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
199 }
200 return NO_RESULTS;
201 }
202
203 public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
204 String userNameSearchString = sqlizeTitleSearchString(configurator);
205 // TODO why are users not identifiable entities?
206 return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
207 }
208
209
210 public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
211 String groupNameSearchString = sqlizeTitleSearchString(configurator);
212 // TODO why are groups not identifiable entities?
213 return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
214 }
215
216
217 /**
218 * @param count
219 * @return
220 */
221 private boolean checkLargeResult(int count) {
222 if(count > MAX_RESULTS_BEFORE_WARNING){
223 return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
224 String.format("The current search will return %s objects. This will " +
225 "take a long time and/or might render the editor unusable. Please consider refining your search.", count));
226 }else{
227 return true;
228 }
229 }
230
231 /**
232 *
233 * @param configurator
234 * @return
235 */
236 private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
237 return configurator.getTitleSearchString().replace(WILDCARD, "%");
238 }
239
240 public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
241 if(checkLargeResult(CdmStore.getService(ITaxonService.class).countByTitle(configurator))){
242 return CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
243 }
244 return NO_RESULTS;
245 }
246
247
248 }