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
.EnumSet
;
14 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
15 import org
.eclipse
.core
.runtime
.jobs
.Job
;
16 import org
.eclipse
.swt
.widgets
.Display
;
17 import org
.springframework
.core
.io
.ClassPathResource
;
18 import org
.springframework
.core
.io
.Resource
;
19 import org
.springframework
.security
.access
.PermissionEvaluator
;
20 import org
.springframework
.security
.authentication
.AuthenticationManager
;
21 import org
.springframework
.security
.core
.Authentication
;
22 import org
.springframework
.security
.core
.context
.SecurityContext
;
23 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
25 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationException
;
26 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
27 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
28 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
29 import eu
.etaxonomy
.cdm
.api
.cache
.CdmServiceCacher
;
30 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IAgentService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ICollectionService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
34 import eu
.etaxonomy
.cdm
.api
.service
.INameService
;
35 import eu
.etaxonomy
.cdm
.api
.service
.IOccurrenceService
;
36 import eu
.etaxonomy
.cdm
.api
.service
.IPolytomousKeyService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.IReferenceService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
39 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
40 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.IAmplificationService
;
41 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.IPrimerService
;
42 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
43 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
44 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
45 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
46 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
47 import eu
.etaxonomy
.cdm
.model
.common
.User
;
48 import eu
.etaxonomy
.cdm
.model
.description
.PolytomousKey
;
49 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
50 import eu
.etaxonomy
.cdm
.model
.molecular
.Primer
;
51 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
52 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
53 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
54 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
55 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
56 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
57 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
58 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
59 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
60 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
61 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
62 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
63 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
64 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
65 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
66 import eu
.etaxonomy
.taxeditor
.session
.mock
.MockCdmEntitySessionManager
;
67 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
68 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
69 import eu
.etaxonomy
.taxeditor
.util
.ProgressMonitorClientManager
;
70 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
71 import eu
.etaxonomy
.taxeditor
.webapp
.CDMServer
;
74 * This implementation of ICdmDataRepository depends on hibernate sessions to
75 * store the data correctly for the current session. No state is held in this
78 * Only methods that either get or manipulate data are exposed here. So this
79 * class acts as a facade for the methods in cdmlib-service.
85 public class CdmStore
{
87 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
88 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
89 TaxeditorStorePlugin
.class);
90 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
92 private static CdmStore instance
;
94 //private final ICdmApplicationConfiguration applicationConfiguration;
96 private static ContextManager contextManager
= new ContextManager();
98 private static LoginManager loginManager
= new LoginManager();
100 private static TermManager termManager
= new TermManager();
102 private static SearchManager searchManager
= new SearchManager();
104 private static EditorManager editorManager
= new EditorManager();
106 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
108 private static ProgressMonitorClientManager progressMonitorClientManager
= new ProgressMonitorClientManager();
110 private static CdmStoreConnector job
;
112 private static CDMServer managedServer
;
114 private Language language
;
116 private ICdmSource cdmSource
;
118 private boolean isConnected
;
127 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
129 protected static CdmStore
getDefault() {
130 if (instance
!= null && instance
.isConnected
) {
132 } else{// if (instance == null || !instance.isConnected) {
134 MessagingUtils
.noDataSourceWarningDialog(instance
);
136 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
142 * Initialize the with the last edited datasource
144 public static void connect() {
146 ICdmSource cdmSource
;
149 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
151 } catch (Exception e
) {
152 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
159 * Initialize with a specific datasource
162 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
164 public static void connect(ICdmSource cdmSource
) {
165 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
166 DEFAULT_APPLICATION_CONTEXT
);
169 public static void connect(ICdmSource cdmSource
, RemotingLoginDialog loginDialog
) {
171 DEFAULT_DB_SCHEMA_VALIDATION
,
172 DEFAULT_APPLICATION_CONTEXT
,
177 * Initialize and provide
180 * @param dbSchemaValidation
181 * @param applicationContextBean
183 private static void connect(final ICdmSource cdmSource
,
184 final DbSchemaValidation dbSchemaValidation
,
185 final Resource applicationContextBean
) {
187 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
189 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
190 dbSchemaValidation
, applicationContextBean
);
192 job
.setPriority(Job
.BUILD
);
197 private static void connect(final ICdmSource cdmSource
,
198 final DbSchemaValidation dbSchemaValidation
,
199 final Resource applicationContextBean
,
200 RemotingLoginDialog remotingLoginDialog
) {
201 RemotingLoginDialog loginDialog
= remotingLoginDialog
;
203 // before we connect we clear the entity caches and the sessions
204 CdmRemoteCacheManager
.removeEntityCaches();
205 if(getCurrentSessionManager() != null) {
206 getCurrentSessionManager().disposeAll();
209 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
211 job
= new CdmStoreConnector(Display
.getDefault(),
214 applicationContextBean
);
215 job
.start(loginDialog
);
219 public static boolean isConnecting() {
220 return job
!= null && job
.getState() == Job
.RUNNING
;
224 * Closes the current application context
227 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
229 public static void close(final IProgressMonitor monitor
) {
230 Display
.getDefault().asyncExec(new Runnable() {
234 * @see java.lang.Runnable#run()
238 getContextManager().notifyContextAboutToStop(monitor
);
239 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
240 getContextManager().notifyContextStop(monitor
);
247 public static void close(IProgressMonitor monitor
, boolean async
) {
251 getContextManager().notifyContextAboutToStop(monitor
);
252 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
253 getContextManager().notifyContextStop(monitor
);
259 private void close() {
262 CdmApplicationState
.dispose();
265 static void setInstance(ICdmApplicationConfiguration applicationController
,
266 ICdmSource cdmSource
) {
267 instance
= new CdmStore(applicationController
, cdmSource
);
268 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
272 private CdmStore(ICdmApplicationConfiguration applicationController
,
273 ICdmSource cdmSource
) {
274 CdmApplicationState
.setCurrentAppConfig(applicationController
);
275 CdmApplicationState
.setCurrentDataChangeService(new CdmUIDataChangeService());
276 this.cdmSource
= cdmSource
;
281 * All calls to the datastore require
285 private ICdmApplicationConfiguration
getApplicationConfiguration() {
287 return CdmApplicationState
.getCurrentAppConfig();
288 } catch (Exception e
) {
289 MessagingUtils
.error(CdmStore
.class, e
);
296 * getCurrentApplicationController
300 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
303 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
304 if (getDefault() != null) {
305 return getDefault().getApplicationConfiguration();
315 * Creates a new conversation, binds resources to the conversation and start
316 * a transaction for this conversation.
318 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
321 public static ConversationHolder
createConversation() {
322 ConversationHolder conversation
= getCurrentApplicationConfiguration()
325 conversation
.startTransaction();
327 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
332 //FIXME:Remoting should be removed after moving completely to remoting
333 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
335 private ICdmEntitySessionManager
getSessionManager() {
336 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
337 // bad hack which should be finally removed
338 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
339 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
341 if(mockCdmEntitySessionManager
== null) {
342 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
344 return mockCdmEntitySessionManager
;
348 public static ICdmEntitySessionManager
getCurrentSessionManager() {
349 if (getDefault() != null) {
350 return getDefault().getSessionManager();
357 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
358 * interface. If a matching getter is found the according service implementation is returned by
359 * invoking the getter otherwise the method returns <code>null</code>.
362 * @param serviceClass
363 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
365 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
368 service
= CdmApplicationState
.getService(serviceClass
);
369 } catch (CdmApplicationException cae
) {
370 MessagingUtils
.error(CdmStore
.class, cae
);
377 * @see #getService(Class)
378 * As ICommonService is not extending IService we need a specific request here
380 public static ICommonService
getCommonService() {
381 return CdmApplicationState
.getCommonService();
387 * getAuthenticationManager
391 * {@link org.springframework.security.authentication.ProviderManager}
394 public static AuthenticationManager
getAuthenticationManager() {
395 return getCurrentApplicationConfiguration().getAuthenticationManager();
400 * getAuthenticationManager
404 * {@link ICdmPermissionEvaluator} object.
406 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
407 return getCurrentApplicationConfiguration().getPermissionEvaluator();
412 * SECURITY RELATED CONVENIENCE METHODS
416 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
418 * @param targetDomainObject
422 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
423 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
424 SecurityContext context
= SecurityContextHolder
.getContext();
425 PermissionEvaluator pe
= getPermissionEvaluator();
426 boolean hasPermission
= false;
428 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
430 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
433 return hasPermission
;
437 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
439 * @param targetDomainObject
443 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
444 boolean hasPermission
= false;
446 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
447 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
450 return hasPermission
;
453 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
454 boolean hasPermission
= false;
456 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
457 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
460 return hasPermission
;
463 public static Authentication
getCurrentAuthentiation() {
464 SecurityContext context
= SecurityContextHolder
.getContext();
465 return context
.getAuthentication();
473 * Provides access to the global default language set in the application preferences.
475 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
477 public static Language
getDefaultLanguage() {
478 if (getDefault().getLanguage() == null) {
479 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
481 return getDefault().getLanguage();
490 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
492 public static void setDefaultLanguage(Language language
) {
493 getDefault().setLanguage(language
);
497 * @return the language
499 private Language
getLanguage() {
505 * the language to set
507 private void setLanguage(Language language
) {
508 this.language
= language
;
517 * Getter for the field <code>loginManager</code>.
520 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
522 public static LoginManager
getLoginManager() {
528 * Getter for the field <code>contextManager</code>.
531 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
533 public static ContextManager
getContextManager() {
534 return contextManager
;
537 public static TermManager
getTermManager() {
541 public static SearchManager
getSearchManager() {
542 return searchManager
;
545 public static EditorManager
getEditorManager() {
546 return editorManager
;
549 public static ProgressMonitorClientManager
getProgressMonitorClientManager() {
550 return progressMonitorClientManager
;
554 * IMPORT/EXPORT FACTORIES
559 * Getter for the field <code>importHandler</code>.
562 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
564 public static ImportManager
getImportManager() {
565 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
570 * Getter for the field <code>exportHandler</code>.
573 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
575 public static ExportManager
getExportManager() {
576 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
580 * Whether this CdmStore is currently connected to a datasource
584 public static boolean isActive() {
585 return instance
!= null && instance
.isConnected
;
588 public static ICdmSource
getActiveCdmSource() {
590 return instance
.getCdmSource();
600 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
601 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
603 // public static ICdmDataSource getDataSource() {
605 // return (ICdmDataSource)instance.getCdmSource();
613 private ICdmSource
getCdmSource() {
618 public static <T
extends CdmBase
> IService
<T
> getService(T cdmBase
){
619 IService
<T
> service
= null;
621 //get corresponding service
622 if(cdmBase
.isInstanceOf(Reference
.class)){
623 service
= (IService
<T
>) getService(IReferenceService
.class);
625 else if (cdmBase
.isInstanceOf(AgentBase
.class)){
626 service
= (IService
<T
>) getService(IAgentService
.class);
628 else if (cdmBase
instanceof NonViralName
) {
629 service
= (IService
<T
>) getService(INameService
.class);
631 else if (cdmBase
instanceof SpecimenOrObservationBase
) {
632 service
= (IService
<T
>) getService(IOccurrenceService
.class);
634 else if (cdmBase
instanceof Collection
) {
635 service
= (IService
<T
>) getService(ICollectionService
.class);
637 else if (cdmBase
instanceof User
) {
638 service
= (IService
<T
>) getService(IUserService
.class);
640 else if (cdmBase
instanceof Primer
) {
641 service
= (IService
<T
>) getService(IPrimerService
.class);
643 else if (cdmBase
instanceof Amplification
) {
644 service
= (IService
<T
>) getService(IAmplificationService
.class);
645 } else if (cdmBase
instanceof PolytomousKey
) {
646 service
= (IService
<T
>) getService(IPolytomousKeyService
.class);
652 public static void setManagedServer(CDMServer server
) {
653 managedServer
= server
;
656 public static CDMServer
getManagedServer() {
657 return managedServer
;