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
.IReferenceService
;
37 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
38 import eu
.etaxonomy
.cdm
.api
.service
.IUserService
;
39 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.IAmplificationService
;
40 import eu
.etaxonomy
.cdm
.api
.service
.molecular
.IPrimerService
;
41 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
42 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
43 import eu
.etaxonomy
.cdm
.model
.agent
.AgentBase
;
44 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
45 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
46 import eu
.etaxonomy
.cdm
.model
.common
.User
;
47 import eu
.etaxonomy
.cdm
.model
.molecular
.Amplification
;
48 import eu
.etaxonomy
.cdm
.model
.molecular
.Primer
;
49 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
50 import eu
.etaxonomy
.cdm
.model
.occurrence
.Collection
;
51 import eu
.etaxonomy
.cdm
.model
.occurrence
.SpecimenOrObservationBase
;
52 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
53 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
54 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
55 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
56 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
57 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
58 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
59 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
60 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
61 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
62 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
63 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
64 import eu
.etaxonomy
.taxeditor
.session
.mock
.MockCdmEntitySessionManager
;
65 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
66 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
67 import eu
.etaxonomy
.taxeditor
.util
.ProgressMonitorClientManager
;
68 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
69 import eu
.etaxonomy
.taxeditor
.webapp
.CDMServer
;
72 * This implementation of ICdmDataRepository depends on hibernate sessions to
73 * store the data correctly for the current session. No state is held in this
76 * Only methods that either get or manipulate data are exposed here. So this
77 * class acts as a facade for the methods in cdmlib-service.
83 public class CdmStore
{
85 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
86 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
87 TaxeditorStorePlugin
.class);
88 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
90 private static CdmStore instance
;
92 //private final ICdmApplicationConfiguration applicationConfiguration;
94 private static ContextManager contextManager
= new ContextManager();
96 private static LoginManager loginManager
= new LoginManager();
98 private static TermManager termManager
= new TermManager();
100 private static SearchManager searchManager
= new SearchManager();
102 private static EditorManager editorManager
= new EditorManager();
104 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
106 private static ProgressMonitorClientManager progressMonitorClientManager
= new ProgressMonitorClientManager();
108 private static CdmStoreConnector job
;
110 private static CDMServer managedServer
;
112 private Language language
;
114 private ICdmSource cdmSource
;
116 private boolean isConnected
;
125 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
127 protected static CdmStore
getDefault() {
128 if (instance
!= null && instance
.isConnected
) {
130 } else{// if (instance == null || !instance.isConnected) {
132 MessagingUtils
.noDataSourceWarningDialog(instance
);
134 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
140 * Initialize the with the last edited datasource
142 public static void connect() {
144 ICdmSource cdmSource
;
147 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
149 } catch (Exception e
) {
150 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
157 * Initialize with a specific datasource
160 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
162 public static void connect(ICdmSource cdmSource
) {
163 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
164 DEFAULT_APPLICATION_CONTEXT
);
167 public static void connect(ICdmSource cdmSource
, RemotingLoginDialog loginDialog
) {
169 DEFAULT_DB_SCHEMA_VALIDATION
,
170 DEFAULT_APPLICATION_CONTEXT
,
175 * Initialize and provide
178 * @param dbSchemaValidation
179 * @param applicationContextBean
181 private static void connect(final ICdmSource cdmSource
,
182 final DbSchemaValidation dbSchemaValidation
,
183 final Resource applicationContextBean
) {
185 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
187 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
188 dbSchemaValidation
, applicationContextBean
);
190 job
.setPriority(Job
.BUILD
);
195 private static void connect(final ICdmSource cdmSource
,
196 final DbSchemaValidation dbSchemaValidation
,
197 final Resource applicationContextBean
,
198 RemotingLoginDialog remotingLoginDialog
) {
199 RemotingLoginDialog loginDialog
= remotingLoginDialog
;
201 // before we connect we clear the entity caches and the sessions
202 CdmRemoteCacheManager
.removeEntityCaches();
203 if(getCurrentSessionManager() != null) {
204 getCurrentSessionManager().disposeAll();
207 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
209 job
= new CdmStoreConnector(Display
.getDefault(),
212 applicationContextBean
);
213 job
.start(loginDialog
);
217 public static boolean isConnecting() {
218 return job
!= null && job
.getState() == Job
.RUNNING
;
222 * Closes the current application context
225 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
227 public static void close(final IProgressMonitor monitor
) {
228 Display
.getDefault().asyncExec(new Runnable() {
232 * @see java.lang.Runnable#run()
236 getContextManager().notifyContextAboutToStop(monitor
);
237 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
238 getContextManager().notifyContextStop(monitor
);
245 public static void close(IProgressMonitor monitor
, boolean async
) {
249 getContextManager().notifyContextAboutToStop(monitor
);
250 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
251 getContextManager().notifyContextStop(monitor
);
257 private void close() {
260 CdmApplicationState
.dispose();
263 static void setInstance(ICdmApplicationConfiguration applicationController
,
264 ICdmSource cdmSource
) {
265 instance
= new CdmStore(applicationController
, cdmSource
);
266 if(getCurrentSessionManager().isRemoting()) {
267 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
271 private CdmStore(ICdmApplicationConfiguration applicationController
,
272 ICdmSource cdmSource
) {
273 CdmApplicationState
.setCurrentAppConfig(applicationController
);
274 CdmApplicationState
.setCurrentDataChangeService(new CdmUIDataChangeService());
275 this.cdmSource
= cdmSource
;
280 * All calls to the datastore require
284 private ICdmApplicationConfiguration
getApplicationConfiguration() {
286 return CdmApplicationState
.getCurrentAppConfig();
287 } catch (Exception e
) {
288 MessagingUtils
.error(CdmStore
.class, e
);
295 * getCurrentApplicationController
299 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
302 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
303 if (getDefault() != null) {
304 return getDefault().getApplicationConfiguration();
314 * Creates a new conversation, binds resources to the conversation and start
315 * a transaction for this conversation.
317 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
320 public static ConversationHolder
createConversation() {
321 ConversationHolder conversation
= getCurrentApplicationConfiguration()
324 conversation
.startTransaction();
326 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
331 //FIXME:Remoting should be removed after moving completely to remoting
332 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
334 private ICdmEntitySessionManager
getSessionManager() {
335 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
336 // bad hack which should be finally removed
337 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
338 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
340 if(mockCdmEntitySessionManager
== null) {
341 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
343 return mockCdmEntitySessionManager
;
347 public static ICdmEntitySessionManager
getCurrentSessionManager() {
348 if (getDefault() != null) {
349 return getDefault().getSessionManager();
356 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
357 * interface. If a matching getter is found the according service implementation is returned by
358 * invoking the getter otherwise the method returns <code>null</code>.
361 * @param serviceClass
362 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
364 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
367 service
= CdmApplicationState
.getService(serviceClass
);
368 } catch (CdmApplicationException cae
) {
369 MessagingUtils
.error(CdmStore
.class, cae
);
376 * @see #getService(Class)
377 * As ICommonService is not extending IService we need a specific request here
379 public static ICommonService
getCommonService() {
380 return CdmApplicationState
.getCommonService();
386 * getAuthenticationManager
390 * {@link org.springframework.security.authentication.ProviderManager}
393 public static AuthenticationManager
getAuthenticationManager() {
394 return getCurrentApplicationConfiguration().getAuthenticationManager();
399 * getAuthenticationManager
403 * {@link ICdmPermissionEvaluator} object.
405 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
406 return getCurrentApplicationConfiguration().getPermissionEvaluator();
411 * SECURITY RELATED CONVENIENCE METHODS
415 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
417 * @param targetDomainObject
421 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
422 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
423 SecurityContext context
= SecurityContextHolder
.getContext();
424 PermissionEvaluator pe
= getPermissionEvaluator();
425 boolean hasPermission
= false;
427 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
429 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
432 return hasPermission
;
436 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
438 * @param targetDomainObject
442 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
443 boolean hasPermission
= false;
445 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
446 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
449 return hasPermission
;
452 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
453 boolean hasPermission
= false;
455 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
456 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
459 return hasPermission
;
462 public static Authentication
getCurrentAuthentiation() {
463 SecurityContext context
= SecurityContextHolder
.getContext();
464 return context
.getAuthentication();
472 * Provides access to the global default language set in the application preferences.
474 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
476 public static Language
getDefaultLanguage() {
477 if (getDefault().getLanguage() == null) {
478 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
480 return getDefault().getLanguage();
489 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
491 public static void setDefaultLanguage(Language language
) {
492 getDefault().setLanguage(language
);
496 * @return the language
498 private Language
getLanguage() {
504 * the language to set
506 private void setLanguage(Language language
) {
507 this.language
= language
;
516 * Getter for the field <code>loginManager</code>.
519 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
521 public static LoginManager
getLoginManager() {
527 * Getter for the field <code>contextManager</code>.
530 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
532 public static ContextManager
getContextManager() {
533 return contextManager
;
536 public static TermManager
getTermManager() {
540 public static SearchManager
getSearchManager() {
541 return searchManager
;
544 public static EditorManager
getEditorManager() {
545 return editorManager
;
548 public static ProgressMonitorClientManager
getProgressMonitorClientManager() {
549 return progressMonitorClientManager
;
553 * IMPORT/EXPORT FACTORIES
558 * Getter for the field <code>importHandler</code>.
561 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
563 public static ImportManager
getImportManager() {
564 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
569 * Getter for the field <code>exportHandler</code>.
572 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
574 public static ExportManager
getExportManager() {
575 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
579 * Whether this CdmStore is currently connected to a datasource
583 public static boolean isActive() {
584 return instance
!= null && instance
.isConnected
;
587 public static ICdmSource
getActiveCdmSource() {
589 return instance
.getCdmSource();
599 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
600 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
602 // public static ICdmDataSource getDataSource() {
604 // return (ICdmDataSource)instance.getCdmSource();
612 private ICdmSource
getCdmSource() {
617 public static <T
extends CdmBase
> IService
<T
> getService(T cdmBase
){
618 IService
<T
> service
= null;
620 //get corresponding service
621 if(cdmBase
.isInstanceOf(Reference
.class)){
622 service
= (IService
<T
>) getService(IReferenceService
.class);
624 else if (cdmBase
.isInstanceOf(AgentBase
.class)){
625 service
= (IService
<T
>) getService(IAgentService
.class);
627 else if (cdmBase
instanceof NonViralName
) {
628 service
= (IService
<T
>) getService(INameService
.class);
630 else if (cdmBase
instanceof SpecimenOrObservationBase
) {
631 service
= (IService
<T
>) getService(IOccurrenceService
.class);
633 else if (cdmBase
instanceof Collection
) {
634 service
= (IService
<T
>) getService(ICollectionService
.class);
636 else if (cdmBase
instanceof User
) {
637 service
= (IService
<T
>) getService(IUserService
.class);
639 else if (cdmBase
instanceof Primer
) {
640 service
= (IService
<T
>) getService(IPrimerService
.class);
642 else if (cdmBase
instanceof Amplification
) {
643 service
= (IService
<T
>) getService(IAmplificationService
.class);
649 public static void setManagedServer(CDMServer server
) {
650 managedServer
= server
;
653 public static CDMServer
getManagedServer() {
654 return managedServer
;