1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
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.
|
8
|
*/
|
9
|
|
10
|
package eu.etaxonomy.taxeditor.store;
|
11
|
|
12
|
import java.lang.reflect.InvocationTargetException;
|
13
|
import java.lang.reflect.Method;
|
14
|
import java.lang.reflect.Type;
|
15
|
|
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;
|
22
|
|
23
|
import eu.etaxonomy.cdm.api.application.CdmApplicationController;
|
24
|
import eu.etaxonomy.cdm.api.application.ICdmApplicationConfiguration;
|
25
|
import eu.etaxonomy.cdm.api.application.ICdmApplicationRemoteConfiguration;
|
26
|
import eu.etaxonomy.cdm.api.conversation.ConversationHolder;
|
27
|
import eu.etaxonomy.cdm.api.service.IService;
|
28
|
import eu.etaxonomy.cdm.database.DbSchemaValidation;
|
29
|
import eu.etaxonomy.cdm.database.ICdmDataSource;
|
30
|
import eu.etaxonomy.cdm.ext.geo.IEditGeoService;
|
31
|
import eu.etaxonomy.cdm.model.common.Language;
|
32
|
import eu.etaxonomy.taxeditor.datasource.CdmDataSourceRepository;
|
33
|
import eu.etaxonomy.taxeditor.io.ExportManager;
|
34
|
import eu.etaxonomy.taxeditor.io.ImportManager;
|
35
|
import eu.etaxonomy.taxeditor.model.ConversationHolderMock;
|
36
|
import eu.etaxonomy.taxeditor.preference.PreferencesUtil;
|
37
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
38
|
import eu.etaxonomy.taxeditor.view.datasource.CdmDataSourceViewPart;
|
39
|
|
40
|
/**
|
41
|
* This implementation of ICdmDataRepository depends on hibernate sessions to
|
42
|
* store the data correctly for the current session. No state is held in this
|
43
|
* class.
|
44
|
*
|
45
|
* Only methods that either get or manipulate data are exposed here. So this
|
46
|
* class acts as a facade for the methods in cdmlib-service.
|
47
|
*
|
48
|
* @author n.hoffmann
|
49
|
* @created 17.03.2009
|
50
|
* @version 1.0
|
51
|
*/
|
52
|
public class CdmStore {
|
53
|
|
54
|
private static final Resource DEFAULT_APPLICATION_CONTEXT = new ClassPathResource(
|
55
|
"/eu/etaxonomy/cdm/editorApplicationContext.xml",
|
56
|
TaxeditorStorePlugin.class);
|
57
|
private static final DbSchemaValidation DEFAULT_DB_SCHEMA_VALIDATION = DbSchemaValidation.VALIDATE;
|
58
|
|
59
|
private static CdmStore instance;
|
60
|
|
61
|
private final ICdmApplicationRemoteConfiguration applicationController;
|
62
|
|
63
|
private static LoginManager loginManager = new LoginManager();
|
64
|
|
65
|
private static ContextManager contextManager = new ContextManager();;
|
66
|
|
67
|
private static TermManager termManager = new TermManager();
|
68
|
|
69
|
private static SearchManager searchManager = new SearchManager();
|
70
|
|
71
|
private static EditorManager editorManager = new EditorManager();
|
72
|
|
73
|
private static CdmStoreConnector job;
|
74
|
|
75
|
private Language language;
|
76
|
|
77
|
private ICdmDataSource cdmDatasource;
|
78
|
|
79
|
private boolean isConnected;
|
80
|
|
81
|
/**
|
82
|
* <p>
|
83
|
* getDefault
|
84
|
* </p>
|
85
|
*
|
86
|
* @return a {@link eu.etaxonomy.taxeditor.store.CdmStore} object.
|
87
|
*/
|
88
|
protected static CdmStore getDefault() {
|
89
|
if (instance != null && instance.isConnected) {
|
90
|
return instance;
|
91
|
} else if (instance == null || !instance.isConnected) {
|
92
|
|
93
|
StoreUtil
|
94
|
.warningDialog(
|
95
|
"Application is not connected to a datastore",
|
96
|
instance,
|
97
|
"The requested operation is only available when "
|
98
|
+ "connected to a datasource. You may choose a datasource to connect to or create a new one in the datasource view.");
|
99
|
|
100
|
StoreUtil.showView(CdmDataSourceViewPart.ID);
|
101
|
|
102
|
}
|
103
|
|
104
|
throw new RuntimeException();
|
105
|
}
|
106
|
|
107
|
/**
|
108
|
* Initialize the with the last edited datasource
|
109
|
*/
|
110
|
public static void connect() {
|
111
|
|
112
|
ICdmDataSource datasource = CdmDataSourceRepository
|
113
|
.getCurrentDataSource();
|
114
|
|
115
|
connect(datasource);
|
116
|
}
|
117
|
|
118
|
/**
|
119
|
* Initialize with a specific datasource
|
120
|
*
|
121
|
* @param datasource
|
122
|
* a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
123
|
*/
|
124
|
public static void connect(ICdmDataSource datasource) {
|
125
|
connect(datasource, DEFAULT_DB_SCHEMA_VALIDATION,
|
126
|
DEFAULT_APPLICATION_CONTEXT);
|
127
|
}
|
128
|
|
129
|
/**
|
130
|
* Initialize and provide
|
131
|
*
|
132
|
* @param datasource
|
133
|
* @param dbSchemaValidation
|
134
|
* @param applicationContextBean
|
135
|
*/
|
136
|
private static void connect(final ICdmDataSource datasource,
|
137
|
final DbSchemaValidation dbSchemaValidation,
|
138
|
final Resource applicationContextBean) {
|
139
|
StoreUtil.info("Connecting to datasource: " + datasource);
|
140
|
|
141
|
job = new CdmStoreConnector(Display.getDefault(), datasource,
|
142
|
dbSchemaValidation, applicationContextBean);
|
143
|
job.setUser(true);
|
144
|
job.setPriority(Job.BUILD);
|
145
|
job.schedule();
|
146
|
|
147
|
}
|
148
|
|
149
|
public static boolean isConnecting() {
|
150
|
return job != null && job.getState() == Job.RUNNING;
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* Closes the current application context
|
155
|
*
|
156
|
* @param monitor
|
157
|
* a {@link org.eclipse.core.runtime.IProgressMonitor} object.
|
158
|
*/
|
159
|
public static void close(final IProgressMonitor monitor) {
|
160
|
Display.getDefault().asyncExec(new Runnable() {
|
161
|
/*
|
162
|
* (non-Javadoc)
|
163
|
*
|
164
|
* @see java.lang.Runnable#run()
|
165
|
*/
|
166
|
@Override
|
167
|
public void run() {
|
168
|
getContextManager().notifyContextAboutToStop(monitor);
|
169
|
if ((monitor == null || (!monitor.isCanceled()) && isActive())) {
|
170
|
getContextManager().notifyContextStop(monitor);
|
171
|
if (instance.getApplicationController() != null) {
|
172
|
// instance.getApplicationController().close();
|
173
|
}
|
174
|
instance.close();
|
175
|
}
|
176
|
}
|
177
|
});
|
178
|
}
|
179
|
|
180
|
private void close() {
|
181
|
isConnected = false;
|
182
|
cdmDatasource = null;
|
183
|
}
|
184
|
|
185
|
static void setInstance(ICdmApplicationRemoteConfiguration applicationController,
|
186
|
ICdmDataSource dataSource) {
|
187
|
instance = new CdmStore(applicationController, dataSource);
|
188
|
}
|
189
|
|
190
|
private CdmStore(ICdmApplicationRemoteConfiguration applicationController,
|
191
|
ICdmDataSource dataSource) {
|
192
|
this.applicationController = applicationController;
|
193
|
this.cdmDatasource = dataSource;
|
194
|
isConnected = true;
|
195
|
}
|
196
|
|
197
|
/**
|
198
|
* All calls to the datastore require
|
199
|
*
|
200
|
* @return
|
201
|
*/
|
202
|
private ICdmApplicationRemoteConfiguration getApplicationController() {
|
203
|
try {
|
204
|
return applicationController;
|
205
|
} catch (Exception e) {
|
206
|
StoreUtil.error(CdmStore.class, e);
|
207
|
}
|
208
|
return null;
|
209
|
}
|
210
|
|
211
|
/**
|
212
|
* <p>
|
213
|
* getCurrentApplicationController
|
214
|
* </p>
|
215
|
*
|
216
|
* @return a
|
217
|
* {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
|
218
|
* object.
|
219
|
*/
|
220
|
public static ICdmApplicationRemoteConfiguration getCurrentApplicationController() {
|
221
|
if (getDefault() != null) {
|
222
|
return getDefault().getApplicationController();
|
223
|
}
|
224
|
return null;
|
225
|
}
|
226
|
|
227
|
/*
|
228
|
* CONVERSATIONS
|
229
|
*/
|
230
|
|
231
|
/**
|
232
|
* Creates a new conversation, binds resources to the conversation and start
|
233
|
* a transaction for this conversation.
|
234
|
*
|
235
|
* @return a {@link eu.etaxonomy.cdm.api.conversation.ConversationHolder}
|
236
|
* object.
|
237
|
*/
|
238
|
public static ConversationHolder createConversation() {
|
239
|
// ConversationHolder conversation = getCurrentApplicationController()
|
240
|
// .NewConversation();
|
241
|
ConversationHolder conversation = new ConversationHolderMock();
|
242
|
|
243
|
conversation.startTransaction();
|
244
|
return conversation;
|
245
|
}
|
246
|
|
247
|
/**
|
248
|
* Generic method that will return an implementation of the given service
|
249
|
* interface or <code>null</code> if the
|
250
|
*
|
251
|
* @param <T>
|
252
|
* @param serviceClass
|
253
|
* @return
|
254
|
*/
|
255
|
public static <T extends IService> T getService(Class<T> serviceClass) {
|
256
|
ICdmApplicationRemoteConfiguration configuration = getCurrentApplicationController();
|
257
|
|
258
|
Method[] methods = ICdmApplicationConfiguration.class
|
259
|
.getDeclaredMethods();
|
260
|
|
261
|
T service = null;
|
262
|
|
263
|
for (Method method : methods) {
|
264
|
Type type = method.getGenericReturnType();
|
265
|
|
266
|
if (type.equals(serviceClass)) {
|
267
|
try {
|
268
|
service = (T) method.invoke(configuration, null);
|
269
|
break;
|
270
|
} catch (IllegalArgumentException e) {
|
271
|
StoreUtil.error(CdmStore.class, e);
|
272
|
} catch (IllegalAccessException e) {
|
273
|
StoreUtil.error(CdmStore.class, e);
|
274
|
} catch (InvocationTargetException e) {
|
275
|
StoreUtil.error(CdmStore.class, e);
|
276
|
}
|
277
|
}
|
278
|
}
|
279
|
|
280
|
return service;
|
281
|
}
|
282
|
|
283
|
/**
|
284
|
* <p>
|
285
|
* getAuthenticationManager
|
286
|
* </p>
|
287
|
*
|
288
|
* @return a
|
289
|
* {@link org.springframework.security.authentication.ProviderManager}
|
290
|
* object.
|
291
|
*/
|
292
|
public static ProviderManager getAuthenticationManager() {
|
293
|
return null; //getCurrentApplicationController().getAuthenticationManager();
|
294
|
}
|
295
|
|
296
|
/**
|
297
|
* <p>
|
298
|
* getGeoService
|
299
|
* </p>
|
300
|
*
|
301
|
* @return a {@link eu.etaxonomy.cdm.ext.geo.IEditGeoService} object.
|
302
|
*/
|
303
|
public static IEditGeoService getGeoService() {
|
304
|
return null;// (IEditGeoService) getCurrentApplicationController().getBean("editGeoService");
|
305
|
}
|
306
|
|
307
|
/*
|
308
|
* LANGUAGE
|
309
|
*/
|
310
|
|
311
|
/**
|
312
|
* <p>
|
313
|
* getDefaultLanguage
|
314
|
* </p>
|
315
|
*
|
316
|
* @return a {@link eu.etaxonomy.cdm.model.common.Language} object.
|
317
|
*/
|
318
|
public static Language getDefaultLanguage() {
|
319
|
if (getDefault().getLanguage() == null) {
|
320
|
getDefault().setLanguage(PreferencesUtil.getGlobalLanguage());
|
321
|
}
|
322
|
return getDefault().getLanguage();
|
323
|
}
|
324
|
|
325
|
/**
|
326
|
* <p>
|
327
|
* setDefaultLanguage
|
328
|
* </p>
|
329
|
*
|
330
|
* @param language
|
331
|
* a {@link eu.etaxonomy.cdm.model.common.Language} object.
|
332
|
*/
|
333
|
public static void setDefaultLanguage(Language language) {
|
334
|
getDefault().setLanguage(language);
|
335
|
}
|
336
|
|
337
|
/**
|
338
|
* @return the language
|
339
|
*/
|
340
|
private Language getLanguage() {
|
341
|
return language;
|
342
|
}
|
343
|
|
344
|
/**
|
345
|
* @param language
|
346
|
* the language to set
|
347
|
*/
|
348
|
private void setLanguage(Language language) {
|
349
|
this.language = language;
|
350
|
}
|
351
|
|
352
|
/*
|
353
|
* LOGIN
|
354
|
*/
|
355
|
|
356
|
/**
|
357
|
* <p>
|
358
|
* Getter for the field <code>loginManager</code>.
|
359
|
* </p>
|
360
|
*
|
361
|
* @return a {@link eu.etaxonomy.taxeditor.store.LoginManager} object.
|
362
|
*/
|
363
|
public static LoginManager getLoginManager() {
|
364
|
return loginManager;
|
365
|
}
|
366
|
|
367
|
/**
|
368
|
* <p>
|
369
|
* Getter for the field <code>contextManager</code>.
|
370
|
* </p>
|
371
|
*
|
372
|
* @return a {@link eu.etaxonomy.taxeditor.store.ContextManager} object.
|
373
|
*/
|
374
|
public static ContextManager getContextManager() {
|
375
|
return contextManager;
|
376
|
}
|
377
|
|
378
|
public static TermManager getTermManager() {
|
379
|
return termManager;
|
380
|
}
|
381
|
|
382
|
public static SearchManager getSearchManager() {
|
383
|
return searchManager;
|
384
|
}
|
385
|
|
386
|
public static EditorManager getEditorManager() {
|
387
|
return editorManager;
|
388
|
}
|
389
|
|
390
|
/*
|
391
|
* IMPORT/EXPORT FACTORIES
|
392
|
*/
|
393
|
|
394
|
/**
|
395
|
* <p>
|
396
|
* Getter for the field <code>importHandler</code>.
|
397
|
* </p>
|
398
|
*
|
399
|
* @return a {@link eu.etaxonomy.taxeditor.io.ImportManager} object.
|
400
|
*/
|
401
|
public static ImportManager getImportManager() {
|
402
|
return ImportManager.NewInstance(null);//getCurrentApplicationController());
|
403
|
}
|
404
|
|
405
|
/**
|
406
|
* <p>
|
407
|
* Getter for the field <code>exportHandler</code>.
|
408
|
* </p>
|
409
|
*
|
410
|
* @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
|
411
|
*/
|
412
|
public static ExportManager getExportManager() {
|
413
|
return ExportManager.NewInstance(null);//getCurrentApplicationController());
|
414
|
}
|
415
|
|
416
|
/**
|
417
|
* Whether this CdmStore is currently connected to a datasource
|
418
|
*
|
419
|
* @return a boolean.
|
420
|
*/
|
421
|
public static boolean isActive() {
|
422
|
return instance != null && instance.isConnected;
|
423
|
}
|
424
|
|
425
|
/**
|
426
|
* <p>
|
427
|
* getDataSource
|
428
|
* </p>
|
429
|
*
|
430
|
* @return a {@link eu.etaxonomy.cdm.database.ICdmDataSource} object.
|
431
|
*/
|
432
|
public static ICdmDataSource getDataSource() {
|
433
|
if (isActive()) {
|
434
|
return instance.getDatasource();
|
435
|
}
|
436
|
return null;
|
437
|
}
|
438
|
|
439
|
/**
|
440
|
* @return
|
441
|
*/
|
442
|
private ICdmDataSource getDatasource() {
|
443
|
return cdmDatasource;
|
444
|
}
|
445
|
|
446
|
}
|