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
.hibernate
.collection
.internal
.AbstractPersistentCollection
;
21 import org
.hibernate
.proxy
.AbstractLazyInitializer
;
22 import org
.springframework
.core
.io
.ClassPathResource
;
23 import org
.springframework
.core
.io
.Resource
;
24 import org
.springframework
.security
.access
.PermissionEvaluator
;
25 import org
.springframework
.security
.authentication
.ProviderManager
;
26 import org
.springframework
.security
.core
.Authentication
;
27 import org
.springframework
.security
.core
.context
.SecurityContext
;
28 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
30 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
31 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
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
.database
.ICdmDataSource
;
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
.cdm
.remote
.CdmRemoteSourceException
;
43 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
44 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
45 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
46 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
47 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
48 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
49 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
52 * This implementation of ICdmDataRepository depends on hibernate sessions to
53 * store the data correctly for the current session. No state is held in this
56 * Only methods that either get or manipulate data are exposed here. So this
57 * class acts as a facade for the methods in cdmlib-service.
63 public class CdmStore
{
65 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
66 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
67 TaxeditorStorePlugin
.class);
68 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
70 private static CdmStore instance
;
72 private final ICdmApplicationConfiguration applicationConfiguration
;
74 private static ContextManager contextManager
= new ContextManager();
76 private static LoginManager loginManager
= new LoginManager();
78 private static TermManager termManager
= new TermManager();
80 private static SearchManager searchManager
= new SearchManager();
82 private static EditorManager editorManager
= new EditorManager();
84 private static CdmStoreConnector job
;
86 private Language language
;
88 private ICdmSource cdmSource
;
90 private boolean isConnected
;
97 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
99 protected static CdmStore
getDefault() {
100 if (instance
!= null && instance
.isConnected
) {
102 } else if (instance
== null || !instance
.isConnected
) {
106 "Application is not connected to a datastore",
108 "The requested operation is only available when "
109 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
111 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
115 throw new RuntimeException();
119 * Initialize the with the last edited datasource
121 public static void connect() {
123 ICdmSource cdmSource
;
125 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
127 } catch (CdmRemoteSourceException e
) {
128 StoreUtil
.errorDialog("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
) {
155 StoreUtil
.info("Connecting to datasource: " + cdmSource
);
157 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
158 dbSchemaValidation
, applicationContextBean
);
160 job
.setPriority(Job
.BUILD
);
165 public static boolean isConnecting() {
166 return job
!= null && job
.getState() == Job
.RUNNING
;
170 * Closes the current application context
173 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
175 public static void close(final IProgressMonitor monitor
) {
176 Display
.getDefault().asyncExec(new Runnable() {
180 * @see java.lang.Runnable#run()
184 getContextManager().notifyContextAboutToStop(monitor
);
185 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
186 getContextManager().notifyContextStop(monitor
);
193 private void close() {
198 static void setInstance(ICdmApplicationConfiguration applicationController
,
199 ICdmSource cdmSource
) {
200 instance
= new CdmStore(applicationController
, cdmSource
);
203 private CdmStore(ICdmApplicationConfiguration applicationController
,
204 ICdmSource cdmSource
) {
205 this.applicationConfiguration
= applicationController
;
206 this.cdmSource
= cdmSource
;
211 * All calls to the datastore require
215 private ICdmApplicationConfiguration
getApplicationConfiguration() {
217 return applicationConfiguration
;
218 } catch (Exception e
) {
219 StoreUtil
.error(CdmStore
.class, e
);
226 * getCurrentApplicationController
230 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
233 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
234 if (getDefault() != null) {
235 return getDefault().getApplicationConfiguration();
245 * Creates a new conversation, binds resources to the conversation and start
246 * a transaction for this conversation.
248 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
251 public static ConversationHolder
createConversation() {
252 ConversationHolder conversation
= getCurrentApplicationConfiguration()
255 conversation
.startTransaction();
257 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
263 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
264 * interface. If a matching getter is found the according service implementation is returned by
265 * invoking the getter otherwise the method returns <code>null</code>.
268 * @param serviceClass
269 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
271 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
272 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
274 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
278 for (Method method
: methods
) {
279 Type type
= method
.getGenericReturnType();
281 if (type
.equals(serviceClass
)) {
283 service
= (T
) method
.invoke(configuration
, null);
285 } catch (IllegalArgumentException e
) {
286 StoreUtil
.error(CdmStore
.class, e
);
287 } catch (IllegalAccessException e
) {
288 StoreUtil
.error(CdmStore
.class, e
);
289 } catch (InvocationTargetException e
) {
290 StoreUtil
.error(CdmStore
.class, e
);
300 * getAuthenticationManager
304 * {@link org.springframework.security.authentication.ProviderManager}
307 public static ProviderManager
getAuthenticationManager() {
308 return getCurrentApplicationConfiguration().getAuthenticationManager();
313 * getAuthenticationManager
317 * {@link ICdmPermissionEvaluator} object.
319 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
320 return getCurrentApplicationConfiguration().getPermissionEvaluator();
328 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
330 public static IEditGeoService
getGeoService() {
331 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
336 * SECURITY RELATED CONVENIENCE METHODS
340 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
342 * @param targetDomainObject
346 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
347 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
348 SecurityContext context
= SecurityContextHolder
.getContext();
349 PermissionEvaluator pe
= getPermissionEvaluator();
350 boolean hasPermission
= false;
352 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
354 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
357 return hasPermission
;
361 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
363 * @param targetDomainObject
367 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
368 boolean hasPermission
= false;
370 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
371 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
374 return hasPermission
;
377 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
378 boolean hasPermission
= false;
380 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
381 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
384 return hasPermission
;
387 public static Authentication
getCurrentAuthentiation() {
388 SecurityContext context
= SecurityContextHolder
.getContext();
389 return context
.getAuthentication();
397 * Provides access to the global default language set in the application preferences.
399 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
401 public static Language
getDefaultLanguage() {
402 if (getDefault().getLanguage() == null) {
403 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
405 return getDefault().getLanguage();
414 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
416 public static void setDefaultLanguage(Language language
) {
417 getDefault().setLanguage(language
);
421 * @return the language
423 private Language
getLanguage() {
429 * the language to set
431 private void setLanguage(Language language
) {
432 this.language
= language
;
441 * Getter for the field <code>loginManager</code>.
444 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
446 public static LoginManager
getLoginManager() {
452 * Getter for the field <code>contextManager</code>.
455 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
457 public static ContextManager
getContextManager() {
458 return contextManager
;
461 public static TermManager
getTermManager() {
465 public static SearchManager
getSearchManager() {
466 return searchManager
;
469 public static EditorManager
getEditorManager() {
470 return editorManager
;
474 * IMPORT/EXPORT FACTORIES
479 * Getter for the field <code>importHandler</code>.
482 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
484 public static ImportManager
getImportManager() {
485 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
490 * Getter for the field <code>exportHandler</code>.
493 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
495 public static ExportManager
getExportManager() {
496 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
500 * Whether this CdmStore is currently connected to a datasource
504 public static boolean isActive() {
505 return instance
!= null && instance
.isConnected
;
508 public static ICdmSource
getActiveCdmSource() {
510 return instance
.getCdmSource();
520 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
521 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
523 // public static ICdmDataSource getDataSource() {
525 // return (ICdmDataSource)instance.getCdmSource();
533 private ICdmSource
getCdmSource() {