Refactor name-author mapping
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / redlist / gefaesspflanzen / RedListGefaesspflanzenImportTaxa.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.io.common.DbImportBase;
23 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
24 import eu.etaxonomy.cdm.io.common.ImportHelper;
25 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.name.NonViralName;
29 import eu.etaxonomy.cdm.model.name.Rank;
30 import eu.etaxonomy.cdm.model.taxon.Taxon;
31 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
32 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
33
34 /**
35 *
36 * @author pplitzner
37 * @date Mar 1, 2016
38 *
39 */
40
41 @Component
42 @SuppressWarnings("serial")
43 public class RedListGefaesspflanzenImportTaxa extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
44 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportTaxa.class);
45
46 private static final String tableName = "Rote Liste Gefäßpflanzen";
47
48 private static final String pluralString = "taxa";
49
50 private static final String TAXON_NAMESPACE = "taxon";
51
52 public RedListGefaesspflanzenImportTaxa() {
53 super(tableName, pluralString);
54 }
55
56 @Override
57 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
58 return "SELECT NAMNR "
59 + "FROM V_TAXATLAS_D20_EXPORT t "
60 + " ORDER BY NAMNR";
61 }
62
63 @Override
64 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
65 String result = " SELECT * "
66 + " FROM V_TAXATLAS_D20_EXPORT t "
67 + " WHERE t.NAMNR IN (@IDSET)";
68 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
69 return result;
70 }
71
72 @Override
73 protected void doInvoke(RedListGefaesspflanzenImportState state) {
74 super.doInvoke(state);
75 }
76
77
78 @Override
79 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
80 ResultSet rs = partitioner.getResultSet();
81 Set<TaxonBase> taxaToSave = new HashSet<>();
82 try {
83 while (rs.next()){
84 makeSingleTaxon(state, rs, taxaToSave);
85
86 }
87 } catch (SQLException e) {
88 e.printStackTrace();
89 }
90
91 getTaxonService().saveOrUpdate(taxaToSave);
92 return true;
93 }
94
95 private void makeSingleTaxon(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonBase> taxaToSave)
96 throws SQLException {
97 long id = rs.getLong("NAMNR");
98 String taxonNameString = rs.getString("TAXNAME");
99
100 NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
101 NonViralName<?> name = parser.parseFullName(taxonNameString);
102 TaxonBase taxon = Taxon.NewInstance(name, null);
103
104 taxaToSave.add(taxon);
105
106 //id
107 ImportHelper.setOriginalSource(taxon, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
108 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, TAXON_NAMESPACE);
109 }
110
111 private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
112 Rank rank = null;
113 try {
114 rank = state.getTransformer().getRankByKey(rankStr);
115 } catch (UndefinedTransformerMethodException e) {
116 e.printStackTrace();
117 }
118 return rank;
119 }
120
121
122
123 @Override
124 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
125 RedListGefaesspflanzenImportState state) {
126 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
127 // Map<String, TeamOrPersonBase<?>> authorMap = new HashMap<>();
128 // Set<String> authorKombSet = new HashSet<>();
129 // Set<String> referenceIdSet = new HashSet<String>();
130 //
131 // try {
132 // while (rs.next()){
133 // String authorStr = rs.getString("tax_author_name");
134 // authorKombSet.add(authorStr);
135 // handleForeignKey(rs, referenceIdSet, "tax_document");
136 // }
137 // } catch (SQLException e) {
138 // e.printStackTrace();
139 // }
140 //
141 // //Authors
142 // Set<UUID> uuidSet = new HashSet<>();
143 // for (String authorStr : authorKombSet){
144 // UUID uuid = state.getAuthorUuid(authorStr);
145 // uuidSet.add(uuid);
146 // }
147 // List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
148 // Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
149 // for (TeamOrPersonBase<?> author : authors){
150 // authorUuidMap.put(author.getUuid(), author);
151 // }
152 //
153 // for (String authorStr : authorKombSet){
154 // UUID uuid = state.getAuthorUuid(authorStr);
155 // TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
156 // authorMap.put(authorStr, author);
157 // }
158 // result.put(AUTHOR_NAMESPACE, authorMap);
159 //
160 // //reference map
161 // String nameSpace = REFERENCE_NAMESPACE;
162 // Class<?> cdmClass = Reference.class;
163 // Set<String> idSet = referenceIdSet;
164 // Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
165 // result.put(nameSpace, referenceMap);
166 //
167 // //secundum
168 // UUID secUuid = state.getConfig().getSecUuid();
169 // Reference<?> secRef = getReferenceService().find(secUuid);
170 // referenceMap.put(secUuid.toString(), secRef);
171
172 return result;
173 }
174
175 @Override
176 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
177 return false;
178 }
179
180 @Override
181 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
182 return false;
183 }
184
185 }