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
.HashMap
;
14 import java
.util
.HashSet
;
15 import java
.util
.List
;
19 import org
.apache
.log4j
.Logger
;
21 import eu
.etaxonomy
.cdm
.common
.CdmUtils
;
22 import eu
.etaxonomy
.cdm
.io
.dwca
.TermUri
;
23 import eu
.etaxonomy
.cdm
.model
.common
.CdmBase
;
24 import eu
.etaxonomy
.cdm
.model
.reference
.Reference
;
25 import eu
.etaxonomy
.cdm
.model
.taxon
.Classification
;
26 import eu
.etaxonomy
.cdm
.model
.taxon
.Synonym
;
27 import eu
.etaxonomy
.cdm
.model
.taxon
.SynonymRelationshipType
;
28 import eu
.etaxonomy
.cdm
.model
.taxon
.Taxon
;
29 import eu
.etaxonomy
.cdm
.model
.taxon
.TaxonBase
;
36 public class DwcTaxonCsv2CdmTaxonRelationConverter
<STATE
extends DwcaImportState
> extends PartitionableConverterBase
<DwcaImportState
>
37 implements IPartitionableConverter
<CsvStreamItem
, INamespaceReader
<CdmBase
>, String
>{
38 private static Logger logger
= Logger
.getLogger(DwcTaxonCsv2CdmTaxonRelationConverter
.class);
40 private static final String ID
= "id";
45 public DwcTaxonCsv2CdmTaxonRelationConverter(DwcaImportState state
) {
51 public IReader
<MappedCdmBase
> map(CsvStreamItem item
){
52 List
<MappedCdmBase
> resultList
= new ArrayList
<MappedCdmBase
>();
54 Map
<String
, String
> csvRecord
= item
.map
;
55 Reference
<?
> sourceReference
= null;
56 String sourceReferecenDetail
= null;
58 String id
= csvRecord
.get(ID
);
59 TaxonBase
<?
> taxonBase
= getTaxonBase(id
, item
, null);
60 if (taxonBase
== null){
61 String warning
= "Taxon not available for id %s.";
62 warning
= String
.format(warning
, id
);
63 fireWarningEvent(warning
, item
, 8);
66 MappedCdmBase mcb
= new MappedCdmBase(taxonBase
);
69 handleAcceptedNameUsage(item
, state
, taxonBase
, id
);
71 handleParentNameUsage(item
, state
, taxonBase
, resultList
);
73 handleKingdom(item
, state
);
75 handlePhylum(item
, state
);
77 handleClass(item
, state
);
79 handleOrder(item
, state
);
81 handleFamily(item
, state
);
83 handleGenus(item
, state
);
85 handleSubGenus(item
, state
);
91 // <!-- Top level group; listed as kingdom but may be interpreted as domain or superkingdom
92 // The following eight groups are recognized: Animalia, Archaea, Bacteria, Chromista,
93 // Fungi, Plantae, Protozoa, Viruses -->
94 // <field index='10' term='http://rs.tdwg.org/dwc/terms/kingdom'/>
96 // <!-- Specific epithet; for hybrids, the multiplication symbol is included in the epithet -->
97 // <field index='17' term='http://rs.tdwg.org/dwc/terms/specificEpithet'/>
99 // <!-- Infraspecific epithet -->
100 // <field index='18' term='http://rs.tdwg.org/dwc/terms/infraspecificEpithet'/>
102 // <!-- Acceptance status published in -->
103 // <field index='20' term='http://purl.org/dc/terms/source'/>
105 // <!-- Reference in which the scientific name was first published -->
106 // <field index='21' term='http://rs.tdwg.org/dwc/terms/namePublishedIn'/>
108 // <!-- Scrutiny date -->
109 // <field index='23' term='http://purl.org/dc/terms/modified'/>
110 // <!-- Additional data for the taxon -->
112 // <field index='24' term='http://purl.org/dc/terms/description'/>
115 return new ListReader
<MappedCdmBase
>(resultList
);
120 public String
getSourceId(CsvStreamItem item
) {
121 String id
= item
.get(ID
);
126 private void handleSubGenus(CsvStreamItem item
, DwcaImportState state
) {
127 // TODO Auto-generated method stub
132 private void handleGenus(CsvStreamItem item
, DwcaImportState state
) {
133 // TODO Auto-generated method stub
138 private void handleFamily(CsvStreamItem item
, DwcaImportState state
) {
139 // TODO Auto-generated method stub
144 private void handleOrder(CsvStreamItem item
, DwcaImportState state
) {
145 // TODO Auto-generated method stub
150 private void handleClass(CsvStreamItem item
, DwcaImportState state
) {
151 // TODO Auto-generated method stub
156 private void handlePhylum(CsvStreamItem item
, DwcaImportState state
) {
157 // TODO Auto-generated method stub
162 private void handleKingdom(CsvStreamItem item
, DwcaImportState state
) {
163 // TODO Auto-generated method stub
168 private void handleParentNameUsage(CsvStreamItem item
, DwcaImportState state
, TaxonBase
<?
> taxonBase
, List
<MappedCdmBase
> resultList
) {
169 if (exists(TermUri
.DWC_PARENT_NAME_USAGE_ID
, item
) || exists(TermUri
.DWC_PARENT_NAME_USAGE
, item
)){
170 if (taxonBase
.isInstanceOf(Taxon
.class)){
171 Taxon taxon
= CdmBase
.deproxy(taxonBase
, Taxon
.class);
172 String accId
= item
.get(TermUri
.DWC_PARENT_NAME_USAGE_ID
);
173 Taxon parentTaxon
= getTaxonBase(accId
, item
, Taxon
.class);
174 if (parentTaxon
== null){
175 fireWarningEvent("NON-ID parent Name Usage not yet implemented or parent name usage id not available", item
, 4);
177 Classification classification
= getClassification(item
);
178 Reference
<?
> citation
= null;
179 classification
.addParentChild(parentTaxon
, taxon
, citation
, null);
180 resultList
.add(new MappedCdmBase(classification
));
183 String message
= "PARENT_NAME_USAGE given for Synonym. This is not allowed in CDM.";
184 //TODO check "is this Taxon"
185 fireWarningEvent(message
, item
, 4);
193 private Classification
getClassification(CsvStreamItem item
) {
194 Set
<Classification
> result
= new HashSet
<Classification
>();
195 String datasetKey
= item
.get(TermUri
.DWC_DATASET_ID
);
196 if (CdmUtils
.areBlank(datasetKey
,item
.get(TermUri
.DWC_DATASET_NAME
))){
197 datasetKey
= DwcTaxonCsv2CdmTaxonConverter
.NO_DATASET
;
200 result
.addAll(state
.get(TermUri
.DWC_DATASET_ID
.toString(), datasetKey
, Classification
.class));
201 result
.addAll(state
.get(TermUri
.DWC_DATASET_NAME
.toString(), item
.get(TermUri
.DWC_DATASET_NAME
), Classification
.class));
202 if (result
.isEmpty()){
204 }else if (result
.size() > 1){
205 fireWarningEvent("Dataset is ambigous. I take arbitrary one.", item
, 8);
207 return result
.iterator().next();
211 private void handleAcceptedNameUsage(CsvStreamItem item
, DwcaImportState state
, TaxonBase taxonBase
, String id
) {
212 if (exists(TermUri
.DWC_ACCEPTED_NAME_USAGE_ID
, item
) || exists(TermUri
.DWC_ACCEPTED_NAME_USAGE
, item
)){
213 String accId
= item
.get(TermUri
.DWC_ACCEPTED_NAME_USAGE_ID
);
214 if (id
.equals(accId
)){
215 return; //mapping to itself needs no further handling
217 if (taxonBase
.isInstanceOf(Synonym
.class)){
218 Synonym synonym
= CdmBase
.deproxy(taxonBase
, Synonym
.class);
219 Taxon accTaxon
= getTaxonBase(accId
, item
, Taxon
.class);
220 if (accTaxon
== null){
221 fireWarningEvent("NON-ID accepted Name Usage not yet implemented or taxon for name usage id not available", item
, 4);
223 accTaxon
.addSynonym(synonym
, SynonymRelationshipType
.SYNONYM_OF(),null, null);
226 String message
= "Accepted name usage is not of type synonym. This is not allowed in CDM. Can't create realtionship";
227 //TODO check "is this Taxon"
228 fireWarningEvent(message
, item
, 4);
231 if (logger
.isDebugEnabled()){logger
.debug("");}
236 private <T
extends TaxonBase
> T
getTaxonBase(String id
, CsvStreamItem item
, Class
<T
> clazz
) {
238 clazz
= (Class
)TaxonBase
.class;
240 List
<T
> taxonList
= state
.get(TermUri
.DWC_TAXON
.toString(), id
, clazz
);
241 if (taxonList
.size() > 1){
242 String message
= "Undefined taxon mapping for id %s.";
243 message
= String
.format(message
, id
);
244 fireWarningEvent(message
, item
, 8);
245 logger
.warn(message
); //TODO remove when events are handled correctly
247 }else if (taxonList
.isEmpty()){
250 return taxonList
.get(0);
255 //**************************** PARTITIONABLE ************************************************
260 protected void makeForeignKeysForItem(CsvStreamItem item
, Map
<String
, Set
<String
>> fkMap
){
261 //do nothing, their are no foreign keys yet to handle
264 //************************************* TO STRING ********************************************
267 public String
toString(){
268 return this.getClass().getName();