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
|
|
10
|
package eu.etaxonomy.cdm.io.dwca.out;
|
11
|
|
12
|
import java.io.FileNotFoundException;
|
13
|
import java.io.IOException;
|
14
|
import java.util.List;
|
15
|
|
16
|
import javax.xml.stream.XMLStreamException;
|
17
|
import javax.xml.stream.XMLStreamWriter;
|
18
|
|
19
|
import org.apache.log4j.Logger;
|
20
|
import org.joda.time.DateTime;
|
21
|
import org.joda.time.Partial;
|
22
|
import org.joda.time.format.DateTimeFormat;
|
23
|
import org.joda.time.format.DateTimeFormatter;
|
24
|
import org.springframework.stereotype.Component;
|
25
|
|
26
|
import eu.etaxonomy.cdm.model.agent.Address;
|
27
|
import eu.etaxonomy.cdm.model.agent.Contact;
|
28
|
import eu.etaxonomy.cdm.model.agent.InstitutionalMembership;
|
29
|
import eu.etaxonomy.cdm.model.agent.Person;
|
30
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
31
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
32
|
|
33
|
/**
|
34
|
* @author a.mueller
|
35
|
* @created 20.04.2011
|
36
|
*/
|
37
|
@Component
|
38
|
public class DwcaEmlExport extends DwcaExportBase {
|
39
|
|
40
|
private static final long serialVersionUID = -1762545757384406718L;
|
41
|
|
42
|
private static final Logger logger = Logger.getLogger(DwcaEmlExport.class);
|
43
|
|
44
|
protected static final String fileName = "eml.xml";
|
45
|
|
46
|
private String emlNamespace = "eml://ecoinformatics.org/eml-2.1.1";
|
47
|
private String mdNamespace="eml://ecoinformatics.org/methods-2.1.0";
|
48
|
private String projNamespace="eml://ecoinformatics.org/project-2.1.0";
|
49
|
private String datasetNamespace="eml://ecoinformatics.org/dataset-2.1.0";
|
50
|
private String resNamespace="eml://ecoinformatics.org/resource-2.1.0";
|
51
|
private String dcNamespace="http://purl.org/dc/terms/";
|
52
|
private String xsiNamespace="http://www.w3.org/2001/XMLSchema-instance";
|
53
|
private String schemaLocation = "eml://ecoinformatics.org/eml-2.1.1 http://rs.gbif.org/schema/eml-gbif-profile/dev/eml.xsd";
|
54
|
|
55
|
/**
|
56
|
* Constructor
|
57
|
*/
|
58
|
public DwcaEmlExport() {
|
59
|
super();
|
60
|
this.ioName = this.getClass().getSimpleName();
|
61
|
}
|
62
|
|
63
|
/**
|
64
|
* Retrieves the MetaData for a Darwin Core Archive File.
|
65
|
* <BR>
|
66
|
* {@inheritDoc}
|
67
|
*/
|
68
|
@Override
|
69
|
protected void doInvoke(DwcaTaxExportState state){
|
70
|
DwcaTaxExportConfigurator config = state.getConfig();
|
71
|
|
72
|
DwcaMetaDataRecord metaRecord = new DwcaMetaDataRecord(! IS_CORE, fileName, null);
|
73
|
metaRecord.setMetaData(true);
|
74
|
state.addMetaRecord(metaRecord);
|
75
|
|
76
|
DwcaEmlRecord emlRecord = config.getEmlRecord();
|
77
|
if (emlRecord == null){
|
78
|
return;
|
79
|
}
|
80
|
|
81
|
XMLStreamWriter writer = null;
|
82
|
try {
|
83
|
writer = createXmlStreamWriter(state, DwcaTaxExportFile.EML);
|
84
|
|
85
|
String rootName = "eml";
|
86
|
|
87
|
// create header
|
88
|
//TODO encoding
|
89
|
writer.writeStartDocument();
|
90
|
// writer.setDefaultNamespace(rootNamespace);
|
91
|
|
92
|
// create root element
|
93
|
// writer.setPrefix("eml",emlNamespace);
|
94
|
writer.writeStartElement("eml", rootName, emlNamespace);
|
95
|
writer.writeNamespace("eml", emlNamespace);
|
96
|
writer.writeNamespace("md", mdNamespace);
|
97
|
writer.writeNamespace("proj", projNamespace);
|
98
|
writer.writeNamespace("d", datasetNamespace);
|
99
|
writer.writeNamespace("res", resNamespace);
|
100
|
writer.writeNamespace("dc", dcNamespace);
|
101
|
writer.writeNamespace("xsi", xsiNamespace);
|
102
|
|
103
|
writer.writeAttribute("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation", schemaLocation);
|
104
|
writer.writeAttribute("packageId", emlRecord.getIdentifier());
|
105
|
writer.writeAttribute("system", "CDM Library Darwin Core Archive Exporter");
|
106
|
writer.writeAttribute("scope", "system");
|
107
|
|
108
|
if (emlRecord.getMetaDataLanguage() != null ){
|
109
|
writer.writeAttribute("xml", "lang", emlRecord.getMetaDataLanguage().getIso639_2()); //TODO needed ?
|
110
|
}
|
111
|
|
112
|
writeDataSet(writer, config, emlRecord);
|
113
|
writeAdditionalMetadata(writer, config, emlRecord);
|
114
|
writer.flush();
|
115
|
writer.writeEndElement();
|
116
|
writer.writeEndDocument();
|
117
|
writer.flush();
|
118
|
writer.close();
|
119
|
} catch (FileNotFoundException e) {
|
120
|
throw new RuntimeException(e);
|
121
|
} catch (XMLStreamException e) {
|
122
|
if (e.getNestedException() != null){
|
123
|
throw new RuntimeException(e.getNestedException());
|
124
|
}else{
|
125
|
throw new RuntimeException(e);
|
126
|
}
|
127
|
} catch (IOException e) {
|
128
|
throw new RuntimeException(e);
|
129
|
} finally{
|
130
|
closeWriter(writer, state);
|
131
|
}
|
132
|
|
133
|
return;
|
134
|
}
|
135
|
|
136
|
|
137
|
private void writeAdditionalMetadata(XMLStreamWriter writer, DwcaTaxExportConfigurator config, DwcaEmlRecord emlRecord) throws XMLStreamException {
|
138
|
writer.writeStartElement("additionalMetadata");
|
139
|
writer.writeStartElement("metadata");
|
140
|
writer.writeStartElement("gbif");
|
141
|
|
142
|
String elementName;
|
143
|
String text;
|
144
|
|
145
|
elementName = "dateStamp";
|
146
|
text = new DateTime().toString();
|
147
|
writeTextElement(writer, elementName, text);
|
148
|
|
149
|
elementName = "citation";
|
150
|
text = emlRecord.getExpectedCitation();
|
151
|
writeTextElement(writer, elementName, text);
|
152
|
|
153
|
writer.writeStartElement("bibliography");
|
154
|
for (Reference ref: emlRecord.getReferences()){
|
155
|
elementName = "citation";
|
156
|
text = ref.getTitleCache();
|
157
|
writeTextElement(writer, elementName, text);
|
158
|
}
|
159
|
writer.writeEndElement();
|
160
|
|
161
|
|
162
|
writer.writeEndElement(); //gbif
|
163
|
writer.writeEndElement(); //metadata
|
164
|
writer.writeEndElement(); //additionalMetadata
|
165
|
|
166
|
}
|
167
|
|
168
|
private void writeDataSet(XMLStreamWriter writer,
|
169
|
DwcaTaxExportConfigurator config, DwcaEmlRecord emlRecord) throws XMLStreamException {
|
170
|
|
171
|
|
172
|
writer.writeStartElement("dataset");
|
173
|
|
174
|
String elementName;
|
175
|
String text;
|
176
|
|
177
|
elementName = "alternateIdentifier";
|
178
|
text = null;
|
179
|
writeTextElement(writer, elementName, text);
|
180
|
|
181
|
|
182
|
elementName = "title";
|
183
|
text = emlRecord.getTitle();
|
184
|
//TODO language attribute
|
185
|
writeTextElement(writer, elementName, text);
|
186
|
|
187
|
//creator
|
188
|
writer.writeStartElement("creator");
|
189
|
writePerson(writer, emlRecord.getResourceCreator());
|
190
|
writer.writeEndElement();
|
191
|
|
192
|
//metadataProvider
|
193
|
writer.writeStartElement("metadataProvider");
|
194
|
writePerson(writer, emlRecord.getResourceCreator());
|
195
|
writer.writeEndElement();
|
196
|
|
197
|
//associatedParty
|
198
|
for (InstitutionalMembership author : emlRecord.getAuthors()){
|
199
|
writer.writeStartElement("associatedParty");
|
200
|
writePerson(writer, author);
|
201
|
writer.writeEndElement();
|
202
|
|
203
|
}
|
204
|
|
205
|
DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("YYYY-MM-dd");
|
206
|
elementName = "pubDate";
|
207
|
text = emlRecord.getPublicationDate().toString(dateFormatter);
|
208
|
writeTextElement(writer, elementName, text);
|
209
|
|
210
|
elementName = "language";
|
211
|
//TODO
|
212
|
text = emlRecord.getResourceLanguage()== null? null:emlRecord.getResourceLanguage().getLabel() ;
|
213
|
writeTextElement(writer, elementName, text);
|
214
|
|
215
|
elementName = "abstract";
|
216
|
//TODO para
|
217
|
text = emlRecord.getAbstractInfo();
|
218
|
writeTextElement(writer, elementName, text);
|
219
|
|
220
|
writer.writeStartElement("keywordSet");
|
221
|
for (String keyword : emlRecord.getKeywords()){
|
222
|
elementName = "keyword";
|
223
|
text = keyword;
|
224
|
writeTextElement(writer, elementName, text);
|
225
|
}
|
226
|
elementName = "keyword";
|
227
|
text = emlRecord.getKeywordThesaurus();
|
228
|
writeTextElement(writer, elementName, text);
|
229
|
writer.writeEndElement();
|
230
|
|
231
|
//TODO taxonomic keywords
|
232
|
|
233
|
//additional Info //TODO para ?
|
234
|
elementName = "additionalInfo";
|
235
|
text = emlRecord.getAdditionalInformation();
|
236
|
writeTextElement(writer, elementName, text);
|
237
|
|
238
|
//TODO intellectualRights
|
239
|
// elementName = "intellectualRights";
|
240
|
// text = emlRecord.getRights(rights);
|
241
|
// writeParaTextElement
|
242
|
|
243
|
//TODO distribution //TODO online
|
244
|
writer.writeStartElement("distribution");
|
245
|
writer.writeAttribute("scope", "document");
|
246
|
writer.writeStartElement("online");
|
247
|
writer.writeStartElement("url");
|
248
|
writer.writeAttribute("function", "information");
|
249
|
writer.writeCharacters(nullSafe(emlRecord.getResourceUrl()));
|
250
|
writer.writeEndElement();
|
251
|
writer.writeEndElement(); //online
|
252
|
writer.writeEndElement(); //distribution
|
253
|
|
254
|
//TODO coverage
|
255
|
writeCoverage(writer, emlRecord);
|
256
|
|
257
|
//contact
|
258
|
writer.writeStartElement("contact");
|
259
|
writePerson(writer, emlRecord.getContact());
|
260
|
writer.writeEndElement();
|
261
|
|
262
|
|
263
|
//TODO project
|
264
|
writer.writeStartElement("project");
|
265
|
//title
|
266
|
elementName = "title";
|
267
|
text = emlRecord.getProjectTitle();
|
268
|
writeTextElement(writer, elementName, text);
|
269
|
|
270
|
writer.writeStartElement("personnel");
|
271
|
elementName = "organizationName";
|
272
|
text = emlRecord.getProjectLead();
|
273
|
writeTextElement(writer, elementName, text);
|
274
|
|
275
|
//TODO ??
|
276
|
elementName = "role";
|
277
|
text = "Distributor";
|
278
|
writeTextElement(writer, elementName, text);
|
279
|
|
280
|
writer.writeEndElement();
|
281
|
|
282
|
|
283
|
writer.writeStartElement("funding");
|
284
|
writer.writeEndElement();
|
285
|
|
286
|
writer.writeStartElement("studyAreaDescription");
|
287
|
writer.writeStartElement("descriptor");
|
288
|
|
289
|
elementName = "descriptorValue";
|
290
|
text = emlRecord.getProjectDescription();
|
291
|
writeTextElement(writer, elementName, text);
|
292
|
|
293
|
writer.writeEndElement();
|
294
|
writer.writeEndElement();
|
295
|
|
296
|
writer.writeStartElement("designDescription");
|
297
|
writer.writeEndElement();
|
298
|
|
299
|
writer.writeEndElement();
|
300
|
}
|
301
|
|
302
|
/**
|
303
|
* @param writer
|
304
|
* @param emlRecord
|
305
|
* @throws XMLStreamException
|
306
|
*/
|
307
|
private void writeCoverage(XMLStreamWriter writer, DwcaEmlRecord emlRecord)
|
308
|
throws XMLStreamException {
|
309
|
String elementName;
|
310
|
String text;
|
311
|
writer.writeStartElement("coverage");
|
312
|
handleGeoCoverage(writer, emlRecord);
|
313
|
handleTermporalCoverage(writer, emlRecord);
|
314
|
writer.writeEndElement(); //coverage
|
315
|
}
|
316
|
|
317
|
/**
|
318
|
* @param writer
|
319
|
* @param emlRecord
|
320
|
* @throws XMLStreamException
|
321
|
*/
|
322
|
private void handleGeoCoverage(XMLStreamWriter writer,
|
323
|
DwcaEmlRecord emlRecord) throws XMLStreamException {
|
324
|
String elementName;
|
325
|
String text;
|
326
|
writer.writeStartElement("geographicCoverage");
|
327
|
|
328
|
//geographic description
|
329
|
elementName = "geographicDescription";
|
330
|
text = emlRecord.getRegionalScope();
|
331
|
writeTextElement(writer, elementName, text);
|
332
|
|
333
|
//boundingCoordinates
|
334
|
writer.writeStartElement("boundingCoordinates");
|
335
|
if (emlRecord.getUpperLeftCorner() != null){
|
336
|
//west
|
337
|
elementName = "westBoundingCoordinate";
|
338
|
text = emlRecord.getUpperLeftCorner().getLatitude().toString();
|
339
|
writeTextElement(writer, elementName, text);
|
340
|
}
|
341
|
|
342
|
if (emlRecord.getLowerRightCorner() != null){
|
343
|
//east
|
344
|
elementName = "eastBoundingCoordinate";
|
345
|
text = emlRecord.getLowerRightCorner().getLatitude().toString();
|
346
|
writeTextElement(writer, elementName, text);
|
347
|
}
|
348
|
|
349
|
if (emlRecord.getUpperLeftCorner() != null){
|
350
|
//north
|
351
|
elementName = "northBoundingCoordinate";
|
352
|
text = emlRecord.getUpperLeftCorner().getLongitude().toString();
|
353
|
writeTextElement(writer, elementName, text);
|
354
|
}
|
355
|
if (emlRecord.getLowerRightCorner() != null){
|
356
|
//south
|
357
|
elementName = "southBoundingCoordinate";
|
358
|
text = emlRecord.getLowerRightCorner().getLongitude().toString();
|
359
|
writeTextElement(writer, elementName, text);
|
360
|
}
|
361
|
writer.writeEndElement(); //boundingCoordinates
|
362
|
|
363
|
writer.writeEndElement(); //geographicCoverage
|
364
|
}
|
365
|
|
366
|
/**
|
367
|
* @param writer
|
368
|
* @param emlRecord
|
369
|
* @throws XMLStreamException
|
370
|
*/
|
371
|
private void handleTermporalCoverage(XMLStreamWriter writer,
|
372
|
DwcaEmlRecord emlRecord) throws XMLStreamException {
|
373
|
|
374
|
TimePeriod timePeriod = emlRecord.getDate();
|
375
|
if (timePeriod == null){
|
376
|
return;
|
377
|
}
|
378
|
|
379
|
writer.writeStartElement("termporalCoverage");
|
380
|
if (! timePeriod.isPeriod()){
|
381
|
//singleDateTime
|
382
|
writer.writeStartElement("singleDateTime");
|
383
|
writeCalendarDate(writer, timePeriod.getStart());
|
384
|
writer.writeEndElement();
|
385
|
}else {
|
386
|
//rangeOfDates
|
387
|
writer.writeStartElement("rangeOfDates");
|
388
|
writer.writeStartElement("beginDate");
|
389
|
writeCalendarDate(writer, timePeriod.getStart());
|
390
|
writer.writeEndElement();
|
391
|
writer.writeStartElement("endDate");
|
392
|
writeCalendarDate(writer, timePeriod.getStart());
|
393
|
writer.writeEndElement();
|
394
|
writer.writeEndElement();
|
395
|
}
|
396
|
|
397
|
|
398
|
|
399
|
writer.writeEndElement(); //termporalCoverage
|
400
|
}
|
401
|
|
402
|
|
403
|
private void writeCalendarDate(XMLStreamWriter writer, Partial partial) throws XMLStreamException {
|
404
|
//calendarDate
|
405
|
String elementName = "calendarDate";
|
406
|
//FIXME must be something like 37723
|
407
|
String text = partial.toDateTime(new DateTime()).toString();
|
408
|
writeTextElement(writer, elementName, text);
|
409
|
|
410
|
}
|
411
|
|
412
|
private String nullSafe(Object object) {
|
413
|
return object == null ? null : object.toString();
|
414
|
}
|
415
|
|
416
|
private void writePerson(XMLStreamWriter writer, InstitutionalMembership member) throws XMLStreamException {
|
417
|
String elementName;
|
418
|
String text;
|
419
|
if (member == null){
|
420
|
return ;
|
421
|
}
|
422
|
|
423
|
writer.writeStartElement("individualName");
|
424
|
if (member.getPerson() != null){
|
425
|
Person person = member.getPerson();
|
426
|
|
427
|
elementName = "givenName";
|
428
|
text = person.getFirstname();
|
429
|
writeTextElement(writer, elementName, text);
|
430
|
|
431
|
elementName = "surName";
|
432
|
text = person.getLastname();
|
433
|
writeTextElement(writer, elementName, text);
|
434
|
|
435
|
}
|
436
|
writer.writeEndElement();
|
437
|
|
438
|
elementName = "organizationName";
|
439
|
text = member.getInstitute()== null? null: member.getInstitute().getTitleCache();
|
440
|
writeTextElement(writer, elementName, text);
|
441
|
|
442
|
|
443
|
if (member.getPerson() != null && member.getPerson().getContact()!= null){
|
444
|
Contact contact = member.getPerson().getContact();
|
445
|
|
446
|
if (contact.getAddresses().size() > 0){
|
447
|
writer.writeStartElement("address");
|
448
|
|
449
|
//TODO empty
|
450
|
Address address = contact.getAddresses().iterator().next();
|
451
|
|
452
|
elementName = "deliveryPoint";
|
453
|
text = address.getStreet();
|
454
|
writeTextElement(writer, elementName, text);
|
455
|
|
456
|
elementName = "city";
|
457
|
text = address.getLocality();
|
458
|
writeTextElement(writer, elementName, text);
|
459
|
|
460
|
elementName = "administrativeArea";
|
461
|
text = address.getRegion();
|
462
|
writeTextElement(writer, elementName, text);
|
463
|
|
464
|
elementName = "postalCode";
|
465
|
text = address.getPostcode();
|
466
|
writeTextElement(writer, elementName, text);
|
467
|
|
468
|
elementName = "country";
|
469
|
text = address.getCountry()== null? null: address.getCountry().getLabel();
|
470
|
writeTextElement(writer, elementName, text);
|
471
|
|
472
|
writer.writeEndElement(); //address
|
473
|
}
|
474
|
|
475
|
elementName = "phone";
|
476
|
text = firstOfList(contact.getPhoneNumbers());
|
477
|
writeTextElement(writer, elementName, text);
|
478
|
|
479
|
elementName = "electronicMailAddress";
|
480
|
text = firstOfList(contact.getEmailAddresses());
|
481
|
writeTextElement(writer, elementName, text);
|
482
|
|
483
|
elementName = "onlineUrl";
|
484
|
text = firstOfList(contact.getPhoneNumbers());
|
485
|
writeTextElement(writer, elementName, text);
|
486
|
|
487
|
|
488
|
}
|
489
|
}
|
490
|
|
491
|
private String firstOfList(List<String> list) {
|
492
|
if (list.size() > 0){
|
493
|
return list.get(0);
|
494
|
}else{
|
495
|
return null;
|
496
|
}
|
497
|
}
|
498
|
|
499
|
/**
|
500
|
* @param writer
|
501
|
* @param altIdentifier
|
502
|
* @param text
|
503
|
* @throws XMLStreamException
|
504
|
*/
|
505
|
private void writeTextElement(XMLStreamWriter writer, String elementName,
|
506
|
String text) throws XMLStreamException {
|
507
|
writer.writeStartElement(elementName);
|
508
|
writer.writeCharacters(text);
|
509
|
writer.writeEndElement();
|
510
|
}
|
511
|
|
512
|
@Override
|
513
|
protected boolean doCheck(DwcaTaxExportState state) {
|
514
|
boolean result = true;
|
515
|
logger.warn("No check implemented for " + this.ioName);
|
516
|
return result;
|
517
|
}
|
518
|
|
519
|
|
520
|
@Override
|
521
|
protected boolean isIgnore(DwcaTaxExportState state) {
|
522
|
return ! state.getConfig().isDoEml();
|
523
|
}
|
524
|
|
525
|
}
|