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