1
|
/**
|
2
|
* Copyright (C) 2009 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.dwca.out;
|
10
|
|
11
|
import java.io.PrintWriter;
|
12
|
import java.net.URI;
|
13
|
import java.net.URISyntaxException;
|
14
|
import java.util.HashMap;
|
15
|
import java.util.HashSet;
|
16
|
import java.util.Map;
|
17
|
import java.util.Set;
|
18
|
import java.util.UUID;
|
19
|
|
20
|
import org.apache.commons.lang.StringUtils;
|
21
|
import org.apache.log4j.Logger;
|
22
|
import org.joda.time.DateTime;
|
23
|
import org.joda.time.Partial;
|
24
|
|
25
|
import eu.etaxonomy.cdm.common.CdmUtils;
|
26
|
import eu.etaxonomy.cdm.io.dwca.TermUri;
|
27
|
import eu.etaxonomy.cdm.model.agent.AgentBase;
|
28
|
import eu.etaxonomy.cdm.model.common.Annotation;
|
29
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
30
|
import eu.etaxonomy.cdm.model.common.DefinedTerm;
|
31
|
import eu.etaxonomy.cdm.model.common.ICdmBase;
|
32
|
import eu.etaxonomy.cdm.model.common.LSID;
|
33
|
import eu.etaxonomy.cdm.model.common.Language;
|
34
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
35
|
import eu.etaxonomy.cdm.model.description.Feature;
|
36
|
import eu.etaxonomy.cdm.model.description.PresenceAbsenceTerm;
|
37
|
import eu.etaxonomy.cdm.model.location.Point;
|
38
|
import eu.etaxonomy.cdm.model.media.Rights;
|
39
|
import eu.etaxonomy.cdm.model.name.NameTypeDesignationStatus;
|
40
|
import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
|
41
|
import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
|
42
|
import eu.etaxonomy.cdm.model.name.Rank;
|
43
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignationStatus;
|
44
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
* @date 20.04.2011
|
49
|
*
|
50
|
*/
|
51
|
public abstract class DwcaRecordBase {
|
52
|
private static final Logger logger = Logger.getLogger(DwcaRecordBase.class);
|
53
|
|
54
|
//TODO Collection_SEPARATOR
|
55
|
protected static final CharSequence COLLECTION_SEPARATOR = "@";
|
56
|
// protected static final String FIELD_ENCLOSER = "\"";
|
57
|
protected static final boolean IS_FIRST = false;
|
58
|
protected static final boolean IS_NOT_FIRST = true;
|
59
|
// protected static final String SEP = ",";
|
60
|
|
61
|
protected Map<String, URI> knownFields = new HashMap<>();
|
62
|
protected Set<TermUri> knownTermFields = new HashSet<>();
|
63
|
|
64
|
public abstract void write(DwcaTaxExportState state, PrintWriter writer);
|
65
|
public void writeCsv(DwcaTaxExportState state){
|
66
|
state.getResult().addWarning(this.getClass().getName() + ".writeCsv() not yet implemented!");
|
67
|
}
|
68
|
|
69
|
|
70
|
protected abstract void registerKnownFields();
|
71
|
|
72
|
protected int count;
|
73
|
private DwcaMetaDataRecord metaDataRecord;
|
74
|
protected DwcaTaxExportConfigurator config;
|
75
|
|
76
|
private Integer id;
|
77
|
private UUID uuid;
|
78
|
|
79
|
|
80
|
protected DwcaRecordBase(DwcaMetaDataRecord metaDataRecord, DwcaTaxExportConfigurator config){
|
81
|
this.metaDataRecord = metaDataRecord;
|
82
|
this.count = metaDataRecord.inc();
|
83
|
this.config = config;
|
84
|
}
|
85
|
|
86
|
|
87
|
public void setId(Integer id) {
|
88
|
this.id = id;
|
89
|
}
|
90
|
|
91
|
public Integer getId() {
|
92
|
return id;
|
93
|
}
|
94
|
|
95
|
public void setUuid(UUID uuid) {
|
96
|
this.uuid = uuid;
|
97
|
}
|
98
|
|
99
|
public UUID getUuid() {
|
100
|
return uuid;
|
101
|
}
|
102
|
|
103
|
protected void printNotes(Set<Annotation> notes, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
104
|
printNotes(notes, writer, addSeparator, fieldKey.getUriString());
|
105
|
}
|
106
|
protected void printNotes(Set<Annotation> notes, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
107
|
//FIXME handles annotations correctly
|
108
|
String value = null;
|
109
|
print(value, writer, addSeparator, fieldKey);
|
110
|
}
|
111
|
|
112
|
// protected void print(Object object, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
113
|
// print(object == null ? null : object.toString(), writer, addSeparator, fieldKey);
|
114
|
// }
|
115
|
protected void print(DwcaId dwcaId, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
116
|
print(dwcaId == null ? null : dwcaId.getId(), writer, addSeparator, fieldKey);
|
117
|
}
|
118
|
protected void print(UUID uuid, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
119
|
print(uuid == null ? null : uuid.toString(), writer, addSeparator, fieldKey);
|
120
|
}
|
121
|
protected void print(AgentBase<?> agent, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
122
|
print(agent, writer, addSeparator, fieldKey.getUriString());
|
123
|
}
|
124
|
protected void print(AgentBase<?> agent, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
125
|
print(agent == null ? null : getAgent(agent), writer, addSeparator, fieldKey);
|
126
|
}
|
127
|
|
128
|
|
129
|
protected void print(Language language, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
130
|
print(language, writer, addSeparator, fieldKey.getUriString());
|
131
|
}
|
132
|
protected void print(Language language, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
133
|
print(language == null ? null : getLanguage(language), writer, addSeparator, fieldKey);
|
134
|
}
|
135
|
protected void print(LSID lsid, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
136
|
print(lsid, writer, addSeparator, fieldKey.getUriString());
|
137
|
}
|
138
|
protected void print(LSID lsid, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
139
|
print(lsid == null ? null : String.valueOf(lsid.toString()), writer, addSeparator, fieldKey);
|
140
|
}
|
141
|
|
142
|
protected void print(Set<Rights> rights, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
143
|
print(rights, writer, addSeparator, fieldKey.getUriString());
|
144
|
}
|
145
|
protected void print(Set<Rights> rights, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
146
|
String rightsString = getRights(rights);
|
147
|
print(rightsString, writer, addSeparator, fieldKey);
|
148
|
}
|
149
|
protected void print(URI uri, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
150
|
print(uri, writer, addSeparator, fieldKey.getUriString());
|
151
|
}
|
152
|
protected void print(URI uri, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
153
|
print(uri == null ? null : String.valueOf(uri), writer, addSeparator, fieldKey);
|
154
|
}
|
155
|
|
156
|
protected void print(Point point, PrintWriter writer, boolean addSeparator, TermUri latitudeKey, TermUri longitudeKey) {
|
157
|
print(point, writer, addSeparator, latitudeKey.getUriString(), longitudeKey.getUriString());
|
158
|
}
|
159
|
|
160
|
protected void print(Point point, PrintWriter writer, boolean addSeparator, String latitudeKey, String longitudeKey) {
|
161
|
if (point == null){
|
162
|
String toPrint = null;
|
163
|
print(toPrint, writer, addSeparator, latitudeKey);
|
164
|
print(toPrint, writer, addSeparator, longitudeKey);
|
165
|
}else{
|
166
|
String latitude = point.getLatitude().toString();
|
167
|
String longitude = point.getLongitude().toString();
|
168
|
print(latitude, writer, addSeparator, latitudeKey);
|
169
|
print(longitude, writer, addSeparator, longitudeKey);
|
170
|
}
|
171
|
}
|
172
|
protected void print(Boolean boolValue, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
173
|
print(boolValue, writer, addSeparator, fieldKey.getUriString());
|
174
|
}
|
175
|
protected void print(Boolean boolValue, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
176
|
print(boolValue == null ? null : String.valueOf(boolValue), writer, addSeparator, fieldKey);
|
177
|
}
|
178
|
|
179
|
protected void print(Integer intValue, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
180
|
print(intValue, writer, addSeparator, fieldKey.getUriString());
|
181
|
}
|
182
|
protected void print(Integer intValue, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
183
|
print(intValue == null ? null : String.valueOf(intValue), writer, addSeparator, fieldKey);
|
184
|
}
|
185
|
|
186
|
protected void printId(Integer intValue, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
187
|
print(intValue == null ? null : String.valueOf(intValue), writer, addSeparator, fieldKey);
|
188
|
}
|
189
|
protected void printId(UUID uuid, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
190
|
print(uuid == null ? null : String.valueOf(uuid), writer, addSeparator, fieldKey);
|
191
|
}
|
192
|
|
193
|
protected void print(String value, PrintWriter writer, boolean addSeparator, TermUri fieldKey) {
|
194
|
print(value, writer, addSeparator, fieldKey, null);
|
195
|
}
|
196
|
|
197
|
protected void print(String value, PrintWriter writer, boolean addSeparator, TermUri fieldKey, String defaultValue) {
|
198
|
print(value, writer, addSeparator, fieldKey.getUriString(), defaultValue);
|
199
|
}
|
200
|
|
201
|
protected void print(String value, PrintWriter writer, boolean addSeparator, String fieldKey) {
|
202
|
print(value, writer, addSeparator, fieldKey, null);
|
203
|
}
|
204
|
|
205
|
protected void print(String value, PrintWriter writer, boolean addSeparator, String fieldKey, String defaultValue) {
|
206
|
if (count == 1 && addSeparator == IS_NOT_FIRST){
|
207
|
registerFieldKey(URI.create(fieldKey), defaultValue);
|
208
|
}
|
209
|
if (StringUtils.isBlank(defaultValue)){
|
210
|
String strToPrint = addSeparator ? config.getFieldsTerminatedBy() : "";
|
211
|
if (StringUtils.isNotBlank(value)){
|
212
|
//Replace quotes by double quotes
|
213
|
value = value.replace("\"", "\"\"");
|
214
|
|
215
|
value = value.replace(config.getLinesTerminatedBy(), "\\r");
|
216
|
|
217
|
//replace all line brakes according to best practices: http://code.google.com/p/gbif-ecat/wiki/BestPractices
|
218
|
value = value.replace("\r\n", "\\r");
|
219
|
value = value.replace("\r", "\\r");
|
220
|
value = value.replace("\n", "\\r");
|
221
|
|
222
|
strToPrint += config.getFieldsEnclosedBy() + value + config.getFieldsEnclosedBy();
|
223
|
}
|
224
|
writer.print(strToPrint);
|
225
|
}
|
226
|
}
|
227
|
|
228
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, Set<Rights> rights) {
|
229
|
String rightsStr = getRights(rights);
|
230
|
if (rights != null){ line(state, csvLine, table, termUri, rightsStr);}
|
231
|
}
|
232
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, Language language) {
|
233
|
if (language != null){ line(state, csvLine, table, termUri, getLanguage(language));}
|
234
|
}
|
235
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, DwcaId id) {
|
236
|
if (id != null){ line(state, csvLine, table, termUri, id.getId());}
|
237
|
}
|
238
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, UUID uuid) {
|
239
|
if (uuid != null){ line(state, csvLine, table, termUri, uuid.toString());}
|
240
|
}
|
241
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, String string) {
|
242
|
line(state, csvLine, table, termUri, string, null);
|
243
|
}
|
244
|
protected void line(DwcaTaxExportState state, String[] csvLine, DwcaTaxExportFile table, TermUri termUri, String string, String defaultValue) {
|
245
|
try {
|
246
|
csvLine[table.getIndex(termUri)] = string;
|
247
|
if (count == 1 && table.getIndex(termUri) != 0){
|
248
|
registerFieldKey(termUri.getUri(), defaultValue);
|
249
|
}
|
250
|
} catch (Exception e) {
|
251
|
String message = "Unhandled exception when handling " + (termUri != null ? termUri.getUriString() : "undefined") + ": " + e.getMessage();
|
252
|
state.getResult().addException(e, message);
|
253
|
}
|
254
|
}
|
255
|
|
256
|
protected void registerFieldKey(URI key, String defaultValue) {
|
257
|
this.metaDataRecord.addFieldEntry(key, defaultValue);
|
258
|
}
|
259
|
|
260
|
|
261
|
protected String getRights(Rights rights) {
|
262
|
if (rights == null){
|
263
|
return "";
|
264
|
}else{
|
265
|
//TODO
|
266
|
return rights.getAbbreviatedText();
|
267
|
}
|
268
|
}
|
269
|
|
270
|
protected String getLanguage(Language language) {
|
271
|
if (language == null){
|
272
|
return "";
|
273
|
}else{
|
274
|
//TODO
|
275
|
return language.getIso639_2();
|
276
|
}
|
277
|
}
|
278
|
|
279
|
protected String getDate(DateTime date) {
|
280
|
if (date == null){
|
281
|
return "";
|
282
|
}else{
|
283
|
//TODO
|
284
|
return date.toString();
|
285
|
}
|
286
|
}
|
287
|
|
288
|
protected String getNomStatus(NomenclaturalStatusType nomStatus) {
|
289
|
String result = DwcaTaxExportTransformer.transformToGbifNomStatus(nomStatus);
|
290
|
if (result == null){
|
291
|
if (nomStatus == null){
|
292
|
return "";
|
293
|
}else{
|
294
|
return nomStatus.getLabel();
|
295
|
}
|
296
|
}else{
|
297
|
return result;
|
298
|
}
|
299
|
}
|
300
|
|
301
|
protected String getNomCode(NomenclaturalCode nomCode) {
|
302
|
if (nomCode == null){
|
303
|
return "";
|
304
|
}else{
|
305
|
//TODO
|
306
|
return nomCode.getTitleCache();
|
307
|
}
|
308
|
}
|
309
|
|
310
|
protected String getRank(Rank rank) {
|
311
|
String result = DwcaTaxExportTransformer.transformToGbifRank(rank);
|
312
|
if (result == null){
|
313
|
if (rank == null){
|
314
|
return "";
|
315
|
}else{
|
316
|
return rank.getLabel();
|
317
|
}
|
318
|
}else{
|
319
|
return result;
|
320
|
}
|
321
|
}
|
322
|
|
323
|
protected String getSex(DefinedTerm sex) {
|
324
|
String result = DwcaTaxExportTransformer.transformToGbifSex(sex);
|
325
|
if (result == null){
|
326
|
if (sex == null){
|
327
|
return "";
|
328
|
}else{
|
329
|
return sex.getLabel();
|
330
|
}
|
331
|
}else{
|
332
|
return result;
|
333
|
}
|
334
|
}
|
335
|
|
336
|
protected String getLifeStage(DefinedTerm stage) {
|
337
|
String result = DwcaTaxExportTransformer.transformToGbifLifeStage(stage);
|
338
|
if (result == null){
|
339
|
if (stage == null){
|
340
|
return "";
|
341
|
}else{
|
342
|
return stage.getLabel();
|
343
|
}
|
344
|
}else{
|
345
|
return result;
|
346
|
}
|
347
|
}
|
348
|
|
349
|
protected String getOccurrenceStatus(PresenceAbsenceTerm status) {
|
350
|
String result = DwcaTaxExportTransformer.transformToGbifOccStatus(status);
|
351
|
if (result == null){
|
352
|
if (status == null){
|
353
|
return "";
|
354
|
}else{
|
355
|
return status.getLabel();
|
356
|
}
|
357
|
}else{
|
358
|
return result;
|
359
|
}
|
360
|
}
|
361
|
|
362
|
protected String getEstablishmentMeans(PresenceAbsenceTerm status) {
|
363
|
String result = DwcaTaxExportTransformer.transformToGbifEstablishmentMeans(status);
|
364
|
if (result == null){
|
365
|
if (status == null){
|
366
|
return "";
|
367
|
}else{
|
368
|
return status.getLabel();
|
369
|
}
|
370
|
}else{
|
371
|
return result;
|
372
|
}
|
373
|
}
|
374
|
|
375
|
|
376
|
|
377
|
protected String getAgent(AgentBase<?> agent) {
|
378
|
if (agent == null){
|
379
|
return "";
|
380
|
}else{
|
381
|
//TODO
|
382
|
return agent.getTitleCache();
|
383
|
}
|
384
|
}
|
385
|
|
386
|
|
387
|
protected String getFeature(Feature feature) {
|
388
|
if (feature == null){
|
389
|
return "";
|
390
|
}else{
|
391
|
//TODO
|
392
|
return feature.getTitleCache();
|
393
|
}
|
394
|
}
|
395
|
|
396
|
|
397
|
protected String getTimePeriod(TimePeriod period) {
|
398
|
if (period == null){
|
399
|
return "";
|
400
|
}else{
|
401
|
//TODO
|
402
|
return period.toString();
|
403
|
}
|
404
|
}
|
405
|
|
406
|
protected String getTimePeriodPart(TimePeriod period, boolean useEnd) {
|
407
|
if (period == null){
|
408
|
return "";
|
409
|
}else{
|
410
|
Partial date = useEnd? period.getEnd(): period.getStart();
|
411
|
if (date == null){
|
412
|
return "";
|
413
|
}else{
|
414
|
//TODO
|
415
|
return date.toString();
|
416
|
}
|
417
|
}
|
418
|
}
|
419
|
|
420
|
protected String getRights(Set<Rights> rights) {
|
421
|
if (rights == null || rights.isEmpty()){
|
422
|
return null;
|
423
|
}else{
|
424
|
String result = null;
|
425
|
for (Rights right: rights){
|
426
|
//TODO usi uri if available ??
|
427
|
String message = "Rights not yet fully implemented";
|
428
|
logger.warn(message);
|
429
|
result = CdmUtils.concat(COLLECTION_SEPARATOR, result, right.getAbbreviatedText());
|
430
|
}
|
431
|
return result;
|
432
|
}
|
433
|
}
|
434
|
|
435
|
|
436
|
protected String getDesignationType(TypeDesignationStatusBase<?> status) {
|
437
|
if (status == null){
|
438
|
return "";
|
439
|
}
|
440
|
String result;
|
441
|
if (status.isInstanceOf(SpecimenTypeDesignationStatus.class)){
|
442
|
SpecimenTypeDesignationStatus specStatus = CdmBase.deproxy(status, SpecimenTypeDesignationStatus.class);
|
443
|
result = DwcaTaxExportTransformer.transformSpecimenTypeStatusToGbif(specStatus);
|
444
|
}else{
|
445
|
NameTypeDesignationStatus nameStatus = CdmBase.deproxy(status, NameTypeDesignationStatus.class);
|
446
|
result = DwcaTaxExportTransformer.transformNameTypeStatusToGbif(nameStatus);
|
447
|
}
|
448
|
if (result == null){
|
449
|
return status.getLabel();
|
450
|
}else{
|
451
|
return result;
|
452
|
}
|
453
|
}
|
454
|
|
455
|
|
456
|
|
457
|
|
458
|
protected void addKnownField(String string, String uri) throws URISyntaxException {
|
459
|
this.knownFields.put(string, new URI(uri));
|
460
|
}
|
461
|
|
462
|
protected void addKnownField(TermUri term) throws URISyntaxException {
|
463
|
this.knownTermFields.add(term);
|
464
|
}
|
465
|
|
466
|
|
467
|
//*************** CSV Methods ******************/
|
468
|
|
469
|
/**
|
470
|
* @param state
|
471
|
* @param taxon
|
472
|
* @return
|
473
|
*/
|
474
|
protected String getId(DwcaTaxExportState state, ICdmBase cdmBase) {
|
475
|
if (cdmBase == null){
|
476
|
return "";
|
477
|
}
|
478
|
//TODO make configurable
|
479
|
return cdmBase.getUuid().toString();
|
480
|
}
|
481
|
}
|