Correctly set relationship direction #5448
[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 //cell values
107 long id = rs.getLong(RedListUtil.NAMNR);
108 String taxNameString = rs.getString(RedListUtil.TAXNAME);
109 String gueltString = rs.getString(RedListUtil.GUELT);
110 String rangString = rs.getString(RedListUtil.RANG);
111 String ep1String = rs.getString(RedListUtil.EPI1);
112 String ep2String = rs.getString(RedListUtil.EPI2);
113 String ep3String = rs.getString(RedListUtil.EPI3);
114 String nomZusatzString = rs.getString(RedListUtil.NOM_ZUSATZ);
115 String taxZusatzString = rs.getString(RedListUtil.TAX_ZUSATZ);
116 String zusatzString = rs.getString(RedListUtil.ZUSATZ);
117 String nonString = rs.getString(RedListUtil.NON);
118 String sensuString = rs.getString(RedListUtil.SENSU);
119 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
120 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
121 String hybString = rs.getString(RedListUtil.HYB);
122 String clTaxonString = rs.getString(RedListUtil.CL_TAXON);
123 String relationE = rs.getString(RedListUtil.E);
124 String relationW = rs.getString(RedListUtil.W);
125 String relationK = rs.getString(RedListUtil.K);
126 String relationAW = rs.getString(RedListUtil.AW);
127 String relationAO = rs.getString(RedListUtil.AO);
128 String relationR = rs.getString(RedListUtil.R);
129 String relationO = rs.getString(RedListUtil.O);
130 String relationS = rs.getString(RedListUtil.S);
131
132 //---NAME---
133 BotanicalName name = importName(state, id, taxNameString, rangString, ep1String, ep2String, ep3String,
134 nomZusatzString, hybString, namesToSave);
135
136
137 //--- AUTHORS ---
138 importAuthors(state, rs, id, nomZusatzString, taxZusatzString, zusatzString, authorKombString,
139 authorBasiString, name);
140
141 //---TAXON---
142 TaxonBase taxonBase = importTaxon(id, taxNameString, gueltString, authorBasiString, hybString, name);
143 if(taxonBase==null){
144 RedListUtil.logMessage(id, "Taxon for name "+name+" could not be created.", logger);
145 return;
146 }
147
148 //---CONCEPT RELATIONSHIPS---
149 /*check if taxon/synonym also exists in other classification
150 * 1. create new taxon with the same name (in that classification)
151 * 2. create concept relationship between both
152 */
153 //checklist
154 if(CdmUtils.isNotBlank(clTaxonString) && !clTaxonString.trim().equals("-")){
155 cloneTaxon(taxonBase, name, TaxonRelationshipType.CONGRUENT_TO(), taxaToSave, id, RedListUtil.TAXON_CHECKLISTE_NAMESPACE, state);
156 }
157 //E, W, K, AW, AO, R, O, S
158 addConceptRelation(relationE, RedListUtil.CLASSIFICATION_NAMESPACE_E, taxonBase, name, taxaToSave, id, state);
159 addConceptRelation(relationW, RedListUtil.CLASSIFICATION_NAMESPACE_W, taxonBase, name, taxaToSave, id, state);
160 addConceptRelation(relationK, RedListUtil.CLASSIFICATION_NAMESPACE_K, taxonBase, name, taxaToSave, id, state);
161 addConceptRelation(relationAW, RedListUtil.CLASSIFICATION_NAMESPACE_AW, taxonBase, name, taxaToSave, id, state);
162 addConceptRelation(relationAO, RedListUtil.CLASSIFICATION_NAMESPACE_AO, taxonBase, name, taxaToSave, id, state);
163 addConceptRelation(relationR, RedListUtil.CLASSIFICATION_NAMESPACE_R, taxonBase, name, taxaToSave, id, state);
164 addConceptRelation(relationO, RedListUtil.CLASSIFICATION_NAMESPACE_O, taxonBase, name, taxaToSave, id, state);
165 addConceptRelation(relationS, RedListUtil.CLASSIFICATION_NAMESPACE_S, taxonBase, name, taxaToSave, id, state);
166
167 //NOTE: the source has to be added after cloning or otherwise the clone would also get the source
168 ImportHelper.setOriginalSource(taxonBase, state.getTransactionalSourceReference(), id, RedListUtil.TAXON_GESAMTLISTE_NAMESPACE);
169 taxaToSave.add(taxonBase);
170 }
171
172 private void addConceptRelation(String relationString, String classificationNamespace, TaxonBase taxonBase, TaxonNameBase name, Set<TaxonBase> taxaToSave, long id, RedListGefaesspflanzenImportState state){
173 if(CdmUtils.isNotBlank(relationString) && !relationString.equals(".")){
174 TaxonRelationshipType taxonRelationshipTypeByKey = new RedListGefaesspflanzenTransformer().getTaxonRelationshipTypeByKey(relationString);
175 if(taxonRelationshipTypeByKey==null){
176 RedListUtil.logMessage(id, "Could not interpret relationship "+relationString+" for taxon "+taxonBase, logger);
177 }
178 cloneTaxon(taxonBase, name, taxonRelationshipTypeByKey, taxaToSave, id, classificationNamespace, state);
179 }
180 }
181
182 /**
183 * <b>NOTE:</b> the {@link TaxonRelationshipType} passed as parameter is
184 * directed <b>from the clone</b> to the taxon
185 */
186 private void cloneTaxon(TaxonBase taxonBase, TaxonNameBase name, TaxonRelationshipType relationFromCloneToTaxon, Set<TaxonBase> taxaToSave, long id, String sourceNameSpace, RedListGefaesspflanzenImportState state){
187 TaxonBase clone = (TaxonBase) taxonBase.clone();
188 clone.setName(name);
189 if(taxonBase.isInstanceOf(Taxon.class)){
190 TaxonRelationship taxonRelation = ((Taxon) clone).addTaxonRelation((Taxon) taxonBase, relationFromCloneToTaxon, null, null);
191 taxonRelation.setDoubtful(true);//TODO Ist das mit " mit Fragezeichen" gemeint?
192 }
193 ImportHelper.setOriginalSource(clone, state.getTransactionalSourceReference(), id, sourceNameSpace);
194 taxaToSave.add(clone);
195 }
196
197 private TaxonBase importTaxon(long id, String taxNameString, String gueltString, String authorBasiString,
198 String hybString, BotanicalName name) {
199 TaxonBase taxonBase = null;
200 if(authorBasiString.trim().contains(RedListUtil.AUCT)){
201 taxonBase = Taxon.NewInstance(name, null);
202 taxonBase.setAppendedPhrase(RedListUtil.AUCT);
203 }
204 else if(gueltString.equals(RedListUtil.GUELT_ACCEPTED_TAXON)){
205 taxonBase = Taxon.NewInstance(name, null);
206 }
207 else if(gueltString.equals(RedListUtil.GUELT_SYNONYM) || gueltString.equals(RedListUtil.GUELT_BASIONYM)){
208 taxonBase = Synonym.NewInstance(name, null);
209 }
210 else{
211 return null;
212 }
213
214 //check taxon name consistency
215 checkTaxonNameConsistency(id, taxNameString, hybString, taxonBase);
216 return taxonBase;
217 }
218
219 private void importAuthors(RedListGefaesspflanzenImportState state, ResultSet rs, long id, String nomZusatzString,
220 String taxZusatzString, String zusatzString, String authorKombString, String authorBasiString,
221 BotanicalName name) throws SQLException {
222 //combination author
223 if(authorKombString.contains(RedListUtil.EX)){
224 //TODO: what happens with multiple ex authors??
225 String[] kombSplit = authorKombString.split(RedListUtil.EX);
226 if(kombSplit.length!=2){
227 RedListUtil.logMessage(id, "Multiple ex combination authors found", logger);
228 }
229 for (int i = 0; i < kombSplit.length; i++) {
230 if(i==0){
231 //first author is ex author
232 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
233 name.setExCombinationAuthorship(authorKomb);
234 }
235 else{
236 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, kombSplit[i]);
237 name.setCombinationAuthorship(authorKomb);
238 }
239 }
240 }
241 else if(authorKombString.trim().contains(RedListUtil.AUCT)){
242 RedListUtil.logMessage(id, "AUCT information in "+RedListUtil.AUTOR_KOMB+" column", logger);
243 }
244 else if(CdmUtils.isNotBlank(authorKombString)){
245 TeamOrPersonBase authorKomb = (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorKombString);
246 name.setCombinationAuthorship(authorKomb);
247 }
248 //basionym author
249 if(authorBasiString.contains(RedListUtil.EX)){
250 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
251 for (int i = 0; i < basiSplit.length; i++) {
252 if(basiSplit.length!=2){
253 RedListUtil.logMessage(id, "Multiple ex basionymn authors found", logger);
254 }
255 if(i==0){
256 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
257 if(CdmUtils.isBlank(authorKombString)){
258 name.setExCombinationAuthorship(authorBasi);
259 }
260 else{
261 name.setExBasionymAuthorship(authorBasi);
262 }
263 }
264 else{
265 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, basiSplit[i]);
266 if(CdmUtils.isBlank(authorKombString)){
267 name.setCombinationAuthorship(authorBasi);
268 }
269 else{
270 name.setBasionymAuthorship(authorBasi);
271 }
272 }
273 }
274 }
275 else if(CdmUtils.isNotBlank(authorBasiString)){
276 //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
277 TeamOrPersonBase authorBasi= (TeamOrPersonBase) state.getRelatedObject(RedListUtil.AUTHOR_NAMESPACE, authorBasiString);
278 if(CdmUtils.isBlank(authorKombString)){
279 name.setCombinationAuthorship(authorBasi);
280 }
281 else{
282 name.setBasionymAuthorship(authorBasi);
283 }
284 }
285
286 //check authorship consistency
287 String authorString = rs.getString(RedListUtil.AUTOR);
288 String authorshipCache = name.getAuthorshipCache();
289 checkAuthorShipConsistency(id, nomZusatzString, taxZusatzString, zusatzString, authorString, authorshipCache);
290 }
291
292 private BotanicalName importName(RedListGefaesspflanzenImportState state, long id, String taxNameString,
293 String rangString, String ep1String, String ep2String, String ep3String, String nomZusatzString,
294 String hybString, Set<TaxonNameBase> namesToSave) {
295 if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
296 RedListUtil.logMessage(id, "No name found!", logger);
297 }
298
299 Rank rank = makeRank(id, state, rangString);
300 BotanicalName name = BotanicalName.NewInstance(rank);
301
302 //ep1 should always be present
303 if(CdmUtils.isBlank(ep1String)){
304 RedListUtil.logMessage(id, RedListUtil.EPI1+" is empty!", logger);
305 }
306 name.setGenusOrUninomial(ep1String);
307 if(CdmUtils.isNotBlank(ep2String)){
308 name.setSpecificEpithet(ep2String);
309 }
310 if(CdmUtils.isNotBlank(ep3String)){
311 if(rank==Rank.SUBSPECIES() ||
312 rank==Rank.VARIETY()){
313 name.setInfraSpecificEpithet(ep3String);
314 }
315 }
316 //nomenclatural status
317 if(CdmUtils.isNotBlank(nomZusatzString)){
318 NomenclaturalStatusType status = makeNomenclaturalStatus(id, state, nomZusatzString);
319 if(status!=null){
320 name.addStatus(NomenclaturalStatus.NewInstance(status));
321 }
322 }
323 //hybrid
324 if(hybString.equals(RedListUtil.HYB_X)){
325 name.setBinomHybrid(true);
326 }
327 else if(hybString.equals(RedListUtil.HYB_XF)){
328 name.setTrinomHybrid(true);
329 }
330 //add source
331 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, RedListUtil.NAME_NAMESPACE);
332
333 namesToSave.add(name);
334 return name;
335 }
336
337 private void checkAuthorShipConsistency(long id, String nomZusatzString, String taxZusatzString,
338 String zusatzString, String authorString, String authorshipCache) {
339 if(CdmUtils.isNotBlank(zusatzString)){
340 authorString = authorString.replace(", "+zusatzString, "");
341 }
342 if(CdmUtils.isNotBlank(nomZusatzString)){
343 authorString = authorString.replace(", "+nomZusatzString, "");
344 }
345 if(CdmUtils.isNotBlank(taxZusatzString)){
346 authorString = authorString.replace(", "+taxZusatzString, "");
347 }
348 if(authorString.equals(RedListUtil.AUCT)){
349 authorString = "";
350 }
351 if(!authorString.equals(authorshipCache)){
352 RedListUtil.logMessage(id, "Authorship inconsistent! name.authorhshipCache <-> Column "+RedListUtil.AUTOR+": "+authorshipCache+" <-> "+authorString, logger);
353 }
354 }
355
356 private void checkTaxonNameConsistency(long id, String taxNameString, String hybString, TaxonBase taxonBase) {
357 String nameCache = ((BotanicalName)taxonBase.getName()).getNameCache().trim();
358
359 if(taxNameString.endsWith("agg.")){
360 taxNameString = taxNameString.replace("agg.", "aggr.");
361 }
362 if(hybString.equalsIgnoreCase(RedListUtil.HYB_X)){
363 taxNameString = taxNameString.replace("× ", "×");//hybrid sign has no space after it in titleCache for binomial hybrids
364 }
365 if(taxNameString.endsWith(Rank.SPECIESGROUP().toString())){
366 taxNameString.replaceAll(Rank.SPECIESGROUP().toString(), "- Gruppe");
367 if(!taxNameString.trim().equals(nameCache)){
368 taxNameString.replaceAll(Rank.SPECIESGROUP().toString(), "- group");
369 }
370 }
371 if(!taxNameString.trim().equals(nameCache)){
372 RedListUtil.logMessage(id, "Taxon name inconsistent! taxon.titleCache <-> Column "+RedListUtil.TAXNAME+": "+nameCache+" <-> "+taxNameString, logger);
373 }
374 }
375
376 private Rank makeRank(long id, RedListGefaesspflanzenImportState state, String rankStr) {
377 Rank rank = null;
378 try {
379 rank = state.getTransformer().getRankByKey(rankStr);
380 } catch (UndefinedTransformerMethodException e) {
381 e.printStackTrace();
382 }
383 if(rank==null){
384 RedListUtil.logMessage(id, rankStr+" could not be associated to a known rank.", logger);
385 }
386 return rank;
387 }
388
389 private NomenclaturalStatusType makeNomenclaturalStatus(long id, RedListGefaesspflanzenImportState state, String nomZusatzString) {
390 NomenclaturalStatusType status = null;
391 try {
392 status = state.getTransformer().getNomenclaturalStatusByKey(nomZusatzString);
393 } catch (UndefinedTransformerMethodException e) {
394 e.printStackTrace();
395 }
396 if(status==null){
397 RedListUtil.logMessage(id, nomZusatzString+" could not be associated to a known nomenclatural status.", logger);
398 }
399 return status;
400 }
401
402
403
404 @Override
405 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
406 RedListGefaesspflanzenImportState state) {
407 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
408 Map<String, AgentBase<?>> authorMap = new HashMap<String, AgentBase<?>>();
409
410 try {
411 while (rs.next()){
412 String authorKombString = rs.getString(RedListUtil.AUTOR_KOMB);
413
414 if(authorKombString.contains(RedListUtil.EX)){
415 String[] kombSplit = authorKombString.split(RedListUtil.EX);
416 for (int i = 0; i < kombSplit.length; i++) {
417 if(!authorMap.containsKey(kombSplit[i])){
418 authorMap.put(kombSplit[i], getAgentService().load(state.getAuthorMap().get(kombSplit[i])));
419 }
420 }
421 }
422 else if(CdmUtils.isNotBlank(authorKombString) && !authorMap.containsKey(authorKombString)){
423 authorMap.put(authorKombString, getAgentService().load(state.getAuthorMap().get(authorKombString)));
424 }
425
426 String authorBasiString = rs.getString(RedListUtil.AUTOR_BASI);
427 //basionym author
428 if(authorBasiString.contains(RedListUtil.EX)){
429 String[] basiSplit = authorBasiString.split(RedListUtil.EX);
430 for (int i = 0; i < basiSplit.length; i++) {
431 if(!authorMap.containsKey(basiSplit[i])){
432 authorMap.put(basiSplit[i], getAgentService().load(state.getAuthorMap().get(basiSplit[i])));
433 }
434 }
435 }
436 else if(CdmUtils.isNotBlank(authorBasiString) && !authorMap.containsKey(authorBasiString)){
437 authorMap.put(authorBasiString, getAgentService().load(state.getAuthorMap().get(authorBasiString)));
438 }
439 }
440 } catch (SQLException e) {
441 e.printStackTrace();
442 }
443 result.put(RedListUtil.AUTHOR_NAMESPACE, authorMap);
444
445 return result;
446 }
447
448 @Override
449 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
450 return false;
451 }
452
453 @Override
454 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
455 return false;
456 }
457
458 }