Import and Export now have a progress monitor.
[taxeditor.git] / taxeditor-store / src / main / java / eu / etaxonomy / taxeditor / io / ExportHandler.java
1 // $Id$
2 /**
3 * Copyright (C) 2007 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
6 *
7 * The contents of this file are subject to the Mozilla Public License Version 1.1
8 * See LICENSE.TXT at the top of this package for the full license terms.
9 */
10
11 package eu.etaxonomy.taxeditor.io;
12
13 import java.lang.reflect.InvocationTargetException;
14
15 import org.apache.log4j.Logger;
16 import org.eclipse.core.runtime.Assert;
17 import org.eclipse.core.runtime.IProgressMonitor;
18 import org.eclipse.jface.dialogs.ProgressMonitorDialog;
19 import org.eclipse.jface.operation.IRunnableWithProgress;
20
21 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
22 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
23 import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
24 import eu.etaxonomy.cdm.io.common.IExportConfigurator;
25 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
26 import eu.etaxonomy.taxeditor.store.StoreUtil;
27
28 /**
29 * @author n.hoffmann
30 * @created Sep 11, 2009
31 * @version 1.0
32 */
33 public class ExportHandler extends AbstractIOHandler{
34 private static final Logger logger = Logger
35 .getLogger(ExportHandler.class);
36
37 /**
38 *
39 * @param applicationController
40 */
41 private ExportHandler(CdmApplicationController applicationController){
42 super(applicationController);
43 }
44
45 /**
46 *
47 * @param applicationController
48 * @return
49 */
50 public static ExportHandler NewInstance(CdmApplicationController applicationController){
51 return new ExportHandler(applicationController);
52 }
53
54 /**
55 * Starts the export process
56 *
57 * @param configurator
58 */
59 public void run(final IExportConfigurator configurator){
60 ProgressMonitorDialog dialog = new ProgressMonitorDialog(StoreUtil.getShell());
61
62 try {
63 dialog.run(false, true, new IRunnableWithProgress() {
64
65 /*
66 * (non-Javadoc)
67 * @see org.eclipse.jface.operation.IRunnableWithProgress#run(org.eclipse.core.runtime.IProgressMonitor)
68 */
69 public void run(IProgressMonitor monitor)
70 throws InvocationTargetException, InterruptedException {
71 doExport(monitor, configurator);
72 }
73 });
74 }catch (InvocationTargetException e) {
75 logger.error(e);
76 throw new RuntimeException(e);
77 } catch (InterruptedException e) {
78 logger.error(e);
79 throw new RuntimeException(e);
80 }
81
82 }
83
84 /**
85 *
86 * @param monitor
87 * @param configurator
88 */
89 private void doExport(IProgressMonitor monitor, IExportConfigurator configurator){
90
91 Assert.isNotNull(configurator, "Configuration may not be null");
92
93 monitor.beginTask("Exporting database. This will take some time.", 100);
94 monitor.worked(10);
95
96 // terminate any open transactions
97 IConversationEnabled activePart = (IConversationEnabled) StoreUtil.getActivePage().getActivePart();
98 activePart.getConversationHolder().commit(false);
99 monitor.worked(10);
100
101 CdmDefaultExport<IExportConfigurator> exporter = new CdmDefaultExport<IExportConfigurator>();
102
103 exporter.setCdmAppController(applicationController);
104 monitor.worked(10);
105
106 try{
107 exporter.invoke(configurator);
108 monitor.worked(60);
109 }catch(RuntimeException e){
110 StoreUtil.errorDialog("Error exporting data", "An error occured while" +
111 "exporting to destination '" + configurator.getDestinationNameString() + "'.\n" +
112 "Please check error log for details.");
113 logger.error("Error exporting data", e);
114 }
115
116 // restarting transaction and committing it to trigger change listener
117 // TODO verify correct behaviour
118 activePart.getConversationHolder().startTransaction();
119 activePart.getConversationHolder().commit();
120 monitor.worked(10);
121 }
122
123 /**
124 * @param jaxb
125 * @return
126 */
127 private JaxbExportConfigurator getConfigurator(TYPE type) {
128 Assert.isNotNull(type, "Type should not be null");
129
130 switch (type){
131 case Jaxb:
132 return JaxbExportConfigurator.NewInstance(null, null);
133 default:
134 StoreUtil.notImplementedMessage();
135 throw new IllegalArgumentException("Export not supported yet");
136 }
137 }
138
139 /**
140 *
141 * @return
142 */
143 public final JaxbExportConfigurator JaxbConfigurator() { return (JaxbExportConfigurator) getConfigurator(TYPE.Jaxb); }
144
145
146 }