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
;
16 import org
.eclipse
.core
.runtime
.IProgressMonitor
;
17 import org
.eclipse
.core
.runtime
.jobs
.Job
;
18 import org
.eclipse
.swt
.widgets
.Display
;
19 import org
.springframework
.core
.io
.ClassPathResource
;
20 import org
.springframework
.core
.io
.Resource
;
21 import org
.springframework
.security
.authentication
.ProviderManager
;
23 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationController
;
24 import eu
.etaxonomy
.cdm
.api
.application
.ICdmApplicationConfiguration
;
25 import eu
.etaxonomy
.cdm
.api
.conversation
.ConversationHolder
;
26 import eu
.etaxonomy
.cdm
.api
.service
.IService
;
27 import eu
.etaxonomy
.cdm
.database
.DbSchemaValidation
;
28 import eu
.etaxonomy
.cdm
.database
.ICdmDataSource
;
29 import eu
.etaxonomy
.cdm
.ext
.geo
.IEditGeoService
;
30 import eu
.etaxonomy
.cdm
.model
.common
.Language
;
31 import eu
.etaxonomy
.taxeditor
.datasource
.CdmDataSourceRepository
;
32 import eu
.etaxonomy
.taxeditor
.io
.ExportManager
;
33 import eu
.etaxonomy
.taxeditor
.io
.ImportManager
;
34 import eu
.etaxonomy
.taxeditor
.preference
.PreferencesUtil
;
35 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
36 import eu
.etaxonomy
.taxeditor
.view
.datasource
.CdmDataSourceViewPart
;
39 * This implementation of ICdmDataRepository depends on hibernate sessions to
40 * store the data correctly for the current session. No state is held in this
43 * Only methods that either get or manipulate data are exposed here. So this
44 * class acts as a facade for the methods in cdmlib-service.
50 public class CdmStore
{
52 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource(
53 "/eu/etaxonomy/cdm/editorApplicationContext.xml",
54 TaxeditorStorePlugin
.class);
55 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
57 private static CdmStore instance
;
59 private final ICdmApplicationConfiguration applicationConfiguration
;
61 private static ContextManager contextManager
= new ContextManager();
63 private static LoginManager loginManager
= new LoginManager();
65 private static TermManager termManager
= new TermManager();
67 private static SearchManager searchManager
= new SearchManager();
69 private static EditorManager editorManager
= new EditorManager();
71 private static CdmStoreConnector job
;
73 private Language language
;
75 private ICdmDataSource cdmDatasource
;
77 private boolean isConnected
;
84 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
86 protected static CdmStore
getDefault() {
87 if (instance
!= null && instance
.isConnected
) {
89 } else if (instance
== null || !instance
.isConnected
) {
93 "Application is not connected to a datastore",
95 "The requested operation is only available when "
96 + "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
98 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
102 throw new RuntimeException();
106 * Initialize the with the last edited datasource
108 public static void connect() {
110 ICdmDataSource datasource
= CdmDataSourceRepository
111 .getCurrentDataSource();
117 * Initialize with a specific datasource
120 * a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
122 public static void connect(ICdmDataSource datasource
) {
123 connect(datasource
, DEFAULT_DB_SCHEMA_VALIDATION
,
124 DEFAULT_APPLICATION_CONTEXT
);
128 * Initialize and provide
131 * @param dbSchemaValidation
132 * @param applicationContextBean
134 private static void connect(final ICdmDataSource datasource
,
135 final DbSchemaValidation dbSchemaValidation
,
136 final Resource applicationContextBean
) {
137 StoreUtil
.info("Connecting to datasource: " + datasource
);
139 job
= new CdmStoreConnector(Display
.getDefault(), datasource
,
140 dbSchemaValidation
, applicationContextBean
);
142 job
.setPriority(Job
.BUILD
);
147 public static boolean isConnecting() {
148 return job
!= null && job
.getState() == Job
.RUNNING
;
152 * Closes the current application context
155 * a {@link org.eclipse.core.runtime.IProgressMonitor} object.
157 public static void close(final IProgressMonitor monitor
) {
158 Display
.getDefault().asyncExec(new Runnable() {
162 * @see java.lang.Runnable#run()
166 getContextManager().notifyContextAboutToStop(monitor
);
167 if ((monitor
== null || (!monitor
.isCanceled()) && isActive())) {
168 getContextManager().notifyContextStop(monitor
);
175 private void close() {
177 cdmDatasource
= null;
180 static void setInstance(CdmApplicationController applicationController
,
181 ICdmDataSource dataSource
) {
182 instance
= new CdmStore(applicationController
, dataSource
);
185 private CdmStore(CdmApplicationController applicationController
,
186 ICdmDataSource dataSource
) {
187 this.applicationConfiguration
= applicationController
;
188 this.cdmDatasource
= dataSource
;
193 * All calls to the datastore require
197 private ICdmApplicationConfiguration
getApplicationConfiguration() {
199 return applicationConfiguration
;
200 } catch (Exception e
) {
201 StoreUtil
.error(CdmStore
.class, e
);
208 * getCurrentApplicationController
212 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
215 public static ICdmApplicationConfiguration
getCurrentApplicationConfiguration() {
216 if (getDefault() != null) {
217 return getDefault().getApplicationConfiguration();
227 * Creates a new conversation, binds resources to the conversation and start
228 * a transaction for this conversation.
230 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
233 public static ConversationHolder
createConversation() {
234 ConversationHolder conversation
= getCurrentApplicationConfiguration()
237 conversation
.startTransaction();
239 StoreUtil
.errorDialog("No database connection", CdmStore
.class, "No database connection available", e
);
245 * Generic method that will return an implementation of the given service
246 * interface or <code>null</code> if the
249 * @param serviceClass
252 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
253 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
255 Method
[] methods
= ICdmApplicationConfiguration
.class
256 .getDeclaredMethods();
260 for (Method method
: methods
) {
261 Type type
= method
.getGenericReturnType();
263 if (type
.equals(serviceClass
)) {
265 service
= (T
) method
.invoke(configuration
, null);
267 } catch (IllegalArgumentException e
) {
268 StoreUtil
.error(CdmStore
.class, e
);
269 } catch (IllegalAccessException e
) {
270 StoreUtil
.error(CdmStore
.class, e
);
271 } catch (InvocationTargetException e
) {
272 StoreUtil
.error(CdmStore
.class, e
);
282 * getAuthenticationManager
286 * {@link org.springframework.security.authentication.ProviderManager}
289 public static ProviderManager
getAuthenticationManager() {
290 return getCurrentApplicationConfiguration().getAuthenticationManager();
298 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
300 public static IEditGeoService
getGeoService() {
301 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
314 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
316 public static Language
getDefaultLanguage() {
317 if (getDefault().getLanguage() == null) {
318 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
320 return getDefault().getLanguage();
329 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
331 public static void setDefaultLanguage(Language language
) {
332 getDefault().setLanguage(language
);
336 * @return the language
338 private Language
getLanguage() {
344 * the language to set
346 private void setLanguage(Language language
) {
347 this.language
= language
;
356 * Getter for the field <code>loginManager</code>.
359 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
361 public static LoginManager
getLoginManager() {
367 * Getter for the field <code>contextManager</code>.
370 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
372 public static ContextManager
getContextManager() {
373 return contextManager
;
376 public static TermManager
getTermManager() {
380 public static SearchManager
getSearchManager() {
381 return searchManager
;
384 public static EditorManager
getEditorManager() {
385 return editorManager
;
389 * IMPORT/EXPORT FACTORIES
394 * Getter for the field <code>importHandler</code>.
397 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
399 public static ImportManager
getImportManager() {
400 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
405 * Getter for the field <code>exportHandler</code>.
408 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
410 public static ExportManager
getExportManager() {
411 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
415 * Whether this CdmStore is currently connected to a datasource
419 public static boolean isActive() {
420 return instance
!= null && instance
.isConnected
;
428 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
430 public static ICdmDataSource
getDataSource() {
432 return instance
.getDatasource();
440 private ICdmDataSource
getDatasource() {
441 return cdmDatasource
;