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