88093316ea251254506b3a3adc20a50bbe85c87c
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / ExportManager.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 org.eclipse.core.runtime.Assert;
14 import org.eclipse.core.runtime.IProgressMonitor;
15 import org.eclipse.core.runtime.IStatus;
16 import org.eclipse.core.runtime.Status;
17 import org.eclipse.core.runtime.jobs.Job;
18 import org.eclipse.swt.widgets.Display;
19
20 import eu.etaxonomy.cdm.api.application.CdmApplicationController;
21 import eu.etaxonomy.cdm.api.conversation.IConversationEnabled;
22 import eu.etaxonomy.cdm.io.common.CdmDefaultExport;
23 import eu.etaxonomy.cdm.io.common.IExportConfigurator;
24 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
25 import eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator;
26 import eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator;
27 import eu.etaxonomy.taxeditor.model.CdmProgressMonitorAdapter;
28 import eu.etaxonomy.taxeditor.store.StoreUtil;
29
30 /**
31 * <p>
32 * ExportHandler class.
33 * </p>
34 *
35 * @author n.hoffmann
36 * @created Sep 11, 2009
37 * @version 1.0
38 */
39 public class ExportManager extends AbstractIOManager<IExportConfigurator> {
40
41 /**
42 *
43 * @param applicationController
44 */
45 private ExportManager(CdmApplicationController applicationController) {
46 super(applicationController);
47 }
48
49 /**
50 * <p>
51 * NewInstance
52 * </p>
53 *
54 * @param applicationController
55 * a
56 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
57 * object.
58 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
59 */
60 public static ExportManager NewInstance(
61 CdmApplicationController applicationController) {
62 return new ExportManager(applicationController);
63 }
64
65 /**
66 * <p>
67 * createIOJob
68 * </p>
69 *
70 * @param configurator
71 * a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
72 * object.
73 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
74 */
75 @Override
76 protected Job createIOJob(final IExportConfigurator configurator) {
77
78 Assert.isNotNull(configurator, "Configuration may not be null");
79
80 final Display display = Display.getCurrent();
81
82 Job job = new Job("Export: " + configurator.getClass().getSimpleName()) {
83 @Override
84 protected IStatus run(IProgressMonitor monitor) {
85 monitor.beginTask(
86 "Exporting database. This will take some time.", 100);
87 monitor.worked(10);
88
89 display.asyncExec(new Runnable() {
90
91 @Override
92 public void run() {
93 // terminate any open transactions
94 IConversationEnabled activePart = (IConversationEnabled) StoreUtil
95 .getActivePage().getActivePart();
96 activePart.getConversationHolder().commit(false);
97 }
98 });
99
100 monitor.worked(10);
101
102 CdmDefaultExport<IExportConfigurator> exporter = new CdmDefaultExport<IExportConfigurator>();
103 configurator.setProgressMonitor(CdmProgressMonitorAdapter
104 .CreateSubMonitor(monitor, 80));
105 exporter.setCdmAppController(applicationController);
106 monitor.worked(10);
107
108 try {
109 exporter.invoke(configurator);
110 monitor.worked(60);
111 } catch (RuntimeException e) {
112 StoreUtil.errorDialog("Error exporting data", this,
113 "An error occured while"
114 + "exporting to destination '"
115 + configurator.getDestinationNameString()
116 + "'.\n"
117 + "Please check error log for details.", e);
118 }
119
120 display.asyncExec(new Runnable() {
121
122 @Override
123 public void run() {
124 // terminate any open transactions
125 IConversationEnabled activePart = (IConversationEnabled) StoreUtil
126 .getActivePage().getActivePart();
127 // restarting transaction and committing it to trigger
128 // change listener
129 // TODO verify correct behaviour
130 activePart.getConversationHolder().startTransaction();
131 activePart.getConversationHolder().commit();
132 }
133 });
134 monitor.worked(10);
135
136 return Status.OK_STATUS;
137 }
138 };
139
140 return job;
141 }
142
143 /**
144 * @param jaxb
145 * @return
146 */
147 private IIoConfigurator getConfigurator(TYPE type) {
148 Assert.isNotNull(type, "Type should not be null");
149
150 switch (type) {
151 case Jaxb:
152 return JaxbExportConfigurator.NewInstance(null, null);
153 case Sdd:
154 return SDDExportConfigurator.NewInstance(null, null, null);
155 default:
156 StoreUtil.notImplementedMessage(this);
157 throw new IllegalArgumentException("Export not supported yet");
158 }
159 }
160
161 /**
162 * <p>
163 * JaxbConfigurator
164 * </p>
165 *
166 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
167 */
168 public final JaxbExportConfigurator JaxbConfigurator() {
169 return (JaxbExportConfigurator) getConfigurator(TYPE.Jaxb);
170 }
171
172 /**
173 * <p>
174 * SddConfigurator
175 * </p>
176 *
177 * @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
178 * object.
179 */
180 public final SDDExportConfigurator SddConfigurator() {
181 return (SDDExportConfigurator) getConfigurator(TYPE.Sdd);
182 }
183
184 }