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 javax
.security
.auth
.spi
.LoginModule
;
19 import org
.apache
.log4j
.Logger
;
20 import org
.springframework
.security
.Authentication
;
21 import org
.springframework
.security
.context
.SecurityContextHolder
;
22 import org
.springframework
.security
.providers
.ProviderManager
;
23 import org
.springframework
.security
.providers
.UsernamePasswordAuthenticationToken
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
26 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
27 import eu
.etaxonomy
.cdm
.api
.service
.ILocationService
;
28 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ITaxonService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.config
.ITaxonServiceConfigurator
;
33 import eu
.etaxonomy
.cdm
.api
.service
.config
.impl
.TaxonServiceConfiguratorImpl
;
34 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
35 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
36 import eu
.etaxonomy
.cdm
.model
.common
.IdentifiableEntity
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
38 import eu
.etaxonomy
.cdm
.model
.common
.User
;
39 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
40 import eu
.etaxonomy
.cdm
.model
.reference
.Article
;
41 import eu
.etaxonomy
.cdm
.model
.reference
.Book
;
42 import eu
.etaxonomy
.cdm
.model
.reference
.BookSection
;
43 import eu
.etaxonomy
.cdm
.model
.reference
.Generic
;
44 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceBase
;
45 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
46 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonomicTree
;
47 import eu
.etaxonomy
.cdm
.persistence
.query
.MatchMode
;
48 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
49 import eu
.etaxonomy
.taxeditor
.model
.TaxeditorLoginModule
;
50 import eu
.etaxonomy
.taxeditor
.user
.AuthenticatedUserBar
;
53 * This implementation of ICdmDataRepository depends on hibernate sessions to store the data correctly
54 * for the current session. No state is held in this class.
56 * Only methods that either get or manipulate data are exposed here. So this class acts as a facade
57 * for the methods in cdmlib-service.
64 public class CdmStore
{
65 private static final Logger logger
= Logger
.getLogger(CdmStore
.class);
67 // FIXME change this to ClassPathResources as soon as it is included into the plugin
68 private static String DEFAULT_APPLICATION_CONTEXT
= null;
69 private static DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.UPDATE
;
71 private static CdmStore instance
;
73 private CdmApplicationController applicationController
;
75 private ConversationHolder globalReadOnlyConversation
;
77 private static DbSchemaValidation dbSchemaValidation
;
79 private static LoginModule loginModule
;
85 public static CdmStore
getDefault(){
86 return getDefault(DEFAULT_APPLICATION_CONTEXT
);
91 * @param applicationContextBean
94 public static CdmStore
getDefault(String applicationContextBean
){
96 logger
.info("Initializing application context ...");
97 ICdmDataSource cdmDatasource
= CdmDataSourceRepository
.getDefault().
98 getCurrentDataSource();
100 instance
= new CdmStore(cdmDatasource
, getDbSchemaValidation(), applicationContextBean
);
102 logger
.info("Application context initialized.");
110 private static DbSchemaValidation
getDbSchemaValidation() {
111 return (dbSchemaValidation
== null) ? DEFAULT_DB_SCHEMA_VALIDATION
: dbSchemaValidation
;
117 private CdmStore(ICdmDataSource dataSource
, DbSchemaValidation dbSchemaValidation
, String applicationContextBean
) {
118 // TODO application context bean is not honored by application controller at the moment.
119 // cdmDefaultApplicationController bean gets loaded per default always.
122 CdmStore
.DEFAULT_APPLICATION_CONTEXT
= applicationContextBean
;
123 // applicationController = CdmApplicationController.NewInstance(applicationContextBean, dataSource, dbSchemaValidation, false);
125 // logger.warn("OMITTING TERM LOADING FOR DEBUGGING");
126 // applicationController = CdmApplicationController.NewInstance(dataSource, dbSchemaValidation, true);
127 applicationController
= CdmApplicationController
.NewInstance(dataSource
, dbSchemaValidation
);
128 } catch (Exception e
) {
129 throw new RuntimeException(e
);
134 public ReferenceBase
<?
> getDefaultSec() {
135 // TODO why is this returning null? and of course, replace w the real deal
136 return applicationController
.getReferenceService().getReferenceByUuid(
137 UUID
.fromString("f3593c18-a8d2-4e51-bdad-0befbf8fb2d1"));
141 * @deprecated use taxonomic tree instead
143 public List
<Taxon
> getRootTaxa() {
144 boolean onlyWithChildren
= false;
145 boolean withMisapplications
= true;
147 return applicationController
.getTaxonService().getRootTaxa(
148 getDefaultSec(), onlyWithChildren
, withMisapplications
);
154 public List
<TaxonomicTree
> getTaxonomicTrees() {
155 return applicationController
.getTaxonService().listTaxonomicTrees(null, null, null, null);
159 private CdmApplicationController
getApplicationControllerInternal(){
160 return applicationController
;
163 public static CdmApplicationController
getApplicationController(){
164 return getDefault().getApplicationControllerInternal();
169 * Create a new conversation and bind resources to it
173 public static ConversationHolder
NewConversation(){
175 CdmStore store
= getDefault();
176 CdmApplicationController controller
= store
.getApplicationController();
178 ConversationHolder conversation
= controller
.NewConversation();
184 * Creates a new conversation, binds resources to the conversation and
185 * start a transaction for this conversation.
189 public static ConversationHolder
NewTransactionalConversation() {
190 ConversationHolder conversation
= NewConversation();
192 conversation
.startTransaction();
197 private ConversationHolder
getGlobalReadOnlyConversation() {
198 ConversationHolder conversation
= globalReadOnlyConversation
== null
200 : globalReadOnlyConversation
;
205 public static ConversationHolder
getGlobalConversation(){
206 return getDefault().getGlobalReadOnlyConversation();
214 public static ITaxonService
getTaxonService(){ return getDefault().getApplicationControllerInternal().getTaxonService();}
216 public static INameService
getNameService(){ return getDefault().getApplicationControllerInternal().getNameService();}
218 public static IReferenceService
getReferenceService(){ return getDefault().getApplicationControllerInternal().getReferenceService();}
220 public static ILocationService
getLocationService(){ return getDefault().getApplicationControllerInternal().getLocationService();}
222 public static ProviderManager
getAuthenticationManager() { return getDefault().getApplicationControllerInternal().getAuthenticationManager();}
224 public static IUserService
getUserService() { return getDefault().getApplicationControllerInternal().getUserService(); }
230 @SuppressWarnings("unchecked")
231 public static List
<TaxonNameBase
> searchNameString(String searchText
) {
232 List
<TaxonNameBase
> resultSet
= new ArrayList
<TaxonNameBase
>();
233 resultSet
.addAll(getNameService()
234 .getNamesByName(searchText
.replace("*", "%")));
238 private List
<IdentifiableEntity
> findTaxaByName(String queryString
, boolean restrictToTaxonObjs
) {
240 ITaxonServiceConfigurator configurator
= new TaxonServiceConfiguratorImpl();
242 configurator
.setSearchString(queryString
.trim());
243 configurator
.setDoTaxa(true);
244 configurator
.setMatchMode(MatchMode
.BEGINNING
);
245 if (restrictToTaxonObjs
) {
246 configurator
.setDoNamesWithoutTaxa(false);
247 configurator
.setDoSynonyms(false);
249 configurator
.setDoNamesWithoutTaxa(true);
250 configurator
.setDoSynonyms(true);
252 configurator
.setSec(null);
253 configurator
.setPageNumber(0);
254 // TODO currently limit results to 1000
255 configurator
.setPageSize(1000);
257 List
<IdentifiableEntity
> result
= getTaxonService().findTaxaAndNames(configurator
).getRecords();
262 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
){
263 return getDefault().findTaxaByName(queryString
, false);
266 public static List
<IdentifiableEntity
> searchTaxaByName(String queryString
, boolean restrictToTaxonObjs
){
267 return getDefault().findTaxaByName(queryString
, restrictToTaxonObjs
);
271 * Searches for references by string. "%" is used as a wildcard.
276 public static List
<ReferenceBase
> getReferencesByTitle(String reference
) {
278 reference
= reference
.replace("*", "%");
279 List
<ReferenceBase
> resultsList
= null;
281 resultsList
= getReferenceService().getReferencesByTitle(reference
);
282 } catch (RuntimeException e
) {
283 // MessageDialog.openError(GlobalController.getShell(), "Search reference error",
284 // "Reference search returned an error. This could be a Hibernate concurrency problem. " +
285 // "Please try saving your work, then searching again.");
296 public static Language
getDefaultLanguage(){
297 return Language
.ENGLISH();
303 public static LinkedHashMap
<Class
<?
>, String
> getReferenceTypes() {
304 LinkedHashMap
<Class
<?
>, String
> nomReferenceTypeMap
= new LinkedHashMap
<Class
<?
>, String
>();
306 // referenceTypeMap.put(BibtexReference.class, "BibTeX Reference");
307 nomReferenceTypeMap
.put(Article
.class, "Article");
308 nomReferenceTypeMap
.put(Generic
.class, "Generic");
309 nomReferenceTypeMap
.put(Book
.class, "Book");
310 nomReferenceTypeMap
.put(BookSection
.class, "Book Section");
312 return nomReferenceTypeMap
;
316 * TODO implement LoginModule to handle the whole login stuff
320 public static void authenticate(UsernamePasswordAuthenticationToken token
){
321 Authentication authentication
= CdmStore
.getAuthenticationManager().authenticate(token
);
322 SecurityContextHolder
.getContext().setAuthentication(authentication
);
324 // TODO register a listener on LoginModule instead of calling a set method here
325 // AuthenticatedUserBar.setAuthenticatedUser(getAuthenticatedUser().getUsername());
328 public static User
getAuthenticatedUser(){
329 Authentication authentication
= SecurityContextHolder
.getContext().getAuthentication();
331 if(authentication
!= null
332 && authentication
.getPrincipal() != null
333 && authentication
.getPrincipal() instanceof User
){
334 return (User
)authentication
.getPrincipal();
340 * incomplete! do note use!
342 public static LoginModule
getLoginModule(){
343 if(loginModule
== null){
344 loginModule
= new TaxeditorLoginModule();