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