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 store the data correctly
40 * for the current session. No state is held in this class.
42 * Only methods that either get or manipulate data are exposed here. So this class acts as a facade
43 * for the methods in cdmlib-service.
49 public class CdmStore
{
51 private static final Resource DEFAULT_APPLICATION_CONTEXT
= new ClassPathResource("/eu/etaxonomy/cdm/editorApplicationContext.xml", TaxeditorStorePlugin
.class);
52 private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION
= DbSchemaValidation
.VALIDATE
;
54 private static CdmStore instance
;
56 private CdmApplicationController applicationController
;
58 private static LoginManager loginManager
;
60 private static ImportManager importManager
;
62 private static ExportManager exportManager
;
64 private static ContextManager contextManager
;
66 private static TermManager termManager
= new TermManager();
68 private static SearchManager searchManager
;
70 private static EditorManager editorManager
;
72 private static CdmStoreConnector job
;
74 private Language language
;
76 private ICdmDataSource cdmDatasource
;
78 private boolean isConnected
;
83 * @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
85 protected static CdmStore
getDefault(){
86 if(instance
!= null && instance
.isConnected
){
88 }else if(instance
== null || !instance
.isConnected
){
90 StoreUtil
.warningDialog("Application is not connected to a datastore", instance
, "The requested operation is only available when " +
91 "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
93 StoreUtil
.showView(CdmDataSourceViewPart
.ID
);
97 throw new RuntimeException();
101 * Initialize the with the last edited datasource
103 public static void connect() {
105 ICdmDataSource datasource
= CdmDataSourceRepository
.getCurrentDataSource();
111 * Initialize with a specific datasource
113 * @param datasource a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
115 public static void connect(ICdmDataSource datasource
) {
116 connect(datasource
, DEFAULT_DB_SCHEMA_VALIDATION
, DEFAULT_APPLICATION_CONTEXT
);
120 * Initialize and provide
123 * @param dbSchemaValidation
124 * @param applicationContextBean
126 private static void connect(final ICdmDataSource datasource
, final DbSchemaValidation dbSchemaValidation
, final Resource applicationContextBean
){
127 StoreUtil
.info("Connecting to datasource: " + datasource
);
129 job
= new CdmStoreConnector(Display
.getDefault(), datasource
, dbSchemaValidation
, applicationContextBean
);
131 job
.setPriority(Job
.BUILD
);
136 public static boolean isConnecting(){
137 return job
!= null && job
.getState() == Job
.RUNNING
;
141 * Closes the current application context
143 * @param monitor a {@link org.eclipse.core.runtime.IProgressMonitor} object.
145 public static void close(final IProgressMonitor monitor
){
146 Display
.getDefault().asyncExec(new Runnable(){
148 * @see java.lang.Runnable#run()
152 getContextManager().notifyContextAboutToStop(monitor
);
153 if((monitor
== null || (!monitor
.isCanceled()) && isActive())){
154 getContextManager().notifyContextStop(monitor
);
155 if(instance
.getApplicationController() != null){
156 instance
.getApplicationController().close();
164 private void close() {
166 cdmDatasource
= null;
169 static void setInstance(CdmApplicationController applicationController
, ICdmDataSource dataSource
){
170 instance
= new CdmStore(applicationController
, dataSource
);
173 private CdmStore(CdmApplicationController applicationController
, ICdmDataSource dataSource
){
174 this.applicationController
= applicationController
;
175 this.cdmDatasource
= dataSource
;
180 * All calls to the datastore require
184 private CdmApplicationController
getApplicationController(){
186 return applicationController
;
188 StoreUtil
.error(CdmStore
.class, e
);
194 * <p>getCurrentApplicationController</p>
196 * @return a {@link eu.etaxonomy.cdm.api.application.CdmApplicationController} object.
198 public static CdmApplicationController
getCurrentApplicationController(){
199 if(getDefault() != null){
200 return getDefault().getApplicationController();
210 * Creates a new conversation, binds resources to the conversation and
211 * start a transaction for this conversation.
213 * @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder} object.
215 public static ConversationHolder
createConversation() {
216 ConversationHolder conversation
= getCurrentApplicationController().NewConversation();
218 conversation
.startTransaction();
223 * Generic method that will return an implementation of the given service interface
224 * or <code>null</code> if the
227 * @param serviceClass
230 public static <T
extends IService
> T
getService(Class
<T
> serviceClass
){
231 ICdmApplicationConfiguration controller
= getCurrentApplicationController();
233 Method
[] methods
= ICdmApplicationConfiguration
.class.getDeclaredMethods();
237 for(Method method
: methods
){
238 Type type
= method
.getGenericReturnType();
240 if(type
.equals(serviceClass
)){
242 service
= (T
) method
.invoke(controller
, null);
244 } catch (IllegalArgumentException e
) {
245 StoreUtil
.error(CdmStore
.class, e
);
246 } catch (IllegalAccessException e
) {
247 StoreUtil
.error(CdmStore
.class, e
);
248 } catch (InvocationTargetException e
) {
249 StoreUtil
.error(CdmStore
.class, e
);
258 * <p>getAuthenticationManager</p>
260 * @return a {@link org.springframework.security.authentication.ProviderManager} object.
262 public static ProviderManager
getAuthenticationManager() { return getCurrentApplicationController().getAuthenticationManager();}
265 * <p>getGeoService</p>
267 * @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
269 public static IEditGeoService
getGeoService(){
270 return (IEditGeoService
) getCurrentApplicationController().getBean("editGeoService");
278 * <p>getDefaultLanguage</p>
280 * @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
282 public static Language
getDefaultLanguage(){
283 if(getDefault().getLanguage() == null){
284 getDefault().setLanguage(PreferencesUtil
.getGlobalLanguage());
286 return getDefault().getLanguage();
290 * <p>setDefaultLanguage</p>
292 * @param language a {@link eu.etaxonomy.cdm.model.common.Language} object.
294 public static void setDefaultLanguage(Language language
){
295 getDefault().setLanguage(language
);
299 * @return the language
301 private Language
getLanguage() {
306 * @param language the language to set
308 private void setLanguage(Language language
) {
309 this.language
= language
;
317 * <p>Getter for the field <code>loginManager</code>.</p>
319 * @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
321 public static LoginManager
getLoginManager(){
322 if(loginManager
== null){
323 loginManager
= new LoginManager();
329 * <p>Getter for the field <code>contextManager</code>.</p>
331 * @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
333 public static ContextManager
getContextManager(){
334 if(contextManager
== null){
335 contextManager
= new ContextManager();
337 return contextManager
;
340 public static TermManager
getTermManager(){
344 public static SearchManager
getSearchManager(){
345 if(searchManager
== null){
346 searchManager
= new SearchManager();
348 return searchManager
;
351 public static EditorManager
getEditorManager() {
352 if(editorManager
== null){
353 editorManager
= new EditorManager();
356 return editorManager
;
360 * IMPORT/EXPORT FACTORIES
364 * <p>Getter for the field <code>importHandler</code>.</p>
366 * @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
368 public static ImportManager
getImportManager(){
369 if(importManager
== null){
370 importManager
= ImportManager
.NewInstance(getCurrentApplicationController());
372 return importManager
;
376 * <p>Getter for the field <code>exportHandler</code>.</p>
378 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
380 public static ExportManager
getExportManager(){
381 if(exportManager
== null){
382 exportManager
= ExportManager
.NewInstance(getCurrentApplicationController());
384 return exportManager
;
388 * Whether this CdmStore is currently connected to a datasource
392 public static boolean isActive(){
393 return instance
!= null && instance
.isConnected
;
397 * <p>getDataSource</p>
399 * @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
401 public static ICdmDataSource
getDataSource(){
403 return instance
.getDatasource();
411 private ICdmDataSource
getDatasource() {
412 return cdmDatasource
;