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
.lang
.reflect
.InvocationTargetException
;
13 import java
.lang
.reflect
.Method
;
14 import java
.lang
.reflect
.Type
;
15 import java
.util
.EnumSet
;
17 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
18 import org
.eclipse
.core
.runtime
.jobs
.Job
;
19 import org
.eclipse
.swt
.widgets
.Display
;
20 import org
.springframework
.core
.io
.ClassPathResource
;
21 import org
.springframework
.core
.io
.Resource
;
22 import org
.springframework
.security
.access
.PermissionEvaluator
;
23 import org
.springframework
.security
.authentication
.ProviderManager
;
24 import org
.springframework
.security
.core
.Authentication
;
25 import org
.springframework
.security
.core
.context
.SecurityContext
;
26 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
28 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationRemoteController
;
29 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
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
.ext
.geo
.IEditGeoService
;
36 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
37 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
40 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
41 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
42 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
43 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
44 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
45 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
46 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
47 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
48 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
49 import eu
.etaxonomy
.taxeditor
.session
.mock
.MockCdmEntitySessionManager
;
50 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
51 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
54 * This implementation of ICdmDataRepository depends on hibernate sessions to
55 * store the data correctly for the current session. No state is held in this
58 * Only methods that either get or manipulate data are exposed here. So this
59 * class acts as a facade for the methods in cdmlib-service.
65 public class CdmStore
{
67 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
68 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
69 TaxeditorStorePlugin
.class);
70 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
72 private static CdmStore instance
;
74 private final ICdmApplicationConfiguration applicationConfiguration
;
76 private static ContextManager contextManager
= new ContextManager();
78 private static LoginManager loginManager
= new LoginManager();
80 private static TermManager termManager
= new TermManager();
82 private static SearchManager searchManager
= new SearchManager();
84 private static EditorManager editorManager
= new EditorManager();
86 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
88 private static CdmStoreConnector job
;
90 private Language language
;
92 private ICdmSource cdmSource
;
94 private boolean isConnected
;
103 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
105 protected static CdmStore
getDefault() {
106 if (instance
!= null && instance
.isConnected
) {
108 } else{// if (instance == null || !instance.isConnected) {
110 MessagingUtils
.noDataSourceWarningDialog(instance
);
112 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
118 * Initialize the with the last edited datasource
120 public static void connect() {
122 ICdmSource cdmSource
;
125 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
127 } catch (Exception e
) {
128 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
135 * Initialize with a specific datasource
138 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
140 public static void connect(ICdmSource cdmSource
) {
141 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
142 DEFAULT_APPLICATION_CONTEXT
);
146 * Initialize and provide
149 * @param dbSchemaValidation
150 * @param applicationContextBean
152 private static void connect(final ICdmSource cdmSource
,
153 final DbSchemaValidation dbSchemaValidation
,
154 final Resource applicationContextBean
) {
156 // before we connect we clear the caches and the sessions
157 CdmRemoteCacheManager
.getInstance().getDefaultCacheManager().removalAll();
158 if(getCurrentSessionManager() != null) {
159 getCurrentSessionManager().disposeAll();
162 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
164 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
165 dbSchemaValidation
, applicationContextBean
);
167 job
.setPriority(Job
.BUILD
);
172 public static boolean isConnecting() {
173 return job
!= null && job
.getState() == Job
.RUNNING
;
177 * Closes the current application context
180 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
182 public static void close(final IProgressMonitor monitor
) {
183 Display
.getDefault().asyncExec(new Runnable() {
187 * @see java.lang.Runnable#run()
191 getContextManager().notifyContextAboutToStop(monitor
);
192 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
193 getContextManager().notifyContextStop(monitor
);
200 private void close() {
205 static void setInstance(ICdmApplicationConfiguration applicationController
,
206 ICdmSource cdmSource
) {
207 instance
= new CdmStore(applicationController
, cdmSource
);
210 private CdmStore(ICdmApplicationConfiguration applicationController
,
211 ICdmSource cdmSource
) {
212 this.applicationConfiguration
= applicationController
;
213 this.cdmSource
= cdmSource
;
218 * All calls to the datastore require
222 private ICdmApplicationConfiguration
getApplicationConfiguration() {
224 return applicationConfiguration
;
225 } catch (Exception e
) {
226 MessagingUtils
.error(CdmStore
.class, e
);
233 * getCurrentApplicationController
237 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
240 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
241 if (getDefault() != null) {
242 return getDefault().getApplicationConfiguration();
252 * Creates a new conversation, binds resources to the conversation and start
253 * a transaction for this conversation.
255 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
258 public static ConversationHolder
createConversation() {
259 ConversationHolder conversation
= getCurrentApplicationConfiguration()
262 conversation
.startTransaction();
264 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
269 //FIXME:Remoting should be removed after moving completely to remoting
270 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
272 private ICdmEntitySessionManager
getSessionManager() {
273 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
274 // bad hack which should be finally removed
275 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
276 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
278 if(mockCdmEntitySessionManager
== null) {
279 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
281 return mockCdmEntitySessionManager
;
285 public static ICdmEntitySessionManager
getCurrentSessionManager() {
286 if (getDefault() != null) {
287 return getDefault().getSessionManager();
294 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
295 * interface. If a matching getter is found the according service implementation is returned by
296 * invoking the getter otherwise the method returns <code>null</code>.
299 * @param serviceClass
300 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
302 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
303 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
305 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
309 for (Method method
: methods
) {
310 Type type
= method
.getGenericReturnType();
312 if (type
.equals(serviceClass
)) {
314 service
= (T
) method
.invoke(configuration
, null);
316 } catch (IllegalArgumentException e
) {
317 MessagingUtils
.error(CdmStore
.class, e
);
318 } catch (IllegalAccessException e
) {
319 MessagingUtils
.error(CdmStore
.class, e
);
320 } catch (InvocationTargetException e
) {
321 MessagingUtils
.error(CdmStore
.class, e
);
330 * @see #getService(Class)
331 * As ICommonService is not extending IService we need a specific request here
333 public static ICommonService
getCommonService() {
334 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
336 return configuration
.getCommonService();
342 * getAuthenticationManager
346 * {@link org.springframework.security.authentication.ProviderManager}
349 public static ProviderManager
getAuthenticationManager() {
350 return getCurrentApplicationConfiguration().getAuthenticationManager();
355 * getAuthenticationManager
359 * {@link ICdmPermissionEvaluator} object.
361 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
362 return getCurrentApplicationConfiguration().getPermissionEvaluator();
370 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
372 public static IEditGeoService
getGeoService() {
373 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
378 * SECURITY RELATED CONVENIENCE METHODS
382 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
384 * @param targetDomainObject
388 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
389 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
390 SecurityContext context
= SecurityContextHolder
.getContext();
391 PermissionEvaluator pe
= getPermissionEvaluator();
392 boolean hasPermission
= false;
394 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
396 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
399 return hasPermission
;
403 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
405 * @param targetDomainObject
409 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
410 boolean hasPermission
= false;
412 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
413 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
416 return hasPermission
;
419 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
420 boolean hasPermission
= false;
422 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
423 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
426 return hasPermission
;
429 public static Authentication
getCurrentAuthentiation() {
430 SecurityContext context
= SecurityContextHolder
.getContext();
431 return context
.getAuthentication();
439 * Provides access to the global default language set in the application preferences.
441 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
443 public static Language
getDefaultLanguage() {
444 if (getDefault().getLanguage() == null) {
445 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
447 return getDefault().getLanguage();
456 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
458 public static void setDefaultLanguage(Language language
) {
459 getDefault().setLanguage(language
);
463 * @return the language
465 private Language
getLanguage() {
471 * the language to set
473 private void setLanguage(Language language
) {
474 this.language
= language
;
483 * Getter for the field <code>loginManager</code>.
486 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
488 public static LoginManager
getLoginManager() {
494 * Getter for the field <code>contextManager</code>.
497 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
499 public static ContextManager
getContextManager() {
500 return contextManager
;
503 public static TermManager
getTermManager() {
507 public static SearchManager
getSearchManager() {
508 return searchManager
;
511 public static EditorManager
getEditorManager() {
512 return editorManager
;
516 * IMPORT/EXPORT FACTORIES
521 * Getter for the field <code>importHandler</code>.
524 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
526 public static ImportManager
getImportManager() {
527 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
532 * Getter for the field <code>exportHandler</code>.
535 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
537 public static ExportManager
getExportManager() {
538 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
542 * Whether this CdmStore is currently connected to a datasource
546 public static boolean isActive() {
547 return instance
!= null && instance
.isConnected
;
550 public static ICdmSource
getActiveCdmSource() {
552 return instance
.getCdmSource();
562 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
563 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
565 // public static ICdmDataSource getDataSource() {
567 // return (ICdmDataSource)instance.getCdmSource();
575 private ICdmSource
getCdmSource() {