ref #6241 change @date to @since in appimport
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / greece / FloraHellenicaExcludedTaxonImport.java
1 /**
2 * Copyright (C) 2016 EDIT
3 * European Distributed Institute of Taxonomy
4 * http://www.e-taxonomy.eu
5 *
6 * The contents of this file are subject to the Mozilla Public License Version 1.1
7 * See LICENSE.TXT at the top of this package for the full license terms.
8 */
9 package eu.etaxonomy.cdm.io.greece;
10
11 import java.util.Arrays;
12 import java.util.HashMap;
13 import java.util.List;
14 import java.util.Set;
15
16 import org.apache.log4j.Logger;
17 import org.springframework.stereotype.Component;
18
19 import eu.etaxonomy.cdm.io.mexico.SimpleExcelTaxonImportState;
20 import eu.etaxonomy.cdm.model.agent.Person;
21 import eu.etaxonomy.cdm.model.common.TimePeriod;
22 import eu.etaxonomy.cdm.model.name.IBotanicalName;
23 import eu.etaxonomy.cdm.model.name.INonViralName;
24 import eu.etaxonomy.cdm.model.name.NomenclaturalCode;
25 import eu.etaxonomy.cdm.model.name.Rank;
26 import eu.etaxonomy.cdm.model.name.TaxonName;
27 import eu.etaxonomy.cdm.model.name.TaxonNameFactory;
28 import eu.etaxonomy.cdm.model.reference.Reference;
29 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
30 import eu.etaxonomy.cdm.model.taxon.Classification;
31 import eu.etaxonomy.cdm.model.taxon.ITaxonTreeNode;
32 import eu.etaxonomy.cdm.model.taxon.Taxon;
33 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
34 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
35
36 /**
37 * @author a.mueller
38 * @since 14.12.2016
39 *
40 */
41
42 @Component
43 public class FloraHellenicaExcludedTaxonImport<CONFIG extends FloraHellenicaImportConfigurator>
44 extends FloraHellenicaImportBase<CONFIG>{
45
46
47 private static final long serialVersionUID = 2629253144140992196L;
48 private static final Logger logger = Logger.getLogger(FloraHellenicaExcludedTaxonImport.class);
49
50 private static final String TAXON = "Taxon";
51 private static final String UNIQUE_ID = "Unique ID";
52 private static final String FAMILY = "Family";
53
54 private static List<String> expectedKeys= Arrays.asList(new String[]{
55 UNIQUE_ID,FAMILY,TAXON
56 });
57
58 private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
59 private TaxonNode excludedFamilyNode;
60
61 @Override
62 protected String getWorksheetName() {
63 return "excluded taxa";
64 }
65
66 boolean isFirst = true;
67 /**
68 * {@inheritDoc}
69 */
70 @Override
71 protected void firstPass(SimpleExcelTaxonImportState<CONFIG> state) {
72
73 String line = state.getCurrentLine() + ": ";
74 HashMap<String, String> record = state.getOriginalRecord();
75
76 Set<String> keys = record.keySet();
77 for (String key: keys) {
78 if (! expectedKeys.contains(key)){
79 logger.warn(line + "Unexpected Key: " + key);
80 }
81 }
82 if (isFirst){
83 System.out.println("Start excluded taxa");
84 isFirst = false;
85 }
86
87 String noStr = getValue(record, UNIQUE_ID);
88 TaxonNode taxonNode = makeTaxon(state, line, record, noStr);
89 if (taxonNode != null){
90 state.putTaxon(noStr, taxonNode.getTaxon());
91 }
92 }
93
94
95 /**
96 * @param state
97 * @param line
98 * @param record
99 * @param noStr
100 * @return
101 */
102 private TaxonNode makeTaxon(SimpleExcelTaxonImportState<CONFIG> state, String line,
103 HashMap<String, String> record,
104 String noStr) {
105
106 TaxonNode familyTaxonNode = getFamilyTaxon(record, state);
107 familyTaxonNode = getTaxonNodeService().find(familyTaxonNode.getUuid());
108 if (familyTaxonNode == null){
109 logger.warn(line + "Family not created, can't add excluded taxon: " + record.get(FAMILY));
110 return null;
111 }
112
113 String taxonStr = getValue(record, TAXON);
114 Reference sec = getSecReference(state);
115 if (taxonStr.endsWith(" sec. Hayek 1929")){
116 sec = makeHayek1929();
117 taxonStr = taxonStr.substring(0, taxonStr.length() - " sec. Hayek 1929".length()).trim();
118 }
119 boolean isSensuStrictu = false;
120 if (taxonStr.endsWith("s.str.")){
121 isSensuStrictu = true;
122 taxonStr = taxonStr.substring(0, taxonStr.length() - "s.str.".length() ).trim();
123 }
124 INonViralName name = parser.parseFullName(taxonStr, NomenclaturalCode.ICNAFP, null);
125 name = replaceNameAuthorsAndReferences(state, name);
126 if (name.isProtectedTitleCache()){
127 logger.warn(line + "Name could not be parsed: " + taxonStr);
128 }
129
130 Taxon taxon = Taxon.NewInstance(name, sec);
131 if (isSensuStrictu){
132 taxon.setAppendedPhrase("s.str.");
133 }
134 taxon.addImportSource(noStr, getWorksheetName(), getSourceCitation(state), null);
135 TaxonNode excludedNode = familyTaxonNode.addChildTaxon(taxon, getSecReference(state), null);
136 excludedNode.setExcluded(true);
137 getTaxonNodeService().saveOrUpdate(excludedNode);
138 return excludedNode;
139 }
140
141
142
143 /**
144 * @return
145 */
146 private Reference makeHayek1929() {
147 Reference ref = ReferenceFactory.newGeneric();
148 Person hayek = Person.NewInstance();
149 hayek.setLastname("Hayek");
150 ref.setAuthorship(hayek);
151 ref.setDatePublished(TimePeriod.NewInstance(1929));
152 return ref;
153 }
154
155
156 /**
157 * @param record
158 * @param state
159 * @return
160 */
161 private TaxonNode getFamilyTaxon(HashMap<String, String> record,
162 SimpleExcelTaxonImportState<CONFIG> state) {
163
164 String familyStr = getValue(record, FAMILY);
165 if (familyStr == null){
166 return null;
167 }
168 familyStr = familyStr.trim();
169
170 // Taxon family = state.getHigherTaxon(familyStr);
171 Taxon family = this.getHigherTaxon(record, state, FAMILY);
172 TaxonNode familyNode;
173 if (family != null){
174 familyNode = family.getTaxonNodes().iterator().next();
175 }else{
176 IBotanicalName name = makeFamilyName(state, familyStr);
177 name = replaceNameAuthorsAndReferences(state, name);
178
179 Reference sec = getSecReference(state);
180 family = Taxon.NewInstance(name, sec);
181
182 ITaxonTreeNode groupNode = getExcludedFamilyTaxon(state);
183 familyNode = groupNode.addChildTaxon(family, sec, null);
184 state.putHigherTaxon(familyStr, family);
185 getTaxonNodeService().saveOrUpdate(familyNode);
186 // logger.warn(state.getCurrentLine() +": " + "Family not found for excluded taxon");
187 }
188 return familyNode;
189 }
190
191
192 private ITaxonTreeNode getExcludedFamilyTaxon(
193 SimpleExcelTaxonImportState<CONFIG> state) {
194
195 if (excludedFamilyNode != null){
196 return this.excludedFamilyNode;
197 }
198 Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
199 TaxonNode plantae = classification.getChildNodes().iterator().next();
200
201 TaxonName name = TaxonNameFactory.NewBotanicalInstance(Rank.SUPERFAMILY());
202 name.setTitleCache("Excluded", true);
203 Taxon taxon = Taxon.NewInstance(name, getSecReference(state));
204 excludedFamilyNode = plantae.addChildTaxon(taxon, getSourceCitation(state), null);
205 excludedFamilyNode.setExcluded(true);
206 getTaxonNodeService().saveOrUpdate(excludedFamilyNode);
207 return excludedFamilyNode;
208 }
209
210 }