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