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
.context
.SecurityContext
;
25 import org
.springframework
.security
.core
.context
.SecurityContextHolder
;
27 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
28 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
29 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
30 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
31 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
32 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
33 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
34 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
35 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CRUD
;
36 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.CdmPermissionEvaluator
;
37 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.ICdmPermissionEvaluator
;
38 import eu
.etaxonomy
.cdm
.persistence
.hibernate
.permission
.Role
;
39 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
40 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
41 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
42 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
43 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
44 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
47 * This implementation of ICdmDataRepository depends on hibernate sessions to
48 * store the data correctly for the current session. No state is held in this
51 * Only methods that either get or manipulate data are exposed here. So this
52 * class acts as a facade for the methods in cdmlib-service.
58 public class CdmStore
{
60 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
61 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
62 TaxeditorStorePlugin
.class);
63 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
65 private static CdmStore instance
;
67 private final ICdmApplicationConfiguration applicationConfiguration
;
69 private static ContextManager contextManager
= new ContextManager();
71 private static LoginManager loginManager
= new LoginManager();
73 private static TermManager termManager
= new TermManager();
75 private static SearchManager searchManager
= new SearchManager();
77 private static EditorManager editorManager
= new EditorManager();
79 private static CdmStoreConnector job
;
81 private Language language
;
83 private ICdmDataSource cdmDatasource
;
85 private boolean isConnected
;
92 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
94 protected static CdmStore
getDefault() {
95 if (instance
!= null && instance
.isConnected
) {
97 } else if (instance
== null || !instance
.isConnected
) {
101 "Application is not connected to a datastore",
103 "The requested operation is only available when "
104 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
106 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
110 throw new RuntimeException();
114 * Initialize the with the last edited datasource
116 public static void connect() {
118 ICdmDataSource datasource
= CdmDataSourceRepository
119 .getCurrentDataSource();
125 * Initialize with a specific datasource
128 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
130 public static void connect(ICdmDataSource datasource
) {
131 connect(datasource
, DEFAULT_DB_SCHEMA_VALIDATION
,
132 DEFAULT_APPLICATION_CONTEXT
);
136 * Initialize and provide
139 * @param dbSchemaValidation
140 * @param applicationContextBean
142 private static void connect(final ICdmDataSource datasource
,
143 final DbSchemaValidation dbSchemaValidation
,
144 final Resource applicationContextBean
) {
145 StoreUtil
.info("Connecting to datasource: " + datasource
);
147 job
= new CdmStoreConnector(Display
.getDefault(), datasource
,
148 dbSchemaValidation
, applicationContextBean
);
150 job
.setPriority(Job
.BUILD
);
155 public static boolean isConnecting() {
156 return job
!= null && job
.getState() == Job
.RUNNING
;
160 * Closes the current application context
163 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
165 public static void close(final IProgressMonitor monitor
) {
166 Display
.getDefault().asyncExec(new Runnable() {
170 * @see java.lang.Runnable#run()
174 getContextManager().notifyContextAboutToStop(monitor
);
175 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
176 getContextManager().notifyContextStop(monitor
);
183 private void close() {
185 cdmDatasource
= null;
188 static void setInstance(ICdmApplicationConfiguration applicationController
,
189 ICdmDataSource dataSource
) {
190 instance
= new CdmStore(applicationController
, dataSource
);
193 private CdmStore(ICdmApplicationConfiguration applicationController
,
194 ICdmDataSource dataSource
) {
195 this.applicationConfiguration
= applicationController
;
196 this.cdmDatasource
= dataSource
;
201 * All calls to the datastore require
205 private ICdmApplicationConfiguration
getApplicationConfiguration() {
207 return applicationConfiguration
;
208 } catch (Exception e
) {
209 StoreUtil
.error(CdmStore
.class, e
);
216 * getCurrentApplicationController
220 * {@link eu.etaxonomy.cdm.remote.api.application.CdmApplicationController}
223 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
224 if (getDefault() != null) {
225 return getDefault().getApplicationConfiguration();
235 * Creates a new conversation, binds resources to the conversation and start
236 * a transaction for this conversation.
238 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
241 public static ConversationHolder
createConversation() {
242 ConversationHolder conversation
= getCurrentApplicationConfiguration()
245 conversation
.startTransaction();
247 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
253 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
254 * interface. If a matching getter is found the according service implementation is returned by
255 * invoking the getter otherwise the method returns <code>null</code>.
258 * @param serviceClass
259 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
261 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
262 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
264 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
268 for (Method method
: methods
) {
269 Type type
= method
.getGenericReturnType();
271 if (type
.equals(serviceClass
)) {
273 service
= (T
) method
.invoke(configuration
, null);
275 } catch (IllegalArgumentException e
) {
276 StoreUtil
.error(CdmStore
.class, e
);
277 } catch (IllegalAccessException e
) {
278 StoreUtil
.error(CdmStore
.class, e
);
279 } catch (InvocationTargetException e
) {
280 StoreUtil
.error(CdmStore
.class, e
);
290 * getAuthenticationManager
294 * {@link org.springframework.security.authentication.ProviderManager}
297 public static ProviderManager
getAuthenticationManager() {
298 return getCurrentApplicationConfiguration().getAuthenticationManager();
303 * getAuthenticationManager
307 * {@link ICdmPermissionEvaluator} object.
309 public static ICdmPermissionEvaluator
getPermissionEvaluator() {
310 return getCurrentApplicationConfiguration().getPermissionEvaluator();
318 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
320 public static IEditGeoService
getGeoService() {
321 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
326 * SECURITY RELATED CONVENIENCE METHODS
330 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
332 * @param targetDomainObject
336 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
337 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
338 SecurityContext context
= SecurityContextHolder
.getContext();
339 PermissionEvaluator pe
= getPermissionEvaluator();
340 return getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
, permission
);
344 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
346 * @param targetDomainObject
350 public static boolean currentAuthentiationHasPermission(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
351 SecurityContext context
= SecurityContextHolder
.getContext();
352 return getPermissionEvaluator().hasPermission(context
.getAuthentication(), null, targetType
.getName(), permission
);
355 public static boolean currentAuthentiationHasOneOfRoles(Role
... roles
){
356 SecurityContext context
= SecurityContextHolder
.getContext();
357 return getPermissionEvaluator().hasOneOfRoles(context
.getAuthentication(), roles
);
369 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
371 public static Language
getDefaultLanguage() {
372 if (getDefault().getLanguage() == null) {
373 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
375 return getDefault().getLanguage();
384 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
386 public static void setDefaultLanguage(Language language
) {
387 getDefault().setLanguage(language
);
391 * @return the language
393 private Language
getLanguage() {
399 * the language to set
401 private void setLanguage(Language language
) {
402 this.language
= language
;
411 * Getter for the field <code>loginManager</code>.
414 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
416 public static LoginManager
getLoginManager() {
422 * Getter for the field <code>contextManager</code>.
425 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
427 public static ContextManager
getContextManager() {
428 return contextManager
;
431 public static TermManager
getTermManager() {
435 public static SearchManager
getSearchManager() {
436 return searchManager
;
439 public static EditorManager
getEditorManager() {
440 return editorManager
;
444 * IMPORT/EXPORT FACTORIES
449 * Getter for the field <code>importHandler</code>.
452 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
454 public static ImportManager
getImportManager() {
455 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
460 * Getter for the field <code>exportHandler</code>.
463 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
465 public static ExportManager
getExportManager() {
466 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
470 * Whether this CdmStore is currently connected to a datasource
474 public static boolean isActive() {
475 return instance
!= null && instance
.isConnected
;
483 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
485 public static ICdmDataSource
getDataSource() {
487 return instance
.getDatasource();
495 private ICdmDataSource
getDatasource() {
496 return cdmDatasource
;