ref #9932 handle Plantae and Tracheophyta
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraFactImport.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 package eu.etaxonomy.cdm.io.mexico;
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.List;
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.ResultSetPartitioner;
24 import eu.etaxonomy.cdm.model.common.CdmBase;
25 import eu.etaxonomy.cdm.model.description.CategoricalData;
26 import eu.etaxonomy.cdm.model.description.Feature;
27 import eu.etaxonomy.cdm.model.description.State;
28 import eu.etaxonomy.cdm.model.description.TaxonDescription;
29 import eu.etaxonomy.cdm.model.reference.Reference;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32
33 /**
34 * @author a.mueller
35 * @since 08.02.2022
36 */
37 @Component
38 public class MexicoEfloraFactImport extends MexicoEfloraImportBase {
39
40 private static final long serialVersionUID = 8097679811768529307L;
41 private static final Logger logger = Logger.getLogger(MexicoEfloraFactImport.class);
42
43 protected static final String NAMESPACE = "Facts";
44
45 private static final String pluralString = "facts";
46 private static final String dbTableName = "Eflora_RelBiblioNombreCatalogoNombre";
47
48 public MexicoEfloraFactImport(){
49 super(dbTableName, pluralString);
50 }
51
52 @Override
53 protected String getIdQuery(MexicoEfloraImportState state) {
54 String sql = " SELECT id "
55 + " FROM " + dbTableName
56 + " ORDER BY IdCAT, IdCatNombre, IdBibliografia ";
57 return sql;
58 }
59
60 @Override
61 protected String getRecordQuery(MexicoEfloraImportConfigurator config) {
62 String sqlSelect = " SELECT f.*, t.uuid taxonUuid ";
63 String sqlFrom = " FROM " + dbTableName + " f LEFT JOIN " + MexicoEfloraTaxonImport.dbTableName + " t ON f.IdCAT = t.IdCAT ";
64 String sqlWhere = " WHERE ( Id IN (" + ID_LIST_TOKEN + ") )";
65 String sqlOrderBy = " ORDER BY IdCAT, IdCatNombre, IdBibliografia";
66
67 String strRecordQuery =sqlSelect + " " + sqlFrom + " " + sqlWhere + sqlOrderBy;
68 return strRecordQuery;
69 }
70
71 private CategoricalData lastFact;
72 private String lastIdCat = "-1";
73 private int lastIdCatNombre = -1;
74 @Override
75 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
76
77 Reference sourceReference = this.getSourceReference(state.getConfig().getSourceReference());
78 //hope this is transaction save
79 if (lastFact != null) {
80 lastFact = (CategoricalData)getDescriptionElementService().find(lastFact.getUuid());
81 }
82 boolean success = true ;
83
84 @SuppressWarnings("unchecked")
85 Map<String, TaxonBase<?>> taxonMap = partitioner.getObjectMap(MexicoEfloraTaxonImport.NAMESPACE);
86 @SuppressWarnings("unchecked")
87 Map<String, Reference> referenceMap = partitioner.getObjectMap(MexicoEfloraReferenceImportBase.NAMESPACE);
88
89 ResultSet rs = partitioner.getResultSet();
90 try{
91 while (rs.next()){
92
93 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
94
95 //create TaxonName element
96 int id = rs.getInt("id"); //only for partitioning and logging
97 String idCAT = rs.getString("IdCAT");
98 int idCatNombre = rs.getInt("IdCatNombre");
99 String uuidTaxonStr = rs.getString("taxonUuid");
100
101 int idBibliografia = rs.getInt("IdBibliografia");
102 //TODO observaciones in facts
103 String observaciones = rs.getString("Observaciones");
104
105 try {
106 CategoricalData categoricalData;
107 if (idCAT.equals(lastIdCat) && idCatNombre == lastIdCatNombre) {
108 categoricalData = lastFact;
109 }else {
110 categoricalData = makeCategoricalData(state, idCatNombre);
111 Feature lastFeature = lastFact == null? null : lastFact.getFeature();
112 if (idCAT.equals(lastIdCat) && categoricalData.getFeature().equals(lastFeature)) {
113 //merge
114 //add the single new state to the existing categorical data
115 //TODO not fully correct if bibliography differs for the single states;
116 State newState = categoricalData.getStatesOnly().stream().findFirst().orElse(null);
117 if (newState != null) {
118 lastFact.addStateData(newState);
119 }
120 categoricalData = lastFact;
121 // lastIdCatNombre = idCatNombre;
122 }else {
123 //new categorical data
124 TaxonBase<?> taxonBase = taxonMap.get(uuidTaxonStr);
125 Taxon taxon;
126 if (taxonBase.isInstanceOf(Taxon.class)) {
127 taxon = CdmBase.deproxy(taxonBase, Taxon.class);
128 }else {
129 logger.warn(idCatNombre + ": Taxon is not accepted: " + idCAT);
130 continue;
131 }
132
133 //TODO source reference correct?
134 TaxonDescription description = this.getTaxonDescription(taxon, sourceReference,
135 false, true);
136 description.addElement(categoricalData);
137 lastFact = categoricalData;
138 lastIdCat = idCAT;
139 lastIdCatNombre = idCatNombre;
140 }
141 }
142 handleBibliografia(state, referenceMap, categoricalData, idBibliografia, id);
143
144 partitioner.startDoSave();
145 } catch (Exception e) {
146 e.printStackTrace();
147 logger.warn("An exception (" +e.getMessage()+") occurred when trying to create fact for id " + id + ".");
148 success = false;
149 }
150 }
151 } catch (Exception e) {
152 logger.error("SQLException:" + e);
153 return false;
154 }
155 logger.warn("Next partition");
156
157 return success;
158 }
159
160 private void handleBibliografia(MexicoEfloraImportState state, Map<String, Reference> referenceMap,
161 CategoricalData categoricalData, int idBibliografia,
162 int id) {
163 Reference ref = referenceMap == null ? null : referenceMap.get(String.valueOf(idBibliografia));
164 // String detail = state.getRefDetailMap().get(idBibliografia);
165 String detail = null;
166
167 if (ref != null) {
168 if (categoricalData != null) {
169 categoricalData.addPrimaryTaxonomicSource(ref, detail);
170 }else {
171 logger.warn("Fact does not exist: " + id);
172 }
173 }else {
174 logger.warn("Source not found for " + id + " and bibID: " + idBibliografia);
175 }
176 }
177
178 private CategoricalData makeCategoricalData(MexicoEfloraImportState importState,
179 int idCatNombre) {
180 Feature feature = getFeature(importState, idCatNombre);
181 State state = getState(importState, idCatNombre);
182 CategoricalData categoricalData = CategoricalData.NewInstance(state, feature);
183 return categoricalData;
184 }
185
186 private State getState(MexicoEfloraImportState importState, int idCatNombre) {
187 State result = importState.getStateMap().get(idCatNombre);
188 if (result == null) {
189 logger.warn("State does not exist: " + idCatNombre);
190 }
191 return result;
192 }
193
194 private Feature getFeature(MexicoEfloraImportState importState, int idCatNombre) {
195 return importState.getFeatureMap().get(idCatNombre);
196 }
197
198 @Override
199 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
200
201 String nameSpace;
202 Set<String> idSet;
203 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
204
205 try{
206 Set<UUID> taxonIdSet = new HashSet<>();
207 Set<String> referenceIdSet = new HashSet<>();
208 while (rs.next()){
209 handleForeignUuidKey(rs, taxonIdSet, "taxonUuid");
210 handleForeignKey(rs, referenceIdSet, "IdBibliografia");
211 }
212
213 //taxon map
214 nameSpace = MexicoEfloraTaxonImport.NAMESPACE;
215 @SuppressWarnings("rawtypes")
216 Map<String, TaxonBase> taxonMap = new HashMap<>();
217 @SuppressWarnings("rawtypes")
218 List<TaxonBase> taxa = getTaxonService().find(taxonIdSet);
219 taxa.stream().forEach(t->taxonMap.put(t.getUuid().toString(), t));
220 result.put(nameSpace, taxonMap);
221
222 //reference map
223 nameSpace = MexicoEfloraReferenceImportBase.NAMESPACE;
224 idSet = referenceIdSet;
225 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
226 result.put(nameSpace, referenceMap);
227
228 } catch (SQLException e) {
229 throw new RuntimeException(e);
230 }
231 return result;
232 }
233
234 @Override
235 protected String getTableName() {
236 return dbTableName;
237 }
238
239 @Override
240 public String getPluralString() {
241 return pluralString;
242 }
243
244 @Override
245 protected boolean doCheck(MexicoEfloraImportState state){
246 return true;
247 }
248
249 @Override
250 protected boolean isIgnore(MexicoEfloraImportState state){
251 return ! state.getConfig().isDoTaxa();
252 }
253 }