(no commit message)
[cdmlib-apps.git] / cdmlib-eflora / src / main / java / eu / etaxonomy / cdm / io / eflora / centralAfrica / ferns / CentralAfricaFernsImportBase.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.eflora.centralAfrica.ferns;
11
12 import java.lang.reflect.Method;
13 import java.sql.ResultSet;
14 import java.sql.ResultSetMetaData;
15 import java.sql.SQLException;
16 import java.sql.Timestamp;
17 import java.util.HashMap;
18 import java.util.HashSet;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.UUID;
22
23 import org.apache.commons.lang.StringUtils;
24 import org.apache.log4j.Logger;
25 import org.joda.time.DateTime;
26
27 import eu.etaxonomy.cdm.common.CdmUtils;
28 import eu.etaxonomy.cdm.io.common.CdmImportBase;
29 import eu.etaxonomy.cdm.io.common.ICdmIO;
30 import eu.etaxonomy.cdm.io.common.IPartitionedIO;
31 import eu.etaxonomy.cdm.io.common.ImportHelper;
32 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
33 import eu.etaxonomy.cdm.io.common.Source;
34 import eu.etaxonomy.cdm.io.common.IImportConfigurator.EDITOR;
35 import eu.etaxonomy.cdm.io.common.mapping.DbImportMapping;
36 import eu.etaxonomy.cdm.model.agent.Team;
37 import eu.etaxonomy.cdm.model.common.AnnotatableEntity;
38 import eu.etaxonomy.cdm.model.common.Annotation;
39 import eu.etaxonomy.cdm.model.common.AnnotationType;
40 import eu.etaxonomy.cdm.model.common.CdmBase;
41 import eu.etaxonomy.cdm.model.common.ExtensionType;
42 import eu.etaxonomy.cdm.model.common.IdentifiableEntity;
43 import eu.etaxonomy.cdm.model.common.Language;
44 import eu.etaxonomy.cdm.model.common.MarkerType;
45 import eu.etaxonomy.cdm.model.common.User;
46 import eu.etaxonomy.cdm.model.name.BotanicalName;
47 import eu.etaxonomy.cdm.model.name.Rank;
48 import eu.etaxonomy.cdm.strategy.parser.NonViralNameParserImpl;
49
50 /**
51 * @author a.mueller
52 * @created 20.03.2008
53 * @version 1.0
54 */
55 public abstract class CentralAfricaFernsImportBase<CDM_BASE extends CdmBase> extends CdmImportBase<CentralAfricaFernsImportConfigurator, CentralAfricaFernsImportState> implements ICdmIO<CentralAfricaFernsImportState>, IPartitionedIO<CentralAfricaFernsImportState> {
56 private static final Logger logger = Logger.getLogger(CentralAfricaFernsImportBase.class);
57
58 public static final UUID ID_IN_SOURCE_EXT_UUID = UUID.fromString("23dac094-e793-40a4-bad9-649fc4fcfd44");
59
60 protected static final String TAXON_NAMESPACE = "African_pteridophytes_Taxon";
61 protected static final String NAME_NAMESPACE = "African_pteridophytes_Name";
62 protected static final String HIGHER_TAXON_NAMESPACE = "African_pteridophytes_Higher_Taxon";
63
64 private NonViralNameParserImpl parser = NonViralNameParserImpl.NewInstance();
65
66
67 private String pluralString;
68 private String dbTableName;
69 //TODO needed?
70 private Class cdmTargetClass;
71
72
73
74
75 /**
76 * @param dbTableName
77 * @param dbTableName2
78 */
79 public CentralAfricaFernsImportBase(String pluralString, String dbTableName, Class cdmTargetClass) {
80 this.pluralString = pluralString;
81 this.dbTableName = dbTableName;
82 this.cdmTargetClass = cdmTargetClass;
83 }
84
85 protected void doInvoke(CentralAfricaFernsImportState state){
86 logger.info("start make " + getPluralString() + " ...");
87 CentralAfricaFernsImportConfigurator config = state.getConfig();
88 Source source = config.getSource();
89
90 String strIdQuery = getIdQuery();
91 String strRecordQuery = getRecordQuery(config);
92
93 int recordsPerTransaction = config.getRecordsPerTransaction();
94 try{
95 ResultSetPartitioner partitioner = ResultSetPartitioner.NewInstance(source, strIdQuery, strRecordQuery, recordsPerTransaction);
96 while (partitioner.nextPartition()){
97 partitioner.doPartition(this, state);
98 }
99 } catch (SQLException e) {
100 logger.error("SQLException:" + e);
101 state.setUnsuccessfull();
102 return;
103 }
104
105 logger.info("end make " + getPluralString() + " ... " + getSuccessString(true));
106 return;
107 }
108
109 public boolean doPartition(ResultSetPartitioner partitioner, CentralAfricaFernsImportState state) {
110 boolean success = true ;
111 Set objectsToSave = new HashSet();
112
113 DbImportMapping<?, ?> mapping = getMapping();
114 mapping.initialize(state, cdmTargetClass);
115
116 ResultSet rs = partitioner.getResultSet();
117 try{
118 while (rs.next()){
119 success &= mapping.invoke(rs,objectsToSave);
120 }
121 } catch (SQLException e) {
122 logger.error("SQLException:" + e);
123 return false;
124 }
125
126 partitioner.startDoSave();
127 getCommonService().save(objectsToSave);
128 return success;
129 }
130
131
132
133 /**
134 * @return
135 */
136 protected abstract DbImportMapping<?, ?> getMapping();
137
138 /**
139 * @return
140 */
141 protected abstract String getRecordQuery(CentralAfricaFernsImportConfigurator config);
142
143 /**
144 * @return
145 */
146 protected String getIdQuery(){
147 String result = " SELECT id FROM " + getTableName();
148 return result;
149 }
150
151 /* (non-Javadoc)
152 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getPluralString()
153 */
154 public String getPluralString(){
155 return pluralString;
156 }
157
158 /**
159 * @return
160 */
161 protected String getTableName(){
162 return this.dbTableName;
163 }
164
165
166 /**
167 * Reads a foreign key field from the result set and adds its value to the idSet.
168 * @param rs
169 * @param teamIdSet
170 * @throws SQLException
171 */
172 protected void handleForeignKey(ResultSet rs, Set<String> idSet, String attributeName)
173 throws SQLException {
174 Object idObj = rs.getObject(attributeName);
175 if (idObj != null){
176 String id = String.valueOf(idObj);
177 idSet.add(id);
178 }
179 }
180
181 /**
182 * Returns true if i is a multiple of recordsPerTransaction
183 * @param i
184 * @param recordsPerTransaction
185 * @return
186 */
187 protected boolean loopNeedsHandling(int i, int recordsPerLoop) {
188 startTransaction();
189 return (i % recordsPerLoop) == 0;
190 }
191
192 protected void doLogPerLoop(int count, int recordsPerLog, String pluralString){
193 if ((count % recordsPerLog ) == 0 && count!= 0 ){ logger.info(pluralString + " handled: " + (count));}
194 }
195
196
197
198 protected void setAuthor(BotanicalName taxonName, ResultSet rs, String taxonNumber, boolean isHigherTaxon) throws SQLException {
199
200 String authorsFull = null;
201 String authorsAbbrev = null;
202 if (! isHigherTaxon){
203 authorsFull = rs.getString("Author/s - full");
204 authorsAbbrev = rs.getString("Author/s - abbreviated");
205 }
206
207 Rank rank = taxonName.getRank();
208 String authorString = null;
209 if (rank != null){
210 if (rank.equals(Rank.ORDER())){
211 authorString = rs.getString("Order name author");
212 }else if (rank.equals(Rank.SUBORDER())){
213 authorString = rs.getString("Suborder name author");
214 }else if (rank.equals(Rank.FAMILY())){
215 authorString = rs.getString("Family name author");
216 }else if (rank.equals(Rank.SUBFAMILY())){
217 authorString = rs.getString("Subfamily name author");
218 }else if (rank.equals(Rank.TRIBE())){
219 authorString = rs.getString("Tribus author");
220 }else if (rank.equals(Rank.SUBTRIBE())){
221 authorString = rs.getString("Subtribus author");
222 }else if (rank.equals(Rank.SECTION_BOTANY())){
223 authorString = rs.getString("Section name author");
224 }else if (rank.equals(Rank.SUBSECTION_BOTANY())){
225 authorString = rs.getString("Subsection author");
226 }else if (rank.equals(Rank.GENUS())){
227 authorString = rs.getString("Genus name author");
228 }else if (rank.equals(Rank.SUBGENUS())){
229 authorString = rs.getString("Subgenus name author");
230 }else if (rank.equals(Rank.SERIES())){
231 authorString = rs.getString("Series name author");
232 }else if (rank.equals(Rank.SPECIES())){
233 authorString = rs.getString("Specific epithet author");
234 }else if (rank.equals(Rank.SUBSPECIES())){
235 authorString = rs.getString("Subspecies author");
236 }else if (rank.equals(Rank.VARIETY())){
237 authorString = rs.getString("Variety name author");
238 }else if (rank.equals(Rank.SUBVARIETY())){
239 authorString = rs.getString("Subvariety author");
240 }else if (rank.equals(Rank.FORM())){
241 authorString = rs.getString("Forma name author");
242 }else if (rank.equals(Rank.SUBFORM())){
243 authorString = rs.getString("Subforma author");
244 }else{
245 logger.warn("Author string could not be defined");
246 if (! isHigherTaxon){
247 authorString = authorsAbbrev;
248 if (StringUtils.isBlank(authorString)){
249 logger.warn("Authors abbrev string could not be defined");
250 authorString = authorsFull;
251 }
252 }
253 }
254 }else{
255 logger.warn(taxonNumber + ": Rank is null");
256 authorString = authorsAbbrev;
257 if (StringUtils.isBlank(authorString)){
258 logger.warn(taxonNumber + ": Authors abbrev string could not be defined");
259 authorString = authorsFull;
260 }
261 }
262
263 if (StringUtils.isNotBlank(authorString)){
264 parser.handleAuthors(taxonName, taxonName.getNameCache().trim() + " " + authorString, authorString);
265 }
266 if (! isHigherTaxon){
267 String combinationAuthor = taxonName.getCombinationAuthorTeam()==null ? "" :taxonName.getCombinationAuthorTeam().getNomenclaturalTitle();
268 if (StringUtils.isNotBlank(authorsAbbrev) && ! authorsAbbrev.equalsIgnoreCase(combinationAuthor)){
269 //it is expected that the fullAuthor and the abbrevAuthor are the combination authors but very often it is not
270 logger.warn(taxonNumber + ": Rank author and abbrev author are not equal: " + authorString + "\t<-> " + combinationAuthor + "\t<-> " + authorsAbbrev);
271 }
272 // if (StringUtils.isNotBlank(authorsFull) && ! authorsFull.equalsIgnoreCase(authorString)){
273 // logger.warn("Rank author and full author are not equal Rankauthor: " + authorString + ", full author " + authorsFull);
274 // }
275 }
276 }
277
278
279
280
281
282 }