Refactor name-author mapping
[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.hibernate.HibernateProxyHelper;
24 import eu.etaxonomy.cdm.io.common.DbImportBase;
25 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
26 import eu.etaxonomy.cdm.io.common.ImportHelper;
27 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
28 import eu.etaxonomy.cdm.io.common.mapping.UndefinedTransformerMethodException;
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
35 /**
36 *
37 * @author pplitzner
38 * @date Mar 1, 2016
39 *
40 */
41
42 @Component
43 @SuppressWarnings("serial")
44 public class RedListGefaesspflanzenImportNames extends DbImportBase<RedListGefaesspflanzenImportState, RedListGefaesspflanzenImportConfigurator> {
45 private static final Logger logger = Logger.getLogger(RedListGefaesspflanzenImportNames.class);
46
47 private static final String tableName = "Rote Liste Gefäßpflanzen";
48
49 private static final String pluralString = "names";
50
51 private static final String NAME_NAMESPACE = "name";
52
53 public RedListGefaesspflanzenImportNames() {
54 super(tableName, pluralString);
55 }
56
57 @Override
58 protected String getIdQuery(RedListGefaesspflanzenImportState state) {
59 return "SELECT NAMNR "
60 + "FROM V_TAXATLAS_D20_EXPORT t "
61 + " ORDER BY NAMNR";
62 }
63
64 @Override
65 protected String getRecordQuery(RedListGefaesspflanzenImportConfigurator config) {
66 String result = " SELECT * "
67 + " FROM V_TAXATLAS_D20_EXPORT t "
68 + " WHERE t.NAMNR IN (@IDSET)";
69 result = result.replace("@IDSET", IPartitionedIO.ID_LIST_TOKEN);
70 return result;
71 }
72
73 @Override
74 protected void doInvoke(RedListGefaesspflanzenImportState state) {
75 super.doInvoke(state);
76 }
77
78
79 @Override
80 public boolean doPartition(ResultSetPartitioner partitioner, RedListGefaesspflanzenImportState state) {
81 ResultSet rs = partitioner.getResultSet();
82 Set<TaxonNameBase> namesToSave = new HashSet<TaxonNameBase>();
83 try {
84 while (rs.next()){
85 makeSingleName(state, rs, namesToSave);
86
87 }
88 } catch (SQLException e) {
89 e.printStackTrace();
90 }
91
92 getNameService().saveOrUpdate(namesToSave);
93 return true;
94 }
95
96 private void makeSingleName(RedListGefaesspflanzenImportState state, ResultSet rs, Set<TaxonNameBase> namesToSave)
97 throws SQLException {
98 long id = rs.getLong("NAMNR");
99 String taxNameString = rs.getString("TAXNAME");
100 String rangString = rs.getString("RANG");
101 String ep1String = rs.getString("EPI1");
102 String ep2String = rs.getString("EPI2");
103 String ep3String = rs.getString("EPI3");
104 String nomZusatzString = rs.getString("NOM_ZUSATZ");
105 String zusatzString = rs.getString("ZUSATZ");
106 String authorKombString = rs.getString("AUTOR_KOMB");
107 String authorBasiString = rs.getString("AUTOR_BASI");
108
109 if(CdmUtils.isBlank(taxNameString) && CdmUtils.isBlank(ep1String)){
110 logger.error("NAMNR: "+id+" No name found!");
111 }
112
113
114 Rank rank = makeRank(state, rangString);
115 BotanicalName name = BotanicalName.NewInstance(rank);
116
117 //ep1 should always be present
118 name.setGenusOrUninomial(ep1String);
119 if(rank==Rank.SPECIES()){
120 name.setGenusOrUninomial(ep1String);
121 }
122
123 //add author
124 TeamOrPersonBase authorKomb = HibernateProxyHelper.deproxy(getAgentService().load(state.getAuthorMap().get(authorKombString)), TeamOrPersonBase.class);
125 name.setCombinationAuthorship(authorKomb);
126
127 //id
128 ImportHelper.setOriginalSource(name, state.getTransactionalSourceReference(), id, NAME_NAMESPACE);
129
130 namesToSave.add(name);
131 }
132
133 private Rank makeRank(RedListGefaesspflanzenImportState state, String rankStr) {
134 Rank rank = null;
135 try {
136 rank = state.getTransformer().getRankByKey(rankStr);
137 } catch (UndefinedTransformerMethodException e) {
138 e.printStackTrace();
139 }
140 if(rank==null){
141 logger.error(rankStr+" could not be associated to a known rank.");
142 }
143 return rank;
144 }
145
146
147
148 @Override
149 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs,
150 RedListGefaesspflanzenImportState state) {
151 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
152 // Map<Long, AgentBase<?>> authorKombMap = new HashMap<>();
153 // Map<Long, AgentBase<?>> authorBasiMap = new HashMap<>();
154 //
155 // //load authors
156 // for(Entry<Long, UUID> entry:state.getAuthorKombMap().entrySet()){
157 // authorKombMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
158 // }
159 // for(Entry<Long, UUID> entry:state.getAuthorBasiMap().entrySet()){
160 // authorBasiMap.put(entry.getKey(), getAgentService().load(entry.getValue()));
161 // }
162 // try {
163 // while (rs.next()){
164 // long id = rs.getLong("NAMNR");
165 // }
166 // } catch (SQLException e) {
167 // e.printStackTrace();
168 // }
169 //
170 // //Authors
171 // Set<UUID> uuidSet = new HashSet<>();
172 // for (String authorStr : authorKombSet){
173 // UUID uuid = state.getAuthorUuid(authorStr);
174 // uuidSet.add(uuid);
175 // }
176 // List<TeamOrPersonBase<?>> authors = (List)getAgentService().find(uuidSet);
177 // Map<UUID, TeamOrPersonBase<?>> authorUuidMap = new HashMap<>();
178 // for (TeamOrPersonBase<?> author : authors){
179 // authorUuidMap.put(author.getUuid(), author);
180 // }
181 //
182 // for (String authorStr : authorKombSet){
183 // UUID uuid = state.getAuthorUuid(authorStr);
184 // TeamOrPersonBase<?> author = authorUuidMap.get(uuid);
185 // authorMap.put(authorStr, author);
186 // }
187 // result.put(AUTHOR_NAMESPACE, authorMap);
188 //
189 // //reference map
190 // String nameSpace = REFERENCE_NAMESPACE;
191 // Class<?> cdmClass = Reference.class;
192 // Set<String> idSet = referenceIdSet;
193 // Map<String, Reference<?>> referenceMap = (Map<String, Reference<?>>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
194 // result.put(nameSpace, referenceMap);
195 //
196 // //secundum
197 // UUID secUuid = state.getConfig().getSecUuid();
198 // Reference<?> secRef = getReferenceService().find(secUuid);
199 // referenceMap.put(secUuid.toString(), secRef);
200
201 return result;
202 }
203
204 @Override
205 protected boolean doCheck(RedListGefaesspflanzenImportState state) {
206 return false;
207 }
208
209 @Override
210 protected boolean isIgnore(RedListGefaesspflanzenImportState state) {
211 return false;
212 }
213
214 }