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.IUserService;
|
28
|
import eu.etaxonomy.cdm.api.service.config.IFindTaxaAndNamesConfigurator;
|
29
|
import eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator;
|
30
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
31
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
32
|
import eu.etaxonomy.cdm.model.common.Group;
|
33
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
34
|
import eu.etaxonomy.cdm.model.common.RelationshipBase;
|
35
|
import eu.etaxonomy.cdm.model.common.User;
|
36
|
import eu.etaxonomy.cdm.model.media.Media;
|
37
|
import eu.etaxonomy.cdm.model.name.NameRelationship;
|
38
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
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.l10n.Messages;
|
44
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
45
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
46
|
|
47
|
/**
|
48
|
* @author n.hoffmann
|
49
|
* @created Dec 8, 2010
|
50
|
*/
|
51
|
public class SearchManager {
|
52
|
|
53
|
public static final List NO_RESULTS = Arrays.asList(new Object[]{});
|
54
|
|
55
|
public static final String WILDCARD = "*"; //$NON-NLS-1$
|
56
|
|
57
|
public static int NO_COUNT = -1;
|
58
|
|
59
|
private static final int MAX_RESULTS_BEFORE_WARNING = 500;
|
60
|
|
61
|
public List<TaxonName> findNames(IIdentifiableEntityServiceConfigurator configurator, ConversationHolder conversation){
|
62
|
List<TaxonName> records = CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
|
63
|
return records;
|
64
|
}
|
65
|
|
66
|
public List<NameRelationship> findNameRelationships(
|
67
|
IIdentifiableEntityServiceConfigurator configurator) {
|
68
|
if(true){
|
69
|
//if activated again remember to add uuid search results like in other searches
|
70
|
return NO_RESULTS;
|
71
|
}
|
72
|
|
73
|
List<NameRelationship> relationships = new ArrayList<>();
|
74
|
List<RelationshipBase> all = CdmStore.getService(INameService.class).getAllRelationships(0, 0);
|
75
|
|
76
|
for (RelationshipBase<?,?,?> relationship : all){
|
77
|
if(relationship instanceof NameRelationship){
|
78
|
relationships.add((NameRelationship) relationship);
|
79
|
}
|
80
|
}
|
81
|
return relationships;
|
82
|
}
|
83
|
|
84
|
public List<UuidAndTitleCache<? extends IdentifiableEntity>> findTaxaAndNames(IFindTaxaAndNamesConfigurator configurator){
|
85
|
return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
|
86
|
}
|
87
|
|
88
|
public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
|
89
|
List<Reference> records = CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
|
90
|
return records;
|
91
|
}
|
92
|
|
93
|
public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
|
94
|
List<AgentBase> records = CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
|
95
|
return records;
|
96
|
}
|
97
|
|
98
|
@SuppressWarnings("unchecked")
|
99
|
public List<TeamOrPersonBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
|
100
|
configurator.setClazz(TeamOrPersonBase.class);
|
101
|
return (List)findAgents(configurator);
|
102
|
}
|
103
|
|
104
|
/**
|
105
|
* Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
|
106
|
* {@link IIdentifiableEntityServiceConfigurator}<br>
|
107
|
* <br>
|
108
|
* Note: FieldUnits are omitted by default. See {@link #findOccurrences(IIdentifiableEntityServiceConfigurator, boolean)}
|
109
|
*
|
110
|
* @param configurator the configurator to use for the search
|
111
|
* @return a list of the SpecimenOrObservationBases matching the search parameters found
|
112
|
*/
|
113
|
public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator, boolean showCountWarning){
|
114
|
// by default we do not show field units. This may be configured via preferences
|
115
|
return findOccurrences(configurator, PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_UNITS), showCountWarning);
|
116
|
}
|
117
|
|
118
|
|
119
|
/**
|
120
|
* Searches for {@link SpecimenOrObservationBase} with the parameters specified in the
|
121
|
* {@link IIdentifiableEntityServiceConfigurator}
|
122
|
*
|
123
|
* @param configurator the configurator to use for the search
|
124
|
* @return a list of the SpecimenOrObservationBases found
|
125
|
* @param showFieldUnits if <code>true</code> then also FieldUnits are searched
|
126
|
* @return
|
127
|
*/
|
128
|
public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator<SpecimenOrObservationBase> configurator, boolean showFieldUnits,
|
129
|
boolean showCountWarning){
|
130
|
List<SpecimenOrObservationBase> records = new ArrayList<SpecimenOrObservationBase>();
|
131
|
final List<String> BASE_OCCURRENCE_INIT_STRATEGY = Arrays.asList(new String[] {
|
132
|
"collection", //$NON-NLS-1$
|
133
|
"descriptions", //$NON-NLS-1$
|
134
|
"identifiers", //$NON-NLS-1$
|
135
|
"derivationEvents.originals", //$NON-NLS-1$
|
136
|
"derivedFrom.originals", //$NON-NLS-1$
|
137
|
"gatheringEvent.country.representations", //$NON-NLS-1$
|
138
|
"gatheringEvent.collector", //$NON-NLS-1$
|
139
|
"gatheringEvent.locality", //$NON-NLS-1$
|
140
|
"descriptions.descriptionElements", //$NON-NLS-1$
|
141
|
"kindOfUnit", //$NON-NLS-1$
|
142
|
"amplificationResults", //$NON-NLS-1$
|
143
|
"sequences.singleReadAlignments", //$NON-NLS-1$
|
144
|
"mediaSpecimen" //$NON-NLS-1$
|
145
|
});
|
146
|
|
147
|
List<String> occurrencePropertyPaths = new ArrayList<String>();
|
148
|
occurrencePropertyPaths.addAll(BASE_OCCURRENCE_INIT_STRATEGY);
|
149
|
for(String propertyPath:BASE_OCCURRENCE_INIT_STRATEGY) {
|
150
|
occurrencePropertyPaths.add("derivationEvents.derivatives." + propertyPath); //$NON-NLS-1$
|
151
|
}
|
152
|
configurator.setPropertyPaths(occurrencePropertyPaths);
|
153
|
|
154
|
if(showCountWarning && checkLargeResult(CdmStore.getService(IOccurrenceService.class).countOccurrences(configurator))){
|
155
|
records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
|
156
|
}
|
157
|
else{
|
158
|
records = CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
|
159
|
}
|
160
|
return records;
|
161
|
}
|
162
|
|
163
|
public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
|
164
|
String userNameSearchString = sqlizeTitleSearchString(configurator);
|
165
|
// TODO why are users not identifiable entities?
|
166
|
List<User> records = CdmStore.getService(IUserService.class).listByUsername(userNameSearchString,
|
167
|
configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
|
168
|
configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
|
169
|
return records;
|
170
|
}
|
171
|
|
172
|
|
173
|
|
174
|
private boolean checkLargeResult(long count) {
|
175
|
return checkLargeResult(count, MAX_RESULTS_BEFORE_WARNING);
|
176
|
}
|
177
|
|
178
|
private boolean checkLargeResult(long count, int maxBeforWarning) {
|
179
|
if(count > maxBeforWarning){
|
180
|
return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), Messages.SearchManager_LARGE_RESULT_EXPECTED,
|
181
|
String.format(Messages.SearchManager_LONG_SEARCH_WARNING, count));
|
182
|
}else{
|
183
|
return true;
|
184
|
}
|
185
|
}
|
186
|
|
187
|
public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
|
188
|
String groupNameSearchString = sqlizeTitleSearchString(configurator);
|
189
|
// TODO why are groups not identifiable entities?
|
190
|
List<Group> records = CdmStore.getService(IGroupService.class).listByName(groupNameSearchString,
|
191
|
configurator.getMatchMode(), configurator.getCriteria(), configurator.getPageSize(),
|
192
|
configurator.getPageNumber(), configurator.getOrderHints(), configurator.getPropertyPaths());
|
193
|
return records;
|
194
|
}
|
195
|
|
196
|
|
197
|
private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
|
198
|
return configurator.getTitleSearchString().replace(WILDCARD, "%"); //$NON-NLS-1$
|
199
|
}
|
200
|
|
201
|
public List findTaxa(IIdentifiableEntityServiceConfigurator configurator) {
|
202
|
List<TaxonBase> records = CdmStore.getService(ITaxonService.class).findByTitle(configurator).getRecords();
|
203
|
return records;
|
204
|
}
|
205
|
|
206
|
public List findMedia(IIdentifiableEntityServiceConfigurator configurator) {
|
207
|
List<Media> records = CdmStore.getService(IMediaService.class).findByTitle(configurator).getRecords();
|
208
|
return records;
|
209
|
}
|
210
|
|
211
|
}
|