2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.taxeditor
.store
;
12 import java
.util
.ArrayList
;
13 import java
.util
.LinkedHashMap
;
14 import java
.util
.List
;
15 import java
.util
.UUID
;
17 import org
.apache
.log4j
.Logger
;
18 import org
.eclipse
.core
.runtime
.Status
;
19 import org
.springframework
.security
.providers
.ProviderManager
;
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
;
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.
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.
60 public class CdmStore
{
61 private static final Logger logger
= Logger
.getLogger(CdmStore
.class);
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
;
67 private static CdmStore instance
;
69 private CdmApplicationController applicationController
;
71 private ConversationHolder globalReadOnlyConversation
;
73 private static DbSchemaValidation dbSchemaValidation
;
75 private static LoginManager loginManager
;
81 public static CdmStore
getDefault(){
82 return getDefault(DEFAULT_APPLICATION_CONTEXT
);
87 * @param applicationContextBean
90 public static CdmStore
getDefault(String applicationContextBean
){
92 logger
.info("Initializing application context ...");
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
);
102 // no datasource chosen.
103 logger
.warn("No datasource chosen. Exiting now.");
107 // Start default cdm database
108 instance
= initialize(applicationContextBean
);
115 private static CdmStore
initialize(String applicationContextBean
){
117 ICdmDataSource cdmDatasource
= CdmDataSourceRepository
.getDefault().getCurrentDataSource();
119 CdmStore instance
= new CdmStore(cdmDatasource
, getDbSchemaValidation(), applicationContextBean
);
121 logger
.info("Application context initialized.");
129 private static DbSchemaValidation
getDbSchemaValidation() {
130 return (dbSchemaValidation
== null) ? DEFAULT_DB_SCHEMA_VALIDATION
: dbSchemaValidation
;
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.
141 CdmStore
.DEFAULT_APPLICATION_CONTEXT
= applicationContextBean
;
142 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
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
);
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"));
160 * @deprecated use taxonomic tree instead
162 public List
<Taxon
> getRootTaxa() {
163 boolean onlyWithChildren
= false;
164 boolean withMisapplications
= true;
166 return applicationController
.getTaxonService().getRootTaxa(
167 getDefaultSec(), onlyWithChildren
, withMisapplications
);
173 public List
<TaxonomicTree
> getTaxonomicTrees() {
174 return applicationController
.getTaxonService().listTaxonomicTrees(null, null, null, null);
178 private CdmApplicationController
getApplicationControllerInternal(){
179 return applicationController
;
182 public static CdmApplicationController
getApplicationController(){
183 return getDefault().getApplicationControllerInternal();
188 * Create a new conversation and bind resources to it
192 public static ConversationHolder
NewConversation(){
194 CdmStore store
= getDefault();
195 CdmApplicationController controller
= store
.getApplicationController();
197 ConversationHolder conversation
= controller
.NewConversation();
203 * Creates a new conversation, binds resources to the conversation and
204 * start a transaction for this conversation.
208 public static ConversationHolder
NewTransactionalConversation() {
209 ConversationHolder conversation
= NewConversation();
211 conversation
.startTransaction();
216 private ConversationHolder
getGlobalReadOnlyConversation() {
217 ConversationHolder conversation
= globalReadOnlyConversation
== null
219 : globalReadOnlyConversation
;
224 public static ConversationHolder
getGlobalConversation(){
225 return getDefault().getGlobalReadOnlyConversation();
233 public static ITaxonService
getTaxonService(){ return getDefault().getApplicationControllerInternal().getTaxonService();}
235 public static INameService
getNameService(){ return getDefault().getApplicationControllerInternal().getNameService();}
237 public static IReferenceService
getReferenceService(){ return getDefault().getApplicationControllerInternal().getReferenceService();}
239 public static ILocationService
getLocationService(){ return getDefault().getApplicationControllerInternal().getLocationService();}
241 public static ProviderManager
getAuthenticationManager() { return getDefault().getApplicationControllerInternal().getAuthenticationManager();}
243 public static IUserService
getUserService() { return getDefault().getApplicationControllerInternal().getUserService(); }
245 public static ICommonService
getCommonService() { return getDefault().getApplicationControllerInternal().getCommonService(); }
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("*", "%")));
261 private List
<IdentifiableEntity
> findTaxaByName(String queryString
, boolean restrictToTaxonObjs
) {
263 ITaxonServiceConfigurator configurator
= new TaxonServiceConfiguratorImpl();
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);
272 configurator
.setDoNamesWithoutTaxa(true);
273 configurator
.setDoSynonyms(true);
275 configurator
.setSec(null);
276 configurator
.setPageNumber(0);
277 // TODO currently limit results to 1000
278 configurator
.setPageSize(1000);
280 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
287 private List
<IdentifiableEntity
> findTaxaByName(ITaxonServiceConfigurator configurator
){
289 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
294 public static List
<IdentifiableEntity
> findNames(ITaxonServiceConfigurator configurator
){
295 return getDefault().findTaxaByName(configurator
);
300 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
){
301 return getDefault().findTaxaByName(queryString
, false);
304 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
, boolean restrictToTaxonObjs
){
305 return getDefault().findTaxaByName(queryString
, restrictToTaxonObjs
);
309 * Searches for references by string. "%" is used as a wildcard.
314 public static List
<ReferenceBase
> getReferencesByTitle(String reference
) {
316 reference
= reference
.replace("*", "%");
317 List
<ReferenceBase
> resultsList
= null;
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.");
334 public static Language
getDefaultLanguage(){
335 return Language
.ENGLISH();
341 public static LinkedHashMap
<Class
<?
>, String
> getReferenceTypes() {
342 LinkedHashMap
<Class
<?
>, String
> nomReferenceTypeMap
= new LinkedHashMap
<Class
<?
>, String
>();
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");
350 return nomReferenceTypeMap
;
354 * incomplete! do note use!
356 public static LoginManager
getLoginManager(){
357 if(loginManager
== null){
358 loginManager
= new LoginManager();