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
;
12 import java
.util
.ArrayList
;
13 import java
.util
.HashSet
;
14 import java
.util
.List
;
18 import org
.apache
.log4j
.Logger
;
20 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
21 import eu
.etaxonomy
.cdm
.io
.dwca
.TermUri
;
22 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
23 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
24 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
35 public class DwcTaxonCsv2CdmTaxonRelationConverter
<STATE
extends DwcaImportState
> extends ConverterBase
<DwcaImportState
>
36 implements IConverter
<CsvStreamItem
, IReader
<CdmBase
>, String
>{
37 private static Logger logger
= Logger
.getLogger(DwcTaxonCsv2CdmTaxonRelationConverter
.class);
39 private static final String ID
= "id";
44 public DwcTaxonCsv2CdmTaxonRelationConverter(DwcaImportState state
) {
50 public IReader
<MappedCdmBase
> map(CsvStreamItem item
){
51 List
<MappedCdmBase
> resultList
= new ArrayList
<MappedCdmBase
>();
53 Map
<String
, String
> csvRecord
= item
.map
;
54 Reference
<?
> sourceReference
= null;
55 String sourceReferecenDetail
= null;
57 String id
= csvRecord
.get(ID
);
58 TaxonBase
<?
> taxonBase
= getTaxonBase(id
, item
, null);
59 if (taxonBase
== null){
60 String warning
= "Taxon not available for id %s.";
61 warning
= String
.format(warning
, id
);
62 fireWarningEvent(warning
, item
, 8);
65 MappedCdmBase mcb
= new MappedCdmBase(taxonBase
);
68 handleAcceptedNameUsage(item
, state
, taxonBase
, id
);
70 handleParentNameUsage(item
, state
, taxonBase
, resultList
);
72 handleKingdom(item
, state
);
74 handlePhylum(item
, state
);
76 handleClass(item
, state
);
78 handleOrder(item
, state
);
80 handleFamily(item
, state
);
82 handleGenus(item
, state
);
84 handleSubGenus(item
, state
);
90 // <!-- Top level group; listed as kingdom but may be interpreted as domain or superkingdom
91 // The following eight groups are recognized: Animalia, Archaea, Bacteria, Chromista,
92 // Fungi, Plantae, Protozoa, Viruses -->
93 // <field index='10' term='http://rs.tdwg.org/dwc/terms/kingdom'/>
95 // <!-- Specific epithet; for hybrids, the multiplication symbol is included in the epithet -->
96 // <field index='17' term='http://rs.tdwg.org/dwc/terms/specificEpithet'/>
98 // <!-- Infraspecific epithet -->
99 // <field index='18' term='http://rs.tdwg.org/dwc/terms/infraspecificEpithet'/>
101 // <!-- Acceptance status published in -->
102 // <field index='20' term='http://purl.org/dc/terms/source'/>
104 // <!-- Reference in which the scientific name was first published -->
105 // <field index='21' term='http://rs.tdwg.org/dwc/terms/namePublishedIn'/>
107 // <!-- Scrutiny date -->
108 // <field index='23' term='http://purl.org/dc/terms/modified'/>
109 // <!-- Additional data for the taxon -->
111 // <field index='24' term='http://purl.org/dc/terms/description'/>
114 return new ListReader
<MappedCdmBase
>(resultList
);
119 public String
getSourceId(CsvStreamItem item
) {
120 String id
= item
.get(ID
);
125 private void handleSubGenus(CsvStreamItem item
, DwcaImportState state
) {
126 // TODO Auto-generated method stub
131 private void handleGenus(CsvStreamItem item
, DwcaImportState state
) {
132 // TODO Auto-generated method stub
137 private void handleFamily(CsvStreamItem item
, DwcaImportState state
) {
138 // TODO Auto-generated method stub
143 private void handleOrder(CsvStreamItem item
, DwcaImportState state
) {
144 // TODO Auto-generated method stub
149 private void handleClass(CsvStreamItem item
, DwcaImportState state
) {
150 // TODO Auto-generated method stub
155 private void handlePhylum(CsvStreamItem item
, DwcaImportState state
) {
156 // TODO Auto-generated method stub
161 private void handleKingdom(CsvStreamItem item
, DwcaImportState state
) {
162 // TODO Auto-generated method stub
167 private void handleParentNameUsage(CsvStreamItem item
, DwcaImportState state
, TaxonBase
<?
> taxonBase
, List
<MappedCdmBase
> resultList
) {
168 if (exists(TermUri
.DWC_PARENT_NAME_USAGE_ID
, item
) || exists(TermUri
.DWC_PARENT_NAME_USAGE
, item
)){
169 if (taxonBase
.isInstanceOf(Taxon
.class)){
170 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
171 String accId
= item
.get(TermUri
.DWC_PARENT_NAME_USAGE_ID
);
172 Taxon parentTaxon
= getTaxonBase(accId
, item
, Taxon
.class);
173 if (parentTaxon
== null){
174 fireWarningEvent("NON-ID parent Name Usage not yet implemented or parent name usage id not available", item
, 4);
176 Classification classification
= getClassification(item
);
177 Reference
<?
> citation
= null;
178 classification
.addParentChild(parentTaxon
, taxon
, citation
, null);
179 resultList
.add(new MappedCdmBase(classification
));
182 String message
= "PARENT_NAME_USAGE given for Synonym. This is not allowed in CDM.";
183 //TODO check "is this Taxon"
184 fireWarningEvent(message
, item
, 4);
192 private Classification
getClassification(CsvStreamItem item
) {
193 Set
<Classification
> result
= new HashSet
<Classification
>();
194 String datasetKey
= item
.get(TermUri
.DWC_DATASET_ID
);
195 if (CdmUtils
.areBlank(datasetKey
,item
.get(TermUri
.DWC_DATASET_NAME
))){
196 datasetKey
= DwcTaxonCsv2CdmTaxonConverter
.NO_DATASET
;
199 result
.addAll(state
.get(TermUri
.DWC_DATASET_ID
.toString(), datasetKey
, Classification
.class));
200 result
.addAll(state
.get(TermUri
.DWC_DATASET_NAME
.toString(), item
.get(TermUri
.DWC_DATASET_NAME
), Classification
.class));
201 if (result
.isEmpty()){
203 }else if (result
.size() > 1){
204 fireWarningEvent("Dataset is ambigous. I take arbitrary one.", item
, 8);
206 return result
.iterator().next();
210 private void handleAcceptedNameUsage(CsvStreamItem item
, DwcaImportState state
, TaxonBase taxonBase
, String id
) {
211 if (exists(TermUri
.DWC_ACCEPTED_NAME_USAGE_ID
, item
) || exists(TermUri
.DWC_ACCEPTED_NAME_USAGE
, item
)){
212 String accId
= item
.get(TermUri
.DWC_ACCEPTED_NAME_USAGE_ID
);
213 if (id
.equals(accId
)){
214 return; //mapping to itself needs no further handling
216 if (taxonBase
.isInstanceOf(Synonym
.class)){
217 Synonym synonym
= CdmBase
.deproxy(taxonBase
, Synonym
.class);
218 Taxon accTaxon
= getTaxonBase(accId
, item
, Taxon
.class);
219 if (accTaxon
== null){
220 fireWarningEvent("NON-ID accepted Name Usage not yet implemented or taxon for name usage id not available", item
, 4);
222 accTaxon
.addSynonym(synonym
, SynonymRelationshipType
.SYNONYM_OF(),null, null);
225 String message
= "Accepted name usage is not of type synonym. This is not allowed in CDM. Can't create realtionship";
226 //TODO check "is this Taxon"
227 fireWarningEvent(message
, item
, 4);
230 if (logger
.isDebugEnabled()){logger
.debug("");}
235 private <T
extends TaxonBase
> T
getTaxonBase(String id
, CsvStreamItem item
, Class
<T
> clazz
) {
237 clazz
= (Class
)TaxonBase
.class;
239 List
<T
> taxonList
= state
.get(TermUri
.DWC_TAXON
.toString(), id
, clazz
);
240 if (taxonList
.size() > 1){
241 String message
= "Undefined taxon mapping for id %s.";
242 message
= String
.format(message
, id
);
243 fireWarningEvent(message
, item
, 8);
244 logger
.warn(message
); //TODO remove when events are handled correctly
246 }else if (taxonList
.isEmpty()){
249 return taxonList
.get(0);
257 public String
toString(){
258 return this.getClass().getName();