3 * Copyright (C) 2009 EDIT
4 * European Distributed Institute of Taxonomy
5 * http://www.e-taxonomy.eu
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.
10 package eu
.etaxonomy
.cdm
.io
.dwca
.in
;
13 import java
.util
.ArrayList
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
19 import org
.apache
.commons
.lang
.StringUtils
;
20 import org
.apache
.log4j
.Logger
;
22 import com
.ibm
.lsid
.MalformedLSIDException
;
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
.OriginalSourceType
;
31 import eu
.etaxonomy
.cdm
.model
.common
.TimePeriod
;
32 import eu
.etaxonomy
.cdm
.model
.description
.Feature
;
33 import eu
.etaxonomy
.cdm
.model
.description
.TaxonDescription
;
34 import eu
.etaxonomy
.cdm
.model
.description
.TextData
;
35 import eu
.etaxonomy
.cdm
.model
.name
.NonViralName
;
36 import eu
.etaxonomy
.cdm
.model
.name
.TaxonNameBase
;
37 import eu
.etaxonomy
.cdm
.model
.name
.ZoologicalName
;
38 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
39 import eu
.etaxonomy
.cdm
.model
.reference
.ReferenceFactory
;
40 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
41 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
42 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
49 public class GbifReferenceCsv2CdmConverter
extends PartitionableConverterBase
<DwcaImportState
>
50 implements IPartitionableConverter
<CsvStreamItem
, IReader
<CdmBase
>, String
>{
52 private static final Logger logger
= Logger
.getLogger(GbifReferenceCsv2CdmConverter
.class);
54 private static final String CORE_ID
= "coreId";
59 public GbifReferenceCsv2CdmConverter(DwcaImportState state
) {
63 public IReader
<MappedCdmBase
> map(CsvStreamItem item
){
64 List
<MappedCdmBase
> resultList
= new ArrayList
<MappedCdmBase
>();
66 Map
<String
, String
> csv
= item
.map
;
67 Reference
<?
> sourceReference
= state
.getTransactionalSourceReference();
68 String sourceReferecenDetail
= null;
70 String id
= getSourceId(item
);
71 TaxonBase
<?
> taxon
= getTaxonBase(id
, item
, TaxonBase
.class, state
);
72 if (isNotBlank(id
) && taxon
== null){
73 String message
= "Taxon for id %s could not be found";
74 message
= String
.format(message
, id
);
75 fireWarningEvent(message
, item
, 8);
78 String strCreator
= getValue(item
, TermUri
.DC_CREATOR
);
79 String strDate
= getValue(item
, TermUri
.DC_DATE
);
80 String strTitle
= getValue(item
, TermUri
.DC_TITLE
);
81 String strSource
= getValue(item
, TermUri
.DC_SOURCE
);
82 String strIdentifier
= getValue(item
, TermUri
.DC_IDENTIFIER
);
83 String strType
= getValue(item
, TermUri
.DC_TYPE
);
85 Reference
<?
> reference
= ReferenceFactory
.newGeneric();
86 resultList
.add(new MappedCdmBase
<CdmBase
>(reference
));
89 TeamOrPersonBase
<?
> author
= handleCreator(strCreator
);
90 reference
.setAuthorTeam(author
);
92 TimePeriod publicationDate
= handleDate(strDate
);
93 reference
.setDatePublished(publicationDate
);
95 reference
.setTitle(strTitle
);
97 Reference
<?
> inRef
= handleInRef(strSource
);
99 reference
.setInReference(inRef
);
100 resultList
.add(new MappedCdmBase
<CdmBase
>(inRef
));
104 handleIdentifier(strIdentifier
, reference
);
107 handleType(reference
, strType
, taxon
, resultList
, item
);
110 return new ListReader
<MappedCdmBase
>(resultList
);
114 private void handleType(Reference
<?
> reference
, String strType
, TaxonBase
<?
> taxon
, List
<MappedCdmBase
> resultList
, CsvStreamItem item
) {
115 // TODO handleType not yet implemented
118 String message
= "Taxon is null. Reference not imported.";
119 fireWarningEvent(message
,item
, 4);
122 boolean isNomRef
= false;
123 if (isNotBlank(strType
)){
124 if (strType
.matches("Botanical Protologue")){
125 if (taxon
.getName() != null && reference
!= null && taxon
.getName().isInstanceOf(NonViralName
.class)){
126 NonViralName
<?
> nvn
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
127 nvn
.setNomenclaturalReference(reference
);
136 if (isNomRef
== false && config
.isGuessNomenclaturalReferences()){
137 //if reference equals in author and year we assume that it is the nom ref
138 //this information is usually only available for ICZN names
139 if (taxon
.getName() != null && reference
!= null && taxon
.getName().isInstanceOf(NonViralName
.class)){
140 NonViralName
<?
> nvn
= CdmBase
.deproxy(taxon
.getName(), NonViralName
.class);
141 String taxonAuthor
= nvn
.getAuthorshipCache();
142 String refAuthor
= reference
.getAuthorTeam().getNomenclaturalTitle();
143 Integer combYear
= null;
144 Integer origYear
= null;
145 if (nvn
.isInstanceOf(ZoologicalName
.class)){
146 ZoologicalName zooName
= CdmBase
.deproxy(nvn
, ZoologicalName
.class);
147 combYear
= zooName
.getPublicationYear();
148 origYear
= zooName
.getOriginalPublicationYear();
150 String refYear
= reference
.getYear();
152 //combination compare
153 if (taxonAuthor
!= null && taxonAuthor
.equals(refAuthor
)){
154 if (combYear
!= null && String
.valueOf(combYear
).equals(refYear
)){
157 nvn
.setNomenclaturalReference(reference
);
158 }else if (origYear
!= null && String
.valueOf(origYear
).equals(refYear
)){
159 //TODO not yet handled by CDM
165 if (config
.isHandleAllRefsAsCitation()){
166 if (taxon
.isInstanceOf(Taxon
.class)){
167 TaxonDescription desc
= getTaxonDescription(CdmBase
.deproxy(taxon
, Taxon
.class), false);
168 createCitation(desc
, reference
, taxon
.getName());
169 resultList
.add(new MappedCdmBase
<CdmBase
>(desc
));
170 }else if (taxon
.isInstanceOf(Synonym
.class)){
171 Synonym syn
= CdmBase
.deproxy(taxon
, Synonym
.class);
172 for (Taxon tax
: syn
.getAcceptedTaxa()){
173 TaxonDescription desc
= getTaxonDescription(tax
, false);
174 createCitation(desc
, reference
, syn
.getName());
175 resultList
.add(new MappedCdmBase
<CdmBase
>(desc
));
186 private void createCitation(TaxonDescription desc
, Reference ref
, TaxonNameBase nameUsedInSource
) {
187 Feature feature
= Feature
.CITATION();
188 TextData textData
= TextData
.NewInstance(feature
);
189 DescriptionElementSource source
= DescriptionElementSource
.NewPrimarySourceInstance(ref
, null, nameUsedInSource
, null);
190 textData
.addSource(source
);
191 desc
.addElement(textData
);
194 private void handleIdentifier(String strIdentifier
, Reference reference
) {
195 if (StringUtils
.isBlank(strIdentifier
)){
197 }else if (LSID
.isLsid(strIdentifier
)){
200 lsid
= new LSID(strIdentifier
);
201 reference
.setLsid(lsid
);
202 } catch (MalformedLSIDException e
) {
203 //TODO should not happen as we have checked before
204 throw new RuntimeException(e
);
208 URI uri
= URI
.create(strIdentifier
);
209 reference
.setUri(uri
);
210 } catch (Exception e
) {
211 logger
.debug("Reference is not an URI");
213 //TODO further identifier types
217 private Reference
<?
> handleInRef(String strSource
) {
218 if (StringUtils
.isBlank(strSource
)){
221 Reference
<?
> inRef
= ReferenceFactory
.newGeneric();
227 private TimePeriod
handleDate(String strDate
) {
228 TimePeriod tp
= TimePeriod
.parseString(strDate
);
232 private TeamOrPersonBase
handleCreator(String strCreator
) {
233 Team team
= Team
.NewTitledInstance(strCreator
, strCreator
);
238 public String
getSourceId(CsvStreamItem item
) {
239 String id
= item
.get(CORE_ID
);
244 //********************** PARTITIONABLE **************************************/
247 protected void makeForeignKeysForItem(CsvStreamItem item
, Map
<String
, Set
<String
>> fkMap
) {
250 if ( hasValue(value
= item
.get(CORE_ID
))){
251 key
= TermUri
.DWC_TAXON
.toString();
252 Set
<String
> keySet
= getKeySet(key
, fkMap
);
259 public Set
<String
> requiredSourceNamespaces() {
260 Set
<String
> result
= new HashSet
<String
>();
261 result
.add(TermUri
.DWC_TAXON
.toString());
265 //******************* TO STRING ******************************************/
268 public String
toString(){
269 return this.getClass().getName();