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.File;
|
13
|
import java.io.FileNotFoundException;
|
14
|
import java.io.FileOutputStream;
|
15
|
import java.io.IOException;
|
16
|
import java.io.OutputStreamWriter;
|
17
|
import java.io.PrintWriter;
|
18
|
import java.io.UnsupportedEncodingException;
|
19
|
import java.util.List;
|
20
|
import java.util.Set;
|
21
|
|
22
|
import org.apache.log4j.Logger;
|
23
|
import org.springframework.stereotype.Component;
|
24
|
import org.springframework.transaction.TransactionStatus;
|
25
|
|
26
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacade;
|
27
|
import eu.etaxonomy.cdm.api.facade.DerivedUnitFacadeNotSupportedException;
|
28
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
29
|
import eu.etaxonomy.cdm.model.description.DescriptionElementBase;
|
30
|
import eu.etaxonomy.cdm.model.description.IndividualsAssociation;
|
31
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
32
|
import eu.etaxonomy.cdm.model.name.NonViralName;
|
33
|
import eu.etaxonomy.cdm.model.name.SpecimenTypeDesignation;
|
34
|
import eu.etaxonomy.cdm.model.name.TaxonNameBase;
|
35
|
import eu.etaxonomy.cdm.model.name.TypeDesignationBase;
|
36
|
import eu.etaxonomy.cdm.model.name.TypeDesignationStatusBase;
|
37
|
import eu.etaxonomy.cdm.model.occurrence.Collection;
|
38
|
import eu.etaxonomy.cdm.model.occurrence.DerivedUnitBase;
|
39
|
import eu.etaxonomy.cdm.model.occurrence.DeterminationEvent;
|
40
|
import eu.etaxonomy.cdm.model.occurrence.SpecimenOrObservationBase;
|
41
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
42
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
43
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
44
|
import eu.etaxonomy.cdm.model.taxon.TaxonNode;
|
45
|
|
46
|
/**
|
47
|
* @author a.mueller
|
48
|
* @created 20.04.2011
|
49
|
*/
|
50
|
@Component
|
51
|
public class DwcaTypesExport extends DwcaExportBase {
|
52
|
private static final Logger logger = Logger.getLogger(DwcaTypesExport.class);
|
53
|
|
54
|
/**
|
55
|
* Constructor
|
56
|
*/
|
57
|
public DwcaTypesExport() {
|
58
|
super();
|
59
|
this.ioName = this.getClass().getSimpleName();
|
60
|
}
|
61
|
|
62
|
/** Retrieves data from a CDM DB and serializes them CDM to XML.
|
63
|
* Starts with root taxa and traverses the classification to retrieve children taxa, synonyms and relationships.
|
64
|
* Taxa that are not part of the classification are not found.
|
65
|
*
|
66
|
* @param exImpConfig
|
67
|
* @param dbname
|
68
|
* @param filename
|
69
|
*/
|
70
|
@Override
|
71
|
protected boolean doInvoke(DwcaTaxExportState state){
|
72
|
DwcaTaxExportConfigurator config = state.getConfig();
|
73
|
String dbname = config.getSource() != null ? config.getSource().getName() : "unknown";
|
74
|
String fileName = config.getDestinationNameString();
|
75
|
logger.info("Serializing DB " + dbname + " to file " + fileName);
|
76
|
TransactionStatus txStatus = startTransaction(true);
|
77
|
|
78
|
try {
|
79
|
|
80
|
final String coreTaxFileName = "typesAndSpecimen.txt";
|
81
|
fileName = fileName + File.separatorChar + coreTaxFileName;
|
82
|
File f = new File(fileName);
|
83
|
if (!f.exists()){
|
84
|
f.createNewFile();
|
85
|
}
|
86
|
FileOutputStream fos = new FileOutputStream(f);
|
87
|
PrintWriter writer = new PrintWriter(new OutputStreamWriter(fos, "UTF8"), true);
|
88
|
|
89
|
|
90
|
List<TaxonNode> allNodes = getAllNodes(null);
|
91
|
|
92
|
for (TaxonNode node : allNodes){
|
93
|
Taxon taxon = CdmBase.deproxy(node.getTaxon(), Taxon.class);
|
94
|
|
95
|
//TODO use API methods to retrieve all related specimen
|
96
|
|
97
|
//individual associations
|
98
|
Set<TaxonDescription> descriptions = taxon.getDescriptions();
|
99
|
for (TaxonDescription description : descriptions){
|
100
|
for (DescriptionElementBase el : description.getElements()){
|
101
|
if (el.isInstanceOf(IndividualsAssociation.class)){
|
102
|
DwcaTypesRecord record = new DwcaTypesRecord();
|
103
|
IndividualsAssociation individualAssociation = CdmBase.deproxy(el,IndividualsAssociation.class);
|
104
|
if (! this.recordExistsUuid(individualAssociation) && handleSpecimen(record, individualAssociation, null, taxon)){
|
105
|
record.write(writer);
|
106
|
this.addExistingRecordUuid(individualAssociation);
|
107
|
}
|
108
|
}
|
109
|
}
|
110
|
}
|
111
|
|
112
|
//type specimen
|
113
|
NonViralName<?> nvn = CdmBase.deproxy(taxon.getName(), NonViralName.class);
|
114
|
handleTypeName(writer, taxon, nvn);
|
115
|
for (Synonym synonym : taxon.getSynonyms()){
|
116
|
handleTypeName(writer, synonym, nvn);
|
117
|
}
|
118
|
|
119
|
//FIXME
|
120
|
//Determinations
|
121
|
|
122
|
|
123
|
writer.flush();
|
124
|
|
125
|
}
|
126
|
} catch (FileNotFoundException e) {
|
127
|
e.printStackTrace();
|
128
|
} catch (UnsupportedEncodingException e) {
|
129
|
e.printStackTrace();
|
130
|
} catch (ClassCastException e) {
|
131
|
e.printStackTrace();
|
132
|
} catch (IOException e) {
|
133
|
e.printStackTrace();
|
134
|
}
|
135
|
commitTransaction(txStatus);
|
136
|
return true;
|
137
|
}
|
138
|
|
139
|
/**
|
140
|
* @param writer
|
141
|
* @param taxon
|
142
|
* @param nvn
|
143
|
* @return
|
144
|
*/
|
145
|
private Set<TypeDesignationBase<?>> handleTypeName(PrintWriter writer, TaxonBase taxonBase, NonViralName<?> nvn) {
|
146
|
Set<TypeDesignationBase<?>> designations = nvn.getTypeDesignations();
|
147
|
for (TypeDesignationBase designation:designations){
|
148
|
DwcaTypesRecord record = new DwcaTypesRecord();
|
149
|
if (! this.recordExistsUuid(designation) && handleSpecimen(record, null, designation, taxonBase)){
|
150
|
record.write(writer);
|
151
|
addExistingRecordUuid(designation);
|
152
|
}
|
153
|
}
|
154
|
return designations;
|
155
|
}
|
156
|
|
157
|
|
158
|
private boolean handleSpecimen(DwcaTypesRecord record, IndividualsAssociation individualsAssociation, TypeDesignationBase designation, TaxonBase taxonBase) {
|
159
|
TypeDesignationStatusBase status = null;
|
160
|
DerivedUnitFacade facade = null;
|
161
|
if (individualsAssociation != null){
|
162
|
facade = getFacadeFromAssociation(individualsAssociation);
|
163
|
}else if (designation != null){
|
164
|
facade = getFacadeFromDesignation(designation);
|
165
|
status = designation.getTypeStatus();
|
166
|
}
|
167
|
if (facade == null){
|
168
|
return false;
|
169
|
}
|
170
|
|
171
|
record.setCoreid(taxonBase.getId());
|
172
|
record.setBibliographicCitation(facade.getTitleCache());
|
173
|
record.setTypeStatus(status);
|
174
|
record.setTypeDesignatedBy( (designation == null || designation.getCitation()==null)? null: designation.getCitation().getTitleCache());
|
175
|
|
176
|
TaxonNameBase scientificName = getScientificName(facade);
|
177
|
if (scientificName != null){
|
178
|
record.setScientificName(scientificName.getTitleCache());
|
179
|
record.setTaxonRank(scientificName.getRank());
|
180
|
}
|
181
|
|
182
|
record.setOccurrenceId(facade.innerDerivedUnit().getUuid().toString());
|
183
|
Collection collection = facade.getCollection();
|
184
|
if (collection != null){
|
185
|
record.setCollectionCode(collection.getCode());
|
186
|
if (collection.getInstitute() != null){
|
187
|
record.setInstitutionCode(collection.getInstitute().getCode());
|
188
|
}
|
189
|
}
|
190
|
record.setCatalogNumber(facade.getCatalogNumber());
|
191
|
record.setLocality(facade.getLocalityText());
|
192
|
record.setSex(facade.getSex());
|
193
|
record.setRecordedBy(facade.getCollector());
|
194
|
//TODO ???
|
195
|
record.setSource(null);
|
196
|
record.setEventDate(facade.getGatheringPeriod());
|
197
|
//TODO missing
|
198
|
record.setVerbatimLabel(null);
|
199
|
if (facade.getExactLocation() != null){
|
200
|
if (facade.getExactLocation().getLongitude() != null){
|
201
|
record.setVerbatimLongitude(facade.getExactLocation().getLongitudeSexagesimal().toString());
|
202
|
}
|
203
|
if (facade.getExactLocation().getLatitude() != null){
|
204
|
record.setVerbatimLatitude(facade.getExactLocation().getLatitudeSexagesimal().toString());
|
205
|
}
|
206
|
}
|
207
|
return true;
|
208
|
}
|
209
|
|
210
|
private TaxonNameBase getScientificName(DerivedUnitFacade facade) {
|
211
|
Set<DeterminationEvent> detEvents = facade.getDeterminations();
|
212
|
for (DeterminationEvent detEvent : detEvents){
|
213
|
if (detEvent.getPreferredFlag()== true){
|
214
|
return detEvent.getTaxon().getName();
|
215
|
}
|
216
|
}
|
217
|
return null;
|
218
|
}
|
219
|
|
220
|
private DerivedUnitFacade getFacadeFromDesignation(TypeDesignationBase designation) {
|
221
|
if (designation.isInstanceOf(SpecimenTypeDesignation.class)){
|
222
|
SpecimenTypeDesignation specDesig = CdmBase.deproxy(designation, SpecimenTypeDesignation.class);
|
223
|
try {
|
224
|
DerivedUnitBase derivedUnit = specDesig.getTypeSpecimen();
|
225
|
if (derivedUnit == null){
|
226
|
return null;
|
227
|
}else{
|
228
|
DerivedUnitFacade facade = DerivedUnitFacade.NewInstance(derivedUnit);
|
229
|
return facade;
|
230
|
}
|
231
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
232
|
String message = "DerivedUnit is too complex to be handled by facade based darwin core archive export";
|
233
|
logger.warn(message);
|
234
|
//TODO handle empty records
|
235
|
return null;
|
236
|
}
|
237
|
}else{
|
238
|
return null;
|
239
|
}
|
240
|
}
|
241
|
|
242
|
private DerivedUnitFacade getFacadeFromAssociation(IndividualsAssociation individualsAssociation) {
|
243
|
SpecimenOrObservationBase specimen = individualsAssociation.getAssociatedSpecimenOrObservation();
|
244
|
DerivedUnitFacade facade;
|
245
|
if (! specimen.isInstanceOf(DerivedUnitBase.class)){
|
246
|
String message = "Non DerivedUnit specimen can not yet be handled by this export";
|
247
|
logger.warn(message);
|
248
|
//TODO handle empty records
|
249
|
return null;
|
250
|
}else{
|
251
|
DerivedUnitBase<?> derivedUnit = CdmBase.deproxy(specimen, DerivedUnitBase.class);
|
252
|
try {
|
253
|
facade = DerivedUnitFacade.NewInstance(derivedUnit);
|
254
|
} catch (DerivedUnitFacadeNotSupportedException e) {
|
255
|
String message = "DerivedUnit is too complex to be handled by facade based darwin core archive export";
|
256
|
logger.warn(message);
|
257
|
//TODO handle empty records
|
258
|
return null;
|
259
|
}
|
260
|
|
261
|
}
|
262
|
return facade;
|
263
|
}
|
264
|
|
265
|
@Override
|
266
|
protected boolean doCheck(DwcaTaxExportState state) {
|
267
|
boolean result = true;
|
268
|
logger.warn("No check implemented for " + this.ioName);
|
269
|
return result;
|
270
|
}
|
271
|
|
272
|
|
273
|
@Override
|
274
|
protected boolean isIgnore(DwcaTaxExportState state) {
|
275
|
return ! state.getConfig().isDoTypesAndSpecimen();
|
276
|
}
|
277
|
|
278
|
}
|