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
;
13 import java
.io
.FileOutputStream
;
14 import java
.util
.List
;
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
;
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
.jaxb
.JaxbExportConfigurator
;
38 import eu
.etaxonomy
.cdm
.io
.outputmodel
.OutputModelConfigurator
;
39 import eu
.etaxonomy
.cdm
.io
.sdd
.out
.SDDExportConfigurator
;
40 import eu
.etaxonomy
.cdm
.io
.service
.IIOService
;
41 import eu
.etaxonomy
.taxeditor
.model
.CdmProgressMonitorAdapter
;
42 import eu
.etaxonomy
.taxeditor
.model
.MessagingUtils
;
43 import eu
.etaxonomy
.taxeditor
.store
.CdmStore
;
44 import eu
.etaxonomy
.taxeditor
.store
.StoreUtil
;
45 import eu
.etaxonomy
.taxeditor
.store
.internal
.TaxeditorStorePlugin
;
46 import eu
.etaxonomy
.taxeditor
.ui
.dialog
.ReportTextDialog
;
50 * ExportHandler class.
54 * @created Sep 11, 2009
57 public class ExportManager
extends AbstractIOManager
<IExportConfigurator
> {
59 private static final Logger logger
= Logger
.getLogger(ExportManager
.class);
63 * @param applicationConfiguration
65 private ExportManager(ICdmRepository applicationConfiguration
) {
66 super(applicationConfiguration
);
74 * @param applicationConfiguration
76 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
78 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
80 public static ExportManager
NewInstance(
81 ICdmRepository applicationConfiguration
) {
82 return new ExportManager(applicationConfiguration
);
91 * a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
93 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
96 protected Job
createIOJob(final IExportConfigurator configurator
) {
98 Assert
.isNotNull(configurator
, "Configuration may not be null");
100 final Display display
= Display
.getCurrent();
102 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) {
104 protected IStatus
run(IProgressMonitor monitor
) {
106 "Exporting database. This will take some time.", 100);
109 /** see ticket # 4456 and */
110 // display.asyncExec(new Runnable() {
113 // public void run() {
115 // // terminate any open transactions
116 // IConversationEnabled activePart = (IConversationEnabled) StoreUtil
117 // .getActivePage().getActivePart();
118 // activePart.getConversationHolder().commit(false);
122 // monitor.worked(10);
124 CdmDefaultExport
<IExportConfigurator
> exporter
= new CdmDefaultExport
<IExportConfigurator
>();
125 configurator
.setProgressMonitor(CdmProgressMonitorAdapter
126 .CreateSubMonitor(monitor
, 80));
127 exporter
.setCdmAppController(applicationConfiguration
);
131 exporter
.invoke(configurator
);
133 } catch (RuntimeException e
) {
134 MessagingUtils
.messageDialog("Error exporting data", this,
135 "An error occured while"
136 + "exporting to destination '"
137 + configurator
.getDestinationNameString()
139 + "Please check error log for details.", e
);
142 display
.asyncExec(new Runnable() {
146 IWorkbenchPart activePart
= StoreUtil
.getActivePage()
148 if (activePart
instanceof IConversationEnabled
) {
149 // terminate any open transactions
150 IConversationEnabled conversationEnabled
= (IConversationEnabled
) activePart
;
151 // restarting transaction and committing it to
154 // TODO verify correct behaviour
157 conversationEnabled
.getConversationHolder()
160 conversationEnabled
.getConversationHolder()
162 }catch(RuntimeException e
){
163 MessagingUtils
.messageDialog("Error starting conversation handling", this, "" +
164 "Conversation Error: "+ e
);
171 return Status
.OK_STATUS
;
178 public Job
createIOServiceJob(final IExportConfigurator configurator
, final File exportFile
) {
179 Assert
.isNotNull(configurator
, "Configuration may not be null");
181 // final Display display = Display.getCurrent();
183 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) { //$NON-NLS-1$
185 protected IStatus
run(IProgressMonitor monitor
) {
186 monitor
.beginTask("Exporting database. This will take some time.", IProgressMonitor
.UNKNOWN
);
188 IIOService ioService
= CdmApplicationState
.getIOService();
190 ExportResult result
= ioService
.export(configurator
);
191 ExportDataWrapper data
= result
.getExportData();
193 if (result
.getExportData().getType().equals(ExportResultType
.BYTE_ARRAY
)){
194 byte[] exportData
= (byte[])data
.getExportData();
195 if(exportData
!= null){
196 FileOutputStream stream
= new FileOutputStream(exportFile
);
197 stream
.write(exportData
);
200 // } else if (result.getExportData().getType().equals(ExportResultType.MAP_BYTE_ARRAY)){
201 // Map<String, byte[]> resultMap = (Map<String, byte[]>)data.getExportData();
202 // Set<String> keySet = resultMap.keySet();
203 // for (String key: keySet){
204 // byte[] fileData = resultMap.get(key);
205 // File file = new File(urlString)
206 // FileOutputStream stream = new FileOutputStream(key);
207 // stream.write(fileData);
212 logger
.error("This kind of result data is not supported yet." + result
.getExportData().getType().toString());
214 } catch(Exception e
){
215 logger
.error(e
.getStackTrace());
221 } catch (Exception e
) {
222 MessagingUtils
.errorDialog("Error exporting data",
225 TaxeditorStorePlugin
.PLUGIN_ID
,
229 return Status
.OK_STATUS
;
238 private IIoConfigurator
getConfigurator(TYPE type
) {
239 Assert
.isNotNull(type
, "Type should not be null");
243 return JaxbExportConfigurator
.NewInstance(null, null);
245 return SDDExportConfigurator
.NewInstance(null, null, null);
247 MessagingUtils
.notImplementedMessage(this);
248 throw new IllegalArgumentException("Export not supported yet");
257 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
259 public final JaxbExportConfigurator
JaxbConfigurator() {
260 return (JaxbExportConfigurator
) getConfigurator(TYPE
.Jaxb
);
268 * @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
271 public final SDDExportConfigurator
SddConfigurator() {
272 return (SDDExportConfigurator
) getConfigurator(TYPE
.Sdd
);
276 * @param configurator
280 public Job
createIOServiceJob(OutputModelConfigurator configurator
, String urlString
) {
281 Assert
.isNotNull(configurator
, "Configuration may not be null");
283 // final Display display = Display.getCurrent();
285 Job job
= new Job("Export: " + configurator
.getClass().getSimpleName()) { //$NON-NLS-1$
287 protected IStatus
run(IProgressMonitor monitor
) {
288 monitor
.beginTask("Exporting database. This will take some time.", IProgressMonitor
.UNKNOWN
);
290 IIOService ioService
= CdmApplicationState
.getIOService();
292 ExportResult result
= ioService
.export(configurator
);
293 ExportDataWrapper data
= result
.getExportData();
295 if (result
.getExportData().getType().equals(ExportResultType
.BYTE_ARRAY
)){
296 byte[] exportData
= (byte[])data
.getExportData();
297 if(exportData
!= null){
298 File file
= new File(urlString
);
299 FileOutputStream stream
= new FileOutputStream(file
);
300 stream
.write(exportData
);
303 } else if (result
.getExportData().getType().equals(ExportResultType
.MAP_BYTE_ARRAY
)){
304 Map
<String
, byte[]> resultMap
= (Map
<String
, byte[]>)data
.getExportData();
305 Set
<String
> keySet
= resultMap
.keySet();
306 for (String key
: keySet
){
307 byte[] fileData
= resultMap
.get(key
);
308 String fileEnding
="";
309 if (configurator
instanceof OutputModelConfigurator
){
312 File file
= new File(urlString
+File
.separator
+ key
+ fileEnding
);
313 FileOutputStream stream
= new FileOutputStream(file
);
314 stream
.write(fileData
);
319 logger
.error("This kind of result data is not supported yet." + result
.getExportData().getType().toString());
321 } catch(Exception e
){
322 logger
.error(e
.getStackTrace());
324 final StringBuilder reportText
= new StringBuilder();
326 List
<byte[]> reports
= result
.getErrors();
327 reportText
.append("Errors:\\r");
328 for (byte[] bs
: reports
) {
329 reportText
.append(new String(bs
));
331 List
<byte[]> warnings
= result
.getWarnings();
332 reportText
.append("Warnings:\\r");
333 for (byte[] bs
: warnings
) {
334 reportText
.append(new String(bs
));
336 List
<Exception
> exceptions
= result
.getExceptions();
337 reportText
.append("Exceptions:\\r");
338 for (Exception ex
: exceptions
) {
339 reportText
.append(ex
.getStackTrace());
344 final Display display
= Display
.getDefault();
345 display
.asyncExec(new Runnable() {
349 // display reports with possibility to save
350 ReportTextDialog dialog
= new ReportTextDialog(PlatformUI
.getWorkbench().getActiveWorkbenchWindow().getShell());
351 dialog
.setTitle(configurator
.getClass().getSimpleName()+" Report");
352 dialog
.setReportText(reportText
.toString());
354 CdmStore
.getContextManager().notifyContextRefresh();
360 } catch (Exception e
) {
361 MessagingUtils
.errorDialog("Error exporting data",
364 TaxeditorStorePlugin
.PLUGIN_ID
,
369 return Status
.OK_STATUS
;