Handle Misapplications
[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.io.common.DbImportBase;
23 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
25 import eu.etaxonomy.cdm.model.common.CdmBase;
26 import eu.etaxonomy.cdm.model.taxon.Classification;
27 import eu.etaxonomy.cdm.model.taxon.Synonym;
28 import eu.etaxonomy.cdm.model.taxon.SynonymRelationshipType;
29 import eu.etaxonomy.cdm.model.taxon.Taxon;
30 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
31
32 /**
33 *
34 * @author pplitzner
35 * @date Mar 1, 2016
36 *
37 */
38
39 @Component
40 @SuppressWarnings("serial")
41 public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
42
43 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
44
45 private static final String tableName = "Rote Liste Gefäßpflanzen";
46
47 private static final String pluralString = "classifications";
48
49 public RedListGefaesspflanzenImportClassification() {
50 super(tableName, pluralString);
51 }
52
53 @Override
54 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
55 return "SELECT NAMNR "
56 + "FROM V_TAXATLAS_D20_EXPORT t "
57 + " ORDER BY NAMNR";
58 }
59
60 @Override
61 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
62 String result = " SELECT * "
63 + " FROM V_TAXATLAS_D20_EXPORT t "
64 + " WHERE t.NAMNR IN (@IDSET)";
65 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
66 return result;
67 }
68
69 @Override
70 protected void doInvoke(RedListGefaesspflanzenImportState state) {
71 makeClassification(state);
72 super.doInvoke(state);
73 }
74
75
76 @Override
77 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
78 ResultSet rs = partitioner.getResultSet();
79 Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
80 try {
81 while (rs.next()){
82 makeSingleTaxonNode(state, rs, classification);
83
84 }
85 } catch (SQLException e) {
86 e.printStackTrace();
87 }
88
89 logger.info("Update classification (1000 nodes)");
90 getClassificationService().saveOrUpdate(classification);
91 return true;
92 }
93
94 private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs, Classification classification)
95 throws SQLException {
96 String id = String.valueOf(rs.getLong("NAMNR"));
97 String parentId = String.valueOf(rs.getLong("LOWER"));
98 String gueltString = rs.getString("GUELT");
99
100 TaxonBase taxonBase = state.getRelatedObject(Namespace.TAXON_NAMESPACE, id, TaxonBase.class);
101 Taxon parent = (Taxon) state.getRelatedObject(Namespace.TAXON_NAMESPACE, parentId, TaxonBase.class);
102
103 //misapplied name
104 if(taxonBase.getName().getAppendedPhrase().contains("auct.")){
105 //TODO why can't I add synonymy as misapplications
106 // parent.addMisappliedName(misappliedNameTaxon, citation, microcitation)
107 }
108 //taxon
109 else if(taxonBase.isInstanceOf(Taxon.class)){
110 classification.addParentChild(parent, (Taxon)taxonBase, null, null);
111 }
112 else if(taxonBase.isInstanceOf(Synonym.class)){
113 //basionym
114 if(gueltString.equals("b")){
115 parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
116 parent.getName().addBasionym(taxonBase.getName());
117 }
118 //regular synonym
119 else{
120 //TODO: how to correctly add a synonym?
121 parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.HETEROTYPIC_SYNONYM_OF(), null, null);
122 // parent.addSynonym((Synonym) taxonBase, SynonymRelationshipType.SYNONYM_OF(), null, null);
123 }
124 }
125 }
126
127 @Override
128 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
129 RedListGefaesspflanzenImportState state) {
130 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
131
132 Set<String> idSet = new HashSet<String>();
133 try {
134 while (rs.next()){
135 idSet.add(String.valueOf(rs.getLong("NAMNR")));
136 idSet.add(String.valueOf(rs.getLong("LOWER")));
137 }
138 } catch (SQLException e) {
139 e.printStackTrace();
140 }
141 Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>) getCommonService().getSourcedObjectsByIdInSource(TaxonBase.class, idSet, Namespace.TAXON_NAMESPACE);
142 result.put(Namespace.TAXON_NAMESPACE, taxonMap);
143 return result;
144 }
145
146 private void makeClassification(RedListGefaesspflanzenImportState state) {
147 Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
148 classification.setUuid(state.getConfig().getClassificationUuid());
149 getClassificationService().save(classification);
150 }
151
152 @Override
153 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
154 return false;
155 }
156
157 @Override
158 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
159 return false;
160 }
161
162 }