Merge branch 'develop' into featureTreeEditor
[taxeditor.git] / eu.etaxonomy.taxeditor.store / src / main / java / eu / etaxonomy / taxeditor / io / AbstractIOManager.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.util.List;
13
14 import org.apache.commons.lang.StringUtils;
15 import org.eclipse.core.runtime.jobs.Job;
16 import org.eclipse.swt.widgets.Display;
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.progress.IProgressConstants;
19
20 import eu.etaxonomy.cdm.api.application.ICdmRepository;
21 import eu.etaxonomy.cdm.common.IoResultBase;
22 import eu.etaxonomy.cdm.io.common.IIoConfigurator;
23 import eu.etaxonomy.cdm.io.common.ImportResult;
24 import eu.etaxonomy.taxeditor.store.CdmStore;
25 import eu.etaxonomy.taxeditor.ui.dialog.ReportTextDialog;
26
27 /**
28 * <p>
29 * Abstract AbstractIOHandler class.
30 * </p>
31 *
32 * @author n.hoffmann
33 * @created Sep 11, 2009
34 * @version 1.0
35 */
36 public abstract class AbstractIOManager<CONFIGURATOR extends IIoConfigurator> {
37
38 protected ICdmRepository applicationConfiguration;
39
40 public static enum TYPE {
41 Jaxb, Tcs, Excel_Taxa, Endnote, Sdd, Abcd, SpecimenCdmExcel, Excel_Name, SpecimenSearch, Gbif, Excel_Distribution, Ris
42 }
43
44 /**
45 * <p>
46 * Constructor for AbstractIOHandler.
47 * </p>
48 *
49 * @param applicationController
50 * a
51 * {@link eu.etaxonomy.cdm.api.application.CdmApplicationController}
52 * object.
53 * @param <CONFIGURATOR>
54 * a CONFIGURATOR object.
55 */
56 protected AbstractIOManager(
57 ICdmRepository applicationConfiguration) {
58 this.applicationConfiguration = applicationConfiguration;
59 }
60
61 /**
62 * Starts the IO process
63 *
64 * @param configurator
65 * a CONFIGURATOR object.
66 */
67 public void run(final CONFIGURATOR configurator) {
68 // create job
69 Job job = createIOJob(configurator);
70 run(job);
71 }
72
73 public void run(Job job) {
74 // configure the job
75 job.setProperty(IProgressConstants.KEEP_PROPERTY, true);
76 job.setUser(true);
77 // schedule job
78 job.schedule();
79 }
80
81 /**
82 * <p>
83 * createIOJob
84 * </p>
85 *
86 * @param configurator
87 * a CONFIGURATOR object.
88 * @return a {@link org.eclipse.core.runtime.jobs.Job} object.
89 */
90 protected abstract Job createIOJob(CONFIGURATOR configurator);
91
92 /**
93 * @param configurator
94 * @param display
95 * @param importResult
96 */
97 protected void showResult(String importName, final Display display,
98 IoResultBase ioResult) {
99 StringBuffer reportTextTemp = ioResult.createReport();
100 final StringBuffer reportText;
101 if (StringUtils.isBlank(reportTextTemp.toString()) && ioResult instanceof ImportResult){
102 //reportTextTemp.append("No update result available");
103 //TODO: this is a workaround because the abcd import writes the report in the report part...
104 ImportResult importResult = (ImportResult)ioResult;
105 if (!importResult.getReports().isEmpty() && importResult.getReports().get(0) != null){
106 reportTextTemp = new StringBuffer();
107 if(importResult!=null){
108 List<byte[]> reports = importResult.getReports();
109 for (byte[] bs : reports) {
110 if (bs != null){ reportTextTemp.append(new String(bs));}
111 }
112 }
113 }
114 }
115 reportText = reportTextTemp;
116 if (StringUtils.isBlank(reportText.toString())){ return;}
117 display.asyncExec(new Runnable() {
118
119 @Override
120 public void run() {
121 // display reports with possibility to save
122 ReportTextDialog dialog = new ReportTextDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell());
123 dialog.setTitle(importName+" Report");
124 dialog.setReportText(reportText.toString());
125 dialog.open();
126 CdmStore.getContextManager().notifyContextRefresh();
127 }
128 });
129 }
130
131 }