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
.cdm
.persistence
.hibernate
.permission
.Operation
;
38 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
39 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
40 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
41 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
42 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
43 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
46 * This implementation of ICdmDataRepository depends on hibernate sessions to
47 * store the data correctly for the current session. No state is held in this
50 * Only methods that either get or manipulate data are exposed here. So this
51 * class acts as a facade for the methods in cdmlib-service.
57 public class CdmStore
{
59 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
60 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
61 TaxeditorStorePlugin
.class);
62 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
64 private static CdmStore instance
;
66 private final ICdmApplicationConfiguration applicationConfiguration
;
68 private static ContextManager contextManager
= new ContextManager();
70 private static LoginManager loginManager
= new LoginManager();
72 private static TermManager termManager
= new TermManager();
74 private static SearchManager searchManager
= new SearchManager();
76 private static EditorManager editorManager
= new EditorManager();
78 private static CdmStoreConnector job
;
80 private Language language
;
82 private ICdmDataSource cdmDatasource
;
84 private boolean isConnected
;
91 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
93 protected static CdmStore
getDefault() {
94 if (instance
!= null && instance
.isConnected
) {
96 } else if (instance
== null || !instance
.isConnected
) {
100 "Application is not connected to a datastore",
102 "The requested operation is only available when "
103 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
105 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
109 throw new RuntimeException();
113 * Initialize the with the last edited datasource
115 public static void connect() {
117 ICdmDataSource datasource
= CdmDataSourceRepository
118 .getCurrentDataSource();
124 * Initialize with a specific datasource
127 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
129 public static void connect(ICdmDataSource datasource
) {
130 connect(datasource
, DEFAULT_DB_SCHEMA_VALIDATION
,
131 DEFAULT_APPLICATION_CONTEXT
);
135 * Initialize and provide
138 * @param dbSchemaValidation
139 * @param applicationContextBean
141 private static void connect(final ICdmDataSource datasource
,
142 final DbSchemaValidation dbSchemaValidation
,
143 final Resource applicationContextBean
) {
144 StoreUtil
.info("Connecting to datasource: " + datasource
);
146 job
= new CdmStoreConnector(Display
.getDefault(), datasource
,
147 dbSchemaValidation
, applicationContextBean
);
149 job
.setPriority(Job
.BUILD
);
154 public static boolean isConnecting() {
155 return job
!= null && job
.getState() == Job
.RUNNING
;
159 * Closes the current application context
162 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
164 public static void close(final IProgressMonitor monitor
) {
165 Display
.getDefault().asyncExec(new Runnable() {
169 * @see java.lang.Runnable#run()
173 getContextManager().notifyContextAboutToStop(monitor
);
174 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
175 getContextManager().notifyContextStop(monitor
);
182 private void close() {
184 cdmDatasource
= null;
187 static void setInstance(CdmApplicationController applicationController
,
188 ICdmDataSource dataSource
) {
189 instance
= new CdmStore(applicationController
, dataSource
);
192 private CdmStore(CdmApplicationController applicationController
,
193 ICdmDataSource dataSource
) {
194 this.applicationConfiguration
= applicationController
;
195 this.cdmDatasource
= dataSource
;
200 * All calls to the datastore require
204 private ICdmApplicationConfiguration
getApplicationConfiguration() {
206 return applicationConfiguration
;
207 } catch (Exception e
) {
208 StoreUtil
.error(CdmStore
.class, e
);
215 * getCurrentApplicationController
219 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
222 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
223 if (getDefault() != null) {
224 return getDefault().getApplicationConfiguration();
234 * Creates a new conversation, binds resources to the conversation and start
235 * a transaction for this conversation.
237 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
240 public static ConversationHolder
createConversation() {
241 ConversationHolder conversation
= getCurrentApplicationConfiguration()
244 conversation
.startTransaction();
246 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
252 * Generic method that will return an implementation of the given service
253 * interface or <code>null</code> if the
256 * @param serviceClass
259 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
260 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
262 Method
[] methods
= ICdmApplicationConfiguration
.class
263 .getDeclaredMethods();
267 for (Method method
: methods
) {
268 Type type
= method
.getGenericReturnType();
270 if (type
.equals(serviceClass
)) {
272 service
= (T
) method
.invoke(configuration
, null);
274 } catch (IllegalArgumentException e
) {
275 StoreUtil
.error(CdmStore
.class, e
);
276 } catch (IllegalAccessException e
) {
277 StoreUtil
.error(CdmStore
.class, e
);
278 } catch (InvocationTargetException e
) {
279 StoreUtil
.error(CdmStore
.class, e
);
289 * getAuthenticationManager
293 * {@link org.springframework.security.authentication.ProviderManager}
296 public static ProviderManager
getAuthenticationManager() {
297 return getCurrentApplicationConfiguration().getAuthenticationManager();
302 * getAuthenticationManager
306 * {@link PermissionEvaluator} object.
308 public static PermissionEvaluator
getPermissionEvaluator() {
309 return getCurrentApplicationConfiguration().getPermissionEvaluator();
317 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
319 public static IEditGeoService
getGeoService() {
320 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
325 * SECURITY RELATED CONVENIENCE METHODS
329 * @see org.springframework.security.access.PermissionEvaluator#hasPermission(org.springframework.security.core.Authentication, java.lang.Object, java.lang.Object)
331 * @param targetDomainObject
335 public static boolean currentAuthentiationHasPermission(CdmBase targetDomainObject
, EnumSet
<CRUD
> permission
){
336 //TODO use getCurrentApplicationConfiguration().currentAuthentiationHasPermission(CdmBase targetDomainObject, Operation permission) instead
337 SecurityContext context
= SecurityContextHolder
.getContext();
338 return getPermissionEvaluator().hasPermission(context
.getAuthentication(), targetDomainObject
, permission
);
350 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
352 public static Language
getDefaultLanguage() {
353 if (getDefault().getLanguage() == null) {
354 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
356 return getDefault().getLanguage();
365 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
367 public static void setDefaultLanguage(Language language
) {
368 getDefault().setLanguage(language
);
372 * @return the language
374 private Language
getLanguage() {
380 * the language to set
382 private void setLanguage(Language language
) {
383 this.language
= language
;
392 * Getter for the field <code>loginManager</code>.
395 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
397 public static LoginManager
getLoginManager() {
403 * Getter for the field <code>contextManager</code>.
406 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
408 public static ContextManager
getContextManager() {
409 return contextManager
;
412 public static TermManager
getTermManager() {
416 public static SearchManager
getSearchManager() {
417 return searchManager
;
420 public static EditorManager
getEditorManager() {
421 return editorManager
;
425 * IMPORT/EXPORT FACTORIES
430 * Getter for the field <code>importHandler</code>.
433 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
435 public static ImportManager
getImportManager() {
436 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
441 * Getter for the field <code>exportHandler</code>.
444 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
446 public static ExportManager
getExportManager() {
447 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
451 * Whether this CdmStore is currently connected to a datasource
455 public static boolean isActive() {
456 return instance
!= null && instance
.isConnected
;
464 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
466 public static ICdmDataSource
getDataSource() {
468 return instance
.getDatasource();
476 private ICdmDataSource
getDatasource() {
477 return cdmDatasource
;