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