1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.store;
|
11
|
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.Arrays;
|
14
|
import java.util.List;
|
15
|
|
16
|
import org.eclipse.jface.dialogs.MessageDialog;
|
17
|
import org.eclipse.swt.widgets.Display;
|
18
|
|
19
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
20
|
import eu.etaxonomy.cdm.api.service.IAgentService;
|
21
|
import eu.etaxonomy.cdm.api.service.IGroupService;
|
22
|
import eu.etaxonomy.cdm.api.service.IMediaService;
|
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.ITaxonService;
|
27
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
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.IdentifiableEntity;
|
34
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
35
|
import eu.etaxonomy.cdm.model.media.Media;
|
36
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
37
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
38
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
39
|
import eu.etaxonomy.cdm.model.permission.Group;
|
40
|
import eu.etaxonomy.cdm.model.permission.User;
|
41
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
42
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
43
|
import eu.etaxonomy.cdm.model.term.TermVocabulary;
|
44
|
import eu.etaxonomy.cdm.persistence.dto.UuidAndTitleCache;
|
45
|
import eu.etaxonomy.taxeditor.l10n.Messages;
|
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
|
*/
|
53
|
public class SearchManager {
|
54
|
|
55
|
public static final List NO_RESULTS = Arrays.asList(new Object[]{});
|
56
|
|
57
|
public static final String WILDCARD = "*"; //$NON-NLS-1$
|
58
|
|
59
|
public static int NO_COUNT = -1;
|
60
|
|
61
|
private static final int MAX_RESULTS_BEFORE_WARNING = 500;
|
62
|
|
63
|
public List<TaxonName> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
|
64
|
List<TaxonName> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
|
65
|
return records;
|
66
|
}
|
67
|
|
68
|
public List<NameRelationship> findNameRelationships(
|
69
|
IIdentifiableEntityServiceConfigurator configurator) {
|
70
|
if(true){
|
71
|
//if activated again remember to add uuid search results like in other searches
|
72
|
return NO_RESULTS;
|
73
|
}
|
74
|
|
75
|
List<NameRelationship> relationships = new ArrayList<>();
|
76
|
|
77
|
// TODO: the method getAllRelationships does not exist anymore.
|
78
|
// This method is not used anymore because there is no NameRelationship Bulkeditor
|
79
|
|
80
|
// List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
|
81
|
//
|
82
|
// for (RelationshipBase<?,?,?> relationship : all){
|
83
|
// if(relationship instanceof NameRelationship){
|
84
|
// relationships.add((NameRelationship) relationship);
|
85
|
// }
|
86
|
// }
|
87
|
return relationships;
|
88
|
}
|
89
|
|
90
|
public List<UuidAndTitleCache<? extends IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator configurator){
|
91
|
return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
|
92
|
}
|
93
|
|
94
|
|
95
|
@SuppressWarnings("unchecked")
|
96
|
public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
|
97
|
List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitleAndAbbrevTitle(configurator);
|
98
|
return records;
|
99
|
}
|
100
|
|
101
|
public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
|
102
|
List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitleAndAbbrevTitle(configurator);
|
103
|
//List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
|
104
|
return records;
|
105
|
}
|
106
|
|
107
|
@SuppressWarnings("unchecked")
|
108
|
public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
|
109
|
configurator.setClazz(TeamOrPersonBase.class);
|
110
|
return (List)findAgents(configurator);
|
111
|
}
|
112
|
|
113
|
/**
|
114
|
* Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
|
115
|
* {@link IIdentifiableEntityServiceConfigurator}<br>
|
116
|
* <br>
|
117
|
* Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
|
118
|
*
|
119
|
* @param configurator the configurator to use for the search
|
120
|
* @return a list of the SpecimenOrObservationBases matching the search parameters found
|
121
|
*/
|
122
|
public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator, boolean showCountWarning){
|
123
|
// by default we do not show field units. This may be configured via preferences
|
124
|
return findOccurrences(configurator, PreferencesUtil.getBooleanValue(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS), showCountWarning);
|
125
|
}
|
126
|
|
127
|
|
128
|
/**
|
129
|
* Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
|
130
|
* {@link IIdentifiableEntityServiceConfigurator}
|
131
|
*
|
132
|
* @param configurator the configurator to use for the search
|
133
|
* @return a list of the SpecimenOrObservationBases found
|
134
|
* @param showFieldUnits if <code>true</code> then also FieldUnits are searched
|
135
|
* @return
|
136
|
*/
|
137
|
public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits,
|
138
|
boolean showCountWarning){
|
139
|
List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
|
140
|
final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
|
141
|
"collection", //$NON-NLS-1$
|
142
|
"descriptions", //$NON-NLS-1$
|
143
|
"identifiers", //$NON-NLS-1$
|
144
|
"derivationEvents.originals", //$NON-NLS-1$
|
145
|
"derivedFrom.originals", //$NON-NLS-1$
|
146
|
"gatheringEvent.country.representations", //$NON-NLS-1$
|
147
|
"gatheringEvent.collector", //$NON-NLS-1$
|
148
|
"gatheringEvent.locality", //$NON-NLS-1$
|
149
|
"descriptions.descriptionElements", //$NON-NLS-1$
|
150
|
"kindOfUnit", //$NON-NLS-1$
|
151
|
"amplificationResults", //$NON-NLS-1$
|
152
|
"sequences.singleReadAlignments", //$NON-NLS-1$
|
153
|
"mediaSpecimen" //$NON-NLS-1$
|
154
|
});
|
155
|
|
156
|
List<String> occurrencePropertyPaths = new ArrayList<String>();
|
157
|
occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
|
158
|
for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
|
159
|
occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath); //$NON-NLS-1$
|
160
|
}
|
161
|
configurator.setPropertyPaths(occurrencePropertyPaths);
|
162
|
|
163
|
if(showCountWarning && checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
|
164
|
records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
|
165
|
}
|
166
|
else{
|
167
|
records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
|
168
|
}
|
169
|
return records;
|
170
|
}
|
171
|
|
172
|
public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
|
173
|
String userNameSearchString = sqlizeTitleSearchString(configurator);
|
174
|
// TODO why are users not identifiable entities?
|
175
|
List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString,
|
176
|
configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
|
177
|
configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
|
178
|
return records;
|
179
|
}
|
180
|
|
181
|
|
182
|
|
183
|
private boolean checkLargeResult(long count) {
|
184
|
return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
|
185
|
}
|
186
|
|
187
|
private boolean checkLargeResult(long count, int maxBeforWarning) {
|
188
|
if(count > maxBeforWarning){
|
189
|
return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), Messages.SearchManager_LARGE_RESULT_EXPECTED,
|
190
|
String.format(Messages.SearchManager_LONG_SEARCH_WARNING, count));
|
191
|
}else{
|
192
|
return true;
|
193
|
}
|
194
|
}
|
195
|
|
196
|
public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
|
197
|
String groupNameSearchString = sqlizeTitleSearchString(configurator);
|
198
|
// TODO why are groups not identifiable entities?
|
199
|
List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString,
|
200
|
configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
|
201
|
configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
|
202
|
return records;
|
203
|
}
|
204
|
|
205
|
|
206
|
private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
|
207
|
return configurator.getTitleSearchString().replace(WILDCARD, "%"); //$NON-NLS-1$
|
208
|
}
|
209
|
|
210
|
public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
|
211
|
List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
|
212
|
return records;
|
213
|
}
|
214
|
|
215
|
public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
|
216
|
List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
|
217
|
return records;
|
218
|
}
|
219
|
|
220
|
/**
|
221
|
* @param searchConfigurator
|
222
|
* @return
|
223
|
*/
|
224
|
public List<UuidAndTitleCache<NamedArea>> findNamedAreas(
|
225
|
IIdentifiableEntityServiceConfigurator searchConfigurator, List<TermVocabulary> vocs) {
|
226
|
List<UuidAndTitleCache<NamedArea>> entities = CdmStore.getService(ITermService.class).getUuidAndTitleCacheNamedArea(vocs, null, searchConfigurator.getTitleSearchString(), PreferencesUtil.getGlobalLanguage());
|
227
|
return entities;
|
228
|
}
|
229
|
|
230
|
}
|