Fix import of misapplied names
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportNames.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.common.CdmUtils;
23 import eu.etaxonomy.cdm.io.common.DbImportBase;
24 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
25 import eu.etaxonomy.cdm.io.common.ImportHelper;
26 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
27 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
28 import eu.etaxonomy.cdm.model.agent.AgentBase;
29 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
30 import eu.etaxonomy.cdm.model.common.CdmBase;
31 import eu.etaxonomy.cdm.model.name.BotanicalName;
32 import eu.etaxonomy.cdm.model.name.NomenclaturalStatus;
33 import eu.etaxonomy.cdm.model.name.NomenclaturalStatusType;
34 import eu.etaxonomy.cdm.model.name.Rank;
35 import eu.etaxonomy.cdm.model.name.TaxonNameBase;
36 import eu.etaxonomy.cdm.model.taxon.Synonym;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 import eu.etaxonomy.cdm.model.taxon.TaxonRelationship;
40 import eu.etaxonomy.cdm.model.taxon.TaxonRelationshipType;
41
42 /**
43 *
44 * @author pplitzner
45 * @date Mar 1, 2016
46 *
47 */
48
49 @Component
50 @SuppressWarnings("serial")
51 public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
52
53 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
54
55 private static final String tableName = "Rote Liste Gefäßpflanzen";
56
57 private static final String pluralString = "names";
58
59 public RedListGefaesspflanzenImportNames() {
60 super(tableName, pluralString);
61 }
62
63 @Override
64 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
65 return "SELECT NAMNR "
66 + "FROM V_TAXATLAS_D20_EXPORT t "
67 + " ORDER BY NAMNR";
68 }
69
70 @Override
71 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
72 String result = " SELECT * "
73 + " FROM V_TAXATLAS_D20_EXPORT t "
74 + " WHERE t.NAMNR IN (@IDSET)";
75 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
76 return result;
77 }
78
79 @Override
80 protected void doInvoke(RedListGefaesspflanzenImportState state) {
81 super.doInvoke(state);
82 }
83
84
85 @Override
86 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
87 ResultSet rs = partitioner.getResultSet();
88 Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
89 Set<TaxonBase> taxaToSave = new HashSet<TaxonBase>();
90 try {
91 while (rs.next()){
92 makeSingleNameAndTaxon(state, rs, namesToSave, taxaToSave);
93
94 }
95 } catch (SQLException e) {
96 e.printStackTrace();
97 }
98
99 getNameService().saveOrUpdate(namesToSave);
100 getTaxonService().saveOrUpdate(taxaToSave);
101 return true;
102 }
103
104 private void makeSingleNameAndTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave, Set<TaxonBase> taxaToSave)
105 throws SQLException {
106 long id = rs.getLong(RedListUtil.NAMNR);
107 String taxNameString = rs.getString(RedListUtil.TAXNAME);
108 String gueltString = rs.getString(RedListUtil.GUELT);
109 String rangString = rs.getString(RedListUtil.RANG);
110 String ep1String = rs.getString(RedListUtil.EPI1);
111 String ep2String = rs.getString(RedListUtil.EPI2);
112 String ep3String = rs.getString(RedListUtil.EPI3);
113 String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
114 String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
115 String zusatzString = rs.getString(RedListUtil.ZUSATZ);
116 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
117 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
118
119 //---NAME---
120 if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
121 RedListUtil.logMessage(id, "No name found!", logger);
122 }
123
124 Rank rank = makeRank(id, state, rangString);
125 BotanicalName name = BotanicalName.NewInstance(rank);
126
127 //ep1 should always be present
128 if(CdmUtils.isBlank(ep1String)){
129 RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
130 }
131 name.setGenusOrUninomial(ep1String);
132 if(CdmUtils.isNotBlank(ep2String)){
133 name.setSpecificEpithet(ep2String);
134 }
135 if(CdmUtils.isNotBlank(ep3String)){
136 if(rank==Rank.SUBSPECIES() ||
137 rank==Rank.VARIETY()){
138 name.setInfraSpecificEpithet(ep3String);
139 }
140 }
141 //nomenclatural status
142 if(CdmUtils.isNotBlank(nomZusatzString)){
143 NomenclaturalStatusType status = makeNomenclaturalStatus(id, state, nomZusatzString);
144 if(status!=null){
145 name.addStatus(NomenclaturalStatus.NewInstance(status));
146 }
147 }
148
149
150 //--- AUTHORS ---
151 //combination author
152 if(authorKombString.contains(RedListUtil.EX)){
153 //TODO: what happens with multiple ex authors??
154 String[] kombSplit = authorKombString.split(RedListUtil.EX);
155 if(kombSplit.length!=2){
156 RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
157 }
158 for (int i = 0; i < kombSplit.length; i++) {
159 if(i==0){
160 //first author is ex author
161 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
162 name.setExCombinationAuthorship(authorKomb);
163 }
164 else{
165 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
166 name.setCombinationAuthorship(authorKomb);
167 }
168 }
169 }
170 else if(authorKombString.trim().contains(RedListUtil.AUCT)){
171 RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
172 }
173 else if(CdmUtils.isNotBlank(authorKombString)){
174 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
175 name.setCombinationAuthorship(authorKomb);
176 }
177 //basionym author
178 if(authorBasiString.contains(RedListUtil.EX)){
179 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
180 for (int i = 0; i < basiSplit.length; i++) {
181 if(basiSplit.length!=2){
182 RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
183 }
184 if(i==0){
185 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
186 if(CdmUtils.isBlank(authorKombString)){
187 name.setExCombinationAuthorship(authorBasi);
188 }
189 else{
190 name.setExBasionymAuthorship(authorBasi);
191 }
192 }
193 else{
194 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
195 if(CdmUtils.isBlank(authorKombString)){
196 name.setCombinationAuthorship(authorBasi);
197 }
198 else{
199 name.setBasionymAuthorship(authorBasi);
200 }
201 }
202 }
203 }
204 else if(CdmUtils.isNotBlank(authorBasiString)){
205 //this seems to be a convention in the source database: When there is only a single author then only the "AUTOR_BASI" column is used
206 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
207 if(CdmUtils.isBlank(authorKombString)){
208 name.setCombinationAuthorship(authorBasi);
209 }
210 else{
211 name.setBasionymAuthorship(authorBasi);
212 }
213 }
214
215 //check authorship consistency
216 String authorString = rs.getString(RedListUtil.AUTOR);
217 String authorshipCache = name.getAuthorshipCache();
218
219 if(CdmUtils.isNotBlank(zusatzString)){
220 authorString = authorString.replace(", "+zusatzString, "");
221 }
222 if(CdmUtils.isNotBlank(nomZusatzString)){
223 authorString = authorString.replace(", "+nomZusatzString, "");
224 }
225 if(CdmUtils.isNotBlank(taxZusatzString)){
226 authorString = authorString.replace(", "+taxZusatzString, "");
227 }
228 if(authorString.equals(RedListUtil.AUCT)){
229 authorString = "";
230 }
231 if(!authorString.equals(authorshipCache)){
232 RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
233 }
234
235 //id
236 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
237 state.getNameMap().put(id, name.getUuid());
238
239 namesToSave.add(name);
240
241 //---TAXON---
242 TaxonBase taxonBase = null;
243 if(authorBasiString.trim().contains(RedListUtil.AUCT)){
244 taxonBase = Taxon.NewInstance(name, null);
245 taxonBase.setAppendedPhrase(RedListUtil.AUCT);
246 }
247 else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
248 taxonBase = Taxon.NewInstance(name, null);
249 }
250 else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
251 taxonBase = Synonym.NewInstance(name, null);
252 }
253 if(taxonBase==null){
254 RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
255 return;
256 }
257 taxaToSave.add(taxonBase);
258
259 /*check if taxon/synonym is also in checklist
260 * 1. create new taxon with the same name (in the checklist classification)
261 * 2. create congruent concept relationship between both
262 */
263 String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
264 if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
265 TaxonBase clone = (TaxonBase) taxonBase.clone();
266 clone.setName(name);
267 if(taxonBase.isInstanceOf(Taxon.class)){
268 TaxonRelationship taxonRelation = ((Taxon) taxonBase).addTaxonRelation((Taxon) clone, TaxonRelationshipType.CONGRUENT_TO(), null, null);
269 taxonRelation.setDoubtful(true);//TODO Ist das mit " mit Fragezeichen" gemeint?
270 }
271 ImportHelper.setOriginalSource(clone, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE);
272 state.getTaxonMap().put(id, clone.getUuid());
273 taxaToSave.add(clone);
274 }
275
276 //id
277 ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
278 state.getTaxonMap().put(id, taxonBase.getUuid());
279 }
280
281 private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr) {
282 Rank rank = null;
283 try {
284 rank = state.getTransformer().getRankByKey(rankStr);
285 } catch (UndefinedTransformerMethodException e) {
286 e.printStackTrace();
287 }
288 if(rank==null){
289 RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
290 }
291 return rank;
292 }
293
294 private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
295 NomenclaturalStatusType status = null;
296 try {
297 status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
298 } catch (UndefinedTransformerMethodException e) {
299 e.printStackTrace();
300 }
301 if(status==null){
302 RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
303 }
304 return status;
305 }
306
307
308
309 @Override
310 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
311 RedListGefaesspflanzenImportState state) {
312 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
313 Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
314
315 try {
316 while (rs.next()){
317 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
318
319 if(authorKombString.contains(RedListUtil.EX)){
320 String[] kombSplit = authorKombString.split(RedListUtil.EX);
321 for (int i = 0; i < kombSplit.length; i++) {
322 if(!authorMap.containsKey(kombSplit[i])){
323 authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
324 }
325 }
326 }
327 else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
328 authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
329 }
330
331 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
332 //basionym author
333 if(authorBasiString.contains(RedListUtil.EX)){
334 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
335 for (int i = 0; i < basiSplit.length; i++) {
336 if(!authorMap.containsKey(basiSplit[i])){
337 authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
338 }
339 }
340 }
341 else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
342 authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
343 }
344 }
345 } catch (SQLException e) {
346 e.printStackTrace();
347 }
348 result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
349
350 return result;
351 }
352
353 @Override
354 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
355 return false;
356 }
357
358 @Override
359 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
360 return false;
361 }
362
363 }