1
|
/**
|
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
|
import eu.etaxonomy.cdm.io.distribution.excelupdate.ExcelDistributionUpdateConfigurator;
|
38
|
import eu.etaxonomy.cdm.io.excel.common.ExcelImportConfiguratorBase;
|
39
|
import eu.etaxonomy.cdm.io.reference.ris.in.RisReferenceImportConfigurator;
|
40
|
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
|
41
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
42
|
|
43
|
/**
|
44
|
* @author cmathew
|
45
|
* @since 31 Jul 2015
|
46
|
*/
|
47
|
@Service
|
48
|
@Transactional(readOnly = false)
|
49
|
public class IOServiceImpl implements IIOService {
|
50
|
|
51
|
@Autowired
|
52
|
CdmApplicationAwareDefaultExport cdmExport;
|
53
|
|
54
|
@Autowired
|
55
|
@Qualifier("defaultImport")
|
56
|
CdmApplicationAwareDefaultImport<?> cdmImport;
|
57
|
|
58
|
@Autowired
|
59
|
IProgressMonitorService progressMonitorService;
|
60
|
|
61
|
@Override
|
62
|
public ExportResult export(IExportConfigurator config) {
|
63
|
config.setTarget(TARGET.EXPORT_DATA);
|
64
|
return cdmExport.execute(config);
|
65
|
}
|
66
|
|
67
|
@Override
|
68
|
public UUID monitImportData(final IImportConfigurator configurator, final byte[] importData, final SOURCE_TYPE type) {
|
69
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
70
|
@Override
|
71
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
72
|
|
73
|
configurator.setProgressMonitor(monitor);
|
74
|
ImportResult result = importData(configurator, importData, type);
|
75
|
for(byte[] report : result.getReports()) {
|
76
|
monitor.addReport(new String(report));
|
77
|
}
|
78
|
return result;
|
79
|
}
|
80
|
};
|
81
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
82
|
monitorThread.setPriority(2);
|
83
|
monitorThread.start();
|
84
|
return uuid;
|
85
|
}
|
86
|
|
87
|
@Override
|
88
|
public UUID monitExportData(@SuppressWarnings("rawtypes") final IExportConfigurator configurator) {
|
89
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
90
|
@Override
|
91
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
92
|
|
93
|
configurator.setProgressMonitor(monitor);
|
94
|
// monitor.beginTask("Start Export", 3);
|
95
|
ExportResult result = export(configurator);
|
96
|
// for(byte[] report : result.getReports()) {
|
97
|
// monitor.addReport(new String(report));
|
98
|
// }
|
99
|
return result;
|
100
|
}
|
101
|
};
|
102
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
103
|
monitorThread.setPriority(3);
|
104
|
monitorThread.start();
|
105
|
return uuid;
|
106
|
}
|
107
|
|
108
|
@Override
|
109
|
public ImportResult importData(IImportConfigurator configurator, byte[] importData, SOURCE_TYPE type) {
|
110
|
ImportResult result;
|
111
|
switch(type) {
|
112
|
case URI:
|
113
|
if (importData.equals(new byte[1])){
|
114
|
result = cdmImport.invoke(configurator);
|
115
|
return result;
|
116
|
}
|
117
|
return importDataFromUri(configurator, importData);
|
118
|
case INPUTSTREAM:
|
119
|
return importDataFromInputStream(configurator,importData);
|
120
|
default :
|
121
|
throw new RuntimeException("Source type is not recongnised");
|
122
|
}
|
123
|
}
|
124
|
|
125
|
@Override
|
126
|
public ImportResult importDataFromUri(IImportConfigurator configurator, byte[] importData) {
|
127
|
ImportResult result;
|
128
|
|
129
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
130
|
String suffix = ".import";
|
131
|
String prefix = "cdm-";
|
132
|
FileOutputStream stream = null;
|
133
|
|
134
|
try {
|
135
|
Path tempFilePath = Files.createTempFile(prefix, suffix);
|
136
|
stream = new FileOutputStream(tempFilePath.toFile());
|
137
|
stream.write(importData);
|
138
|
config.setSource(tempFilePath.toUri());
|
139
|
result = cdmImport.invoke(config);
|
140
|
// Files.delete(tempFilePath);
|
141
|
} catch (Exception e) {
|
142
|
throw new RuntimeException(e);
|
143
|
} finally {
|
144
|
if(stream != null) {
|
145
|
try {
|
146
|
stream.close();
|
147
|
} catch (IOException e) {
|
148
|
throw new RuntimeException(e);
|
149
|
}
|
150
|
}
|
151
|
}
|
152
|
return result;
|
153
|
}
|
154
|
|
155
|
@Override
|
156
|
public ImportResult importDataFromInputStream(IImportConfigurator configurator, byte[] importData) {
|
157
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
158
|
ImportResult result;
|
159
|
try {
|
160
|
if (config instanceof ExcelImportConfiguratorBase){
|
161
|
ExcelImportConfiguratorBase excelConfig = (ExcelImportConfiguratorBase)config;
|
162
|
//excelConfig.setStream(importData);
|
163
|
}else{
|
164
|
config.setSource(new ByteArrayInputStream(importData));
|
165
|
}
|
166
|
result = cdmImport.invoke(config);
|
167
|
} catch (Exception e) {
|
168
|
throw new RuntimeException(e);
|
169
|
}
|
170
|
return result;
|
171
|
}
|
172
|
|
173
|
|
174
|
@Override
|
175
|
public ImportResult importDataFromStream(SpecimenImportConfiguratorBase configurator) {
|
176
|
ImportResult result = new ImportResult();
|
177
|
result = cdmImport.invoke(configurator);
|
178
|
return result;
|
179
|
}
|
180
|
|
181
|
@Override
|
182
|
public ImportResult importDataFromStream(List<Abcd206ImportConfigurator> configurators) {
|
183
|
ImportResult result = new ImportResult();
|
184
|
|
185
|
for (SpecimenImportConfiguratorBase configurator:configurators){
|
186
|
result = cdmImport.invoke(configurator);
|
187
|
}
|
188
|
return result;
|
189
|
}
|
190
|
|
191
|
@Override
|
192
|
public ImportResult updateDistributionData(ExcelDistributionUpdateConfigurator configurator) {
|
193
|
ImportResult result = new ImportResult();
|
194
|
result = cdmImport.invoke(configurator);
|
195
|
return result;
|
196
|
}
|
197
|
|
198
|
@Override
|
199
|
public ImportResult importRISData(RisReferenceImportConfigurator configurator) {
|
200
|
ImportResult result = new ImportResult();
|
201
|
result = cdmImport.invoke(configurator);
|
202
|
return result;
|
203
|
}
|
204
|
}
|