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 LoginManager loginManager
= new LoginManager();
63 private static ContextManager contextManager
= new ContextManager();;
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();
242 * Generic method that will return an implementation of the given service
243 * interface or <code>null</code> if the
246 * @param serviceClass
249 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
) {
250 ICdmApplicationConfiguration configuration
= getCurrentApplicationConfiguration();
252 Method
[] methods
= ICdmApplicationConfiguration
.class
253 .getDeclaredMethods();
257 for (Method method
: methods
) {
258 Type type
= method
.getGenericReturnType();
260 if (type
.equals(serviceClass
)) {
262 service
= (T
) method
.invoke(configuration
, null);
264 } catch (IllegalArgumentException e
) {
265 StoreUtil
.error(CdmStore
.class, e
);
266 } catch (IllegalAccessException e
) {
267 StoreUtil
.error(CdmStore
.class, e
);
268 } catch (InvocationTargetException e
) {
269 StoreUtil
.error(CdmStore
.class, e
);
279 * getAuthenticationManager
283 * {@link org.springframework.security.authentication.ProviderManager}
286 public static ProviderManager
getAuthenticationManager() {
287 return getCurrentApplicationConfiguration().getAuthenticationManager();
295 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
297 public static IEditGeoService
getGeoService() {
298 return (IEditGeoService
) getCurrentApplicationConfiguration().getBean(
311 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
313 public static Language
getDefaultLanguage() {
314 if (getDefault().getLanguage() == null) {
315 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
317 return getDefault().getLanguage();
326 * a {@link eu.etaxonomy.cdm.model.common.Language} object.
328 public static void setDefaultLanguage(Language language
) {
329 getDefault().setLanguage(language
);
333 * @return the language
335 private Language
getLanguage() {
341 * the language to set
343 private void setLanguage(Language language
) {
344 this.language
= language
;
353 * Getter for the field <code>loginManager</code>.
356 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
358 public static LoginManager
getLoginManager() {
364 * Getter for the field <code>contextManager</code>.
367 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
369 public static ContextManager
getContextManager() {
370 return contextManager
;
373 public static TermManager
getTermManager() {
377 public static SearchManager
getSearchManager() {
378 return searchManager
;
381 public static EditorManager
getEditorManager() {
382 return editorManager
;
386 * IMPORT/EXPORT FACTORIES
391 * Getter for the field <code>importHandler</code>.
394 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
396 public static ImportManager
getImportManager() {
397 return ImportManager
.NewInstance(getCurrentApplicationConfiguration());
402 * Getter for the field <code>exportHandler</code>.
405 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
407 public static ExportManager
getExportManager() {
408 return ExportManager
.NewInstance(getCurrentApplicationConfiguration());
412 * Whether this CdmStore is currently connected to a datasource
416 public static boolean isActive() {
417 return instance
!= null && instance
.isConnected
;
425 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
427 public static ICdmDataSource
getDataSource() {
429 return instance
.getDatasource();
437 private ICdmDataSource
getDatasource() {
438 return cdmDatasource
;