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
.AuthenticationManager
;
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
.CdmApplicationState
;
30 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
32 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
33 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
34 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
35 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
36 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
37 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
38 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
39 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
40 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
41 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
42 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
43 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
44 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
45 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
46 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.remoting
.cache
.CdmRemoteCacheManager
;
49 import eu
.etaxonomy
.taxeditor
.session
.ICdmEntitySessionManager
;
50 import eu
.etaxonomy
.taxeditor
.session
.mock
.MockCdmEntitySessionManager
;
51 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
52 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
55 * This implementation of ICdmDataRepository depends on hibernate sessions to
56 * store the data correctly for the current session. No state is held in this
59 * Only methods that either get or manipulate data are exposed here. So this
60 * class acts as a facade for the methods in cdmlib-service.
66 public class CdmStore
{
68 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
69 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
70 TaxeditorStorePlugin
.class);
71 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
73 private static CdmStore instance
;
75 //private final ICdmApplicationConfiguration applicationConfiguration;
77 private static ContextManager contextManager
= new ContextManager();
79 private static LoginManager loginManager
= new LoginManager();
81 private static TermManager termManager
= new TermManager();
83 private static SearchManager searchManager
= new SearchManager();
85 private static EditorManager editorManager
= new EditorManager();
87 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
89 private static CdmStoreConnector job
;
91 private Language language
;
93 private ICdmSource cdmSource
;
95 private boolean isConnected
;
104 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
106 protected static CdmStore
getDefault() {
107 if (instance
!= null && instance
.isConnected
) {
109 } else{// if (instance == null || !instance.isConnected) {
111 MessagingUtils
.noDataSourceWarningDialog(instance
);
113 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
119 * Initialize the with the last edited datasource
121 public static void connect() {
123 ICdmSource cdmSource
;
126 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
128 } catch (Exception e
) {
129 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
136 * Initialize with a specific datasource
139 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
141 public static void connect(ICdmSource cdmSource
) {
142 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
143 DEFAULT_APPLICATION_CONTEXT
);
147 * Initialize and provide
150 * @param dbSchemaValidation
151 * @param applicationContextBean
153 private static void connect(final ICdmSource cdmSource
,
154 final DbSchemaValidation dbSchemaValidation
,
155 final Resource applicationContextBean
) {
157 // before we connect we clear the entity caches and the sessions
158 CdmRemoteCacheManager
.removeEntityCaches();
159 if(getCurrentSessionManager() != null) {
160 getCurrentSessionManager().disposeAll();
163 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
165 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
166 dbSchemaValidation
, applicationContextBean
);
168 job
.setPriority(Job
.BUILD
);
173 public static boolean isConnecting() {
174 return job
!= null && job
.getState() == Job
.RUNNING
;
178 * Closes the current application context
181 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
183 public static void close(final IProgressMonitor monitor
) {
184 Display
.getDefault().asyncExec(new Runnable() {
188 * @see java.lang.Runnable#run()
192 getContextManager().notifyContextAboutToStop(monitor
);
193 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
194 getContextManager().notifyContextStop(monitor
);
201 private void close() {
206 static void setInstance(ICdmApplicationConfiguration applicationController
,
207 ICdmSource cdmSource
) {
208 instance
= new CdmStore(applicationController
, cdmSource
);
211 private CdmStore(ICdmApplicationConfiguration applicationController
,
212 ICdmSource cdmSource
) {
213 CdmApplicationState
.setCurrentAppConfig(applicationController
);
214 this.cdmSource
= cdmSource
;
219 * All calls to the datastore require
223 private ICdmApplicationConfiguration
getApplicationConfiguration() {
225 return CdmApplicationState
.getCurrentAppConfig();
226 } catch (Exception e
) {
227 MessagingUtils
.error(CdmStore
.class, e
);
234 * getCurrentApplicationController
238 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
241 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
242 if (getDefault() != null) {
243 return getDefault().getApplicationConfiguration();
253 * Creates a new conversation, binds resources to the conversation and start
254 * a transaction for this conversation.
256 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
259 public static ConversationHolder
createConversation() {
260 ConversationHolder conversation
= getCurrentApplicationConfiguration()
263 conversation
.startTransaction();
265 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
270 //FIXME:Remoting should be removed after moving completely to remoting
271 private MockCdmEntitySessionManager mockCdmEntitySessionManager
;
273 private ICdmEntitySessionManager
getSessionManager() {
274 //FIXME:Remoting we should only have CdmApplicationRemoteConfiguration after move to remoting
275 // bad hack which should be finally removed
276 if(getCurrentApplicationConfiguration() instanceof CdmApplicationRemoteController
) {
277 return ((CdmApplicationRemoteController
)getCurrentApplicationConfiguration()).getCdmEntitySessionManager();
279 if(mockCdmEntitySessionManager
== null) {
280 mockCdmEntitySessionManager
= new MockCdmEntitySessionManager();
282 return mockCdmEntitySessionManager
;
286 public static ICdmEntitySessionManager
getCurrentSessionManager() {
287 if (getDefault() != null) {
288 return getDefault().getSessionManager();
295 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
296 * interface. If a matching getter is found the according service implementation is returned by
297 * invoking the getter otherwise the method returns <code>null</code>.
300 * @param serviceClass
301 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
303 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
304 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
306 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
310 for (Method method
: methods
) {
311 Type type
= method
.getGenericReturnType();
313 if (type
.equals(serviceClass
)) {
315 service
= (T
) method
.invoke(configuration
, null);
317 } catch (IllegalArgumentException e
) {
318 MessagingUtils
.error(CdmStore
.class, e
);
319 } catch (IllegalAccessException e
) {
320 MessagingUtils
.error(CdmStore
.class, e
);
321 } catch (InvocationTargetException e
) {
322 MessagingUtils
.error(CdmStore
.class, e
);
331 * @see #getService(Class)
332 * As ICommonService is not extending IService we need a specific request here
334 public static ICommonService
getCommonService() {
335 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
337 return configuration
.getCommonService();
343 * getAuthenticationManager
347 * {@link org.springframework.security.authentication.ProviderManager}
350 public static AuthenticationManager
getAuthenticationManager() {
351 return getCurrentApplicationConfiguration().getAuthenticationManager();
356 * getAuthenticationManager
360 * {@link ICdmPermissionEvaluator} object.
362 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
363 return getCurrentApplicationConfiguration().getPermissionEvaluator();
371 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
373 public static IEditGeoService
getGeoService() {
374 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
379 * SECURITY RELATED CONVENIENCE METHODS
383 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
385 * @param targetDomainObject
389 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
390 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
391 SecurityContext context
= SecurityContextHolder
.getContext();
392 PermissionEvaluator pe
= getPermissionEvaluator();
393 boolean hasPermission
= false;
395 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
397 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
400 return hasPermission
;
404 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
406 * @param targetDomainObject
410 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
411 boolean hasPermission
= false;
413 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
414 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
417 return hasPermission
;
420 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
421 boolean hasPermission
= false;
423 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
424 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
427 return hasPermission
;
430 public static Authentication
getCurrentAuthentiation() {
431 SecurityContext context
= SecurityContextHolder
.getContext();
432 return context
.getAuthentication();
440 * Provides access to the global default language set in the application preferences.
442 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
444 public static Language
getDefaultLanguage() {
445 if (getDefault().getLanguage() == null) {
446 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
448 return getDefault().getLanguage();
457 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
459 public static void setDefaultLanguage(Language language
) {
460 getDefault().setLanguage(language
);
464 * @return the language
466 private Language
getLanguage() {
472 * the language to set
474 private void setLanguage(Language language
) {
475 this.language
= language
;
484 * Getter for the field <code>loginManager</code>.
487 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
489 public static LoginManager
getLoginManager() {
495 * Getter for the field <code>contextManager</code>.
498 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
500 public static ContextManager
getContextManager() {
501 return contextManager
;
504 public static TermManager
getTermManager() {
508 public static SearchManager
getSearchManager() {
509 return searchManager
;
512 public static EditorManager
getEditorManager() {
513 return editorManager
;
517 * IMPORT/EXPORT FACTORIES
522 * Getter for the field <code>importHandler</code>.
525 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
527 public static ImportManager
getImportManager() {
528 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
533 * Getter for the field <code>exportHandler</code>.
536 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
538 public static ExportManager
getExportManager() {
539 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
543 * Whether this CdmStore is currently connected to a datasource
547 public static boolean isActive() {
548 return instance
!= null && instance
.isConnected
;
551 public static ICdmSource
getActiveCdmSource() {
553 return instance
.getCdmSource();
563 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
564 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
566 // public static ICdmDataSource getDataSource() {
568 // return (ICdmDataSource)instance.getCdmSource();
576 private ICdmSource
getCdmSource() {