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 CdmStoreConnector job
;
85 private Language language
;
87 private ICdmSource cdmSource
;
89 private boolean isConnected
;
96 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
98 protected static CdmStore
getDefault() {
99 if (instance
!= null && instance
.isConnected
) {
101 } else{// if (instance == null || !instance.isConnected) {
103 MessagingUtils
.noDataSourceWarningDialog(instance
);
105 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
111 * Initialize the with the last edited datasource
113 public static void connect() {
115 ICdmSource cdmSource
;
117 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
119 } catch (CdmRemoteSourceException e
) {
120 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
127 * Initialize with a specific datasource
130 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
132 public static void connect(ICdmSource cdmSource
) {
133 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
134 DEFAULT_APPLICATION_CONTEXT
);
138 * Initialize and provide
141 * @param dbSchemaValidation
142 * @param applicationContextBean
144 private static void connect(final ICdmSource cdmSource
,
145 final DbSchemaValidation dbSchemaValidation
,
146 final Resource applicationContextBean
) {
147 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
149 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
150 dbSchemaValidation
, applicationContextBean
);
152 job
.setPriority(Job
.BUILD
);
157 public static boolean isConnecting() {
158 return job
!= null && job
.getState() == Job
.RUNNING
;
162 * Closes the current application context
165 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
167 public static void close(final IProgressMonitor monitor
) {
168 Display
.getDefault().asyncExec(new Runnable() {
172 * @see java.lang.Runnable#run()
176 getContextManager().notifyContextAboutToStop(monitor
);
177 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
178 getContextManager().notifyContextStop(monitor
);
185 private void close() {
190 static void setInstance(ICdmApplicationConfiguration applicationController
,
191 ICdmSource cdmSource
) {
192 instance
= new CdmStore(applicationController
, cdmSource
);
195 private CdmStore(ICdmApplicationConfiguration applicationController
,
196 ICdmSource cdmSource
) {
197 this.applicationConfiguration
= applicationController
;
198 this.cdmSource
= cdmSource
;
203 * All calls to the datastore require
207 private ICdmApplicationConfiguration
getApplicationConfiguration() {
209 return applicationConfiguration
;
210 } catch (Exception e
) {
211 MessagingUtils
.error(CdmStore
.class, e
);
218 * getCurrentApplicationController
222 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
225 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
226 if (getDefault() != null) {
227 return getDefault().getApplicationConfiguration();
237 * Creates a new conversation, binds resources to the conversation and start
238 * a transaction for this conversation.
240 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
243 public static ConversationHolder
createConversation() {
244 ConversationHolder conversation
= getCurrentApplicationConfiguration()
247 conversation
.startTransaction();
249 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
255 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
256 * interface. If a matching getter is found the according service implementation is returned by
257 * invoking the getter otherwise the method returns <code>null</code>.
260 * @param serviceClass
261 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
263 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
264 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
266 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
270 for (Method method
: methods
) {
271 Type type
= method
.getGenericReturnType();
273 if (type
.equals(serviceClass
)) {
275 service
= (T
) method
.invoke(configuration
, null);
277 } catch (IllegalArgumentException e
) {
278 MessagingUtils
.error(CdmStore
.class, e
);
279 } catch (IllegalAccessException e
) {
280 MessagingUtils
.error(CdmStore
.class, e
);
281 } catch (InvocationTargetException e
) {
282 MessagingUtils
.error(CdmStore
.class, e
);
291 * @see #getService(Class)
292 * As ICommonService is not extending IService we need a specific request here
294 public static ICommonService
getCommonService() {
295 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
297 return configuration
.getCommonService();
303 * getAuthenticationManager
307 * {@link org.springframework.security.authentication.ProviderManager}
310 public static ProviderManager
getAuthenticationManager() {
311 return getCurrentApplicationConfiguration().getAuthenticationManager();
316 * getAuthenticationManager
320 * {@link ICdmPermissionEvaluator} object.
322 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
323 return getCurrentApplicationConfiguration().getPermissionEvaluator();
331 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
333 public static IEditGeoService
getGeoService() {
334 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
339 * SECURITY RELATED CONVENIENCE METHODS
343 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
345 * @param targetDomainObject
349 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
350 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
351 SecurityContext context
= SecurityContextHolder
.getContext();
352 PermissionEvaluator pe
= getPermissionEvaluator();
353 boolean hasPermission
= false;
355 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
357 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
360 return hasPermission
;
364 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
366 * @param targetDomainObject
370 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
371 boolean hasPermission
= false;
373 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
374 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
377 return hasPermission
;
380 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
381 boolean hasPermission
= false;
383 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
384 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
387 return hasPermission
;
390 public static Authentication
getCurrentAuthentiation() {
391 SecurityContext context
= SecurityContextHolder
.getContext();
392 return context
.getAuthentication();
400 * Provides access to the global default language set in the application preferences.
402 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
404 public static Language
getDefaultLanguage() {
405 if (getDefault().getLanguage() == null) {
406 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
408 return getDefault().getLanguage();
417 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
419 public static void setDefaultLanguage(Language language
) {
420 getDefault().setLanguage(language
);
424 * @return the language
426 private Language
getLanguage() {
432 * the language to set
434 private void setLanguage(Language language
) {
435 this.language
= language
;
444 * Getter for the field <code>loginManager</code>.
447 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
449 public static LoginManager
getLoginManager() {
455 * Getter for the field <code>contextManager</code>.
458 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
460 public static ContextManager
getContextManager() {
461 return contextManager
;
464 public static TermManager
getTermManager() {
468 public static SearchManager
getSearchManager() {
469 return searchManager
;
472 public static EditorManager
getEditorManager() {
473 return editorManager
;
477 * IMPORT/EXPORT FACTORIES
482 * Getter for the field <code>importHandler</code>.
485 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
487 public static ImportManager
getImportManager() {
488 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
493 * Getter for the field <code>exportHandler</code>.
496 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
498 public static ExportManager
getExportManager() {
499 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
503 * Whether this CdmStore is currently connected to a datasource
507 public static boolean isActive() {
508 return instance
!= null && instance
.isConnected
;
511 public static ICdmSource
getActiveCdmSource() {
513 return instance
.getCdmSource();
523 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
524 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
526 // public static ICdmDataSource getDataSource() {
528 // return (ICdmDataSource)instance.getCdmSource();
536 private ICdmSource
getCdmSource() {