1 |
1600af50
|
Katja Luther
|
/**
|
2 |
|
|
* Copyright (C) 2015 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 |
|
|
package eu.etaxonomy.cdm.io.service;
|
10 |
|
|
|
11 |
|
|
import java.io.ByteArrayInputStream;
|
12 |
|
|
import java.io.FileOutputStream;
|
13 |
|
|
import java.io.IOException;
|
14 |
|
|
import java.io.Serializable;
|
15 |
|
|
import java.nio.file.Files;
|
16 |
|
|
import java.nio.file.Path;
|
17 |
|
|
import java.util.List;
|
18 |
|
|
import java.util.UUID;
|
19 |
|
|
|
20 |
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
21 |
|
|
import org.springframework.beans.factory.annotation.Qualifier;
|
22 |
|
|
import org.springframework.stereotype.Service;
|
23 |
|
|
import org.springframework.transaction.annotation.Transactional;
|
24 |
|
|
|
25 |
|
|
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
|
26 |
|
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
27 |
|
|
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
|
28 |
|
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
|
29 |
|
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
|
30 |
|
|
import eu.etaxonomy.cdm.io.common.ExportResult;
|
31 |
|
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
32 |
|
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator.TARGET;
|
33 |
|
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
34 |
|
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
|
35 |
|
|
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
|
36 |
|
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
37 |
4da4f88a
|
Katja Luther
|
import eu.etaxonomy.cdm.io.distribution.excelupdate.ExcelDistributionUpdateConfigurator;
|
38 |
17d2281a
|
Katja Luther
|
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
|
39 |
4df9f28c
|
Andreas Müller
|
import eu.etaxonomy.cdm.io.operation.config.CacheUpdaterConfigurator;
|
40 |
|
|
import eu.etaxonomy.cdm.io.operation.config.SortIndexUpdaterConfigurator;
|
41 |
17d2281a
|
Katja Luther
|
import eu.etaxonomy.cdm.io.reference.ris.in.RisReferenceImportConfigurator;
|
42 |
1600af50
|
Katja Luther
|
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
|
43 |
|
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
44 |
|
|
|
45 |
|
|
/**
|
46 |
|
|
* @author cmathew
|
47 |
53db84af
|
Andreas Müller
|
* @since 31 Jul 2015
|
48 |
1600af50
|
Katja Luther
|
*
|
49 |
|
|
*/
|
50 |
|
|
@Service
|
51 |
|
|
@Transactional(readOnly = false)
|
52 |
|
|
public class IOServiceImpl implements IIOService {
|
53 |
|
|
|
54 |
|
|
@Autowired
|
55 |
|
|
CdmApplicationAwareDefaultExport cdmExport;
|
56 |
|
|
|
57 |
|
|
@Autowired
|
58 |
|
|
@Qualifier("defaultImport")
|
59 |
ad4315e4
|
unknown
|
CdmApplicationAwareDefaultImport<?> cdmImport;
|
60 |
1600af50
|
Katja Luther
|
|
61 |
|
|
@Autowired
|
62 |
|
|
IProgressMonitorService progressMonitorService;
|
63 |
22d2dd81
|
Katja Luther
|
//
|
64 |
|
|
// @Autowired
|
65 |
|
|
// @Qualifier("defaultUpdate")
|
66 |
|
|
// CdmApplicationAwareDefaultUpdate cdmUpdate;
|
67 |
1600af50
|
Katja Luther
|
|
68 |
|
|
|
69 |
|
|
@Override
|
70 |
|
|
public ExportResult export(IExportConfigurator config) {
|
71 |
|
|
config.setTarget(TARGET.EXPORT_DATA);
|
72 |
|
|
return cdmExport.execute(config);
|
73 |
|
|
}
|
74 |
|
|
|
75 |
|
|
|
76 |
|
|
@Override
|
77 |
|
|
public UUID monitImportData(final IImportConfigurator configurator, final byte[] importData, final SOURCE_TYPE type) {
|
78 |
|
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
79 |
|
|
@Override
|
80 |
|
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
81 |
62d8efcf
|
Katja Luther
|
|
82 |
1600af50
|
Katja Luther
|
configurator.setProgressMonitor(monitor);
|
83 |
|
|
ImportResult result = importData(configurator, importData, type);
|
84 |
|
|
for(byte[] report : result.getReports()) {
|
85 |
|
|
monitor.addReport(new String(report));
|
86 |
|
|
}
|
87 |
|
|
return result;
|
88 |
|
|
}
|
89 |
|
|
};
|
90 |
|
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
91 |
b6cbd329
|
Katja Luther
|
monitorThread.setPriority(2);
|
92 |
1600af50
|
Katja Luther
|
monitorThread.start();
|
93 |
|
|
return uuid;
|
94 |
|
|
}
|
95 |
|
|
|
96 |
03ebde9e
|
Katja Luther
|
@Override
|
97 |
0befeb80
|
unknown
|
public UUID monitExportData(@SuppressWarnings("rawtypes") final IExportConfigurator configurator) {
|
98 |
03ebde9e
|
Katja Luther
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
99 |
|
|
@Override
|
100 |
|
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
101 |
|
|
|
102 |
|
|
configurator.setProgressMonitor(monitor);
|
103 |
7588eacb
|
Katja Luther
|
// monitor.beginTask("Start Export", 3);
|
104 |
03ebde9e
|
Katja Luther
|
ExportResult result = export(configurator);
|
105 |
|
|
// for(byte[] report : result.getReports()) {
|
106 |
|
|
// monitor.addReport(new String(report));
|
107 |
|
|
// }
|
108 |
|
|
return result;
|
109 |
|
|
}
|
110 |
|
|
};
|
111 |
|
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
112 |
|
|
monitorThread.setPriority(3);
|
113 |
|
|
monitorThread.start();
|
114 |
|
|
return uuid;
|
115 |
|
|
}
|
116 |
|
|
|
117 |
bd38ac23
|
Katja Luther
|
// @Override
|
118 |
|
|
// public UUID monitUpdateData(final IImportConfigurator configurator) {
|
119 |
|
|
// RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
120 |
|
|
// @Override
|
121 |
|
|
// public Serializable doRun(IRemotingProgressMonitor monitor) {
|
122 |
|
|
//
|
123 |
|
|
// configurator.setProgressMonitor(monitor);
|
124 |
|
|
// ImportResult result =updateData((SecundumForSubtreeConfigurator)configurator);
|
125 |
|
|
//
|
126 |
|
|
// return result;
|
127 |
|
|
// }
|
128 |
|
|
// };
|
129 |
|
|
// UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
130 |
|
|
// monitorThread.setPriority(3);
|
131 |
|
|
// monitorThread.start();
|
132 |
|
|
// return uuid;
|
133 |
|
|
// }
|
134 |
22d2dd81
|
Katja Luther
|
|
135 |
1600af50
|
Katja Luther
|
@Override
|
136 |
|
|
public ImportResult importData(IImportConfigurator configurator, byte[] importData, SOURCE_TYPE type) {
|
137 |
|
|
ImportResult result;
|
138 |
|
|
switch(type) {
|
139 |
|
|
case URI:
|
140 |
62d8efcf
|
Katja Luther
|
if (importData.equals(new byte[1])){
|
141 |
ad4315e4
|
unknown
|
result = cdmImport.invoke(configurator);
|
142 |
62d8efcf
|
Katja Luther
|
return result;
|
143 |
|
|
}
|
144 |
1600af50
|
Katja Luther
|
return importDataFromUri(configurator, importData);
|
145 |
|
|
case INPUTSTREAM:
|
146 |
|
|
return importDataFromInputStream(configurator,importData);
|
147 |
|
|
default :
|
148 |
|
|
throw new RuntimeException("Source type is not recongnised");
|
149 |
|
|
}
|
150 |
|
|
}
|
151 |
|
|
|
152 |
bd38ac23
|
Katja Luther
|
// @Override
|
153 |
|
|
// public ImportResult updateData(SecundumForSubtreeConfigurator configurator) {
|
154 |
|
|
// ImportResult result;
|
155 |
|
|
//
|
156 |
|
|
// result = cdmImport.invoke(configurator);
|
157 |
|
|
// return result;
|
158 |
|
|
// }
|
159 |
22d2dd81
|
Katja Luther
|
|
160 |
1600af50
|
Katja Luther
|
@Override
|
161 |
|
|
public ImportResult importDataFromUri(IImportConfigurator configurator, byte[] importData) {
|
162 |
|
|
ImportResult result;
|
163 |
|
|
|
164 |
|
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
165 |
|
|
String suffix = ".import";
|
166 |
|
|
String prefix = "cdm-";
|
167 |
|
|
FileOutputStream stream = null;
|
168 |
|
|
|
169 |
|
|
try {
|
170 |
|
|
Path tempFilePath = Files.createTempFile(prefix, suffix);
|
171 |
|
|
stream = new FileOutputStream(tempFilePath.toFile());
|
172 |
|
|
stream.write(importData);
|
173 |
|
|
config.setSource(tempFilePath.toUri());
|
174 |
ad4315e4
|
unknown
|
result = cdmImport.invoke(config);
|
175 |
1600af50
|
Katja Luther
|
// Files.delete(tempFilePath);
|
176 |
|
|
} catch (Exception e) {
|
177 |
|
|
throw new RuntimeException(e);
|
178 |
|
|
} finally {
|
179 |
|
|
if(stream != null) {
|
180 |
|
|
try {
|
181 |
|
|
stream.close();
|
182 |
|
|
} catch (IOException e) {
|
183 |
|
|
throw new RuntimeException(e);
|
184 |
|
|
}
|
185 |
|
|
}
|
186 |
|
|
}
|
187 |
|
|
return result;
|
188 |
|
|
}
|
189 |
|
|
|
190 |
|
|
@Override
|
191 |
|
|
public ImportResult importDataFromInputStream(IImportConfigurator configurator, byte[] importData) {
|
192 |
|
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
193 |
|
|
ImportResult result;
|
194 |
|
|
try {
|
195 |
17d2281a
|
Katja Luther
|
if (config instanceof ExcelImportConfiguratorBase){
|
196 |
|
|
ExcelImportConfiguratorBase excelConfig = (ExcelImportConfiguratorBase)config;
|
197 |
4ed92181
|
Katja Luther
|
//excelConfig.setStream(importData);
|
198 |
62d8efcf
|
Katja Luther
|
}else{
|
199 |
|
|
config.setSource(new ByteArrayInputStream(importData));
|
200 |
|
|
}
|
201 |
ad4315e4
|
unknown
|
result = cdmImport.invoke(config);
|
202 |
1600af50
|
Katja Luther
|
} catch (Exception e) {
|
203 |
|
|
throw new RuntimeException(e);
|
204 |
|
|
}
|
205 |
|
|
return result;
|
206 |
|
|
}
|
207 |
|
|
|
208 |
|
|
|
209 |
|
|
@Override
|
210 |
|
|
public ImportResult importDataFromStream(SpecimenImportConfiguratorBase configurator) {
|
211 |
|
|
ImportResult result = new ImportResult();
|
212 |
ad4315e4
|
unknown
|
result = cdmImport.invoke(configurator);
|
213 |
1600af50
|
Katja Luther
|
return result;
|
214 |
|
|
}
|
215 |
|
|
|
216 |
|
|
@Override
|
217 |
|
|
public ImportResult importDataFromStream(List<Abcd206ImportConfigurator> configurators) {
|
218 |
|
|
ImportResult result = new ImportResult();
|
219 |
|
|
|
220 |
|
|
for (SpecimenImportConfiguratorBase configurator:configurators){
|
221 |
ad4315e4
|
unknown
|
result = cdmImport.invoke(configurator);
|
222 |
1600af50
|
Katja Luther
|
}
|
223 |
|
|
return result;
|
224 |
|
|
}
|
225 |
|
|
|
226 |
|
|
|
227 |
6a3f4c50
|
Katja Luther
|
/**
|
228 |
|
|
* {@inheritDoc}
|
229 |
|
|
*/
|
230 |
|
|
@Override
|
231 |
|
|
public ImportResult updateSortIndex(SortIndexUpdaterConfigurator config) {
|
232 |
|
|
ImportResult result = new ImportResult();
|
233 |
|
|
|
234 |
|
|
result = cdmImport.invoke(config);
|
235 |
|
|
return result;
|
236 |
|
|
}
|
237 |
|
|
|
238 |
7cbe2b56
|
Katja Luther
|
/**
|
239 |
|
|
* {@inheritDoc}
|
240 |
|
|
*/
|
241 |
|
|
@Override
|
242 |
|
|
public ImportResult updateCaches(CacheUpdaterConfigurator config) {
|
243 |
|
|
ImportResult result = new ImportResult();
|
244 |
|
|
|
245 |
|
|
result = cdmImport.invoke(config);
|
246 |
|
|
return result;
|
247 |
|
|
}
|
248 |
|
|
|
249 |
6a3f4c50
|
Katja Luther
|
|
250 |
4da4f88a
|
Katja Luther
|
/**
|
251 |
|
|
* {@inheritDoc}
|
252 |
|
|
*/
|
253 |
|
|
@Override
|
254 |
|
|
public ImportResult updateDistributionData(ExcelDistributionUpdateConfigurator configurator) {
|
255 |
|
|
ImportResult result = new ImportResult();
|
256 |
|
|
result = cdmImport.invoke(configurator);
|
257 |
|
|
return result;
|
258 |
|
|
}
|
259 |
|
|
|
260 |
17d2281a
|
Katja Luther
|
/**
|
261 |
|
|
* {@inheritDoc}
|
262 |
|
|
*/
|
263 |
|
|
@Override
|
264 |
|
|
public ImportResult importRISData(RisReferenceImportConfigurator configurator) {
|
265 |
|
|
ImportResult result = new ImportResult();
|
266 |
|
|
result = cdmImport.invoke(configurator);
|
267 |
|
|
return result;
|
268 |
|
|
}
|
269 |
|
|
|
270 |
4da4f88a
|
Katja Luther
|
|
271 |
1600af50
|
Katja Luther
|
|
272 |
|
|
|
273 |
|
|
|
274 |
|
|
}
|