2b05d45835409f5b3ea0a93b5ebcbc90a2ad3f06
[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.UUID;
16
17 import org.apache.log4j.Logger;
18 import org.eclipse.core.runtime.Status;
19 import org.springframework.security.providers.ProviderManager;
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.IUserService;
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.model.common.IdentifiableEntity;
33 import eu.etaxonomy.cdm.model.common.Language;
34 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
35 import eu.etaxonomy.cdm.model.reference.Article;
36 import eu.etaxonomy.cdm.model.reference.Book;
37 import eu.etaxonomy.cdm.model.reference.BookSection;
38 import eu.etaxonomy.cdm.model.reference.Generic;
39 import eu.etaxonomy.cdm.model.reference.ReferenceBase;
40 import eu.etaxonomy.cdm.model.taxon.Taxon;
41 import eu.etaxonomy.cdm.model.taxon.TaxonomicTree;
42 import eu.etaxonomy.cdm.persistence.query.MatchMode;
43 import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
44 import eu.etaxonomy.taxeditor.datasource.DatasourceLauncherDialog;
45 import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
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 private static LoginManager loginManager;
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
93 // Prompt user for datasource if there are more than one
94 if(! PreferencesUtil.getPreferenceStore().getBoolean(PreferencesUtil.HIDE_DATASOURCE_CHOOSER)
95 && CdmDataSourceRepository.getDefault().getAll().size() > 1){
96 DatasourceLauncherDialog chooseDataSource = new DatasourceLauncherDialog(StoreUtil.getShell());
97 if(chooseDataSource.open() == Status.OK){
98 instance = initialize(applicationContextBean);
99
100 }else{
101 // no datasource chosen.
102 logger.warn("No datasource chosen. Exiting now.");
103 System.exit(1);
104 }
105 }else{
106 // Start default cdm database
107 instance = initialize(applicationContextBean);
108 }
109
110 }
111 return instance;
112 }
113
114 private static CdmStore initialize(String applicationContextBean){
115
116 ICdmDataSource cdmDatasource = CdmDataSourceRepository.getDefault().getCurrentDataSource();
117
118 CdmStore instance = new CdmStore(cdmDatasource, getDbSchemaValidation(), applicationContextBean);
119
120 logger.info("Application context initialized.");
121
122 return instance;
123 }
124
125 /**
126 * @return
127 */
128 private static DbSchemaValidation getDbSchemaValidation() {
129 return (dbSchemaValidation == null) ? DEFAULT_DB_SCHEMA_VALIDATION : dbSchemaValidation;
130 }
131
132 /**
133 *
134 */
135 private CdmStore(ICdmDataSource dataSource, DbSchemaValidation dbSchemaValidation, String applicationContextBean) {
136 // TODO application context bean is not honored by application controller at the moment.
137 // cdmDefaultApplicationController bean gets loaded per default always.
138
139 try {
140 CdmStore.DEFAULT_APPLICATION_CONTEXT = applicationContextBean;
141 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
142
143 // logger.warn("OMITTING TERM LOADING FOR DEBUGGING");
144 // applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation, true);
145 applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation);
146 } catch (Exception e) {
147 throw new RuntimeException(e);
148 }
149 }
150
151
152 public ReferenceBase<?> getDefaultSec() {
153 // TODO why is this returning null? and of course, replace w the real deal
154 return applicationController.getReferenceService().getReferenceByUuid(
155 UUID.fromString("f3593c18-a8d2-4e51-bdad-0befbf8fb2d1"));
156 }
157
158 /**
159 * @deprecated use taxonomic tree instead
160 */
161 public List<Taxon> getRootTaxa() {
162 boolean onlyWithChildren = false;
163 boolean withMisapplications = true;
164
165 return applicationController.getTaxonService().getRootTaxa(
166 getDefaultSec(), onlyWithChildren, withMisapplications);
167 }
168
169 /**
170 * @return
171 */
172 public List<TaxonomicTree> getTaxonomicTrees() {
173 return applicationController.getTaxonService().listTaxonomicTrees(null, null, null, null);
174 }
175
176
177 private CdmApplicationController getApplicationControllerInternal(){
178 return applicationController;
179 }
180
181 public static CdmApplicationController getApplicationController(){
182 return getDefault().getApplicationControllerInternal();
183 }
184
185
186 /**
187 * Create a new conversation and bind resources to it
188 *
189 * @return
190 */
191 public static ConversationHolder NewConversation(){
192
193 CdmStore store = getDefault();
194 CdmApplicationController controller = store.getApplicationController();
195
196 ConversationHolder conversation = controller.NewConversation();
197
198 return conversation;
199 }
200
201 /**
202 * Creates a new conversation, binds resources to the conversation and
203 * start a transaction for this conversation.
204 *
205 * @return
206 */
207 public static ConversationHolder NewTransactionalConversation() {
208 ConversationHolder conversation = NewConversation();
209
210 conversation.startTransaction();
211 return conversation;
212 }
213
214
215 private ConversationHolder getGlobalReadOnlyConversation() {
216 ConversationHolder conversation = globalReadOnlyConversation == null
217 ? NewConversation()
218 : globalReadOnlyConversation;
219 conversation.bind();
220 return conversation;
221 }
222
223 public static ConversationHolder getGlobalConversation(){
224 return getDefault().getGlobalReadOnlyConversation();
225 }
226
227
228 /*
229 * EXPOSING SERVICES
230 */
231
232 public static ITaxonService getTaxonService(){ return getDefault().getApplicationControllerInternal().getTaxonService();}
233
234 public static INameService getNameService(){ return getDefault().getApplicationControllerInternal().getNameService();}
235
236 public static IReferenceService getReferenceService(){ return getDefault().getApplicationControllerInternal().getReferenceService();}
237
238 public static ILocationService getLocationService(){ return getDefault().getApplicationControllerInternal().getLocationService();}
239
240 public static ProviderManager getAuthenticationManager() { return getDefault().getApplicationControllerInternal().getAuthenticationManager();}
241
242 public static IUserService getUserService() { return getDefault().getApplicationControllerInternal().getUserService(); }
243
244 /**
245 * @param searchText
246 * @return
247 */
248 @SuppressWarnings("unchecked")
249 public static List<TaxonNameBase> searchNameString(String searchText) {
250 List<TaxonNameBase> resultSet = new ArrayList<TaxonNameBase>();
251 resultSet.addAll(getNameService()
252 .getNamesByName(searchText.replace("*", "%")));
253 return resultSet;
254 }
255
256 @Deprecated
257 private List<IdentifiableEntity> findTaxaByName(String queryString, boolean restrictToTaxonObjs) {
258
259 ITaxonServiceConfigurator configurator = new TaxonServiceConfiguratorImpl();
260
261 configurator.setSearchString(queryString.trim());
262 configurator.setDoTaxa(true);
263 configurator.setMatchMode(MatchMode.BEGINNING);
264 if (restrictToTaxonObjs) {
265 configurator.setDoNamesWithoutTaxa(false);
266 configurator.setDoSynonyms(false);
267 } else {
268 configurator.setDoNamesWithoutTaxa(true);
269 configurator.setDoSynonyms(true);
270 }
271 configurator.setSec(null);
272 configurator.setPageNumber(0);
273 // TODO currently limit results to 1000
274 configurator.setPageSize(1000);
275
276 List<IdentifiableEntity> result = getTaxonService().findTaxaAndNames(configurator).getRecords();
277
278 return result;
279 }
280
281
282
283 private List<IdentifiableEntity> findTaxaByName(ITaxonServiceConfigurator configurator){
284
285 List<IdentifiableEntity> result = getTaxonService().findTaxaAndNames(configurator).getRecords();
286
287 return result;
288 }
289
290 public static List<IdentifiableEntity> findNames(ITaxonServiceConfigurator configurator){
291 return getDefault().findTaxaByName(configurator);
292 }
293
294
295
296 public static List<IdentifiableEntity> searchTaxaByName(String queryString){
297 return getDefault().findTaxaByName(queryString, false);
298 }
299
300 public static List<IdentifiableEntity> searchTaxaByName(String queryString, boolean restrictToTaxonObjs){
301 return getDefault().findTaxaByName(queryString, restrictToTaxonObjs);
302 }
303
304 /**
305 * Searches for references by string. "%" is used as a wildcard.
306 *
307 * @param text
308 * @return
309 */
310 public static List<ReferenceBase> getReferencesByTitle(String reference) {
311
312 reference = reference.replace("*", "%");
313 List<ReferenceBase> resultsList = null;
314 try {
315 resultsList = getReferenceService().getReferencesByTitle(reference);
316 } catch (RuntimeException e) {
317 // MessageDialog.openError(GlobalController.getShell(), "Search reference error",
318 // "Reference search returned an error. This could be a Hibernate concurrency problem. " +
319 // "Please try saving your work, then searching again.");
320 e.printStackTrace();
321 }
322 return resultsList;
323 }
324
325 /**
326 * FIXME mock
327 *
328 * @return
329 */
330 public static Language getDefaultLanguage(){
331 return Language.ENGLISH();
332 }
333
334 /**
335 * @return
336 */
337 public static LinkedHashMap<Class<?>, String> getReferenceTypes() {
338 LinkedHashMap<Class<?>, String> nomReferenceTypeMap = new LinkedHashMap<Class<?>, String>();
339
340 // referenceTypeMap.put(BibtexReference.class, "BibTeX Reference");
341 nomReferenceTypeMap.put(Article.class, "Article");
342 nomReferenceTypeMap.put(Generic.class, "Generic");
343 nomReferenceTypeMap.put(Book.class, "Book");
344 nomReferenceTypeMap.put(BookSection.class, "Book Section");
345
346 return nomReferenceTypeMap;
347 }
348
349 /**
350 * incomplete! do note use!
351 */
352 public static LoginManager getLoginManager(){
353 if(loginManager == null){
354 loginManager = new LoginManager();
355 }
356 return loginManager;
357 }
358 }