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
.ICdmApplicationConfiguration
;
29 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
30 import eu
.etaxonomy
.cdm
.api
.service
.ICommonService
;
31 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
32 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
33 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
34 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
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
.cdm
.remote
.CdmRemoteSourceException
;
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
.store
.internal
.TaxeditorStorePlugin
;
48 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
51 * This implementation of ICdmDataRepository depends on hibernate sessions to
52 * store the data correctly for the current session. No state is held in this
55 * Only methods that either get or manipulate data are exposed here. So this
56 * class acts as a facade for the methods in cdmlib-service.
62 public class CdmStore
{
64 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
65 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
66 TaxeditorStorePlugin
.class);
67 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
69 private static CdmStore instance
;
71 private final ICdmApplicationConfiguration applicationConfiguration
;
73 private static ContextManager contextManager
= new ContextManager();
75 private static LoginManager loginManager
= new LoginManager();
77 private static TermManager termManager
= new TermManager();
79 private static SearchManager searchManager
= new SearchManager();
81 private static EditorManager editorManager
= new EditorManager();
83 private static UseObjectStore useObjectInitializer
= new UseObjectStore();
85 private static CdmStoreConnector job
;
87 private Language language
;
89 private ICdmSource cdmSource
;
91 private boolean isConnected
;
98 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
100 protected static CdmStore
getDefault() {
101 if (instance
!= null && instance
.isConnected
) {
103 } else{// if (instance == null || !instance.isConnected) {
105 MessagingUtils
.noDataSourceWarningDialog(instance
);
107 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
113 * Initialize the with the last edited datasource
115 public static void connect() {
117 ICdmSource cdmSource
;
119 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
121 } catch (CdmRemoteSourceException e
) {
122 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
129 * Initialize with a specific datasource
132 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
134 public static void connect(ICdmSource cdmSource
) {
135 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
136 DEFAULT_APPLICATION_CONTEXT
);
140 * Initialize and provide
143 * @param dbSchemaValidation
144 * @param applicationContextBean
146 private static void connect(final ICdmSource cdmSource
,
147 final DbSchemaValidation dbSchemaValidation
,
148 final Resource applicationContextBean
) {
149 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
151 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
152 dbSchemaValidation
, applicationContextBean
);
154 job
.setPriority(Job
.BUILD
);
159 public static boolean isConnecting() {
160 return job
!= null && job
.getState() == Job
.RUNNING
;
164 * Closes the current application context
167 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
169 public static void close(final IProgressMonitor monitor
) {
170 Display
.getDefault().asyncExec(new Runnable() {
174 * @see java.lang.Runnable#run()
178 getContextManager().notifyContextAboutToStop(monitor
);
179 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
180 getContextManager().notifyContextStop(monitor
);
187 private void close() {
192 static void setInstance(ICdmApplicationConfiguration applicationController
,
193 ICdmSource cdmSource
) {
194 instance
= new CdmStore(applicationController
, cdmSource
);
197 private CdmStore(ICdmApplicationConfiguration applicationController
,
198 ICdmSource cdmSource
) {
199 this.applicationConfiguration
= applicationController
;
200 this.cdmSource
= cdmSource
;
205 * All calls to the datastore require
209 private ICdmApplicationConfiguration
getApplicationConfiguration() {
211 return applicationConfiguration
;
212 } catch (Exception e
) {
213 MessagingUtils
.error(CdmStore
.class, e
);
220 * getCurrentApplicationController
224 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
227 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
228 if (getDefault() != null) {
229 return getDefault().getApplicationConfiguration();
239 * Creates a new conversation, binds resources to the conversation and start
240 * a transaction for this conversation.
242 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
245 public static ConversationHolder
createConversation() {
246 ConversationHolder conversation
= getCurrentApplicationConfiguration()
249 conversation
.startTransaction();
251 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
257 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
258 * interface. If a matching getter is found the according service implementation is returned by
259 * invoking the getter otherwise the method returns <code>null</code>.
262 * @param serviceClass
263 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
265 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
266 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
268 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
272 for (Method method
: methods
) {
273 Type type
= method
.getGenericReturnType();
275 if (type
.equals(serviceClass
)) {
277 service
= (T
) method
.invoke(configuration
, null);
279 } catch (IllegalArgumentException e
) {
280 MessagingUtils
.error(CdmStore
.class, e
);
281 } catch (IllegalAccessException e
) {
282 MessagingUtils
.error(CdmStore
.class, e
);
283 } catch (InvocationTargetException e
) {
284 MessagingUtils
.error(CdmStore
.class, e
);
293 * @see #getService(Class)
294 * As ICommonService is not extending IService we need a specific request here
296 public static ICommonService
getCommonService() {
297 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
299 return configuration
.getCommonService();
305 * getAuthenticationManager
309 * {@link org.springframework.security.authentication.ProviderManager}
312 public static ProviderManager
getAuthenticationManager() {
313 return getCurrentApplicationConfiguration().getAuthenticationManager();
318 * getAuthenticationManager
322 * {@link ICdmPermissionEvaluator} object.
324 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
325 return getCurrentApplicationConfiguration().getPermissionEvaluator();
333 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
335 public static IEditGeoService
getGeoService() {
336 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
341 * SECURITY RELATED CONVENIENCE METHODS
345 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
347 * @param targetDomainObject
351 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
352 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
353 SecurityContext context
= SecurityContextHolder
.getContext();
354 PermissionEvaluator pe
= getPermissionEvaluator();
355 boolean hasPermission
= false;
357 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
359 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
362 return hasPermission
;
366 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
368 * @param targetDomainObject
372 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
373 boolean hasPermission
= false;
375 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
376 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
379 return hasPermission
;
382 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
383 boolean hasPermission
= false;
385 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
386 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
389 return hasPermission
;
392 public static Authentication
getCurrentAuthentiation() {
393 SecurityContext context
= SecurityContextHolder
.getContext();
394 return context
.getAuthentication();
402 * Provides access to the global default language set in the application preferences.
404 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
406 public static Language
getDefaultLanguage() {
407 if (getDefault().getLanguage() == null) {
408 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
410 return getDefault().getLanguage();
419 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
421 public static void setDefaultLanguage(Language language
) {
422 getDefault().setLanguage(language
);
426 * @return the language
428 private Language
getLanguage() {
434 * the language to set
436 private void setLanguage(Language language
) {
437 this.language
= language
;
446 * Getter for the field <code>loginManager</code>.
449 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
451 public static LoginManager
getLoginManager() {
457 * Getter for the field <code>contextManager</code>.
460 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
462 public static ContextManager
getContextManager() {
463 return contextManager
;
466 public static TermManager
getTermManager() {
470 public static SearchManager
getSearchManager() {
471 return searchManager
;
474 public static EditorManager
getEditorManager() {
475 return editorManager
;
479 * IMPORT/EXPORT FACTORIES
484 * Getter for the field <code>importHandler</code>.
487 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
489 public static ImportManager
getImportManager() {
490 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
495 * Getter for the field <code>exportHandler</code>.
498 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
500 public static ExportManager
getExportManager() {
501 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
505 * Whether this CdmStore is currently connected to a datasource
509 public static boolean isActive() {
510 return instance
!= null && instance
.isConnected
;
513 public static ICdmSource
getActiveCdmSource() {
515 return instance
.getCdmSource();
525 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
526 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
528 // public static ICdmDataSource getDataSource() {
530 // return (ICdmDataSource)instance.getCdmSource();
538 private ICdmSource
getCdmSource() {