add abcd import configuration to preferences
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / ExportManager.java
1 /**
2 * Copyright (C) 2007 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
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.
8 */
9
10 package eu.etaxonomy.taxeditor.io;
11
12 import java.io.File;
13 import java.io.FileOutputStream;
14 import java.util.List;
15 import java.util.Map;
16 import java.util.Set;
17
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;
27
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;
47
48 /**
49 * <p>
50 * ExportHandler class.
51 * </p>
52 *
53 * @author n.hoffmann
54 * @created Sep 11, 2009
55 * @version 1.0
56 */
57 public class ExportManager extends AbstractIOManager<IExportConfigurator> {
58
59 private static final Logger logger = Logger.getLogger(ExportManager.class);
60
61 /**
62 *
63 * @param applicationConfiguration
64 */
65 private ExportManager(ICdmRepository applicationConfiguration) {
66 super(applicationConfiguration);
67 }
68
69 /**
70 * <p>
71 * NewInstance
72 * </p>
73 *
74 * @param applicationConfiguration
75 * a
76 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
77 * object.
78 * @return a {@link eu.etaxonomy.taxeditor.io.ExportManager} object.
79 */
80 public static ExportManager NewInstance(
81 ICdmRepository applicationConfiguration) {
82 return new ExportManager(applicationConfiguration);
83 }
84
85 /**
86 * <p>
87 * createIOJob
88 * </p>
89 *
90 * @param configurator
91 * a {@link eu.etaxonomy.cdm.io.common.IExportConfigurator}
92 * object.
93 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
94 */
95 @Override
96 protected Job createIOJob(final IExportConfigurator configurator) {
97
98 Assert.isNotNull(configurator, "Configuration may not be null");
99
100 final Display display = Display.getCurrent();
101
102 Job job = new Job("Export: " + configurator.getClass().getSimpleName()) {
103 @Override
104 protected IStatus run(IProgressMonitor monitor) {
105 monitor.beginTask(
106 "Exporting database. This will take some time.", 100);
107 monitor.worked(10);
108
109 /** see ticket # 4456 and */
110 // display.asyncExec(new Runnable() {
111 //
112 // @Override
113 // public void run() {
114 //
115 // // terminate any open transactions
116 // IConversationEnabled activePart = (IConversationEnabled) StoreUtil
117 // .getActivePage().getActivePart();
118 // activePart.getConversationHolder().commit(false);
119 // }
120 // });
121
122 // monitor.worked(10);
123
124 CdmDefaultExport<IExportConfigurator> exporter = new CdmDefaultExport<IExportConfigurator>();
125 configurator.setProgressMonitor(CdmProgressMonitorAdapter
126 .CreateSubMonitor(monitor, 80));
127 exporter.setCdmAppController(applicationConfiguration);
128 monitor.worked(10);
129
130 try {
131 exporter.invoke(configurator);
132 monitor.worked(60);
133 } catch (RuntimeException e) {
134 MessagingUtils.messageDialog("Error exporting data", this,
135 "An error occured while"
136 + "exporting to destination '"
137 + configurator.getDestinationNameString()
138 + "'.\n"
139 + "Please check error log for details.", e);
140 }
141
142 display.asyncExec(new Runnable() {
143
144 @Override
145 public void run() {
146 IWorkbenchPart activePart = StoreUtil.getActivePage()
147 .getActivePart();
148 if (activePart instanceof IConversationEnabled) {
149 // terminate any open transactions
150 IConversationEnabled conversationEnabled = (IConversationEnabled) activePart;
151 // restarting transaction and committing it to
152 // trigger
153 // change listener
154 // TODO verify correct behaviour
155
156 try{
157 conversationEnabled.getConversationHolder()
158 .startTransaction();
159
160 conversationEnabled.getConversationHolder()
161 .commit();
162 }catch(RuntimeException e){
163 MessagingUtils.messageDialog("Error starting conversation handling", this, "" +
164 "Conversation Error: "+ e);
165 }
166 }
167 }
168 });
169 monitor.worked(10);
170
171 return Status.OK_STATUS;
172 }
173 };
174
175 return job;
176 }
177
178 public Job createIOServiceJob(final IExportConfigurator configurator, final File exportFile) {
179 Assert.isNotNull(configurator, "Configuration may not be null");
180
181 // final Display display = Display.getCurrent();
182
183 Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
184 @Override
185 protected IStatus run(IProgressMonitor monitor) {
186 monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
187 try {
188 IIOService ioService = CdmApplicationState.getIOService();
189
190 ExportResult result = ioService.export(configurator);
191 ExportDataWrapper data = result.getExportData();
192 try{
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);
198 stream.close();
199 }
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);
208 // stream.close();
209 // }
210
211 }else{
212 logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
213 }
214 } catch(Exception e){
215 logger.error(e.getStackTrace());
216 }
217
218
219
220
221 } catch (Exception e) {
222 MessagingUtils.errorDialog("Error exporting data",
223 this,
224 e.getMessage(),
225 TaxeditorStorePlugin.PLUGIN_ID,
226 e,
227 true);
228 }
229 return Status.OK_STATUS;
230 }
231 };
232 return job;
233 }
234 /**
235 * @param jaxb
236 * @return
237 */
238 private IIoConfigurator getConfigurator(TYPE type) {
239 Assert.isNotNull(type, "Type should not be null");
240
241 switch (type) {
242 case Jaxb:
243 return JaxbExportConfigurator.NewInstance(null, null);
244 case Sdd:
245 return SDDExportConfigurator.NewInstance(null, null, null);
246 default:
247 MessagingUtils.notImplementedMessage(this);
248 throw new IllegalArgumentException("Export not supported yet");
249 }
250 }
251
252 /**
253 * <p>
254 * JaxbConfigurator
255 * </p>
256 *
257 * @return a {@link eu.etaxonomy.cdm.io.jaxb.JaxbExportConfigurator} object.
258 */
259 public final JaxbExportConfigurator JaxbConfigurator() {
260 return (JaxbExportConfigurator) getConfigurator(TYPE.Jaxb);
261 }
262
263 /**
264 * <p>
265 * SddConfigurator
266 * </p>
267 *
268 * @return a {@link eu.etaxonomy.cdm.io.sdd.out.SDDExportConfigurator}
269 * object.
270 */
271 public final SDDExportConfigurator SddConfigurator() {
272 return (SDDExportConfigurator) getConfigurator(TYPE.Sdd);
273 }
274
275 /**
276 * @param configurator
277 * @param urlString
278 * @return
279 */
280 public Job createIOServiceJob(OutputModelConfigurator configurator, String urlString) {
281 Assert.isNotNull(configurator, "Configuration may not be null");
282
283 // final Display display = Display.getCurrent();
284
285 Job job = new Job("Export: " + configurator.getClass().getSimpleName()) { //$NON-NLS-1$
286 @Override
287 protected IStatus run(IProgressMonitor monitor) {
288 monitor.beginTask("Exporting database. This will take some time.", IProgressMonitor.UNKNOWN);
289 try {
290 IIOService ioService = CdmApplicationState.getIOService();
291
292 ExportResult result = ioService.export(configurator);
293 ExportDataWrapper data = result.getExportData();
294 try{
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);
301 stream.close();
302 }
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){
310 fileEnding = ".csv";
311 }
312 File file = new File(urlString+File.separator + key + fileEnding);
313 FileOutputStream stream = new FileOutputStream(file);
314 stream.write(fileData);
315 stream.close();
316 }
317
318 }else{
319 logger.error("This kind of result data is not supported yet." + result.getExportData().getType().toString());
320 }
321 } catch(Exception e){
322 logger.error(e.getStackTrace());
323 }
324 final StringBuilder reportText = new StringBuilder();
325 if(result!=null){
326 List<byte[]> reports = result.getErrors();
327 reportText.append("Errors:\\r");
328 for (byte[] bs : reports) {
329 reportText.append(new String(bs));
330 }
331 List<byte[]> warnings = result.getWarnings();
332 reportText.append("Warnings:\\r");
333 for (byte[] bs : warnings) {
334 reportText.append(new String(bs));
335 }
336 List<Exception> exceptions = result.getExceptions();
337 reportText.append("Exceptions:\\r");
338 for (Exception ex : exceptions) {
339 reportText.append(ex.getStackTrace());
340 }
341
342
343 }
344 final Display display = Display.getDefault();
345 display.asyncExec(new Runnable() {
346
347 @Override
348 public void run() {
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());
353 dialog.open();
354 CdmStore.getContextManager().notifyContextRefresh();
355 }
356 });
357
358
359
360 } catch (Exception e) {
361 MessagingUtils.errorDialog("Error exporting data",
362 this,
363 e.getMessage(),
364 TaxeditorStorePlugin.PLUGIN_ID,
365 e,
366 true);
367 }
368
369 return Status.OK_STATUS;
370 }
371 };
372 return job;
373 }
374
375
376
377 }