Ordered input for easier comparison
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / edaphobase / EdaphobaseClassificationImport.java
1 // $Id$
2 /**
3 * Copyright (C) 2015 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.edaphobase;
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 import java.util.UUID;
19
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
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.name.TaxonNameBase;
27 import eu.etaxonomy.cdm.model.reference.Reference;
28 import eu.etaxonomy.cdm.model.taxon.Classification;
29 import eu.etaxonomy.cdm.model.taxon.Synonym;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32
33 /**
34 * @author a.mueller
35 * @date 21.12.2015
36 *
37 */
38 @Component
39 public class EdaphobaseClassificationImport extends EdaphobaseImportBase {
40 private static final long serialVersionUID = -9138378836474086070L;
41
42 private static final Logger logger = Logger.getLogger(EdaphobaseClassificationImport.class);
43
44 private static final String tableName = "tax_taxon";
45
46 private static final String pluralString = "taxon relationships";
47
48
49 /**
50 * @param tableName
51 * @param pluralString
52 */
53 public EdaphobaseClassificationImport() {
54 super(tableName, pluralString);
55 }
56
57 @Override
58 protected String getIdQuery(EdaphobaseImportState state) {
59 return "SELECT taxon_id FROM "
60 + " (SELECT DISTINCT taxon_id, length(path_to_root) FROM tax_taxon t "
61 + " ORDER BY length(path_to_root), taxon_id) as drvTbl ";
62 }
63
64 @Override
65 protected String getRecordQuery(EdaphobaseImportConfigurator config) {
66 String result = "SELECT DISTINCT t.* "
67 + " FROM tax_taxon t"
68 + " WHERE taxon_id IN (@IDSET)";
69 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
70 return result;
71 }
72
73 @Override
74 protected void doInvoke(EdaphobaseImportState state) {
75 makeClassification(state);
76 super.doInvoke(state);
77 }
78
79
80 @Override
81 public boolean doPartition(ResultSetPartitioner partitioner, EdaphobaseImportState state) {
82 ResultSet rs = partitioner.getResultSet();
83 Map<String, Classification> map = partitioner.getObjectMap(CLASSIFICATION_NAMESPACE);
84 Classification classification = map.get(state.getConfig().getClassificationUuid().toString());
85 Reference<?> sourceReference = state.getTransactionalSourceReference();
86
87 Set<TaxonBase> taxaToSave = new HashSet<>();
88 try {
89 while (rs.next()){
90 int id = rs.getInt("taxon_id");
91 //parentTaxonFk
92 boolean isValid = rs.getBoolean("valid");
93 // boolean idDeleted = rs.getBoolean("deleted");
94 // String treeIndex = rs.getString("path_to_root");
95 // Integer rankFk = rs.getInt("tax_rank_fk");
96 // String officialRemark = rs.getString("official_remark");
97 // boolean isGroup = rs.getBoolean("taxonomic_group");
98 Integer parentTaxonFk = nullSafeInt(rs, "parent_taxon_fk");
99
100 if (parentTaxonFk != null){
101 TaxonBase<?> parent = state.getRelatedObject(TAXON_NAMESPACE, parentTaxonFk.toString(), TaxonBase.class);
102 if (parent == null){
103 logger.warn("Parent taxon " + parentTaxonFk + " not found for taxon " + id );
104 }else{
105
106 TaxonNameBase<?,?> parentName = parent.getName();
107
108 TaxonBase<?> child = state.getRelatedObject(TAXON_NAMESPACE, String.valueOf(id), TaxonBase.class);
109 // TaxonNameBase<?,?> childName = child.getName();
110
111 // handleMissingNameParts(CdmBase.deproxy(childName, NonViralName.class), CdmBase.deproxy(parentName, NonViralName.class));
112
113 if (isValid){
114 if (parent.isInstanceOf(Synonym.class)){
115 logger.warn("Parent taxon (" + parentTaxonFk + " is not valid for valid child " + id);
116 }else{
117 Taxon accParent = CdmBase.deproxy(parent, Taxon.class);
118 classification.addParentChild(accParent, (Taxon)child, sourceReference, null);
119 taxaToSave.add(accParent);
120 }
121 }else{
122 // Synonym synonym = CdmBase.deproxy(child, Synonym.class);
123 // if (synonym == null){
124 // logger.warn("Synonym " + id + " not found for taxon ");
125 // }
126 // if(parent.isInstanceOf(Synonym.class)){
127 // String message = "Taxon ("+parentTaxonFk+") is not accepted but synonym. Can't add synonym ("+id+")";
128 // logger.warn(message);
129 // }else{
130 // Taxon accepted = CdmBase.deproxy(parent, Taxon.class);
131 //// accepted.addSynonym(synonym, SynonymRelationshipType.SYNONYM_OF());
132 // taxaToSave.add(accepted);
133 // }
134 }
135 }
136 }
137
138 // //id
139 // String nameSpace = "tax_taxon";
140 // ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, nameSpace);
141 // ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, nameSpace);
142
143
144 }
145 } catch (SQLException e) {
146 e.printStackTrace();
147 }
148
149 getTaxonService().saveOrUpdate(taxaToSave);
150 return true;
151 }
152
153 // /**
154 // * @param childName
155 // * @param parentName
156 // */
157 // private void handleMissingNameParts(NonViralName<?> childName, NonViralName<?> parentName) {
158 // if (childName.getGenusOrUninomial())
159 // }
160
161 @Override
162 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
163 EdaphobaseImportState state) {
164 String nameSpace;
165 Class<?> cdmClass;
166 Set<String> idSet;
167 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
168
169 try{
170 Set<String> taxonIdSet = new HashSet<String>();
171 while (rs.next()){
172 handleForeignKey(rs, taxonIdSet, "taxon_id");
173 handleForeignKey(rs, taxonIdSet, "parent_taxon_fk");
174 }
175
176 //name map
177 nameSpace = TAXON_NAMESPACE;
178 cdmClass = TaxonBase.class;
179 idSet = taxonIdSet;
180 Map<String, TaxonBase> taxonMap = (Map<String, TaxonBase>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
181 result.put(nameSpace, taxonMap);
182
183 //Classification
184 Map<String, Classification> classificationMap = new HashMap<>();
185 UUID classificationUuid = state.getConfig().getClassificationUuid();
186 Classification classification = getClassificationService().find(state.getConfig().getClassificationUuid());
187 classificationMap.put(classificationUuid.toString(), classification);
188 result.put(CLASSIFICATION_NAMESPACE, classificationMap);
189
190 } catch (SQLException e) {
191 throw new RuntimeException(e);
192 }
193
194 return result;
195 }
196
197
198 /**
199 * @param state
200 */
201 private void makeClassification(EdaphobaseImportState state) {
202 Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
203 classification.setUuid(state.getConfig().getClassificationUuid());
204 getClassificationService().save(classification);
205 }
206
207
208 @Override
209 protected boolean doCheck(EdaphobaseImportState state) {
210 return false;
211 }
212
213 @Override
214 protected boolean isIgnore(EdaphobaseImportState state) {
215 return ! state.getConfig().isDoTaxa();
216 }
217
218 }