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.List;
|
14
|
import java.util.Set;
|
15
|
import java.util.SortedSet;
|
16
|
import java.util.UUID;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
|
20
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
21
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
22
|
import eu.etaxonomy.cdm.api.service.ILocationService;
|
23
|
import eu.etaxonomy.cdm.api.service.INameService;
|
24
|
import eu.etaxonomy.cdm.api.service.IReferenceService;
|
25
|
import eu.etaxonomy.cdm.api.service.ITaxonService;
|
26
|
import eu.etaxonomy.cdm.api.service.ITermService;
|
27
|
import eu.etaxonomy.cdm.api.service.config.ITaxonServiceConfigurator;
|
28
|
import eu.etaxonomy.cdm.api.service.config.impl.TaxonServiceConfiguratorImpl;
|
29
|
import eu.etaxonomy.cdm.database.DataSourceNotFoundException;
|
30
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
31
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
32
|
import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
|
33
|
import eu.etaxonomy.cdm.model.common.Language;
|
34
|
import eu.etaxonomy.cdm.model.common.OrderedTermVocabulary;
|
35
|
import eu.etaxonomy.cdm.model.common.TermVocabulary;
|
36
|
import eu.etaxonomy.cdm.model.common.init.TermNotFoundException;
|
37
|
import eu.etaxonomy.cdm.model.description.Feature;
|
38
|
import eu.etaxonomy.cdm.model.name.NameRelationshipType;
|
39
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
40
|
import eu.etaxonomy.cdm.model.name.Rank;
|
41
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
42
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
43
|
import eu.etaxonomy.cdm.model.reference.ReferenceBase;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
|
46
|
import eu.etaxonomy.cdm.persistence.query.MatchMode;
|
47
|
import eu.etaxonomy.taxeditor.store.datasource.CdmDataSourceRepository;
|
48
|
|
49
|
/**
|
50
|
* This implementation of ICdmDataRepository depends on hibernate sessions to store the data correctly
|
51
|
* for the current session. No state is held in this class.
|
52
|
*
|
53
|
* Only methods that either get or manipulate data are exposed here. So this class acts as a facade
|
54
|
* for the methods in cdmlib-service.
|
55
|
*
|
56
|
*
|
57
|
* @author n.hoffmann
|
58
|
* @created 17.03.2009
|
59
|
* @version 1.0
|
60
|
*/
|
61
|
public class CdmStore{
|
62
|
private static final Logger logger = Logger.getLogger(CdmStore.class);
|
63
|
|
64
|
// FIXME change this to ClassPathResources as soon as it is included into the plugin
|
65
|
private static String DEFAULT_APPLICATION_CONTEXT = null;
|
66
|
private static DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.UPDATE;
|
67
|
|
68
|
private static CdmStore instance;
|
69
|
|
70
|
private CdmApplicationController applicationController;
|
71
|
|
72
|
private ConversationHolder globalReadOnlyConversation;
|
73
|
|
74
|
private static DbSchemaValidation dbSchemaValidation;
|
75
|
|
76
|
/**
|
77
|
*
|
78
|
* @return
|
79
|
*/
|
80
|
public static CdmStore getDefault(){
|
81
|
return getDefault(DEFAULT_APPLICATION_CONTEXT);
|
82
|
}
|
83
|
|
84
|
/**
|
85
|
*
|
86
|
* @param applicationContextBean
|
87
|
* @return
|
88
|
*/
|
89
|
public static CdmStore getDefault(String applicationContextBean){
|
90
|
if(instance == null){
|
91
|
logger.info("Initializing application context ...");
|
92
|
ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().
|
93
|
getCurrentDataSource();
|
94
|
|
95
|
instance = new CdmStore(cdmDatasource, getDbSchemaValidation(), applicationContextBean);
|
96
|
|
97
|
logger.info("Application context initialized.");
|
98
|
}
|
99
|
return instance;
|
100
|
}
|
101
|
|
102
|
/**
|
103
|
* @return
|
104
|
*/
|
105
|
private static DbSchemaValidation getDbSchemaValidation() {
|
106
|
return (dbSchemaValidation == null) ? DEFAULT_DB_SCHEMA_VALIDATION : dbSchemaValidation;
|
107
|
}
|
108
|
|
109
|
/**
|
110
|
*
|
111
|
*/
|
112
|
private CdmStore(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, String applicationContextBean) {
|
113
|
// TODO application context bean is not honored by application controller at the moment.
|
114
|
// cdmDefaultApplicationController bean gets loaded per default always.
|
115
|
|
116
|
try {
|
117
|
CdmStore.DEFAULT_APPLICATION_CONTEXT = applicationContextBean;
|
118
|
// applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
|
119
|
|
120
|
// logger.warn("OMITTING TERM LOADING FOR DEBUGGING");
|
121
|
// applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation, true);
|
122
|
applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation);
|
123
|
} catch (Exception e) {
|
124
|
throw new RuntimeException(e);
|
125
|
}
|
126
|
}
|
127
|
|
128
|
public Set<TaxonRelationshipType> getConceptRelationshipTypes() {
|
129
|
Set<TaxonRelationshipType> conceptRelationshipTypes = getTaxonRelationshipTypes().getTerms();
|
130
|
// remove these two relations as they are considered standard taxon relations
|
131
|
conceptRelationshipTypes.remove(TaxonRelationshipType.MISAPPLIED_NAME_FOR());
|
132
|
conceptRelationshipTypes.remove(TaxonRelationshipType.TAXONOMICALLY_INCLUDED_IN());
|
133
|
|
134
|
return conceptRelationshipTypes;
|
135
|
}
|
136
|
|
137
|
public ReferenceBase getDefaultSec() {
|
138
|
// TODO why is this returning null? and of course, replace w the real deal
|
139
|
return applicationController.getReferenceService().getReferenceByUuid(
|
140
|
UUID.fromString("f3593c18-a8d2-4e51-bdad-0befbf8fb2d1"));
|
141
|
}
|
142
|
|
143
|
private TermVocabulary<Feature> getFeaturesInternal() {
|
144
|
return applicationController.getDescriptionService().getDefaultFeatureVocabulary();
|
145
|
}
|
146
|
|
147
|
public static TermVocabulary<Feature> getFeatures() {
|
148
|
return getDefault().getFeaturesInternal();
|
149
|
}
|
150
|
|
151
|
public SortedSet<NameRelationshipType> getNameRelationshipTypes() {
|
152
|
// if (nameRelationshipTypes == null)
|
153
|
return applicationController.getNameService().getNameRelationshipTypeVocabulary()
|
154
|
.getTermsOrderedByLabels(Language.DEFAULT());
|
155
|
}
|
156
|
|
157
|
public TermVocabulary<NomenclaturalStatusType> getNomStatus() {
|
158
|
return applicationController.getNameService().getStatusTypeVocabulary();
|
159
|
}
|
160
|
|
161
|
private SortedSet<Rank> getRanksInternal() {
|
162
|
return applicationController.getNameService().getRankVocabulary().getOrderedTerms(null);
|
163
|
}
|
164
|
|
165
|
public static SortedSet<Rank> getRanks() {
|
166
|
return getDefault().getRanksInternal();
|
167
|
}
|
168
|
|
169
|
public List<Taxon> getRootTaxa() {
|
170
|
boolean onlyWithChildren = false;
|
171
|
boolean withMisapplications = true;
|
172
|
|
173
|
return applicationController.getTaxonService().getRootTaxa(
|
174
|
getDefaultSec(), onlyWithChildren, withMisapplications);
|
175
|
}
|
176
|
|
177
|
public OrderedTermVocabulary<TaxonRelationshipType> getTaxonRelationshipTypes() {
|
178
|
return applicationController.getTaxonService().getTaxonRelationshipTypeVocabulary();
|
179
|
}
|
180
|
|
181
|
public Set<Taxon> getTaxonomicChildren(Taxon parentTaxon) {
|
182
|
new Throwable("Not implemented.");
|
183
|
return null;
|
184
|
}
|
185
|
|
186
|
public TermVocabulary<SpecimenTypeDesignationStatus> getSpecimenTypeDesignationStatus() {
|
187
|
return applicationController.getNameService().getSpecimenTypeDesignationVocabulary();
|
188
|
}
|
189
|
|
190
|
public void removeAllTaxa() {
|
191
|
new Throwable("Not implemented.");
|
192
|
}
|
193
|
|
194
|
public void removeTaxon(Taxon taxon) {
|
195
|
new Throwable("Not implemented.");
|
196
|
}
|
197
|
|
198
|
public UUID saveTaxon(Taxon taxon) {
|
199
|
return applicationController.getTaxonService().save(taxon);
|
200
|
}
|
201
|
|
202
|
public void setApplicationController(
|
203
|
CdmApplicationController applicationController) {
|
204
|
this.applicationController = applicationController;
|
205
|
}
|
206
|
|
207
|
public CdmApplicationController getApplicationController(){
|
208
|
return applicationController;
|
209
|
}
|
210
|
|
211
|
public void setTaxonomicParent(Taxon taxon, Taxon newParentTaxon) {
|
212
|
new Throwable("Not implemented.");
|
213
|
}
|
214
|
|
215
|
/**
|
216
|
* Create a new conversation and bind resources to it
|
217
|
*
|
218
|
* @return
|
219
|
*/
|
220
|
public static ConversationHolder NewConversation(){
|
221
|
|
222
|
CdmStore store = getDefault();
|
223
|
CdmApplicationController controller = store.getApplicationController();
|
224
|
|
225
|
ConversationHolder conversation = controller.NewConversation();
|
226
|
|
227
|
return conversation;
|
228
|
}
|
229
|
|
230
|
/**
|
231
|
* Creates a new conversation, binds resources to the conversation and
|
232
|
* start a transaction for this conversation.
|
233
|
*
|
234
|
* @return
|
235
|
*/
|
236
|
public static ConversationHolder NewTransactionalConversation() {
|
237
|
ConversationHolder conversation = NewConversation();
|
238
|
|
239
|
conversation.startTransaction();
|
240
|
return conversation;
|
241
|
}
|
242
|
|
243
|
|
244
|
private ConversationHolder getGlobalReadOnlyConversation() {
|
245
|
ConversationHolder conversation = globalReadOnlyConversation == null
|
246
|
? NewConversation()
|
247
|
: globalReadOnlyConversation;
|
248
|
conversation.bind();
|
249
|
return conversation;
|
250
|
}
|
251
|
|
252
|
public static ConversationHolder getGlobalConversation(){
|
253
|
return getDefault().getGlobalReadOnlyConversation();
|
254
|
}
|
255
|
|
256
|
public static ITaxonService getTaxonService(){
|
257
|
return getDefault().getApplicationController().getTaxonService();
|
258
|
}
|
259
|
|
260
|
public static INameService getNameService(){
|
261
|
return getDefault().getApplicationController().getNameService();
|
262
|
}
|
263
|
|
264
|
public static IReferenceService getReferenceService(){
|
265
|
return getDefault().getApplicationController().getReferenceService();
|
266
|
}
|
267
|
|
268
|
public static ILocationService getLocationService(){
|
269
|
return getDefault().getApplicationController().getLocationService();
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* @param searchText
|
274
|
* @return
|
275
|
*/
|
276
|
@SuppressWarnings("unchecked")
|
277
|
public static List<TaxonNameBase> searchNameString(String searchText) {
|
278
|
List<TaxonNameBase> resultSet = new ArrayList<TaxonNameBase>();
|
279
|
resultSet.addAll(getNameService()
|
280
|
.getNamesByName(searchText.replace("*", "%")));
|
281
|
return resultSet;
|
282
|
}
|
283
|
|
284
|
private List findTaxaByName(String queryString, boolean restrictToTaxonObjs) {
|
285
|
|
286
|
ITaxonServiceConfigurator configurator = new TaxonServiceConfiguratorImpl();
|
287
|
|
288
|
configurator.setSearchString(queryString);
|
289
|
configurator.setDoTaxa(true);
|
290
|
configurator.setMatchMode(MatchMode.BEGINNING);
|
291
|
if (restrictToTaxonObjs) {
|
292
|
configurator.setDoNamesWithoutTaxa(false);
|
293
|
configurator.setDoSynonyms(false);
|
294
|
} else {
|
295
|
configurator.setDoNamesWithoutTaxa(true);
|
296
|
configurator.setDoSynonyms(true);
|
297
|
}
|
298
|
configurator.setReferenceBase(null);
|
299
|
configurator.setPageNumber(0);
|
300
|
// TODO currently limit results to 1000 for now
|
301
|
configurator.setPageSize(1000);
|
302
|
|
303
|
List<IdentifiableEntity> result = getTaxonService().findTaxaAndNames(configurator).getRecords();
|
304
|
|
305
|
return result;
|
306
|
}
|
307
|
|
308
|
public static List searchTaxaByName(String queryString){
|
309
|
return getDefault().findTaxaByName(queryString, false);
|
310
|
}
|
311
|
|
312
|
public static List searchTaxaByName(String queryString, boolean restrictToTaxonObjs){
|
313
|
return getDefault().findTaxaByName(queryString, restrictToTaxonObjs);
|
314
|
}
|
315
|
|
316
|
/**
|
317
|
* Searches for references by string. "%" is used as a wildcard.
|
318
|
*
|
319
|
* @param text
|
320
|
* @return
|
321
|
*/
|
322
|
public static List<ReferenceBase> getReferencesByTitle(String reference) {
|
323
|
|
324
|
reference = reference.replace("*", "%");
|
325
|
List<ReferenceBase> resultsList = null;
|
326
|
try {
|
327
|
resultsList = getReferenceService().getReferencesByTitle(reference);
|
328
|
} catch (RuntimeException e) {
|
329
|
// MessageDialog.openError(GlobalController.getShell(), "Search reference error",
|
330
|
// "Reference search returned an error. This could be a Hibernate concurrency problem. " +
|
331
|
// "Please try saving your work, then searching again.");
|
332
|
e.printStackTrace();
|
333
|
}
|
334
|
return resultsList;
|
335
|
}
|
336
|
|
337
|
|
338
|
public static List<Language> getLanguages() {
|
339
|
ITermService termService = getDefault().getApplicationController().getTermService();
|
340
|
ArrayList<Language> languages = new ArrayList<Language>();
|
341
|
for (Language language : termService.getLanguageVocabulary()) {
|
342
|
languages.add(language);
|
343
|
}
|
344
|
// languages.add(Language.ENGLISH());
|
345
|
// languages.add(Language.GERMAN());
|
346
|
return languages;
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* FIXME mock
|
351
|
*
|
352
|
* @return
|
353
|
*/
|
354
|
public static Language getDefaultLanguage(){
|
355
|
return Language.ENGLISH();
|
356
|
}
|
357
|
|
358
|
public static boolean changeDataSource(ICdmDataSource dataSource){
|
359
|
|
360
|
// some more things have to happen here
|
361
|
|
362
|
instance = new CdmStore(dataSource, getDbSchemaValidation(), getApplicationContextBean());
|
363
|
|
364
|
return true;
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* @return
|
369
|
*/
|
370
|
private static String getApplicationContextBean() {
|
371
|
return DEFAULT_APPLICATION_CONTEXT;
|
372
|
}
|
373
|
}
|