ref #9932 handle Plantae and Tracheophyta
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / mexico / MexicoEfloraCommonNameRefImport.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.CommonTaxonName;
26 import eu.etaxonomy.cdm.model.description.DescriptionElementSource;
27 import eu.etaxonomy.cdm.model.name.TaxonName;
28 import eu.etaxonomy.cdm.model.reference.Reference;
29
30 /**
31 * @author a.mueller
32 * @since 08.02.2022
33 */
34 @Component
35 public class MexicoEfloraCommonNameRefImport extends MexicoEfloraImportBase {
36
37 private static final long serialVersionUID = -1712022169834400067L;
38 private static final Logger logger = Logger.getLogger(MexicoEfloraCommonNameRefImport.class);
39
40 protected static final String NAMESPACE = "CommonNameRef";
41
42 private static final String pluralString = "common name sources";
43 private static final String dbTableName = "Eflora_RelBiblioNomComun";
44
45 public MexicoEfloraCommonNameRefImport(){
46 super(dbTableName, pluralString);
47 }
48
49 @Override
50 protected String getIdQuery(MexicoEfloraImportState state) {
51 String sql = " SELECT IdCombinado "
52 + " FROM " + dbTableName
53 + " ORDER BY IdCombinado ";
54 return sql;
55 }
56
57 @Override
58 protected String getRecordQuery(MexicoEfloraImportConfigurator config) {
59 String sqlSelect = " SELECT * ";
60 String sqlFrom = " FROM " + dbTableName;
61 String sqlWhere = " WHERE ( IdCombinado IN (" + ID_LIST_TOKEN + ") )";
62
63 String strRecordQuery =sqlSelect + " " + sqlFrom + " " + sqlWhere ;
64 return strRecordQuery;
65 }
66
67 @Override
68 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, MexicoEfloraImportState state) {
69
70 boolean success = true ;
71
72 @SuppressWarnings("unchecked")
73 Map<String, CommonTaxonName> commonNameMap = partitioner.getObjectMap(MexicoEfloraCommonNameImport.NAMESPACE);
74
75 @SuppressWarnings("unchecked")
76 Map<String, Reference> referenceMap = partitioner.getObjectMap(MexicoEfloraReferenceImportBase.NAMESPACE);
77
78 ResultSet rs = partitioner.getResultSet();
79 try{
80 while (rs.next()){
81
82 // if ((i++ % modCount) == 0 && i!= 1 ){ logger.info("PTaxa handled: " + (i-1));}
83
84 //create TaxonName element
85 String idCombi = rs.getString("IdCombinado");
86 int idBibliografia = rs.getInt("IdBibliografia");
87 String observaciones = rs.getString("Observaciones");
88 //needed?
89 // String idNomComun = rs.getString("IdNomComun");
90
91 try {
92 CommonTaxonName commonName = commonNameMap.get(idCombi);
93
94 Reference ref = referenceMap.get(String.valueOf(idBibliografia));
95 // String detail = state.getRefDetailMap().get(idBibliografia);
96 String detail = null;
97
98 if (commonName != null) {
99 DescriptionElementSource source = commonName.addPrimaryTaxonomicSource(ref, detail);
100 if (source!= null) {
101 TaxonName nameUsedInSource = getNameUsedInSource(state, observaciones);
102 source.setNameUsedInSource(nameUsedInSource);
103 //TODO other observaciones
104 } else {
105 logger.warn("Source not found for " + idCombi + " and bibID: " + idBibliografia);
106 }
107 }else {
108 logger.warn("CommonName not found for " + idCombi);
109 }
110
111 partitioner.startDoSave();
112 } catch (Exception e) {
113 e.printStackTrace();
114 logger.warn("An exception (" +e.getMessage()+") occurred when trying to create common name for id " + idCombi + ".");
115 success = false;
116 }
117 }
118 } catch (Exception e) {
119 logger.error("SQLException:" + e);
120 return false;
121 }
122
123 logger.warn("Finished partition");
124
125 return success;
126 }
127
128 private TaxonName getNameUsedInSource(MexicoEfloraImportState state, String observaciones) {
129 // TODO named used in source for common names
130 return null;
131 }
132
133 @Override
134 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, MexicoEfloraImportState state) {
135
136 String nameSpace;
137 Set<String> idSet;
138 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
139
140 try{
141 Set<String> commonNameIdSet = new HashSet<>();
142 Set<String> referenceIdSet = new HashSet<>();
143 while (rs.next()){
144 handleForeignKey(rs, referenceIdSet, "IdBibliografia");
145 handleForeignKey(rs, commonNameIdSet, "IdCombinado");
146 }
147
148 //common name map
149 nameSpace = MexicoEfloraCommonNameImport.NAMESPACE;
150 Map<UUID,String> uuidMap = new HashMap<>();
151 commonNameIdSet.stream().forEach(cnId->uuidMap.put(state.getCommonNameMap().get(cnId),cnId));
152 @SuppressWarnings({ "rawtypes", "unchecked" })
153 List<CommonTaxonName> commonNames = (List)getDescriptionElementService().find(uuidMap.keySet());
154 Map<String, CommonTaxonName> commonNameMap = new HashMap<>();
155 commonNames.stream().forEach(cn->commonNameMap.put(uuidMap.get(cn.getUuid()), cn));
156 result.put(nameSpace, commonNameMap);
157
158 //reference map
159 nameSpace = MexicoEfloraReferenceImportBase.NAMESPACE;
160 idSet = referenceIdSet;
161 Map<String, Reference> referenceMap = getCommonService().getSourcedObjectsByIdInSourceC(Reference.class, idSet, nameSpace);
162 result.put(nameSpace, referenceMap);
163
164 } catch (SQLException e) {
165 throw new RuntimeException(e);
166 }
167 return result;
168 }
169
170 @Override
171 protected String getTableName() {
172 return dbTableName;
173 }
174
175 @Override
176 public String getPluralString() {
177 return pluralString;
178 }
179
180 @Override
181 protected boolean doCheck(MexicoEfloraImportState state){
182 return true;
183 }
184
185 @Override
186 protected boolean isIgnore(MexicoEfloraImportState state){
187 return ! state.getConfig().isDoTaxa();
188 }
189 }