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