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