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.DerivedUnitBase;
|
38
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
39
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
40
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
41
|
import eu.etaxonomy.taxeditor.preference.IPreferenceKeys;
|
42
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
43
|
|
44
|
/**
|
45
|
* @author n.hoffmann
|
46
|
* @created Dec 8, 2010
|
47
|
* @version 1.0
|
48
|
*/
|
49
|
public class SearchManager {
|
50
|
|
51
|
public static final List NO_RESULTS = Arrays.asList(new Object[]{});
|
52
|
|
53
|
public static final String WILDCARD = "*";
|
54
|
|
55
|
public static int NO_COUNT = -1;
|
56
|
|
57
|
// TODO make this configurable via preferences
|
58
|
private static final int MAX_RESULTS_BEFORE_WARNING = 500;
|
59
|
|
60
|
/**
|
61
|
* <p>findNames</p>
|
62
|
*
|
63
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
|
64
|
* @return a {@link java.util.List} object.
|
65
|
*/
|
66
|
public List<TaxonNameBase> findNames(IIdentifiableEntityServiceConfigurator configurator){
|
67
|
if(checkLargeResult(CdmStore.getService(INameService.class).countByTitle(configurator))){
|
68
|
return CdmStore.getService(INameService.class).findByTitle(configurator).getRecords();
|
69
|
}
|
70
|
return NO_RESULTS;
|
71
|
}
|
72
|
|
73
|
public List<NameRelationship> findNameRelationships(
|
74
|
IIdentifiableEntityServiceConfigurator configurator) {
|
75
|
if(true){
|
76
|
return NO_RESULTS;
|
77
|
}
|
78
|
|
79
|
List<NameRelationship> relationships = new ArrayList<NameRelationship>();
|
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
|
|
88
|
|
89
|
return relationships;
|
90
|
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* <p>findTaxaAndNames</p>
|
95
|
*
|
96
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator} object.
|
97
|
* @return a {@link java.util.List} object.
|
98
|
*/
|
99
|
public List<UuidAndTitleCache<TaxonBase>> findTaxaAndNames(IFindTaxaAndNamesConfigurator<TaxonBase> configurator){
|
100
|
return CdmStore.getService(ITaxonService.class).findTaxaAndNamesForEditor(configurator);
|
101
|
}
|
102
|
|
103
|
|
104
|
/**
|
105
|
* <p>findReferences</p>
|
106
|
*
|
107
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
|
108
|
* @return a {@link java.util.List} object.
|
109
|
*/
|
110
|
public List<Reference> findReferences(IIdentifiableEntityServiceConfigurator configurator){
|
111
|
if(checkLargeResult(CdmStore.getService(IReferenceService.class).countByTitle(configurator))){
|
112
|
return CdmStore.getService(IReferenceService.class).findByTitle(configurator).getRecords();
|
113
|
}
|
114
|
return NO_RESULTS;
|
115
|
}
|
116
|
|
117
|
|
118
|
/**
|
119
|
* <p>findAgents</p>
|
120
|
*
|
121
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
|
122
|
* @return a {@link java.util.List} object.
|
123
|
*/
|
124
|
public List<AgentBase> findAgents(IIdentifiableEntityServiceConfigurator configurator){
|
125
|
if(checkLargeResult(CdmStore.getService(IAgentService.class).countByTitle(configurator))){
|
126
|
return CdmStore.getService(IAgentService.class).findByTitle(configurator).getRecords();
|
127
|
}
|
128
|
return NO_RESULTS;
|
129
|
}
|
130
|
|
131
|
/**
|
132
|
* <p>findTeamOrPersons</p>
|
133
|
*
|
134
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
|
135
|
* @return a {@link java.util.List} object.
|
136
|
*/
|
137
|
public List<AgentBase> findTeamOrPersons(IIdentifiableEntityServiceConfigurator configurator){
|
138
|
configurator.setClazz(TeamOrPersonBase.class);
|
139
|
return findAgents(configurator);
|
140
|
}
|
141
|
|
142
|
|
143
|
/**
|
144
|
* <p>findOccurrences</p>
|
145
|
*
|
146
|
* @param configurator a {@link eu.etaxonomy.cdm.api.service.config.IIdentifiableEntityServiceConfigurator} object.
|
147
|
* @return a {@link java.util.List} object.
|
148
|
*/
|
149
|
public List<SpecimenOrObservationBase> findOccurrences(IIdentifiableEntityServiceConfigurator configurator){
|
150
|
// by default we do not show field observations. This may be configured via preferences
|
151
|
boolean showFieldObservations = PreferencesUtil.getPreferenceStore().getBoolean(IPreferenceKeys.BULK_EDITOR_OCCURRENCE_SHOW_FIELD_OBSERVATIONS);
|
152
|
if(! showFieldObservations){
|
153
|
configurator.setClazz(DerivedUnitBase.class);
|
154
|
}
|
155
|
if(checkLargeResult(CdmStore.getService(IOccurrenceService.class).countByTitle(configurator))){
|
156
|
return CdmStore.getService(IOccurrenceService.class).findByTitle(configurator).getRecords();
|
157
|
}
|
158
|
return NO_RESULTS;
|
159
|
}
|
160
|
|
161
|
|
162
|
public List<User> findUsers(IIdentifiableEntityServiceConfigurator configurator){
|
163
|
String userNameSearchString = sqlizeTitleSearchString(configurator);
|
164
|
// TODO why are users not identifiable entities?
|
165
|
return CdmStore.getService(IUserService.class).listByUsername(userNameSearchString, null, null, null, null, null, null);
|
166
|
}
|
167
|
|
168
|
|
169
|
public List<Group> findGroups(IIdentifiableEntityServiceConfigurator configurator){
|
170
|
String groupNameSearchString = sqlizeTitleSearchString(configurator);
|
171
|
// TODO why are groups not identifiable entities?
|
172
|
return CdmStore.getService(IGroupService.class).listByName(groupNameSearchString, null, null, null, null, null, null);
|
173
|
}
|
174
|
|
175
|
|
176
|
/**
|
177
|
* @param count
|
178
|
* @return
|
179
|
*/
|
180
|
private boolean checkLargeResult(int count) {
|
181
|
if(count > MAX_RESULTS_BEFORE_WARNING){
|
182
|
return MessageDialog.openConfirm(Display.getDefault().getActiveShell(), "Large result expected",
|
183
|
String.format("The current search will return %s objects. This will " +
|
184
|
"take a long time and/or might render the editor unusable. Please consider refining your search.", count));
|
185
|
}else{
|
186
|
return true;
|
187
|
}
|
188
|
}
|
189
|
|
190
|
/**
|
191
|
*
|
192
|
* @param configurator
|
193
|
* @return
|
194
|
*/
|
195
|
private String sqlizeTitleSearchString(IIdentifiableEntityServiceConfigurator configurator){
|
196
|
return configurator.getTitleSearchString().replace(WILDCARD, "%");
|
197
|
}
|
198
|
|
199
|
|
200
|
}
|