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
.Authentication
;
20 import org
.springframework
.security
.context
.SecurityContextHolder
;
21 import org
.springframework
.security
.providers
.ProviderManager
;
22 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
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
;
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.
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.
62 public class CdmStore
{
63 private static final Logger logger
= Logger
.getLogger(CdmStore
.class);
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
;
69 private static CdmStore instance
;
71 private CdmApplicationController applicationController
;
73 private ConversationHolder globalReadOnlyConversation
;
75 private static DbSchemaValidation dbSchemaValidation
;
77 private static LoginManager loginManager
;
83 public static CdmStore
getDefault(){
84 return getDefault(DEFAULT_APPLICATION_CONTEXT
);
89 * @param applicationContextBean
92 public static CdmStore
getDefault(String applicationContextBean
){
94 logger
.info("Initializing application context ...");
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
);
103 // no datasource chosen.
104 logger
.warn("No datasource chosen. Exiting now.");
108 // Start default cdm database
109 instance
= initialize(applicationContextBean
);
116 private static CdmStore
initialize(String applicationContextBean
){
118 ICdmDataSource cdmDatasource
= CdmDataSourceRepository
.getDefault().getCurrentDataSource();
120 CdmStore instance
= new CdmStore(cdmDatasource
, getDbSchemaValidation(), applicationContextBean
);
122 logger
.info("Application context initialized.");
130 private static DbSchemaValidation
getDbSchemaValidation() {
131 return (dbSchemaValidation
== null) ? DEFAULT_DB_SCHEMA_VALIDATION
: dbSchemaValidation
;
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.
142 CdmStore
.DEFAULT_APPLICATION_CONTEXT
= applicationContextBean
;
143 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
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
);
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"));
161 * @deprecated use taxonomic tree instead
163 public List
<Taxon
> getRootTaxa() {
164 boolean onlyWithChildren
= false;
165 boolean withMisapplications
= true;
167 return applicationController
.getTaxonService().getRootTaxa(
168 getDefaultSec(), onlyWithChildren
, withMisapplications
);
174 public List
<TaxonomicTree
> getTaxonomicTrees() {
175 return applicationController
.getTaxonService().listTaxonomicTrees(null, null, null, null);
179 private CdmApplicationController
getApplicationControllerInternal(){
180 return applicationController
;
183 public static CdmApplicationController
getApplicationController(){
184 return getDefault().getApplicationControllerInternal();
189 * Create a new conversation and bind resources to it
193 public static ConversationHolder
NewConversation(){
195 CdmStore store
= getDefault();
196 CdmApplicationController controller
= store
.getApplicationController();
198 ConversationHolder conversation
= controller
.NewConversation();
204 * Creates a new conversation, binds resources to the conversation and
205 * start a transaction for this conversation.
209 public static ConversationHolder
NewTransactionalConversation() {
210 ConversationHolder conversation
= NewConversation();
212 conversation
.startTransaction();
217 private ConversationHolder
getGlobalReadOnlyConversation() {
218 ConversationHolder conversation
= globalReadOnlyConversation
== null
220 : globalReadOnlyConversation
;
225 public static ConversationHolder
getGlobalConversation(){
226 return getDefault().getGlobalReadOnlyConversation();
234 public static ITaxonService
getTaxonService(){ return getDefault().getApplicationControllerInternal().getTaxonService();}
236 public static INameService
getNameService(){ return getDefault().getApplicationControllerInternal().getNameService();}
238 public static IReferenceService
getReferenceService(){ return getDefault().getApplicationControllerInternal().getReferenceService();}
240 public static ILocationService
getLocationService(){ return getDefault().getApplicationControllerInternal().getLocationService();}
242 public static ProviderManager
getAuthenticationManager() { return getDefault().getApplicationControllerInternal().getAuthenticationManager();}
244 public static IUserService
getUserService() { return getDefault().getApplicationControllerInternal().getUserService(); }
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("*", "%")));
259 private List
<IdentifiableEntity
> findTaxaByName(String queryString
, boolean restrictToTaxonObjs
) {
261 ITaxonServiceConfigurator configurator
= new TaxonServiceConfiguratorImpl();
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);
270 configurator
.setDoNamesWithoutTaxa(true);
271 configurator
.setDoSynonyms(true);
273 configurator
.setSec(null);
274 configurator
.setPageNumber(0);
275 // TODO currently limit results to 1000
276 configurator
.setPageSize(1000);
278 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
285 private List
<IdentifiableEntity
> findTaxaByName(ITaxonServiceConfigurator configurator
){
287 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
292 public static List
<IdentifiableEntity
> findNames(ITaxonServiceConfigurator configurator
){
293 return getDefault().findTaxaByName(configurator
);
298 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
){
299 return getDefault().findTaxaByName(queryString
, false);
302 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
, boolean restrictToTaxonObjs
){
303 return getDefault().findTaxaByName(queryString
, restrictToTaxonObjs
);
307 * Searches for references by string. "%" is used as a wildcard.
312 public static List
<ReferenceBase
> getReferencesByTitle(String reference
) {
314 reference
= reference
.replace("*", "%");
315 List
<ReferenceBase
> resultsList
= null;
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.");
332 public static Language
getDefaultLanguage(){
333 return Language
.ENGLISH();
339 public static LinkedHashMap
<Class
<?
>, String
> getReferenceTypes() {
340 LinkedHashMap
<Class
<?
>, String
> nomReferenceTypeMap
= new LinkedHashMap
<Class
<?
>, String
>();
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");
348 return nomReferenceTypeMap
;
352 * incomplete! do note use!
354 public static LoginManager
getLoginManager(){
355 if(loginManager
== null){
356 loginManager
= new LoginManager();