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