a05283274045857f07a112620c769c60505e21a9
[cdmlib.git] / cdmlib-io / src / main / java / eu / etaxonomy / cdm / io / dwca / in / DwcTaxonCsv2CdmTaxonRelationConverter.java
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.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.log4j.Logger;
19
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;
29
30 /**
31 * @author a.mueller
32 * @date 23.11.2011
33 *
34 */
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);
38
39 private static final String ID = "id";
40
41 /**
42 * @param state
43 */
44 public DwcTaxonCsv2CdmTaxonRelationConverter(DwcaImportState state) {
45 super();
46 this.state = state;
47 }
48
49
50 public IReader<MappedCdmBase> map(CsvStreamItem item){
51 List<MappedCdmBase> resultList = new ArrayList<MappedCdmBase>();
52
53 Map<String, String> csvRecord = item.map;
54 Reference<?> sourceReference = null;
55 String sourceReferecenDetail = null;
56
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);
63 }else{
64
65 MappedCdmBase mcb = new MappedCdmBase(taxonBase);
66 resultList.add(mcb);
67
68 handleAcceptedNameUsage(item, state, taxonBase, id);
69
70 handleParentNameUsage(item, state, taxonBase, resultList);
71
72 handleKingdom(item, state);
73
74 handlePhylum(item, state);
75
76 handleClass(item, state);
77
78 handleOrder(item, state);
79
80 handleFamily(item, state);
81
82 handleGenus(item, state);
83
84 handleSubGenus(item, state);
85
86 }
87 csvRecord.remove(ID);
88
89
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'/>
94
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'/>
97
98 // <!-- Infraspecific epithet -->
99 // <field index='18' term='http://rs.tdwg.org/dwc/terms/infraspecificEpithet'/>
100
101 // <!-- Acceptance status published in -->
102 // <field index='20' term='http://purl.org/dc/terms/source'/>
103
104 // <!-- Reference in which the scientific name was first published -->
105 // <field index='21' term='http://rs.tdwg.org/dwc/terms/namePublishedIn'/>
106
107 // <!-- Scrutiny date -->
108 // <field index='23' term='http://purl.org/dc/terms/modified'/>
109 // <!-- Additional data for the taxon -->
110
111 // <field index='24' term='http://purl.org/dc/terms/description'/>
112 // </core>
113
114 return new ListReader<MappedCdmBase>(resultList);
115 }
116
117
118 @Override
119 public String getSourceId(CsvStreamItem item) {
120 String id = item.get(ID);
121 return id;
122 }
123
124
125 private void handleSubGenus(CsvStreamItem item, DwcaImportState state) {
126 // TODO Auto-generated method stub
127
128 }
129
130
131 private void handleGenus(CsvStreamItem item, DwcaImportState state) {
132 // TODO Auto-generated method stub
133
134 }
135
136
137 private void handleFamily(CsvStreamItem item, DwcaImportState state) {
138 // TODO Auto-generated method stub
139
140 }
141
142
143 private void handleOrder(CsvStreamItem item, DwcaImportState state) {
144 // TODO Auto-generated method stub
145
146 }
147
148
149 private void handleClass(CsvStreamItem item, DwcaImportState state) {
150 // TODO Auto-generated method stub
151
152 }
153
154
155 private void handlePhylum(CsvStreamItem item, DwcaImportState state) {
156 // TODO Auto-generated method stub
157
158 }
159
160
161 private void handleKingdom(CsvStreamItem item, DwcaImportState state) {
162 // TODO Auto-generated method stub
163
164 }
165
166
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);
175 }else{
176 Classification classification = getClassification(item);
177 Reference<?> citation = null;
178 classification.addParentChild(parentTaxon, taxon, citation, null);
179 resultList.add(new MappedCdmBase(classification));
180 }
181 }else{
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);
185 }
186 }
187
188
189 }
190
191
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;
197 }
198
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()){
202 return null;
203 }else if (result.size() > 1){
204 fireWarningEvent("Dataset is ambigous. I take arbitrary one.", item, 8);
205 }
206 return result.iterator().next();
207 }
208
209
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
215 }
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);
221 }else{
222 accTaxon.addSynonym(synonym, SynonymRelationshipType.SYNONYM_OF(),null, null);
223 }
224 } else{
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);
228 }
229 }else{
230 if (logger.isDebugEnabled()){logger.debug("");}
231 }
232 }
233
234
235 private <T extends TaxonBase> T getTaxonBase(String id, CsvStreamItem item, Class<T> clazz) {
236 if (clazz == null){
237 clazz = (Class)TaxonBase.class;
238 }
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
245 return null;
246 }else if (taxonList.isEmpty()){
247 return null;
248 }else{
249 return taxonList.get(0);
250 }
251 }
252
253
254
255
256 @Override
257 public String toString(){
258 return this.getClass().getName();
259 }
260
261 }