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