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.io;
|
11
|
|
12
|
import java.io.File;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.log4j.Logger;
|
19
|
import org.eclipse.core.runtime.Assert;
|
20
|
import org.eclipse.core.runtime.IProgressMonitor;
|
21
|
import org.eclipse.core.runtime.IStatus;
|
22
|
import org.eclipse.core.runtime.Status;
|
23
|
import org.eclipse.core.runtime.jobs.Job;
|
24
|
import org.eclipse.swt.widgets.Display;
|
25
|
import org.eclipse.ui.IWorkbenchPart;
|
26
|
import org.eclipse.ui.PlatformUI;
|
27
|
|
28
|
import eu.etaxonomy.cdm.api.application.CdmApplicationState;
|
29
|
import eu.etaxonomy.cdm.api.application.ICdmRepository;
|
30
|
import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
|
31
|
import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
|
32
|
import eu.etaxonomy.cdm.io.common.ExportDataWrapper;
|
33
|
import eu.etaxonomy.cdm.io.common.ExportResult;
|
34
|
import eu.etaxonomy.cdm.io.common.ExportResultType;
|
35
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
36
|
import eu.etaxonomy.cdm.io.common.IIoConfigurator;
|
37
|
import eu.etaxonomy.cdm.io.common.IoResultBase;
|
38
|
import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
|
39
|
import eu.etaxonomy.cdm.io.outputmodel.OutputModelConfigurator;
|
40
|
import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
|
41
|
import eu.etaxonomy.cdm.io.service.IIOService;
|
42
|
import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
|
43
|
import eu.etaxonomy.taxeditor.model.MessagingUtils;
|
44
|
import eu.etaxonomy.taxeditor.store.CdmStore;
|
45
|
import eu.etaxonomy.taxeditor.store.StoreUtil;
|
46
|
import eu.etaxonomy.taxeditor.store.internal.TaxeditorStorePlugin;
|
47
|
import eu.etaxonomy.taxeditor.ui.dialog.ReportTextDialog;
|
48
|
|
49
|
/**
|
50
|
* <p>
|
51
|
* ExportHandler class.
|
52
|
* </p>
|
53
|
*
|
54
|
* @author n.hoffmann
|
55
|
* @created Sep 11, 2009
|
56
|
* @version 1.0
|
57
|
*/
|
58
|
public class ExportManager extends AbstractIOManager<IExportConfigurator> {
|
59
|
|
60
|
private static final Logger logger = Logger.getLogger(ExportManager.class);
|
61
|
|
62
|
/**
|
63
|
*
|
64
|
* @param applicationConfiguration
|
65
|
*/
|
66
|
private ExportManager(ICdmRepository applicationConfiguration) {
|
67
|
super(applicationConfiguration);
|
68
|
}
|
69
|
|
70
|
/**
|
71
|
* <p>
|
72
|
* NewInstance
|
73
|
* </p>
|
74
|
*
|
75
|
* @param applicationConfiguration
|
76
|
* a
|
77
|
* {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
|
78
|
* object.
|
79
|
* @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
|
80
|
*/
|
81
|
public static ExportManager NewInstance(
|
82
|
ICdmRepository applicationConfiguration) {
|
83
|
return new ExportManager(applicationConfiguration);
|
84
|
}
|
85
|
|
86
|
/**
|
87
|
* <p>
|
88
|
* createIOJob
|
89
|
* </p>
|
90
|
*
|
91
|
* @param configurator
|
92
|
* a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
|
93
|
* object.
|
94
|
* @return a {@link org.eclipse.core.runtime.jobs.Job} object.
|
95
|
*/
|
96
|
@Override
|
97
|
protected Job createIOJob(final IExportConfigurator configurator) {
|
98
|
|
99
|
Assert.isNotNull(configurator, "Configuration may not be null");
|
100
|
|
101
|
final Display display = Display.getCurrent();
|
102
|
|
103
|
Job job = new Job("Export: " + configurator.getClass().getSimpleName()) {
|
104
|
@Override
|
105
|
protected IStatus run(IProgressMonitor monitor) {
|
106
|
monitor.beginTask(
|
107
|
"Exporting database. This will take some time.", 100);
|
108
|
monitor.worked(10);
|
109
|
|
110
|
/** see ticket # 4456 and */
|
111
|
// display.asyncExec(new Runnable() {
|
112
|
//
|
113
|
// @Override
|
114
|
// public void run() {
|
115
|
//
|
116
|
// // terminate any open transactions
|
117
|
// IConversationEnabled activePart = (IConversationEnabled) StoreUtil
|
118
|
// .getActivePage().getActivePart();
|
119
|
// activePart.getConversationHolder().commit(false);
|
120
|
// }
|
121
|
// });
|
122
|
|
123
|
// monitor.worked(10);
|
124
|
|
125
|
CdmDefaultExport<IExportConfigurator> exporter = new CdmDefaultExport<IExportConfigurator>();
|
126
|
configurator.setProgressMonitor(CdmProgressMonitorAdapter
|
127
|
.CreateSubMonitor(monitor, 80));
|
128
|
exporter.setCdmAppController(applicationConfiguration);
|
129
|
monitor.worked(10);
|
130
|
|
131
|
try {
|
132
|
exporter.invoke(configurator);
|
133
|
monitor.worked(60);
|
134
|
} catch (RuntimeException e) {
|
135
|
MessagingUtils.messageDialog("Error exporting data", this,
|
136
|
"An error occured while"
|
137
|
+ "exporting to destination '"
|
138
|
+ configurator.getDestinationNameString()
|
139
|
+ "'.\n"
|
140
|
+ "Please check error log for details.", e);
|
141
|
}
|
142
|
|
143
|
display.asyncExec(new Runnable() {
|
144
|
|
145
|
@Override
|
146
|
public void run() {
|
147
|
IWorkbenchPart activePart = StoreUtil.getActivePage()
|
148
|
.getActivePart();
|
149
|
if (activePart instanceof IConversationEnabled) {
|
150
|
// terminate any open transactions
|
151
|
IConversationEnabled conversationEnabled = (IConversationEnabled) activePart;
|
152
|
// restarting transaction and committing it to
|
153
|
// trigger
|
154
|
// change listener
|
155
|
// TODO verify correct behaviour
|
156
|
|
157
|
try{
|
158
|
conversationEnabled.getConversationHolder()
|
159
|
.startTransaction();
|
160
|
|
161
|
conversationEnabled.getConversationHolder()
|
162
|
.commit();
|
163
|
}catch(RuntimeException e){
|
164
|
MessagingUtils.messageDialog("Error starting conversation handling", this, "" +
|
165
|
"Conversation Error: "+ e);
|
166
|
}
|
167
|
}
|
168
|
}
|
169
|
});
|
170
|
monitor.worked(10);
|
171
|
|
172
|
return Status.OK_STATUS;
|
173
|
}
|
174
|
};
|
175
|
|
176
|
return job;
|
177
|
}
|
178
|
|
179
|
public Job createIOServiceJob(final IExportConfigurator configurator, final File exportFile) {
|
180
|
Assert.isNotNull(configurator, "Configuration may not be null");
|
181
|
|
182
|
// final Display display = Display.getCurrent();
|
183
|
|
184
|
Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
|
185
|
@Override
|
186
|
protected IStatus run(IProgressMonitor monitor) {
|
187
|
monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
|
188
|
try {
|
189
|
IIOService ioService = CdmApplicationState.getIOService();
|
190
|
|
191
|
ExportResult result = ioService.export(configurator);
|
192
|
ExportDataWrapper data = result.getExportData();
|
193
|
try{
|
194
|
if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY)){
|
195
|
byte[] exportData = (byte[])data.getExportData();
|
196
|
if(exportData != null){
|
197
|
FileOutputStream stream = new FileOutputStream(exportFile);
|
198
|
stream.write(exportData);
|
199
|
stream.close();
|
200
|
}
|
201
|
// } else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
|
202
|
// Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
|
203
|
// Set<String> keySet = resultMap.keySet();
|
204
|
// for (String key: keySet){
|
205
|
// byte[] fileData = resultMap.get(key);
|
206
|
// File file = new File(urlString)
|
207
|
// FileOutputStream stream = new FileOutputStream(key);
|
208
|
// stream.write(fileData);
|
209
|
// stream.close();
|
210
|
// }
|
211
|
|
212
|
}else{
|
213
|
logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
|
214
|
}
|
215
|
} catch(Exception e){
|
216
|
logger.error(e.getStackTrace());
|
217
|
}
|
218
|
|
219
|
|
220
|
|
221
|
|
222
|
} catch (Exception e) {
|
223
|
MessagingUtils.errorDialog("Error exporting data",
|
224
|
this,
|
225
|
e.getMessage(),
|
226
|
TaxeditorStorePlugin.PLUGIN_ID,
|
227
|
e,
|
228
|
true);
|
229
|
}
|
230
|
return Status.OK_STATUS;
|
231
|
}
|
232
|
};
|
233
|
return job;
|
234
|
}
|
235
|
/**
|
236
|
* @param jaxb
|
237
|
* @return
|
238
|
*/
|
239
|
private IIoConfigurator getConfigurator(TYPE type) {
|
240
|
Assert.isNotNull(type, "Type should not be null");
|
241
|
|
242
|
switch (type) {
|
243
|
case Jaxb:
|
244
|
return JaxbExportConfigurator.NewInstance(null, null);
|
245
|
case Sdd:
|
246
|
return SDDExportConfigurator.NewInstance(null, null, null);
|
247
|
default:
|
248
|
MessagingUtils.notImplementedMessage(this);
|
249
|
throw new IllegalArgumentException("Export not supported yet");
|
250
|
}
|
251
|
}
|
252
|
|
253
|
/**
|
254
|
* <p>
|
255
|
* JaxbConfigurator
|
256
|
* </p>
|
257
|
*
|
258
|
* @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
|
259
|
*/
|
260
|
public final JaxbExportConfigurator JaxbConfigurator() {
|
261
|
return (JaxbExportConfigurator) getConfigurator(TYPE.Jaxb);
|
262
|
}
|
263
|
|
264
|
/**
|
265
|
* <p>
|
266
|
* SddConfigurator
|
267
|
* </p>
|
268
|
*
|
269
|
* @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
|
270
|
* object.
|
271
|
*/
|
272
|
public final SDDExportConfigurator SddConfigurator() {
|
273
|
return (SDDExportConfigurator) getConfigurator(TYPE.Sdd);
|
274
|
}
|
275
|
|
276
|
/**
|
277
|
* @param configurator
|
278
|
* @param urlString
|
279
|
* @return
|
280
|
*/
|
281
|
public Job createIOServiceJob(OutputModelConfigurator configurator, String urlString) {
|
282
|
Assert.isNotNull(configurator, "Configuration may not be null");
|
283
|
|
284
|
// final Display display = Display.getCurrent();
|
285
|
|
286
|
Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
|
287
|
@Override
|
288
|
protected IStatus run(IProgressMonitor monitor) {
|
289
|
monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
|
290
|
try {
|
291
|
IIOService ioService = CdmApplicationState.getIOService();
|
292
|
|
293
|
ExportResult result = ioService.export(configurator);
|
294
|
ExportDataWrapper data = result.getExportData();
|
295
|
try{
|
296
|
if (result.getExportData().getType().equals(ExportResultType.BYTE_ARRAY)){
|
297
|
byte[] exportData = (byte[])data.getExportData();
|
298
|
if(exportData != null){
|
299
|
File file = new File(urlString);
|
300
|
FileOutputStream stream = new FileOutputStream(file);
|
301
|
stream.write(exportData);
|
302
|
stream.close();
|
303
|
}
|
304
|
} else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
|
305
|
Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
|
306
|
Set<String> keySet = resultMap.keySet();
|
307
|
for (String key: keySet){
|
308
|
byte[] fileData = resultMap.get(key);
|
309
|
String fileEnding ="";
|
310
|
if (configurator instanceof OutputModelConfigurator){
|
311
|
fileEnding = ".csv";
|
312
|
}
|
313
|
File file = new File(urlString+File.separator + key + fileEnding);
|
314
|
FileOutputStream stream = new FileOutputStream(file);
|
315
|
stream.write(fileData);
|
316
|
stream.close();
|
317
|
}
|
318
|
|
319
|
}else{
|
320
|
logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
|
321
|
}
|
322
|
} catch(Exception e){
|
323
|
logger.error(e.getStackTrace());
|
324
|
}
|
325
|
final StringBuilder reportText = new StringBuilder();
|
326
|
if(result!=null){
|
327
|
List<IoResultBase.Error> reports = result.getErrors();
|
328
|
reportText.append("Errors:\\r");
|
329
|
for (IoResultBase.Error bs : reports) {
|
330
|
reportText.append(bs.getMessage() + " - " bs.getException().getStacktrace());
|
331
|
}
|
332
|
List<String> warnings = result.getWarnings();
|
333
|
reportText.append("Warnings:\\r");
|
334
|
for (String bs : warnings) {
|
335
|
reportText.append(bs);
|
336
|
}
|
337
|
List<IoResultBase.Error> exceptions = result.getExceptions();
|
338
|
reportText.append("Exceptions:\\r");
|
339
|
for (IoResultBase.Error bs : exceptions) {
|
340
|
reportText.append(bs.getMessage() + " - " bs.getException().getStacktrace());
|
341
|
}
|
342
|
|
343
|
|
344
|
}
|
345
|
final Display display = Display.getDefault();
|
346
|
display.asyncExec(new Runnable() {
|
347
|
|
348
|
@Override
|
349
|
public void run() {
|
350
|
// display reports with possibility to save
|
351
|
ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
|
352
|
dialog.setTitle(configurator.getClass().getSimpleName()+" Report");
|
353
|
dialog.setReportText(reportText.toString());
|
354
|
dialog.open();
|
355
|
CdmStore.getContextManager().notifyContextRefresh();
|
356
|
}
|
357
|
});
|
358
|
|
359
|
|
360
|
|
361
|
} catch (Exception e) {
|
362
|
MessagingUtils.errorDialog("Error exporting data",
|
363
|
this,
|
364
|
e.getMessage(),
|
365
|
TaxeditorStorePlugin.PLUGIN_ID,
|
366
|
e,
|
367
|
true);
|
368
|
}
|
369
|
|
370
|
return Status.OK_STATUS;
|
371
|
}
|
372
|
};
|
373
|
return job;
|
374
|
}
|
375
|
|
376
|
|
377
|
|
378
|
}
|