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