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
.taxeditor
.datasource
.CdmDataSourceRepository
;
43 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
44 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
45 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
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
) {
105 "Application is not connected to a datastore",
107 "The requested operation is only available when "
108 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
110 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
114 throw new RuntimeException();
118 * Initialize the with the last edited datasource
120 public static void connect() {
122 ICdmDataSource datasource
= CdmDataSourceRepository
123 .getCurrentDataSource();
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 StoreUtil
.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 AbstractLazyInitializer
.setConfiguration(applicationController
);
201 AbstractPersistentCollection
.setConfiguration(applicationController
);
202 this.cdmSource
= cdmSource
;
207 * All calls to the datastore require
211 private ICdmApplicationConfiguration
getApplicationConfiguration() {
213 return applicationConfiguration
;
214 } catch (Exception e
) {
215 StoreUtil
.error(CdmStore
.class, e
);
222 * getCurrentApplicationController
226 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
229 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
230 if (getDefault() != null) {
231 return getDefault().getApplicationConfiguration();
241 * Creates a new conversation, binds resources to the conversation and start
242 * a transaction for this conversation.
244 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
247 public static ConversationHolder
createConversation() {
248 ConversationHolder conversation
= getCurrentApplicationConfiguration()
251 conversation
.startTransaction();
253 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
259 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
260 * interface. If a matching getter is found the according service implementation is returned by
261 * invoking the getter otherwise the method returns <code>null</code>.
264 * @param serviceClass
265 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
267 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
268 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
270 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
274 for (Method method
: methods
) {
275 Type type
= method
.getGenericReturnType();
277 if (type
.equals(serviceClass
)) {
279 service
= (T
) method
.invoke(configuration
, null);
281 } catch (IllegalArgumentException e
) {
282 StoreUtil
.error(CdmStore
.class, e
);
283 } catch (IllegalAccessException e
) {
284 StoreUtil
.error(CdmStore
.class, e
);
285 } catch (InvocationTargetException e
) {
286 StoreUtil
.error(CdmStore
.class, e
);
296 * getAuthenticationManager
300 * {@link org.springframework.security.authentication.ProviderManager}
303 public static ProviderManager
getAuthenticationManager() {
304 return getCurrentApplicationConfiguration().getAuthenticationManager();
309 * getAuthenticationManager
313 * {@link ICdmPermissionEvaluator} object.
315 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
316 return getCurrentApplicationConfiguration().getPermissionEvaluator();
324 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
326 public static IEditGeoService
getGeoService() {
327 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
332 * SECURITY RELATED CONVENIENCE METHODS
336 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
338 * @param targetDomainObject
342 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
343 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
344 SecurityContext context
= SecurityContextHolder
.getContext();
345 PermissionEvaluator pe
= getPermissionEvaluator();
346 boolean hasPermission
= false;
348 hasPermission
= getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
,
350 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
353 return hasPermission
;
357 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
359 * @param targetDomainObject
363 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
364 boolean hasPermission
= false;
366 hasPermission
= getPermissionEvaluator().hasPermission(getCurrentAuthentiation(), null, targetType
.getName(), permission
);
367 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
370 return hasPermission
;
373 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
374 boolean hasPermission
= false;
376 hasPermission
= getPermissionEvaluator().hasOneOfRoles(getCurrentAuthentiation(), roles
);
377 } catch (org
.springframework
.security
.access
.AccessDeniedException e
) {
380 return hasPermission
;
383 public static Authentication
getCurrentAuthentiation() {
384 SecurityContext context
= SecurityContextHolder
.getContext();
385 return context
.getAuthentication();
393 * Provides access to the global default language set in the application preferences.
395 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
397 public static Language
getDefaultLanguage() {
398 if (getDefault().getLanguage() == null) {
399 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
401 return getDefault().getLanguage();
410 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
412 public static void setDefaultLanguage(Language language
) {
413 getDefault().setLanguage(language
);
417 * @return the language
419 private Language
getLanguage() {
425 * the language to set
427 private void setLanguage(Language language
) {
428 this.language
= language
;
437 * Getter for the field <code>loginManager</code>.
440 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
442 public static LoginManager
getLoginManager() {
448 * Getter for the field <code>contextManager</code>.
451 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
453 public static ContextManager
getContextManager() {
454 return contextManager
;
457 public static TermManager
getTermManager() {
461 public static SearchManager
getSearchManager() {
462 return searchManager
;
465 public static EditorManager
getEditorManager() {
466 return editorManager
;
470 * IMPORT/EXPORT FACTORIES
475 * Getter for the field <code>importHandler</code>.
478 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
480 public static ImportManager
getImportManager() {
481 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
486 * Getter for the field <code>exportHandler</code>.
489 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
491 public static ExportManager
getExportManager() {
492 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
496 * Whether this CdmStore is currently connected to a datasource
500 public static boolean isActive() {
501 return instance
!= null && instance
.isConnected
;
504 public static ICdmSource
getActiveCdmSource() {
506 return instance
.getCdmSource();
516 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
517 * @deprecated currently retained for backward compatibility - use {@link getActiveCdmSource()} instead
519 public static ICdmDataSource
getDataSource() {
521 return (ICdmDataSource
)instance
.getCdmSource();
529 private ICdmSource
getCdmSource() {