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
.CdmApplicationController
;
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
.database
.DbSchemaValidation
;
32 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
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
.taxeditor
.datasource
.CdmDataSourceRepository
;
38 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
39 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
40 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
41 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
42 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
45 * This implementation of ICdmDataRepository depends on hibernate sessions to
46 * store the data correctly for the current session. No state is held in this
49 * Only methods that either get or manipulate data are exposed here. So this
50 * class acts as a facade for the methods in cdmlib-service.
56 public class CdmStore
{
58 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
59 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
60 TaxeditorStorePlugin
.class);
61 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
63 private static CdmStore instance
;
65 private final ICdmApplicationConfiguration applicationConfiguration
;
67 private static ContextManager contextManager
= new ContextManager();
69 private static LoginManager loginManager
= new LoginManager();
71 private static TermManager termManager
= new TermManager();
73 private static SearchManager searchManager
= new SearchManager();
75 private static EditorManager editorManager
= new EditorManager();
77 private static CdmStoreConnector job
;
79 private Language language
;
81 private ICdmDataSource cdmDatasource
;
83 private boolean isConnected
;
90 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
92 protected static CdmStore
getDefault() {
93 if (instance
!= null && instance
.isConnected
) {
95 } else if (instance
== null || !instance
.isConnected
) {
99 "Application is not connected to a datastore",
101 "The requested operation is only available when "
102 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
104 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
108 throw new RuntimeException();
112 * Initialize the with the last edited datasource
114 public static void connect() {
116 ICdmDataSource datasource
= CdmDataSourceRepository
117 .getCurrentDataSource();
123 * Initialize with a specific datasource
126 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
128 public static void connect(ICdmDataSource datasource
) {
129 connect(datasource
, DEFAULT_DB_SCHEMA_VALIDATION
,
130 DEFAULT_APPLICATION_CONTEXT
);
134 * Initialize and provide
137 * @param dbSchemaValidation
138 * @param applicationContextBean
140 private static void connect(final ICdmDataSource datasource
,
141 final DbSchemaValidation dbSchemaValidation
,
142 final Resource applicationContextBean
) {
143 StoreUtil
.info("Connecting to datasource: " + datasource
);
145 job
= new CdmStoreConnector(Display
.getDefault(), datasource
,
146 dbSchemaValidation
, applicationContextBean
);
148 job
.setPriority(Job
.BUILD
);
153 public static boolean isConnecting() {
154 return job
!= null && job
.getState() == Job
.RUNNING
;
158 * Closes the current application context
161 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
163 public static void close(final IProgressMonitor monitor
) {
164 Display
.getDefault().asyncExec(new Runnable() {
168 * @see java.lang.Runnable#run()
172 getContextManager().notifyContextAboutToStop(monitor
);
173 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
174 getContextManager().notifyContextStop(monitor
);
181 private void close() {
183 cdmDatasource
= null;
186 static void setInstance(CdmApplicationController applicationController
,
187 ICdmDataSource dataSource
) {
188 instance
= new CdmStore(applicationController
, dataSource
);
191 private CdmStore(CdmApplicationController applicationController
,
192 ICdmDataSource dataSource
) {
193 this.applicationConfiguration
= applicationController
;
194 this.cdmDatasource
= dataSource
;
199 * All calls to the datastore require
203 private ICdmApplicationConfiguration
getApplicationConfiguration() {
205 return applicationConfiguration
;
206 } catch (Exception e
) {
207 StoreUtil
.error(CdmStore
.class, e
);
214 * getCurrentApplicationController
218 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
221 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
222 if (getDefault() != null) {
223 return getDefault().getApplicationConfiguration();
233 * Creates a new conversation, binds resources to the conversation and start
234 * a transaction for this conversation.
236 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
239 public static ConversationHolder
createConversation() {
240 ConversationHolder conversation
= getCurrentApplicationConfiguration()
243 conversation
.startTransaction();
245 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
251 * Generic method that will scan the getters of {@link ICdmApplicationConfiguration} for the given service
252 * interface. If a matching getter is found the according service implementation is returned by
253 * invoking the getter otherwise the method returns <code>null</code>.
256 * @param serviceClass
257 * @return the configured implementation of <code>serviceClass</code> or <code>null</code>
259 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
260 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
262 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
266 for (Method method
: methods
) {
267 Type type
= method
.getGenericReturnType();
269 if (type
.equals(serviceClass
)) {
271 service
= (T
) method
.invoke(configuration
, null);
273 } catch (IllegalArgumentException e
) {
274 StoreUtil
.error(CdmStore
.class, e
);
275 } catch (IllegalAccessException e
) {
276 StoreUtil
.error(CdmStore
.class, e
);
277 } catch (InvocationTargetException e
) {
278 StoreUtil
.error(CdmStore
.class, e
);
288 * getAuthenticationManager
292 * {@link org.springframework.security.authentication.ProviderManager}
295 public static ProviderManager
getAuthenticationManager() {
296 return getCurrentApplicationConfiguration().getAuthenticationManager();
301 * getAuthenticationManager
305 * {@link PermissionEvaluator} object.
307 public static PermissionEvaluator
getPermissionEvaluator() {
308 return getCurrentApplicationConfiguration().getPermissionEvaluator();
316 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
318 public static IEditGeoService
getGeoService() {
319 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
324 * SECURITY RELATED CONVENIENCE METHODS
328 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
330 * @param targetDomainObject
334 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
335 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
336 SecurityContext context
= SecurityContextHolder
.getContext();
337 return getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
, permission
);
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(Class
<?
extends CdmBase
> targetType
, EnumSet
<CRUD
> permission
){
348 SecurityContext context
= SecurityContextHolder
.getContext();
349 return getPermissionEvaluator().hasPermission(context
.getAuthentication(), null, targetType
.getName(), permission
);
361 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
363 public static Language
getDefaultLanguage() {
364 if (getDefault().getLanguage() == null) {
365 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
367 return getDefault().getLanguage();
376 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
378 public static void setDefaultLanguage(Language language
) {
379 getDefault().setLanguage(language
);
383 * @return the language
385 private Language
getLanguage() {
391 * the language to set
393 private void setLanguage(Language language
) {
394 this.language
= language
;
403 * Getter for the field <code>loginManager</code>.
406 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
408 public static LoginManager
getLoginManager() {
414 * Getter for the field <code>contextManager</code>.
417 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
419 public static ContextManager
getContextManager() {
420 return contextManager
;
423 public static TermManager
getTermManager() {
427 public static SearchManager
getSearchManager() {
428 return searchManager
;
431 public static EditorManager
getEditorManager() {
432 return editorManager
;
436 * IMPORT/EXPORT FACTORIES
441 * Getter for the field <code>importHandler</code>.
444 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
446 public static ImportManager
getImportManager() {
447 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
452 * Getter for the field <code>exportHandler</code>.
455 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
457 public static ExportManager
getExportManager() {
458 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
462 * Whether this CdmStore is currently connected to a datasource
466 public static boolean isActive() {
467 return instance
!= null && instance
.isConnected
;
475 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
477 public static ICdmDataSource
getDataSource() {
479 return instance
.getDatasource();
487 private ICdmDataSource
getDatasource() {
488 return cdmDatasource
;