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