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
.ICommonService
;
32 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
33 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
34 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
35 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
36 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
40 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
41 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
42 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
43 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
44 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
45 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
46 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
47 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
48 import eu
.etaxonomy
.taxeditor
.session
.mock
.MockCdmEntitySessionManager
;
49 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
50 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.RemotingLoginDialog
;
51 import eu
.etaxonomy
.taxeditor
.util
.ProgressMonitorClientManager
;
52 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
53 import eu
.etaxonomy
.taxeditor
.webapp
.CDMServer
;
56 * This implementation of ICdmDataRepository depends on hibernate sessions to
57 * store the data correctly for the current session. No state is held in this
60 * Only methods that either get or manipulate data are exposed here. So this
61 * class acts as a facade for the methods in cdmlib-service.
67 public class CdmStore
{
69 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
70 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
71 TaxeditorStorePlugin
.class);
72 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
74 private static CdmStore instance
;
76 //private final ICdmApplicationConfiguration applicationConfiguration;
78 private static ContextManager contextManager
= new ContextManager();
80 private static LoginManager loginManager
= new LoginManager();
82 private static TermManager termManager
= new TermManager();
84 private static SearchManager searchManager
= new SearchManager();
86 private static EditorManager editorManager
= new EditorManager();
88 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
90 private static ProgressMonitorClientManager progressMonitorClientManager
= new ProgressMonitorClientManager();
92 private static CdmStoreConnector job
;
94 private static CDMServer managedServer
;
96 private Language language
;
98 private ICdmSource cdmSource
;
100 private boolean isConnected
;
109 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
111 protected static CdmStore
getDefault() {
112 if (instance
!= null && instance
.isConnected
) {
114 } else{// if (instance == null || !instance.isConnected) {
116 MessagingUtils
.noDataSourceWarningDialog(instance
);
118 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
124 * Initialize the with the last edited datasource
126 public static void connect() {
128 ICdmSource cdmSource
;
131 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
133 } catch (Exception e
) {
134 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
141 * Initialize with a specific datasource
144 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
146 public static void connect(ICdmSource cdmSource
) {
147 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
148 DEFAULT_APPLICATION_CONTEXT
);
151 public static void connect(ICdmSource cdmSource
, RemotingLoginDialog loginDialog
) {
153 DEFAULT_DB_SCHEMA_VALIDATION
,
154 DEFAULT_APPLICATION_CONTEXT
,
159 * Initialize and provide
162 * @param dbSchemaValidation
163 * @param applicationContextBean
165 private static void connect(final ICdmSource cdmSource
,
166 final DbSchemaValidation dbSchemaValidation
,
167 final Resource applicationContextBean
) {
169 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
171 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
172 dbSchemaValidation
, applicationContextBean
);
174 job
.setPriority(Job
.BUILD
);
179 private static void connect(final ICdmSource cdmSource
,
180 final DbSchemaValidation dbSchemaValidation
,
181 final Resource applicationContextBean
,
182 RemotingLoginDialog remotingLoginDialog
) {
183 RemotingLoginDialog loginDialog
= remotingLoginDialog
;
185 // before we connect we clear the entity caches and the sessions
186 CdmRemoteCacheManager
.removeEntityCaches();
187 if(getCurrentSessionManager() != null) {
188 getCurrentSessionManager().disposeAll();
191 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
193 job
= new CdmStoreConnector(Display
.getDefault(),
196 applicationContextBean
);
197 job
.start(loginDialog
);
201 public static boolean isConnecting() {
202 return job
!= null && job
.getState() == Job
.RUNNING
;
206 * Closes the current application context
209 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
211 public static void close(final IProgressMonitor monitor
) {
212 Display
.getDefault().asyncExec(new Runnable() {
216 * @see java.lang.Runnable#run()
220 getContextManager().notifyContextAboutToStop(monitor
);
221 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
222 getContextManager().notifyContextStop(monitor
);
229 public static void close(IProgressMonitor monitor
, boolean async
) {
233 getContextManager().notifyContextAboutToStop(monitor
);
234 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
235 getContextManager().notifyContextStop(monitor
);
241 private void close() {
244 CdmApplicationState
.dispose();
247 static void setInstance(ICdmApplicationConfiguration applicationController
,
248 ICdmSource cdmSource
) {
249 instance
= new CdmStore(applicationController
, cdmSource
);
250 if(getCurrentSessionManager().isRemoting()) {
251 CdmApplicationState
.setCdmServiceCacher(new CdmServiceCacher());
255 private CdmStore(ICdmApplicationConfiguration applicationController
,
256 ICdmSource cdmSource
) {
257 CdmApplicationState
.setCurrentAppConfig(applicationController
);
258 CdmApplicationState
.setCurrentDataChangeService(new CdmUIDataChangeService());
259 this.cdmSource
= cdmSource
;
264 * All calls to the datastore require
268 private ICdmApplicationConfiguration
getApplicationConfiguration() {
270 return CdmApplicationState
.getCurrentAppConfig();
271 } catch (Exception e
) {
272 MessagingUtils
.error(CdmStore
.class, e
);
279 * getCurrentApplicationController
283 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
286 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
287 if (getDefault() != null) {
288 return getDefault().getApplicationConfiguration();
298 * Creates a new conversation, binds resources to the conversation and start
299 * a transaction for this conversation.
301 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
304 public static ConversationHolder
createConversation() {
305 ConversationHolder conversation
= getCurrentApplicationConfiguration()
308 conversation
.startTransaction();
310 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
315 //FIXME:Remoting should be removed after moving completely to remoting
316 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
318 private ICdmEntitySessionManager
getSessionManager() {
319 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
320 // bad hack which should be finally removed
321 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
322 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
324 if(mockCdmEntitySessionManager
== null) {
325 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
327 return mockCdmEntitySessionManager
;
331 public static ICdmEntitySessionManager
getCurrentSessionManager() {
332 if (getDefault() != null) {
333 return getDefault().getSessionManager();
340 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
341 * interface. If a matching getter is found the according service implementation is returned by
342 * invoking the getter otherwise the method returns <code>null</code>.
345 * @param serviceClass
346 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
348 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
351 service
= CdmApplicationState
.getService(serviceClass
);
352 } catch (CdmApplicationException cae
) {
353 MessagingUtils
.error(CdmStore
.class, cae
);
360 * @see #getService(Class)
361 * As ICommonService is not extending IService we need a specific request here
363 public static ICommonService
getCommonService() {
364 return CdmApplicationState
.getCommonService();
370 * getAuthenticationManager
374 * {@link org.springframework.security.authentication.ProviderManager}
377 public static AuthenticationManager
getAuthenticationManager() {
378 return getCurrentApplicationConfiguration().getAuthenticationManager();
383 * getAuthenticationManager
387 * {@link ICdmPermissionEvaluator} object.
389 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
390 return getCurrentApplicationConfiguration().getPermissionEvaluator();
395 * SECURITY RELATED CONVENIENCE METHODS
399 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
401 * @param targetDomainObject
405 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
406 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
407 SecurityContext context
= SecurityContextHolder
.getContext();
408 PermissionEvaluator pe
= getPermissionEvaluator();
409 boolean hasPermission
= false;
411 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
413 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
416 return hasPermission
;
420 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
422 * @param targetDomainObject
426 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
427 boolean hasPermission
= false;
429 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
430 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
433 return hasPermission
;
436 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
437 boolean hasPermission
= false;
439 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
440 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
443 return hasPermission
;
446 public static Authentication
getCurrentAuthentiation() {
447 SecurityContext context
= SecurityContextHolder
.getContext();
448 return context
.getAuthentication();
456 * Provides access to the global default language set in the application preferences.
458 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
460 public static Language
getDefaultLanguage() {
461 if (getDefault().getLanguage() == null) {
462 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
464 return getDefault().getLanguage();
473 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
475 public static void setDefaultLanguage(Language language
) {
476 getDefault().setLanguage(language
);
480 * @return the language
482 private Language
getLanguage() {
488 * the language to set
490 private void setLanguage(Language language
) {
491 this.language
= language
;
500 * Getter for the field <code>loginManager</code>.
503 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
505 public static LoginManager
getLoginManager() {
511 * Getter for the field <code>contextManager</code>.
514 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
516 public static ContextManager
getContextManager() {
517 return contextManager
;
520 public static TermManager
getTermManager() {
524 public static SearchManager
getSearchManager() {
525 return searchManager
;
528 public static EditorManager
getEditorManager() {
529 return editorManager
;
532 public static ProgressMonitorClientManager
getProgressMonitorClientManager() {
533 return progressMonitorClientManager
;
537 * IMPORT/EXPORT FACTORIES
542 * Getter for the field <code>importHandler</code>.
545 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
547 public static ImportManager
getImportManager() {
548 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
553 * Getter for the field <code>exportHandler</code>.
556 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
558 public static ExportManager
getExportManager() {
559 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
563 * Whether this CdmStore is currently connected to a datasource
567 public static boolean isActive() {
568 return instance
!= null && instance
.isConnected
;
571 public static ICdmSource
getActiveCdmSource() {
573 return instance
.getCdmSource();
583 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
584 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
586 // public static ICdmDataSource getDataSource() {
588 // return (ICdmDataSource)instance.getCdmSource();
596 private ICdmSource
getCdmSource() {
601 public static void setManagedServer(CDMServer server
) {
602 managedServer
= server
;
605 public static CDMServer
getManagedServer() {
606 return managedServer
;