comments
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportClassification.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.Map;
16
17 import org.apache.log4j.Logger;
18 import org.springframework.stereotype.Component;
19
20 import eu.etaxonomy.cdm.io.common.DbImportBase;
21 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
22 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
23 import eu.etaxonomy.cdm.model.common.CdmBase;
24 import eu.etaxonomy.cdm.model.taxon.Classification;
25 import eu.etaxonomy.cdm.model.taxon.Synonym;
26 import eu.etaxonomy.cdm.model.taxon.Taxon;
27 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
28
29 /**
30 *
31 * @author pplitzner
32 * @date Mar 1, 2016
33 *
34 */
35
36 @Component
37 @SuppressWarnings("serial")
38 public class RedListGefaesspflanzenImportClassification extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
39
40 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportClassification.class);
41
42 private static final String tableName = "Rote Liste Gefäßpflanzen";
43
44 private static final String pluralString = "classifications";
45
46 public RedListGefaesspflanzenImportClassification() {
47 super(tableName, pluralString);
48 }
49
50 @Override
51 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
52 return "SELECT NAMNR "
53 + "FROM V_TAXATLAS_D20_EXPORT t "
54 + " ORDER BY NAMNR";
55 }
56
57 @Override
58 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
59 String result = " SELECT * "
60 + " FROM V_TAXATLAS_D20_EXPORT t "
61 + " WHERE t.NAMNR IN (@IDSET)";
62 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
63 return result;
64 }
65
66 @Override
67 protected void doInvoke(RedListGefaesspflanzenImportState state) {
68 makeClassification(state);
69 super.doInvoke(state);
70 }
71
72
73 @Override
74 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
75 ResultSet rs = partitioner.getResultSet();
76 Classification classification = getClassificationService().load(state.getConfig().getClassificationUuid());
77 try {
78 while (rs.next()){
79 makeSingleTaxonNode(state, rs, classification);
80
81 }
82 } catch (SQLException e) {
83 e.printStackTrace();
84 }
85
86 getClassificationService().saveOrUpdate(classification);
87 return true;
88 }
89
90 private void makeSingleTaxonNode(RedListGefaesspflanzenImportState state, ResultSet rs, Classification classification)
91 throws SQLException {
92 long id = rs.getLong("NAMNR");
93 long parentId = rs.getLong("LOWER");
94 String gueltString = rs.getString("GUELT");
95
96 TaxonBase taxonBase = getTaxonService().load(state.getTaxonMap().get(id));
97 Taxon parent = (Taxon) getTaxonService().load(state.getTaxonMap().get(parentId));
98
99 if(taxonBase.isInstanceOf(Taxon.class)){
100 classification.addParentChild(parent, (Taxon)taxonBase, null, null);
101 }
102 else if(taxonBase.isInstanceOf(Synonym.class)){
103 if(gueltString.equals("b")){
104 //TODO
105 }
106 else{
107 //TODO: how to correctly add a synonym?
108 parent.addHomotypicSynonym((Synonym) taxonBase, null, null);
109 }
110 }
111 }
112
113 @Override
114 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
115 RedListGefaesspflanzenImportState state) {
116 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
117 return result;
118 }
119
120 private void makeClassification(RedListGefaesspflanzenImportState state) {
121 Classification classification = Classification.NewInstance(state.getConfig().getClassificationName());
122 classification.setUuid(state.getConfig().getClassificationUuid());
123 getClassificationService().save(classification);
124 }
125
126 @Override
127 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
128 return false;
129 }
130
131 @Override
132 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
133 return false;
134 }
135
136 }