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
.io
;
12 import java
.io
.BufferedWriter
;
14 import java
.io
.FileOutputStream
;
15 import java
.io
.OutputStreamWriter
;
16 import java
.io
.Writer
;
17 import java
.text
.SimpleDateFormat
;
18 import java
.util
.ArrayList
;
19 import java
.util
.Calendar
;
20 import java
.util
.List
;
23 import java
.util
.UUID
;
24 import java
.util
.zip
.ZipEntry
;
25 import java
.util
.zip
.ZipOutputStream
;
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
.SubMonitor
;
33 import org
.eclipse
.core
.runtime
.jobs
.Job
;
34 import org
.eclipse
.swt
.widgets
.Display
;
36 import eu
.etaxonomy
.cdm
.api
.application
.CdmApplicationState
;
37 import eu
.etaxonomy
.cdm
.api
.application
.ICdmRepository
;
38 import eu
.etaxonomy
.cdm
.api
.conversation
.IConversationEnabled
;
39 import eu
.etaxonomy
.cdm
.common
.monitor
.IRemotingProgressMonitor
;
40 import eu
.etaxonomy
.cdm
.io
.cdmLight
.CdmLightExportConfigurator
;
41 import eu
.etaxonomy
.cdm
.io
.common
.CdmDefaultExport
;
42 import eu
.etaxonomy
.cdm
.io
.common
.ExportDataWrapper
;
43 import eu
.etaxonomy
.cdm
.io
.common
.ExportResult
;
44 import eu
.etaxonomy
.cdm
.io
.common
.ExportResultType
;
45 import eu
.etaxonomy
.cdm
.io
.common
.IExportConfigurator
;
46 import eu
.etaxonomy
.cdm
.io
.common
.IIoConfigurator
;
47 import eu
.etaxonomy
.cdm
.io
.dwca
.out
.DwcaTaxExportConfigurator
;
48 import eu
.etaxonomy
.cdm
.io
.jaxb
.JaxbExportConfigurator
;
49 import eu
.etaxonomy
.cdm
.io
.sdd
.out
.SDDExportConfigurator
;
50 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
51 import eu
.etaxonomy
.taxeditor
.event
.EventUtility
;
52 import eu
.etaxonomy
.taxeditor
.model
.AbstractUtility
;
53 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
54 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
55 import eu
.etaxonomy
.taxeditor
.operation
.IFeedbackGenerator
;
56 import eu
.etaxonomy
.taxeditor
.operation
.IPostMoniteredOperationEnabled
;
57 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
58 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
62 * ExportHandler class.
66 * @created Sep 11, 2009
69 public class ExportManager
extends AbstractIOManager
<IExportConfigurator
> implements IPostMoniteredOperationEnabled
{
71 private final String successMessage
= "The export was successful";
73 private static final Logger logger
= Logger
.getLogger(ExportManager
.class);
74 /** Constant <code>DATE_FORMAT_NOW="yyyyMMddHHmm"</code> */
75 public static final String DATE_FORMAT_NOW
= "yyyyMMddHHmm";
78 * @param applicationConfiguration
80 private ExportManager(ICdmRepository applicationConfiguration
) {
81 super(applicationConfiguration
);
89 * @param applicationConfiguration
91 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
93 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
95 public static ExportManager
NewInstance(
96 ICdmRepository applicationConfiguration
) {
97 return new ExportManager(applicationConfiguration
);
105 * @param configurator
106 * a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
108 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
111 protected Job
createIOJob(final IExportConfigurator configurator
) {
113 Assert
.isNotNull(configurator
, "Configuration may not be null");
115 final Display display
= Display
.getCurrent();
117 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) {
119 protected IStatus
run(IProgressMonitor monitor
) {
121 "Exporting database. This will take some time.", 100);
124 /** see ticket # 4456 and */
125 // display.asyncExec(new Runnable() {
128 // public void run() {
130 // // terminate any open transactions
131 // IConversationEnabled activePart = (IConversationEnabled) StoreUtil
132 // .getActivePage().getActivePart();
133 // activePart.getConversationHolder().commit(false);
137 // monitor.worked(10);
139 CdmDefaultExport
<IExportConfigurator
> exporter
= new CdmDefaultExport
<IExportConfigurator
>();
140 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
141 .CreateSubMonitor(monitor
, 80));
142 exporter
.setCdmAppController(applicationConfiguration
);
146 exporter
.invoke(configurator
);
148 } catch (RuntimeException e
) {
149 MessagingUtils
.messageDialog("Error exporting data", this,
150 "An error occurred while"
151 + "exporting to destination '"
152 + configurator
.getDestinationNameString()
154 + "Please check error log for details.", e
);
157 display
.asyncExec(new Runnable() {
161 Object activePart
= EventUtility
.getActivePart();
162 if (activePart
instanceof IConversationEnabled
) {
163 // terminate any open transactions
164 IConversationEnabled conversationEnabled
= (IConversationEnabled
) activePart
;
165 // restarting transaction and committing it to
168 // TODO verify correct behaviour
171 conversationEnabled
.getConversationHolder()
174 conversationEnabled
.getConversationHolder()
176 }catch(RuntimeException e
){
177 MessagingUtils
.messageDialog("Error starting conversation handling", this, "" +
178 "Conversation Error: "+ e
);
185 return Status
.OK_STATUS
;
194 public Job
createIOServiceJob(final IExportConfigurator configurator
, final File exportFile
) {
195 Assert
.isNotNull(configurator
, "Configuration may not be null");
197 final Display display
= Display
.getCurrent();
199 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) { //$NON-NLS-1$
201 protected IStatus
run(IProgressMonitor monitor
) {
202 SubMonitor subMonitor
= SubMonitor
.convert(monitor
, 100);
203 subMonitor
.beginTask("Exporting database. This will take some time.", IProgressMonitor
.UNKNOWN
);
205 IIOService ioService
= CdmApplicationState
.getIOService();
206 UUID uuid
= ioService
.monitExportData(configurator
);
207 IRemotingProgressMonitor remotingMonitor
;
208 List
<IFeedbackGenerator
> feedBackGeneratorList
= new ArrayList
<IFeedbackGenerator
>();
210 remotingMonitor
= CdmStore
.getProgressMonitorClientManager().pollMonitor("Export Data",
214 feedBackGeneratorList
,
217 } catch (Exception ex
) {
218 return new Status(Status
.ERROR
, TaxeditorStorePlugin
.PLUGIN_ID
, "Operation Interrupted", ex
);
221 ExportResult result
= (ExportResult
)remotingMonitor
.getResult();
222 ExportDataWrapper data
= result
.getExportData();
224 if (result
.getExportData().getType().equals(ExportResultType
.BYTE_ARRAY
) || result
.getExportData().getType().equals(ExportResultType
.LIST_BYTE_ARRAY
)){
225 if (data
.getExportData() instanceof byte[]){
226 byte[] exportData
= (byte[])data
.getExportData();
227 if(exportData
!= null){
228 FileOutputStream stream
= new FileOutputStream(exportFile
);
229 Writer out
= new BufferedWriter(new OutputStreamWriter(
232 stream
.write(exportData
);
236 }else if (data
.getExportData() instanceof List
<?
>){
237 //TODO:This needs to be improved
238 List
<byte[]> exportData
= (List
<byte[]>)data
.getExportData();
239 if(exportData
!= null){
240 FileOutputStream stream
= new FileOutputStream(exportFile
);
241 Writer out
= new BufferedWriter(new OutputStreamWriter(
244 stream
.write(exportData
.get(0));
250 } else if (result
.getExportData().getType().equals(ExportResultType
.MAP_BYTE_ARRAY
)){
251 Map
<String
, byte[]> resultMap
= (Map
<String
, byte[]>)data
.getExportData();
252 Set
<String
> keySet
= resultMap
.keySet();
253 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
254 Calendar cal
= Calendar
.getInstance();
255 String fileEnding
= ".zip";
256 if (configurator
instanceof DwcaTaxExportConfigurator
){
257 DwcaTaxExportConfigurator dwcaConfig
= (DwcaTaxExportConfigurator
)configurator
;
259 File file
= new File(dwcaConfig
.getDestination().toURI());
260 FileOutputStream stream
= new FileOutputStream(file
);
261 ZipOutputStream zos
= new ZipOutputStream(stream
);
262 for (String key
: keySet
){
263 byte[] fileData
= resultMap
.get(key
);
264 ZipEntry entry
= new ZipEntry( key
+ fileEnding
);
265 zos
.putNextEntry(entry
);
272 logger
.error("This kind of result data is not supported yet." + result
.getExportData().getType().toString());
275 } catch(Exception e
){
276 logger
.error(e
.getStackTrace());
279 showResult(configurator
.getClass().getSimpleName(), display
, result
, successMessage
);
282 } catch (Exception e
) {
283 MessagingUtils
.errorDialog("Error exporting data",
286 TaxeditorStorePlugin
.PLUGIN_ID
,
290 return Status
.OK_STATUS
;
299 private IIoConfigurator
getConfigurator(TYPE type
) {
300 Assert
.isNotNull(type
, "Type should not be null");
304 return JaxbExportConfigurator
.NewInstance(null, null);
306 return SDDExportConfigurator
.NewInstance(null, null, null);
308 MessagingUtils
.notImplementedMessage(this);
309 throw new IllegalArgumentException("Export not supported yet");
318 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
320 public final JaxbExportConfigurator
JaxbConfigurator() {
321 return (JaxbExportConfigurator
) getConfigurator(TYPE
.Jaxb
);
329 * @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
332 public final SDDExportConfigurator
SddConfigurator() {
333 return (SDDExportConfigurator
) getConfigurator(TYPE
.Sdd
);
337 * @param configurator
341 public Job
createIOServiceJob(CdmLightExportConfigurator configurator
, String urlString
) {
342 Assert
.isNotNull(configurator
, "Configuration may not be null");
343 final Display display
= Display
.getDefault();
345 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) { //$NON-NLS-1$
347 protected IStatus
run(IProgressMonitor monitor
) {
348 monitor
.beginTask("Exporting database. This will take some time.", IProgressMonitor
.UNKNOWN
);
350 IIOService ioService
= CdmApplicationState
.getIOService();
352 "Exporting database. This will take some time.", 100);
354 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
355 .CreateSubMonitor(monitor
, 80));
356 ExportResult result
= ioService
.export(configurator
);
357 ExportDataWrapper data
= result
.getExportData();
359 if (result
.getExportData().getType().equals(ExportResultType
.BYTE_ARRAY
) || result
.getExportData().getType().equals(ExportResultType
.LIST_BYTE_ARRAY
)){
360 byte[] exportData
= (byte[])data
.getExportData();
361 if(exportData
!= null){
362 File file
= new File(urlString
);
363 FileOutputStream stream
= new FileOutputStream(file
);
364 Writer out
= new BufferedWriter(new OutputStreamWriter(
367 stream
.write(exportData
);
371 } else if (result
.getExportData().getType().equals(ExportResultType
.MAP_BYTE_ARRAY
)){
372 Map
<String
, byte[]> resultMap
= (Map
<String
, byte[]>)data
.getExportData();
373 Set
<String
> keySet
= resultMap
.keySet();
374 SimpleDateFormat sdf
= new SimpleDateFormat(DATE_FORMAT_NOW
);
375 Calendar cal
= Calendar
.getInstance();
376 String fileEnding
= ".csv";
378 if (configurator
.isCreateZipFile()){
379 File file
= new File(urlString
+File
.separator
+ "csv_light_" + sdf
.format(cal
.getTime())+ ".zip");
380 FileOutputStream stream
= new FileOutputStream(file
);
381 ZipOutputStream zos
= new ZipOutputStream(stream
);
382 for (String key
: keySet
){
383 byte[] fileData
= resultMap
.get(key
);
384 ZipEntry entry
= new ZipEntry( key
+ fileEnding
);
385 zos
.putNextEntry(entry
);
391 for (String key
: keySet
){
392 byte[] fileData
= resultMap
.get(key
);
393 File file
= new File(urlString
+File
.separator
+ key
+ fileEnding
);
394 FileOutputStream stream
= new FileOutputStream(file
);
395 Writer out
= new BufferedWriter(new OutputStreamWriter(
397 stream
.write(fileData
);
402 logger
.error("This kind of result data is not supported yet." + result
.getExportData().getType().toString());
404 } catch(Exception e
){
405 logger
.error(e
.getStackTrace());
407 showResult(configurator
.getClass().getSimpleName(), display
, result
, successMessage
);
410 } catch (Exception e
) {
411 MessagingUtils
.errorDialog("Error exporting data",
414 TaxeditorStorePlugin
.PLUGIN_ID
,
419 return Status
.OK_STATUS
;
425 public void runMoniteredOperation(final IExportConfigurator configurator
, String urlString
) {
426 IIOService ioService
= CdmApplicationState
.getIOService();
427 final UUID uuid
= ioService
.monitExportData(configurator
);
429 Display
.getDefault().asyncExec(new Runnable() {
432 boolean isZip
= false;
433 if (configurator
instanceof CdmLightExportConfigurator
){
434 isZip
= ((CdmLightExportConfigurator
)configurator
).isCreateZipFile();
436 AbstractUtility
.executeMoniteredExport("Export: " + configurator
.getClass().getSimpleName(),
451 public void postOperation(IRemotingProgressMonitor monitor
) {
452 Display
.getDefault().asyncExec(new Runnable() {
456 CdmStore
.getContextManager().notifyContextRefresh();