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