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
|
package eu.etaxonomy.cdm.io.csv.redlist.demo;
|
10
|
|
11
|
import java.io.File;
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.FileOutputStream;
|
14
|
import java.io.IOException;
|
15
|
import java.io.OutputStream;
|
16
|
import java.io.OutputStreamWriter;
|
17
|
import java.io.PrintWriter;
|
18
|
import java.io.UnsupportedEncodingException;
|
19
|
import java.util.ArrayList;
|
20
|
import java.util.HashSet;
|
21
|
import java.util.List;
|
22
|
import java.util.Set;
|
23
|
import java.util.UUID;
|
24
|
|
25
|
import javax.xml.stream.XMLOutputFactory;
|
26
|
import javax.xml.stream.XMLStreamException;
|
27
|
import javax.xml.stream.XMLStreamWriter;
|
28
|
|
29
|
import org.apache.commons.lang.StringUtils;
|
30
|
import org.apache.log4j.Logger;
|
31
|
|
32
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
33
|
import eu.etaxonomy.cdm.io.common.CdmExportBase;
|
34
|
import eu.etaxonomy.cdm.io.common.ICdmExport;
|
35
|
import eu.etaxonomy.cdm.io.common.mapping.out.IExportTransformer;
|
36
|
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportConfiguratorRedlist;
|
37
|
import eu.etaxonomy.cdm.io.csv.redlist.out.CsvTaxExportStateRedlist;
|
38
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
39
|
import eu.etaxonomy.cdm.model.common.IOriginalSource;
|
40
|
import eu.etaxonomy.cdm.model.common.ISourceable;
|
41
|
import eu.etaxonomy.cdm.model.location.Country;
|
42
|
import eu.etaxonomy.cdm.model.location.NamedArea;
|
43
|
import eu.etaxonomy.cdm.model.taxon.Classification;
|
44
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
45
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
46
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
47
|
|
48
|
/**
|
49
|
* @author a.mueller, a.oppermann
|
50
|
\* @since 18.10.2012
|
51
|
*
|
52
|
*/
|
53
|
public abstract class CsvDemoBase
|
54
|
extends CdmExportBase<CsvDemoExportConfigurator, CsvDemoExportState, IExportTransformer,File>
|
55
|
implements ICdmExport<CsvDemoExportConfigurator, CsvDemoExportState>{
|
56
|
|
57
|
private static final long serialVersionUID = -2962456879635841019L;
|
58
|
private static final Logger logger = Logger.getLogger(CsvDemoBase.class);
|
59
|
|
60
|
protected static final boolean IS_CORE = true;
|
61
|
|
62
|
|
63
|
protected Set<Integer> existingRecordIds = new HashSet<>();
|
64
|
protected Set<UUID> existingRecordUuids = new HashSet<>();
|
65
|
|
66
|
|
67
|
@Override
|
68
|
public long countSteps(CsvDemoExportState state) {
|
69
|
List<TaxonNode> allNodes = getClassificationService().getAllNodes();
|
70
|
return allNodes.size();
|
71
|
}
|
72
|
|
73
|
|
74
|
|
75
|
/**
|
76
|
* Returns the list of {@link TaxonNode taxon nodes} that are part in one of the given {@link Classification classifications}
|
77
|
* and do have a {@link Taxon} attached (empty taxon nodes should not but do exist in CDM databases).
|
78
|
* If <code>classificationList</code> is <code>null</code> or empty then all {@link TaxonNode taxon nodes} of all
|
79
|
* {@link Classification classifications} are returned.<BR>
|
80
|
* Preliminary implementation. Better implement API method for this.
|
81
|
* @return
|
82
|
*/
|
83
|
protected List<TaxonNode> getAllNodes(Set<Classification> classificationList) {
|
84
|
//handle empty list as no filter defined
|
85
|
if (classificationList != null && classificationList.isEmpty()){
|
86
|
classificationList = null;
|
87
|
}
|
88
|
|
89
|
List<TaxonNode> allNodes = getClassificationService().getAllNodes();
|
90
|
List<TaxonNode> result = new ArrayList<>();
|
91
|
for (TaxonNode node : allNodes){
|
92
|
if (node.getClassification() == null ){
|
93
|
continue;
|
94
|
}else if (classificationList != null && ! classificationList.contains(node.getClassification())){
|
95
|
continue;
|
96
|
}else{
|
97
|
Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
|
98
|
if (taxon == null){
|
99
|
String message = "There is a taxon node without taxon: " + node.getId();
|
100
|
logger.warn(message);
|
101
|
continue;
|
102
|
}
|
103
|
result.add(node);
|
104
|
}
|
105
|
}
|
106
|
return result;
|
107
|
}
|
108
|
|
109
|
|
110
|
/**
|
111
|
* Creates the locationId, locality, countryCode triple
|
112
|
* @param record
|
113
|
* @param area
|
114
|
*/
|
115
|
protected void handleArea(ICsvAreaRecord record, NamedArea area, TaxonBase<?> taxon, boolean required) {
|
116
|
if (area != null){
|
117
|
record.setLocationId(area);
|
118
|
record.setLocality(area.getLabel());
|
119
|
if (area.isInstanceOf(Country.class)){
|
120
|
Country country = CdmBase.deproxy(area, Country.class);
|
121
|
record.setCountryCode(country.getIso3166_A2());
|
122
|
}
|
123
|
}else{
|
124
|
if (required){
|
125
|
String message = "Description requires area but area does not exist for taxon " + getTaxonLogString(taxon);
|
126
|
logger.warn(message);
|
127
|
}
|
128
|
}
|
129
|
}
|
130
|
|
131
|
|
132
|
protected String getTaxonLogString(TaxonBase<?> taxon) {
|
133
|
return taxon.getTitleCache() + "(" + taxon.getId() + ")";
|
134
|
}
|
135
|
|
136
|
|
137
|
/**
|
138
|
* @param el
|
139
|
* @return
|
140
|
*/
|
141
|
protected boolean recordExists(CdmBase el) {
|
142
|
return existingRecordIds.contains(el.getId());
|
143
|
}
|
144
|
|
145
|
|
146
|
/**
|
147
|
* @param sec
|
148
|
*/
|
149
|
protected void addExistingRecord(CdmBase cdmBase) {
|
150
|
existingRecordIds.add(cdmBase.getId());
|
151
|
}
|
152
|
|
153
|
/**
|
154
|
* @param el
|
155
|
* @return
|
156
|
*/
|
157
|
protected boolean recordExistsUuid(CdmBase el) {
|
158
|
return existingRecordUuids.contains(el.getUuid());
|
159
|
}
|
160
|
|
161
|
/**
|
162
|
* @param sec
|
163
|
*/
|
164
|
protected void addExistingRecordUuid(CdmBase cdmBase) {
|
165
|
existingRecordUuids.add(cdmBase.getUuid());
|
166
|
}
|
167
|
|
168
|
|
169
|
protected String getSources(ISourceable<?> sourceable, CsvTaxExportConfiguratorRedlist config) {
|
170
|
String result = "";
|
171
|
for (IOriginalSource<?> source: sourceable.getSources()){
|
172
|
if (StringUtils.isBlank(source.getIdInSource())){//idInSource indicates that this source is only data provenance, may be changed in future
|
173
|
if (source.getCitation() != null){
|
174
|
String ref = source.getCitation().getTitleCache();
|
175
|
result = CdmUtils.concat(config.getSetSeparator(), result, ref);
|
176
|
}
|
177
|
}
|
178
|
}
|
179
|
return result;
|
180
|
}
|
181
|
|
182
|
|
183
|
/**
|
184
|
* @param config
|
185
|
* @return
|
186
|
* @throws IOException
|
187
|
* @throws FileNotFoundException
|
188
|
*/
|
189
|
protected FileOutputStream createFileOutputStream(CsvTaxExportConfiguratorRedlist config, String thisFileName) throws IOException, FileNotFoundException {
|
190
|
String filePath = config.getDestinationNameString();
|
191
|
String fileName = filePath + File.separatorChar + thisFileName;
|
192
|
File f = new File(fileName);
|
193
|
if (!f.exists()){
|
194
|
f.createNewFile();
|
195
|
}
|
196
|
FileOutputStream fos = new FileOutputStream(f);
|
197
|
return fos;
|
198
|
}
|
199
|
|
200
|
|
201
|
/**
|
202
|
* @param config
|
203
|
* @param factory
|
204
|
* @return
|
205
|
* @throws IOException
|
206
|
* @throws FileNotFoundException
|
207
|
* @throws XMLStreamException
|
208
|
*/
|
209
|
protected XMLStreamWriter createXmlStreamWriter(CsvTaxExportStateRedlist state, String fileName)
|
210
|
throws IOException, FileNotFoundException, XMLStreamException {
|
211
|
XMLOutputFactory factory = XMLOutputFactory.newInstance();
|
212
|
OutputStream os;
|
213
|
boolean useZip = state.isZip();
|
214
|
if (useZip){
|
215
|
os = state.getZipStream(fileName);
|
216
|
}else{
|
217
|
os = createFileOutputStream(state.getConfig(), fileName);
|
218
|
}
|
219
|
XMLStreamWriter writer = factory.createXMLStreamWriter(os);
|
220
|
return writer;
|
221
|
}
|
222
|
|
223
|
|
224
|
/**
|
225
|
* @param coreTaxFileName
|
226
|
* @param config
|
227
|
* @return
|
228
|
* @throws IOException
|
229
|
* @throws FileNotFoundException
|
230
|
* @throws UnsupportedEncodingException
|
231
|
*/
|
232
|
protected PrintWriter createPrintWriter(final String fileName, CsvTaxExportStateRedlist state)
|
233
|
throws IOException, FileNotFoundException, UnsupportedEncodingException {
|
234
|
|
235
|
OutputStream os;
|
236
|
boolean useZip = state.isZip();
|
237
|
if (useZip){
|
238
|
os = state.getZipStream(fileName);
|
239
|
}else{
|
240
|
os = createFileOutputStream(state.getConfig(), fileName);
|
241
|
}
|
242
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(os, "UTF8"), true);
|
243
|
|
244
|
return writer;
|
245
|
}
|
246
|
|
247
|
|
248
|
|
249
|
|
250
|
/**
|
251
|
* Closes the writer
|
252
|
* @param writer
|
253
|
* @param state
|
254
|
*/
|
255
|
protected void closeWriter(PrintWriter writer, CsvTaxExportStateRedlist state) {
|
256
|
if (writer != null && state.isZip() == false){
|
257
|
writer.close();
|
258
|
}
|
259
|
}
|
260
|
|
261
|
|
262
|
|
263
|
/**
|
264
|
* Closes the writer.
|
265
|
* Note: XMLStreamWriter does not close the underlying stream.
|
266
|
* @param writer
|
267
|
* @param state
|
268
|
*/
|
269
|
protected void closeWriter(XMLStreamWriter writer, CsvTaxExportStateRedlist state) {
|
270
|
if (writer != null && state.isZip() == false){
|
271
|
try {
|
272
|
writer.close();
|
273
|
} catch (XMLStreamException e) {
|
274
|
throw new RuntimeException(e);
|
275
|
}
|
276
|
}
|
277
|
}
|
278
|
protected void clearExistingRecordIds(){
|
279
|
existingRecordIds.clear();
|
280
|
}
|
281
|
}
|