1
|
// $Id$
|
2
|
/**
|
3
|
* Copyright (C) 2015 EDIT
|
4
|
* European Distributed Institute of Taxonomy
|
5
|
* http://www.e-taxonomy.eu
|
6
|
*
|
7
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
8
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
9
|
*/
|
10
|
package eu.etaxonomy.cdm.io.service;
|
11
|
|
12
|
import java.io.ByteArrayInputStream;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.IOException;
|
15
|
import java.io.Serializable;
|
16
|
import java.nio.file.Files;
|
17
|
import java.nio.file.Path;
|
18
|
import java.util.List;
|
19
|
import java.util.UUID;
|
20
|
|
21
|
import org.springframework.beans.factory.annotation.Autowired;
|
22
|
import org.springframework.beans.factory.annotation.Qualifier;
|
23
|
import org.springframework.stereotype.Service;
|
24
|
import org.springframework.transaction.annotation.Transactional;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.service.IProgressMonitorService;
|
27
|
import eu.etaxonomy.cdm.common.monitor.IRemotingProgressMonitor;
|
28
|
import eu.etaxonomy.cdm.common.monitor.RemotingProgressMonitorThread;
|
29
|
import eu.etaxonomy.cdm.ext.occurrence.OccurenceQuery;
|
30
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultExport;
|
31
|
import eu.etaxonomy.cdm.io.common.CdmApplicationAwareDefaultImport;
|
32
|
import eu.etaxonomy.cdm.io.common.ExportResult;
|
33
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator;
|
34
|
import eu.etaxonomy.cdm.io.common.IExportConfigurator.TARGET;
|
35
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator;
|
36
|
import eu.etaxonomy.cdm.io.common.IImportConfigurator.SOURCE_TYPE;
|
37
|
import eu.etaxonomy.cdm.io.common.ImportConfiguratorBase;
|
38
|
import eu.etaxonomy.cdm.io.common.ImportResult;
|
39
|
import eu.etaxonomy.cdm.io.specimen.SpecimenImportConfiguratorBase;
|
40
|
import eu.etaxonomy.cdm.io.specimen.abcd206.in.Abcd206ImportConfigurator;
|
41
|
|
42
|
/**
|
43
|
* @author cmathew
|
44
|
* @date 31 Jul 2015
|
45
|
*
|
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
|
|
62
|
|
63
|
/* (non-Javadoc)
|
64
|
* @see eu.etaxonomy.cdm.io.service.IExportService#export(eu.etaxonomy.cdm.io.common.IExportConfigurator)
|
65
|
*/
|
66
|
@Override
|
67
|
public ExportResult export(IExportConfigurator config) {
|
68
|
config.setTarget(TARGET.EXPORT_DATA);
|
69
|
return cdmExport.execute(config);
|
70
|
}
|
71
|
|
72
|
|
73
|
@Override
|
74
|
public UUID monitImportData(final IImportConfigurator configurator, final byte[] importData, final SOURCE_TYPE type) {
|
75
|
RemotingProgressMonitorThread monitorThread = new RemotingProgressMonitorThread() {
|
76
|
@Override
|
77
|
public Serializable doRun(IRemotingProgressMonitor monitor) {
|
78
|
configurator.setProgressMonitor(monitor);
|
79
|
ImportResult result = importData(configurator, importData, type);
|
80
|
for(byte[] report : result.getReports()) {
|
81
|
monitor.addReport(new String(report));
|
82
|
}
|
83
|
return result;
|
84
|
}
|
85
|
};
|
86
|
UUID uuid = progressMonitorService.registerNewRemotingMonitor(monitorThread);
|
87
|
monitorThread.setPriority(3);
|
88
|
monitorThread.start();
|
89
|
return uuid;
|
90
|
}
|
91
|
|
92
|
@Override
|
93
|
public ImportResult importData(IImportConfigurator configurator, byte[] importData, SOURCE_TYPE type) {
|
94
|
ImportResult result;
|
95
|
switch(type) {
|
96
|
case URI:
|
97
|
return importDataFromUri(configurator, importData);
|
98
|
case INPUTSTREAM:
|
99
|
return importDataFromInputStream(configurator,importData);
|
100
|
default :
|
101
|
throw new RuntimeException("Source type is not recongnised");
|
102
|
}
|
103
|
}
|
104
|
|
105
|
@Override
|
106
|
public ImportResult importDataFromUri(IImportConfigurator configurator, byte[] importData) {
|
107
|
ImportResult result;
|
108
|
|
109
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
110
|
String suffix = ".import";
|
111
|
String prefix = "cdm-";
|
112
|
FileOutputStream stream = null;
|
113
|
|
114
|
try {
|
115
|
Path tempFilePath = Files.createTempFile(prefix, suffix);
|
116
|
stream = new FileOutputStream(tempFilePath.toFile());
|
117
|
stream.write(importData);
|
118
|
config.setSource(tempFilePath.toUri());
|
119
|
result = cdmImport.execute(config);
|
120
|
Files.delete(tempFilePath);
|
121
|
} catch (Exception e) {
|
122
|
throw new RuntimeException(e);
|
123
|
} finally {
|
124
|
if(stream != null) {
|
125
|
try {
|
126
|
stream.close();
|
127
|
} catch (IOException e) {
|
128
|
throw new RuntimeException(e);
|
129
|
}
|
130
|
}
|
131
|
}
|
132
|
return result;
|
133
|
}
|
134
|
|
135
|
@Override
|
136
|
public ImportResult importDataFromInputStream(IImportConfigurator configurator, byte[] importData) {
|
137
|
ImportConfiguratorBase config = (ImportConfiguratorBase)configurator;
|
138
|
ImportResult result;
|
139
|
try {
|
140
|
config.setSource(new ByteArrayInputStream(importData));
|
141
|
result = cdmImport.execute(config);
|
142
|
} catch (Exception e) {
|
143
|
throw new RuntimeException(e);
|
144
|
}
|
145
|
return result;
|
146
|
}
|
147
|
|
148
|
|
149
|
@Override
|
150
|
public ImportResult importDataFromStream(SpecimenImportConfiguratorBase configurator) {
|
151
|
ImportResult result = new ImportResult();
|
152
|
|
153
|
OccurenceQuery query;
|
154
|
result = cdmImport.execute(configurator);
|
155
|
return result;
|
156
|
}
|
157
|
|
158
|
@Override
|
159
|
public ImportResult importDataFromStream(List<Abcd206ImportConfigurator> configurators) {
|
160
|
ImportResult result = new ImportResult();
|
161
|
|
162
|
OccurenceQuery query;
|
163
|
for (SpecimenImportConfiguratorBase configurator:configurators){
|
164
|
result = cdmImport.execute(configurator);
|
165
|
}
|
166
|
return result;
|
167
|
}
|
168
|
|
169
|
|
170
|
|
171
|
|
172
|
|
173
|
}
|