Fix import of misapplied names
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportClassification.java
1 /**
2 * Copyright (C) 2007 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
10 package eu.etaxonomy.cdm.io.redlist.gefaesspflanzen;
11
12 import java.sql.ResultSet;
13 import java.sql.SQLException;
14 import java.util.HashMap;
15 import java.util.HashSet;
16 import java.util.Map;
17 import java.util.Set;
18
19 import org.apache.log4j.Logger;
20 import org.springframework.stereotype.Component;
21
22 import eu.etaxonomy.cdm.common.CdmUtils;
23 import eu.etaxonomy.cdm.io.common.DbImportBase;
24 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26 import eu.etaxonomy.cdm.model.common.CdmBase;
27 import eu.etaxonomy.cdm.model.common.Language;
28 import eu.etaxonomy.cdm.model.common.LanguageString;
29 import eu.etaxonomy.cdm.model.taxon.Classification;
30 import eu.etaxonomy.cdm.model.taxon.Synonym;
31 import eu.etaxonomy.cdm.model.taxon.SynonymRelationship;
32 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
33 import eu.etaxonomy.cdm.model.taxon.Taxon;
34 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
35
36 /**
37 *
38 * @author pplitzner
39 * @date Mar 1, 2016
40 *
41 */
42
43 @Component
44 @SuppressWarnings("serial")
45 public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
46
47 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
48
49 private static final String tableName = "Rote Liste Gefäßpflanzen";
50
51 private static final String pluralString = "classifications";
52
53 public RedListGefaesspflanzenImportClassification() {
54 super(tableName, pluralString);
55 }
56
57 @Override
58 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
59 return "SELECT NAMNR "
60 + "FROM V_TAXATLAS_D20_EXPORT t "
61 + " ORDER BY NAMNR";
62 }
63
64 @Override
65 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
66 String result = " SELECT * "
67 + " FROM V_TAXATLAS_D20_EXPORT t "
68 + " WHERE t.NAMNR IN (@IDSET)";
69 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
70 return result;
71 }
72
73 @Override
74 protected void doInvoke(RedListGefaesspflanzenImportState state) {
75 makeClassification(state);
76 super.doInvoke(state);
77 }
78
79
80 @Override
81 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
82 ResultSet rs = partitioner.getResultSet();
83 Classification gesamtListeClassification = getClassificationService().load(state.getConfig().getClassificationUuid());
84 Classification checklistClassification = getClassificationService().load(RedListUtil.checkListClassificationUuid);
85 try {
86 while (rs.next()){
87 makeSingleTaxonNode(state, rs, gesamtListeClassification, checklistClassification);
88
89 }
90 } catch (SQLException e) {
91 e.printStackTrace();
92 }
93
94 logger.info("Update classification (1000 nodes)");
95 getClassificationService().saveOrUpdate(gesamtListeClassification);
96 getClassificationService().saveOrUpdate(checklistClassification);
97 return true;
98 }
99
100 private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs, Classification gesamtListeClassification, Classification checklistClassification)
101 throws SQLException {
102 long id = rs.getLong(RedListUtil.NAMNR);
103 String parentId = String.valueOf(rs.getLong(RedListUtil.LOWER));
104 String gueltString = rs.getString(RedListUtil.GUELT);
105 String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
106
107 //Gesamtliste
108 TaxonBase taxonBaseGL = state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
109 Taxon parentGL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, parentId, TaxonBase.class);
110 createParentChildNodes(gesamtListeClassification, id, gueltString, taxZusatzString, taxonBaseGL, parentGL);
111
112 //Checkliste
113 TaxonBase taxonBaseCL = state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, String.valueOf(id), TaxonBase.class);
114 Taxon parentCL = (Taxon) state.getRelatedObject(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, parentId, TaxonBase.class);
115 if(taxonBaseCL!=null){//null check necessary because not all taxa exist in the checklist
116 createParentChildNodes(checklistClassification, id, gueltString, taxZusatzString, taxonBaseCL, parentCL);
117 }
118 }
119
120 private void createParentChildNodes(Classification classification, long id, String gueltString,
121 String taxZusatzString, TaxonBase taxonBase, Taxon parent) {
122 if(parent==null){
123 RedListUtil.logMessage(id, "parent taxon of "+taxonBase+" is null." , logger);
124 return;
125 }
126 if(taxonBase==null){
127 RedListUtil.logMessage(id, "child taxon/synonym of "+parent+" is null." , logger);
128 return;
129 }
130 //taxon
131 if(taxonBase.isInstanceOf(Taxon.class)){
132 //misapplied name
133 String appendedPhrase = taxonBase.getAppendedPhrase();
134 if(appendedPhrase!=null && appendedPhrase.equals(RedListUtil.AUCT)){
135 parent.addMisappliedName((Taxon) taxonBase, null, null);
136 }
137 else{
138 classification.addParentChild(parent, (Taxon)taxonBase, null, null);
139 }
140
141 if(CdmUtils.isNotBlank(taxZusatzString)){
142 if(taxZusatzString.trim().equals("p. p.")){
143 RedListUtil.logMessage(id, "pro parte for accepted taxon "+taxonBase, logger);
144 }
145 }
146 }
147 //synonym
148 else if(taxonBase.isInstanceOf(Synonym.class)){
149 //basionym
150 if(gueltString.equals(RedListUtil.GUELT_BASIONYM)){
151 parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
152 parent.getName().addBasionym(taxonBase.getName());
153 }
154 //regular synonym
155 else{
156 SynonymRelationship synonymRelationship = parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
157
158 //TAX_ZUSATZ
159 if(CdmUtils.isNotBlank(taxZusatzString)){
160 if(taxZusatzString.trim().equals("p. p.")){
161 synonymRelationship.setProParte(true);
162 }
163 else if(taxZusatzString.trim().equals("s. l. p. p.")){
164 synonymRelationship.setProParte(true);
165 taxonBase.setAppendedPhrase("s. l.");
166 }
167 else if(taxZusatzString.trim().equals("s. str. p. p.")){
168 synonymRelationship.setProParte(true);
169 taxonBase.setAppendedPhrase("s. str.");
170 }
171 else if(taxZusatzString.trim().equals("s. l.")
172 || taxZusatzString.trim().equals("s. str.")){
173 taxonBase.setAppendedPhrase(taxZusatzString);
174 }
175 else{
176 RedListUtil.logMessage(id, "unknown value "+taxZusatzString+" for column "+RedListUtil.TAX_ZUSATZ, logger);
177 }
178 }
179 }
180 }
181 }
182
183 @Override
184 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
185 RedListGefaesspflanzenImportState state) {
186 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
187
188 Set<String> idSet = new HashSet<String>();
189 try {
190 while (rs.next()){
191 idSet.add(String.valueOf(rs.getLong(RedListUtil.NAMNR)));
192 idSet.add(String.valueOf(rs.getLong(RedListUtil.LOWER)));
193 }
194 } catch (SQLException e) {
195 e.printStackTrace();
196 }
197 Map<String, TaxonBase> taxonMapGesamtListe = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
198 result.put(RedListUtil.TAXON_GESAMTLISTE_NAMESPACE, taxonMapGesamtListe);
199 Map<String, TaxonBase> taxonMapCheckliste = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
200 result.put(RedListUtil.TAXON_CHECKLISTE_NAMESPACE, taxonMapCheckliste);
201 return result;
202 }
203
204 private void makeClassification(RedListGefaesspflanzenImportState state) {
205 //Gesamtliste
206 Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
207 classification.setName(LanguageString.NewInstance("Gesamtliste", Language.DEFAULT()));
208 classification.setUuid(state.getConfig().getClassificationUuid());
209 getClassificationService().save(classification);
210 //checkliste
211 Classification checklistClassification = Classification.NewInstance("Checkliste");
212 checklistClassification.setUuid(RedListUtil.checkListClassificationUuid);
213 getClassificationService().save(checklistClassification);
214 }
215
216 @Override
217 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
218 return false;
219 }
220
221 @Override
222 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
223 return false;
224 }
225
226 }