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) {
104 "Application is not connected to a datastore",
106 "The requested operation is only available when "
107 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
109 AbstractUtility
.showView(CdmDataSourceViewPart
.ID
);
115 * Initialize the with the last edited datasource
117 public static void connect() {
119 ICdmSource cdmSource
;
121 cdmSource
= CdmDataSourceRepository
.getCurrentCdmSource();
123 } catch (CdmRemoteSourceException e
) {
124 MessagingUtils
.messageDialog("Connection to CDM Source Failed", CdmStore
.class, "Could not connect to target CDM Source", e
);
131 * Initialize with a specific datasource
134 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
136 public static void connect(ICdmSource cdmSource
) {
137 connect(cdmSource
, DEFAULT_DB_SCHEMA_VALIDATION
,
138 DEFAULT_APPLICATION_CONTEXT
);
142 * Initialize and provide
145 * @param dbSchemaValidation
146 * @param applicationContextBean
148 private static void connect(final ICdmSource cdmSource
,
149 final DbSchemaValidation dbSchemaValidation
,
150 final Resource applicationContextBean
) {
151 MessagingUtils
.info("Connecting to datasource: " + cdmSource
);
153 job
= new CdmStoreConnector(Display
.getDefault(), cdmSource
,
154 dbSchemaValidation
, applicationContextBean
);
156 job
.setPriority(Job
.BUILD
);
161 public static boolean isConnecting() {
162 return job
!= null && job
.getState() == Job
.RUNNING
;
166 * Closes the current application context
169 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
171 public static void close(final IProgressMonitor monitor
) {
172 Display
.getDefault().asyncExec(new Runnable() {
176 * @see java.lang.Runnable#run()
180 getContextManager().notifyContextAboutToStop(monitor
);
181 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
182 getContextManager().notifyContextStop(monitor
);
189 private void close() {
194 static void setInstance(ICdmApplicationConfiguration applicationController
,
195 ICdmSource cdmSource
) {
196 instance
= new CdmStore(applicationController
, cdmSource
);
199 private CdmStore(ICdmApplicationConfiguration applicationController
,
200 ICdmSource cdmSource
) {
201 this.applicationConfiguration
= applicationController
;
202 this.cdmSource
= cdmSource
;
207 * All calls to the datastore require
211 private ICdmApplicationConfiguration
getApplicationConfiguration() {
213 return applicationConfiguration
;
214 } catch (Exception e
) {
215 MessagingUtils
.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 MessagingUtils
.messageDialog("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 MessagingUtils
.error(CdmStore
.class, e
);
283 } catch (IllegalAccessException e
) {
284 MessagingUtils
.error(CdmStore
.class, e
);
285 } catch (InvocationTargetException e
) {
286 MessagingUtils
.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() {