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
.IService
;
31 import eu
.etaxonomy
.cdm
.config
.ICdmSource
;
32 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
33 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
34 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
35 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
39 import eu
.etaxonomy
.cdm
.remote
.CdmRemoteSourceException
;
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
.store
.internal
.TaxeditorStorePlugin
;
47 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
50 * This implementation of ICdmDataRepository depends on hibernate sessions to
51 * store the data correctly for the current session. No state is held in this
54 * Only methods that either get or manipulate data are exposed here. So this
55 * class acts as a facade for the methods in cdmlib-service.
61 public class CdmStore
{
63 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
64 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
65 TaxeditorStorePlugin
.class);
66 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
68 private static CdmStore instance
;
70 private final ICdmApplicationConfiguration applicationConfiguration
;
72 private static ContextManager contextManager
= new ContextManager();
74 private static LoginManager loginManager
= new LoginManager();
76 private static TermManager termManager
= new TermManager();
78 private static SearchManager searchManager
= new SearchManager();
80 private static EditorManager editorManager
= new EditorManager();
82 private static CdmStoreConnector job
;
84 private Language language
;
86 private ICdmSource cdmSource
;
88 private boolean isConnected
;
95 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
97 protected static CdmStore
getDefault() {
98 if (instance
!= null && instance
.isConnected
) {
100 } else{// if (instance == null || !instance.isConnected) {
102 MessagingUtils
.noDataSourceWarningDialog(instance
);
104 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
110 * Initialize the with the last edited datasource
112 public static void connect() {
114 ICdmSource cdmSource
;
116 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
118 } catch (CdmRemoteSourceException e
) {
119 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
126 * Initialize with a specific datasource
129 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
131 public static void connect(ICdmSource cdmSource
) {
132 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
133 DEFAULT_APPLICATION_CONTEXT
);
137 * Initialize and provide
140 * @param dbSchemaValidation
141 * @param applicationContextBean
143 private static void connect(final ICdmSource cdmSource
,
144 final DbSchemaValidation dbSchemaValidation
,
145 final Resource applicationContextBean
) {
146 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
148 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
149 dbSchemaValidation
, applicationContextBean
);
151 job
.setPriority(Job
.BUILD
);
156 public static boolean isConnecting() {
157 return job
!= null && job
.getState() == Job
.RUNNING
;
161 * Closes the current application context
164 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
166 public static void close(final IProgressMonitor monitor
) {
167 Display
.getDefault().asyncExec(new Runnable() {
171 * @see java.lang.Runnable#run()
175 getContextManager().notifyContextAboutToStop(monitor
);
176 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
177 getContextManager().notifyContextStop(monitor
);
184 private void close() {
189 static void setInstance(ICdmApplicationConfiguration applicationController
,
190 ICdmSource cdmSource
) {
191 instance
= new CdmStore(applicationController
, cdmSource
);
194 private CdmStore(ICdmApplicationConfiguration applicationController
,
195 ICdmSource cdmSource
) {
196 this.applicationConfiguration
= applicationController
;
197 this.cdmSource
= cdmSource
;
202 * All calls to the datastore require
206 private ICdmApplicationConfiguration
getApplicationConfiguration() {
208 return applicationConfiguration
;
209 } catch (Exception e
) {
210 MessagingUtils
.error(CdmStore
.class, e
);
217 * getCurrentApplicationController
221 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
224 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
225 if (getDefault() != null) {
226 return getDefault().getApplicationConfiguration();
236 * Creates a new conversation, binds resources to the conversation and start
237 * a transaction for this conversation.
239 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
242 public static ConversationHolder
createConversation() {
243 ConversationHolder conversation
= getCurrentApplicationConfiguration()
246 conversation
.startTransaction();
248 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
254 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
255 * interface. If a matching getter is found the according service implementation is returned by
256 * invoking the getter otherwise the method returns <code>null</code>.
259 * @param serviceClass
260 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
262 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
263 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
265 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
269 for (Method method
: methods
) {
270 Type type
= method
.getGenericReturnType();
272 if (type
.equals(serviceClass
)) {
274 service
= (T
) method
.invoke(configuration
, null);
276 } catch (IllegalArgumentException e
) {
277 MessagingUtils
.error(CdmStore
.class, e
);
278 } catch (IllegalAccessException e
) {
279 MessagingUtils
.error(CdmStore
.class, e
);
280 } catch (InvocationTargetException e
) {
281 MessagingUtils
.error(CdmStore
.class, e
);
291 * getAuthenticationManager
295 * {@link org.springframework.security.authentication.ProviderManager}
298 public static ProviderManager
getAuthenticationManager() {
299 return getCurrentApplicationConfiguration().getAuthenticationManager();
304 * getAuthenticationManager
308 * {@link ICdmPermissionEvaluator} object.
310 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
311 return getCurrentApplicationConfiguration().getPermissionEvaluator();
319 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
321 public static IEditGeoService
getGeoService() {
322 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
327 * SECURITY RELATED CONVENIENCE METHODS
331 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
333 * @param targetDomainObject
337 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
338 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
339 SecurityContext context
= SecurityContextHolder
.getContext();
340 PermissionEvaluator pe
= getPermissionEvaluator();
341 boolean hasPermission
= false;
343 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
345 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
348 return hasPermission
;
352 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
354 * @param targetDomainObject
358 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
359 boolean hasPermission
= false;
361 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
362 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
365 return hasPermission
;
368 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
369 boolean hasPermission
= false;
371 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
372 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
375 return hasPermission
;
378 public static Authentication
getCurrentAuthentiation() {
379 SecurityContext context
= SecurityContextHolder
.getContext();
380 return context
.getAuthentication();
388 * Provides access to the global default language set in the application preferences.
390 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
392 public static Language
getDefaultLanguage() {
393 if (getDefault().getLanguage() == null) {
394 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
396 return getDefault().getLanguage();
405 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
407 public static void setDefaultLanguage(Language language
) {
408 getDefault().setLanguage(language
);
412 * @return the language
414 private Language
getLanguage() {
420 * the language to set
422 private void setLanguage(Language language
) {
423 this.language
= language
;
432 * Getter for the field <code>loginManager</code>.
435 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
437 public static LoginManager
getLoginManager() {
443 * Getter for the field <code>contextManager</code>.
446 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
448 public static ContextManager
getContextManager() {
449 return contextManager
;
452 public static TermManager
getTermManager() {
456 public static SearchManager
getSearchManager() {
457 return searchManager
;
460 public static EditorManager
getEditorManager() {
461 return editorManager
;
465 * IMPORT/EXPORT FACTORIES
470 * Getter for the field <code>importHandler</code>.
473 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
475 public static ImportManager
getImportManager() {
476 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
481 * Getter for the field <code>exportHandler</code>.
484 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
486 public static ExportManager
getExportManager() {
487 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
491 * Whether this CdmStore is currently connected to a datasource
495 public static boolean isActive() {
496 return instance
!= null && instance
.isConnected
;
499 public static ICdmSource
getActiveCdmSource() {
501 return instance
.getCdmSource();
511 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
512 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
514 // public static ICdmDataSource getDataSource() {
516 // return (ICdmDataSource)instance.getCdmSource();
524 private ICdmSource
getCdmSource() {