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 if(getCurrentSessionManager().isRemoting()) {
269 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
273 private CdmStore(ICdmApplicationConfiguration applicationController
,
274 ICdmSource cdmSource
) {
275 CdmApplicationState
.setCurrentAppConfig(applicationController
);
276 CdmApplicationState
.setCurrentDataChangeService(new CdmUIDataChangeService());
277 this.cdmSource
= cdmSource
;
282 * All calls to the datastore require
286 private ICdmApplicationConfiguration
getApplicationConfiguration() {
288 return CdmApplicationState
.getCurrentAppConfig();
289 } catch (Exception e
) {
290 MessagingUtils
.error(CdmStore
.class, e
);
297 * getCurrentApplicationController
301 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
304 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
305 if (getDefault() != null) {
306 return getDefault().getApplicationConfiguration();
316 * Creates a new conversation, binds resources to the conversation and start
317 * a transaction for this conversation.
319 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
322 public static ConversationHolder
createConversation() {
323 ConversationHolder conversation
= getCurrentApplicationConfiguration()
326 conversation
.startTransaction();
328 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
333 //FIXME:Remoting should be removed after moving completely to remoting
334 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
336 private ICdmEntitySessionManager
getSessionManager() {
337 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
338 // bad hack which should be finally removed
339 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
340 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
342 if(mockCdmEntitySessionManager
== null) {
343 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
345 return mockCdmEntitySessionManager
;
349 public static ICdmEntitySessionManager
getCurrentSessionManager() {
350 if (getDefault() != null) {
351 return getDefault().getSessionManager();
358 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
359 * interface. If a matching getter is found the according service implementation is returned by
360 * invoking the getter otherwise the method returns <code>null</code>.
363 * @param serviceClass
364 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
366 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
369 service
= CdmApplicationState
.getService(serviceClass
);
370 } catch (CdmApplicationException cae
) {
371 MessagingUtils
.error(CdmStore
.class, cae
);
378 * @see #getService(Class)
379 * As ICommonService is not extending IService we need a specific request here
381 public static ICommonService
getCommonService() {
382 return CdmApplicationState
.getCommonService();
388 * getAuthenticationManager
392 * {@link org.springframework.security.authentication.ProviderManager}
395 public static AuthenticationManager
getAuthenticationManager() {
396 return getCurrentApplicationConfiguration().getAuthenticationManager();
401 * getAuthenticationManager
405 * {@link ICdmPermissionEvaluator} object.
407 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
408 return getCurrentApplicationConfiguration().getPermissionEvaluator();
413 * SECURITY RELATED CONVENIENCE METHODS
417 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
419 * @param targetDomainObject
423 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
424 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
425 SecurityContext context
= SecurityContextHolder
.getContext();
426 PermissionEvaluator pe
= getPermissionEvaluator();
427 boolean hasPermission
= false;
429 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
431 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
434 return hasPermission
;
438 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
440 * @param targetDomainObject
444 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
445 boolean hasPermission
= false;
447 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
448 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
451 return hasPermission
;
454 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
455 boolean hasPermission
= false;
457 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
458 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
461 return hasPermission
;
464 public static Authentication
getCurrentAuthentiation() {
465 SecurityContext context
= SecurityContextHolder
.getContext();
466 return context
.getAuthentication();
474 * Provides access to the global default language set in the application preferences.
476 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
478 public static Language
getDefaultLanguage() {
479 if (getDefault().getLanguage() == null) {
480 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
482 return getDefault().getLanguage();
491 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
493 public static void setDefaultLanguage(Language language
) {
494 getDefault().setLanguage(language
);
498 * @return the language
500 private Language
getLanguage() {
506 * the language to set
508 private void setLanguage(Language language
) {
509 this.language
= language
;
518 * Getter for the field <code>loginManager</code>.
521 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
523 public static LoginManager
getLoginManager() {
529 * Getter for the field <code>contextManager</code>.
532 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
534 public static ContextManager
getContextManager() {
535 return contextManager
;
538 public static TermManager
getTermManager() {
542 public static SearchManager
getSearchManager() {
543 return searchManager
;
546 public static EditorManager
getEditorManager() {
547 return editorManager
;
550 public static ProgressMonitorClientManager
getProgressMonitorClientManager() {
551 return progressMonitorClientManager
;
555 * IMPORT/EXPORT FACTORIES
560 * Getter for the field <code>importHandler</code>.
563 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
565 public static ImportManager
getImportManager() {
566 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
571 * Getter for the field <code>exportHandler</code>.
574 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
576 public static ExportManager
getExportManager() {
577 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
581 * Whether this CdmStore is currently connected to a datasource
585 public static boolean isActive() {
586 return instance
!= null && instance
.isConnected
;
589 public static ICdmSource
getActiveCdmSource() {
591 return instance
.getCdmSource();
601 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
602 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
604 // public static ICdmDataSource getDataSource() {
606 // return (ICdmDataSource)instance.getCdmSource();
614 private ICdmSource
getCdmSource() {
619 public static <T
extends CdmBase
> IService
<T
> getService(T cdmBase
){
620 IService
<T
> service
= null;
622 //get corresponding service
623 if(cdmBase
.isInstanceOf(Reference
.class)){
624 service
= (IService
<T
>) getService(IReferenceService
.class);
626 else if (cdmBase
.isInstanceOf(AgentBase
.class)){
627 service
= (IService
<T
>) getService(IAgentService
.class);
629 else if (cdmBase
instanceof NonViralName
) {
630 service
= (IService
<T
>) getService(INameService
.class);
632 else if (cdmBase
instanceof SpecimenOrObservationBase
) {
633 service
= (IService
<T
>) getService(IOccurrenceService
.class);
635 else if (cdmBase
instanceof Collection
) {
636 service
= (IService
<T
>) getService(ICollectionService
.class);
638 else if (cdmBase
instanceof User
) {
639 service
= (IService
<T
>) getService(IUserService
.class);
641 else if (cdmBase
instanceof Primer
) {
642 service
= (IService
<T
>) getService(IPrimerService
.class);
644 else if (cdmBase
instanceof Amplification
) {
645 service
= (IService
<T
>) getService(IAmplificationService
.class);
646 } else if (cdmBase
instanceof PolytomousKey
) {
647 service
= (IService
<T
>) getService(IPolytomousKeyService
.class);
653 public static void setManagedServer(CDMServer server
) {
654 managedServer
= server
;
657 public static CDMServer
getManagedServer() {
658 return managedServer
;