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.in;
|
10
|
|
11
|
import java.net.URI;
|
12
|
import java.util.ArrayList;
|
13
|
import java.util.HashSet;
|
14
|
import java.util.List;
|
15
|
import java.util.Map;
|
16
|
import java.util.Set;
|
17
|
|
18
|
import org.apache.commons.lang.StringUtils;
|
19
|
import org.apache.log4j.Logger;
|
20
|
|
21
|
import com.ibm.lsid.MalformedLSIDException;
|
22
|
|
23
|
import eu.etaxonomy.cdm.io.dwca.TermUri;
|
24
|
import eu.etaxonomy.cdm.io.stream.StreamItem;
|
25
|
import eu.etaxonomy.cdm.model.agent.Team;
|
26
|
import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
|
27
|
import eu.etaxonomy.cdm.model.common.CdmBase;
|
28
|
import eu.etaxonomy.cdm.model.common.LSID;
|
29
|
import eu.etaxonomy.cdm.model.common.TimePeriod;
|
30
|
import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
|
31
|
import eu.etaxonomy.cdm.model.description.Feature;
|
32
|
import eu.etaxonomy.cdm.model.description.TaxonDescription;
|
33
|
import eu.etaxonomy.cdm.model.description.TextData;
|
34
|
import eu.etaxonomy.cdm.model.name.INonViralName;
|
35
|
import eu.etaxonomy.cdm.model.name.IZoologicalName;
|
36
|
import eu.etaxonomy.cdm.model.name.TaxonName;
|
37
|
import eu.etaxonomy.cdm.model.reference.Reference;
|
38
|
import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
|
39
|
import eu.etaxonomy.cdm.model.taxon.Synonym;
|
40
|
import eu.etaxonomy.cdm.model.taxon.Taxon;
|
41
|
import eu.etaxonomy.cdm.model.taxon.TaxonBase;
|
42
|
import eu.etaxonomy.cdm.strategy.parser.TimePeriodParser;
|
43
|
|
44
|
/**
|
45
|
* @author a.mueller
|
46
|
* @date 22.11.2011
|
47
|
*
|
48
|
*/
|
49
|
public class GbifReferenceCsv2CdmConverter extends PartitionableConverterBase<DwcaDataImportConfiguratorBase, DwcaDataImportStateBase<DwcaDataImportConfiguratorBase>>
|
50
|
implements IPartitionableConverter<StreamItem, IReader<CdmBase>, String>{
|
51
|
|
52
|
private static final Logger logger = Logger.getLogger(GbifReferenceCsv2CdmConverter.class);
|
53
|
|
54
|
private static final String CORE_ID = "coreId";
|
55
|
|
56
|
/**
|
57
|
* @param state
|
58
|
*/
|
59
|
public GbifReferenceCsv2CdmConverter(DwcaDataImportStateBase state) {
|
60
|
super(state);
|
61
|
}
|
62
|
|
63
|
@Override
|
64
|
public IReader<MappedCdmBase> map(StreamItem item ){
|
65
|
List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
|
66
|
|
67
|
Map<String, String> csv = item.map;
|
68
|
Reference sourceReference = state.getTransactionalSourceReference();
|
69
|
String sourceReferecenDetail = null;
|
70
|
|
71
|
String id = getSourceId(item);
|
72
|
TaxonBase<?> taxon = getTaxonBase(id, item, TaxonBase.class, state);
|
73
|
if (isNotBlank(id) && taxon == null){
|
74
|
String message = "Taxon for id %s could not be found";
|
75
|
message = String.format(message, id);
|
76
|
fireWarningEvent(message, item, 8);
|
77
|
}
|
78
|
|
79
|
String strCreator = getValue(item, TermUri.DC_CREATOR);
|
80
|
String strDate = getValue(item, TermUri.DC_DATE);
|
81
|
String strTitle = getValue(item, TermUri.DC_TITLE);
|
82
|
String strSource = getValue(item, TermUri.DC_SOURCE);
|
83
|
String strIdentifier = getValue(item, TermUri.DC_IDENTIFIER);
|
84
|
String strType = getValue(item, TermUri.DC_TYPE);
|
85
|
|
86
|
Reference reference = ReferenceFactory.newGeneric();
|
87
|
resultList.add(new MappedCdmBase<CdmBase>(reference));
|
88
|
|
89
|
//author
|
90
|
TeamOrPersonBase<?> author = handleCreator(strCreator);
|
91
|
reference.setAuthorship(author);
|
92
|
//date
|
93
|
TimePeriod publicationDate = handleDate(strDate);
|
94
|
reference.setDatePublished(publicationDate);
|
95
|
//title
|
96
|
reference.setTitle(strTitle);
|
97
|
//inreference
|
98
|
Reference inRef = handleInRef(strSource);
|
99
|
if (inRef != null){
|
100
|
reference.setInReference(inRef);
|
101
|
resultList.add(new MappedCdmBase<CdmBase>(inRef));
|
102
|
}
|
103
|
|
104
|
//URI
|
105
|
handleIdentifier(strIdentifier, reference);
|
106
|
|
107
|
//type
|
108
|
handleType(reference, strType, taxon, resultList, item);
|
109
|
|
110
|
|
111
|
return new ListReader<MappedCdmBase>(resultList);
|
112
|
}
|
113
|
|
114
|
|
115
|
private void handleType(Reference reference, String strType, TaxonBase<?> taxon, List<MappedCdmBase> resultList, StreamItem item) {
|
116
|
// TODO handleType not yet implemented
|
117
|
|
118
|
if (taxon == null){
|
119
|
String message = "Taxon is null. Reference not imported.";
|
120
|
fireWarningEvent(message,item, 4);
|
121
|
//do nothing
|
122
|
}else{
|
123
|
boolean isNomRef = false;
|
124
|
if (isNotBlank(strType)){
|
125
|
if (strType.matches("Botanical Protologue")){
|
126
|
if (taxon.getName() != null && reference != null && taxon.getName().isNonViral()){
|
127
|
INonViralName nvn = taxon.getName();
|
128
|
nvn.setNomenclaturalReference(reference);
|
129
|
isNomRef = true;
|
130
|
}else{
|
131
|
//TODO
|
132
|
}
|
133
|
}
|
134
|
}
|
135
|
|
136
|
//guess a nom ref
|
137
|
if (isNomRef == false && config.isGuessNomenclaturalReferences()){
|
138
|
//if reference equals in author and year we assume that it is the nom ref
|
139
|
//this information is usually only available for ICZN names
|
140
|
if (taxon.getName() != null && reference != null && taxon.getName().isNonViral()){
|
141
|
INonViralName nvn = taxon.getName();
|
142
|
String taxonAuthor = nvn.getAuthorshipCache();
|
143
|
String refAuthor = reference.getAuthorship().getNomenclaturalTitle();
|
144
|
Integer combYear = null;
|
145
|
Integer origYear = null;
|
146
|
if (nvn.isZoological()){
|
147
|
IZoologicalName zooName = (IZoologicalName)CdmBase.deproxy(nvn);
|
148
|
combYear = zooName.getPublicationYear();
|
149
|
origYear = zooName.getOriginalPublicationYear();
|
150
|
}
|
151
|
String refYear = reference.getYear();
|
152
|
|
153
|
//combination compare
|
154
|
if (taxonAuthor != null && taxonAuthor.equals(refAuthor)){
|
155
|
if (combYear != null && String.valueOf(combYear).equals(refYear)){
|
156
|
//is nom Ref
|
157
|
isNomRef = true;
|
158
|
nvn.setNomenclaturalReference(reference);
|
159
|
}else if (origYear != null && String.valueOf(origYear).equals(refYear)){
|
160
|
//TODO not yet handled by CDM
|
161
|
}
|
162
|
}
|
163
|
|
164
|
}
|
165
|
}
|
166
|
if (config.isHandleAllRefsAsCitation()){
|
167
|
if (taxon.isInstanceOf(Taxon.class)){
|
168
|
TaxonDescription desc = getTaxonDescription(CdmBase.deproxy(taxon, Taxon.class), false);
|
169
|
createCitation(desc, reference, taxon.getName());
|
170
|
resultList.add(new MappedCdmBase<CdmBase>(desc));
|
171
|
}else if (taxon.isInstanceOf(Synonym.class)){
|
172
|
Synonym syn = CdmBase.deproxy(taxon, Synonym.class);
|
173
|
Taxon tax = syn.getAcceptedTaxon();
|
174
|
if (tax != null){
|
175
|
TaxonDescription desc = getTaxonDescription(tax, false);
|
176
|
createCitation(desc, reference, syn.getName());
|
177
|
resultList.add(new MappedCdmBase<CdmBase>(desc));
|
178
|
}
|
179
|
}
|
180
|
|
181
|
}
|
182
|
|
183
|
}
|
184
|
|
185
|
|
186
|
}
|
187
|
|
188
|
private void createCitation(TaxonDescription desc, Reference ref, TaxonName nameUsedInSource) {
|
189
|
Feature feature = Feature.CITATION();
|
190
|
TextData textData = TextData.NewInstance(feature);
|
191
|
DescriptionElementSource source = DescriptionElementSource.NewPrimarySourceInstance(ref, null, nameUsedInSource, null);
|
192
|
textData.addSource(source);
|
193
|
desc.addElement(textData);
|
194
|
}
|
195
|
|
196
|
private void handleIdentifier(String strIdentifier, Reference reference) {
|
197
|
if (StringUtils.isBlank(strIdentifier)){
|
198
|
return;
|
199
|
}else if (LSID.isLsid(strIdentifier)){
|
200
|
LSID lsid;
|
201
|
try {
|
202
|
lsid = new LSID(strIdentifier);
|
203
|
reference.setLsid(lsid);
|
204
|
} catch (MalformedLSIDException e) {
|
205
|
//TODO should not happen as we have checked before
|
206
|
throw new RuntimeException(e);
|
207
|
}
|
208
|
}
|
209
|
try {
|
210
|
URI uri = URI.create(strIdentifier);
|
211
|
reference.setUri(uri);
|
212
|
} catch (Exception e) {
|
213
|
logger.debug("Reference is not an URI");
|
214
|
}
|
215
|
//TODO further identifier types
|
216
|
|
217
|
}
|
218
|
|
219
|
private Reference handleInRef(String strSource) {
|
220
|
if (StringUtils.isBlank(strSource)){
|
221
|
return null;
|
222
|
}else{
|
223
|
Reference inRef = ReferenceFactory.newGeneric();
|
224
|
return inRef;
|
225
|
}
|
226
|
}
|
227
|
|
228
|
|
229
|
private TimePeriod handleDate(String strDate) {
|
230
|
TimePeriod tp = TimePeriodParser.parseString(strDate);
|
231
|
return tp;
|
232
|
}
|
233
|
|
234
|
private TeamOrPersonBase handleCreator(String strCreator) {
|
235
|
Team team = Team.NewTitledInstance(strCreator, strCreator);
|
236
|
return team;
|
237
|
}
|
238
|
|
239
|
@Override
|
240
|
public String getSourceId(StreamItem item) {
|
241
|
String id = item.get(CORE_ID);
|
242
|
return id;
|
243
|
}
|
244
|
|
245
|
|
246
|
//********************** PARTITIONABLE **************************************/
|
247
|
|
248
|
@Override
|
249
|
protected void makeForeignKeysForItem(StreamItem item, Map<String, Set<String>> fkMap) {
|
250
|
String value;
|
251
|
String key;
|
252
|
if ( hasValue(value = item.get(CORE_ID))){
|
253
|
key = TermUri.DWC_TAXON.toString();
|
254
|
Set<String> keySet = getKeySet(key, fkMap);
|
255
|
keySet.add(value);
|
256
|
}
|
257
|
}
|
258
|
|
259
|
|
260
|
@Override
|
261
|
public Set<String> requiredSourceNamespaces() {
|
262
|
Set<String> result = new HashSet<String>();
|
263
|
result.add(TermUri.DWC_TAXON.toString());
|
264
|
return result;
|
265
|
}
|
266
|
|
267
|
//******************* TO STRING ******************************************/
|
268
|
|
269
|
@Override
|
270
|
public String toString(){
|
271
|
return this.getClass().getName();
|
272
|
}
|
273
|
|
274
|
|
275
|
}
|