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
.model
.MessagingUtils
;
48 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
49 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
50 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
53 * This implementation of ICdmDataRepository depends on hibernate sessions to
54 * store the data correctly for the current session. No state is held in this
57 * Only methods that either get or manipulate data are exposed here. So this
58 * class acts as a facade for the methods in cdmlib-service.
64 public class CdmStore
{
66 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
67 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
68 TaxeditorStorePlugin
.class);
69 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
71 private static CdmStore instance
;
73 private final ICdmApplicationConfiguration applicationConfiguration
;
75 private static ContextManager contextManager
= new ContextManager();
77 private static LoginManager loginManager
= new LoginManager();
79 private static TermManager termManager
= new TermManager();
81 private static SearchManager searchManager
= new SearchManager();
83 private static EditorManager editorManager
= new EditorManager();
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
) {
107 "Application is not connected to a datastore",
109 "The requested operation is only available when "
110 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
112 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
116 throw new RuntimeException();
120 * Initialize the with the last edited datasource
122 public static void connect() {
124 ICdmSource cdmSource
;
126 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
128 } catch (CdmRemoteSourceException 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
) {
156 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
158 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
159 dbSchemaValidation
, applicationContextBean
);
161 job
.setPriority(Job
.BUILD
);
166 public static boolean isConnecting() {
167 return job
!= null && job
.getState() == Job
.RUNNING
;
171 * Closes the current application context
174 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
176 public static void close(final IProgressMonitor monitor
) {
177 Display
.getDefault().asyncExec(new Runnable() {
181 * @see java.lang.Runnable#run()
185 getContextManager().notifyContextAboutToStop(monitor
);
186 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
187 getContextManager().notifyContextStop(monitor
);
194 private void close() {
199 static void setInstance(ICdmApplicationConfiguration applicationController
,
200 ICdmSource cdmSource
) {
201 instance
= new CdmStore(applicationController
, cdmSource
);
204 private CdmStore(ICdmApplicationConfiguration applicationController
,
205 ICdmSource cdmSource
) {
206 this.applicationConfiguration
= applicationController
;
207 this.cdmSource
= cdmSource
;
212 * All calls to the datastore require
216 private ICdmApplicationConfiguration
getApplicationConfiguration() {
218 return applicationConfiguration
;
219 } catch (Exception e
) {
220 MessagingUtils
.error(CdmStore
.class, e
);
227 * getCurrentApplicationController
231 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
234 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
235 if (getDefault() != null) {
236 return getDefault().getApplicationConfiguration();
246 * Creates a new conversation, binds resources to the conversation and start
247 * a transaction for this conversation.
249 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
252 public static ConversationHolder
createConversation() {
253 ConversationHolder conversation
= getCurrentApplicationConfiguration()
256 conversation
.startTransaction();
258 MessagingUtils
.messageDialog("No database connection", CdmStore
.class, "No database connection available", e
);
264 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
265 * interface. If a matching getter is found the according service implementation is returned by
266 * invoking the getter otherwise the method returns <code>null</code>.
269 * @param serviceClass
270 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
272 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
273 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
275 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
279 for (Method method
: methods
) {
280 Type type
= method
.getGenericReturnType();
282 if (type
.equals(serviceClass
)) {
284 service
= (T
) method
.invoke(configuration
, null);
286 } catch (IllegalArgumentException e
) {
287 MessagingUtils
.error(CdmStore
.class, e
);
288 } catch (IllegalAccessException e
) {
289 MessagingUtils
.error(CdmStore
.class, e
);
290 } catch (InvocationTargetException e
) {
291 MessagingUtils
.error(CdmStore
.class, e
);
301 * getAuthenticationManager
305 * {@link org.springframework.security.authentication.ProviderManager}
308 public static ProviderManager
getAuthenticationManager() {
309 return getCurrentApplicationConfiguration().getAuthenticationManager();
314 * getAuthenticationManager
318 * {@link ICdmPermissionEvaluator} object.
320 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
321 return getCurrentApplicationConfiguration().getPermissionEvaluator();
329 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
331 public static IEditGeoService
getGeoService() {
332 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
337 * SECURITY RELATED CONVENIENCE METHODS
341 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
343 * @param targetDomainObject
347 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
348 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
349 SecurityContext context
= SecurityContextHolder
.getContext();
350 PermissionEvaluator pe
= getPermissionEvaluator();
351 boolean hasPermission
= false;
353 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
355 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
358 return hasPermission
;
362 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
364 * @param targetDomainObject
368 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
369 boolean hasPermission
= false;
371 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
372 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
375 return hasPermission
;
378 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
379 boolean hasPermission
= false;
381 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
382 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
385 return hasPermission
;
388 public static Authentication
getCurrentAuthentiation() {
389 SecurityContext context
= SecurityContextHolder
.getContext();
390 return context
.getAuthentication();
398 * Provides access to the global default language set in the application preferences.
400 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
402 public static Language
getDefaultLanguage() {
403 if (getDefault().getLanguage() == null) {
404 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
406 return getDefault().getLanguage();
415 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
417 public static void setDefaultLanguage(Language language
) {
418 getDefault().setLanguage(language
);
422 * @return the language
424 private Language
getLanguage() {
430 * the language to set
432 private void setLanguage(Language language
) {
433 this.language
= language
;
442 * Getter for the field <code>loginManager</code>.
445 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
447 public static LoginManager
getLoginManager() {
453 * Getter for the field <code>contextManager</code>.
456 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
458 public static ContextManager
getContextManager() {
459 return contextManager
;
462 public static TermManager
getTermManager() {
466 public static SearchManager
getSearchManager() {
467 return searchManager
;
470 public static EditorManager
getEditorManager() {
471 return editorManager
;
475 * IMPORT/EXPORT FACTORIES
480 * Getter for the field <code>importHandler</code>.
483 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
485 public static ImportManager
getImportManager() {
486 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
491 * Getter for the field <code>exportHandler</code>.
494 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
496 public static ExportManager
getExportManager() {
497 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
501 * Whether this CdmStore is currently connected to a datasource
505 public static boolean isActive() {
506 return instance
!= null && instance
.isConnected
;
509 public static ICdmSource
getActiveCdmSource() {
511 return instance
.getCdmSource();
521 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
522 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
524 // public static ICdmDataSource getDataSource() {
526 // return (ICdmDataSource)instance.getCdmSource();
534 private ICdmSource
getCdmSource() {