minor
[cdmlib-apps.git] / app-import / src / main / java / eu / etaxonomy / cdm / io / globis / GlobisCommonNameImport.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.globis;
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.commons.lang.StringUtils;
20 import org.apache.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.common.CdmUtils;
24 import eu.etaxonomy.cdm.io.common.IOValidator;
25 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
26 import eu.etaxonomy.cdm.io.globis.validation.GlobisCurrentSpeciesImportValidator;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.Language;
29 import eu.etaxonomy.cdm.model.description.Distribution;
30 import eu.etaxonomy.cdm.model.description.PresenceTerm;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.location.WaterbodyOrCountry;
33 import eu.etaxonomy.cdm.model.name.Rank;
34 import eu.etaxonomy.cdm.model.name.ZoologicalName;
35 import eu.etaxonomy.cdm.model.reference.Reference;
36 import eu.etaxonomy.cdm.model.taxon.Classification;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39 import eu.etaxonomy.cdm.model.taxon.TaxonNode;
40
41
42 /**
43 * @author a.mueller
44 * @created 20.02.2010
45 * @version 1.0
46 */
47 @Component
48 public class GlobisCommonNameImport extends GlobisImportBase<Taxon> {
49 private static final Logger logger = Logger.getLogger(GlobisCommonNameImport.class);
50
51 private int modCount = 10000;
52 private static final String pluralString = "common names";
53 private static final String dbTableName = "species_language";
54 private static final Class cdmTargetClass = Taxon.class; //not needed
55
56 public GlobisCommonNameImport(){
57 super(pluralString, dbTableName, cdmTargetClass);
58 }
59
60
61
62
63 /* (non-Javadoc)
64 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#getIdQuery()
65 */
66 @Override
67 protected String getIdQuery() {
68 String strRecordQuery =
69 " SELECT ID " +
70 " FROM " + dbTableName;
71 return strRecordQuery;
72 }
73
74
75
76
77 /* (non-Javadoc)
78 * @see eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportBase#getRecordQuery(eu.etaxonomy.cdm.io.berlinModel.in.BerlinModelImportConfigurator)
79 */
80 @Override
81 protected String getRecordQuery(GlobisImportConfigurator config) {
82 String strRecordQuery =
83 " SELECT * " +
84 " FROM " + getTableName() + " sl " +
85 " WHERE ( sl.ID IN (" + ID_LIST_TOKEN + ") )";
86 return strRecordQuery;
87 }
88
89
90
91 /* (non-Javadoc)
92 * @see eu.etaxonomy.cdm.io.globis.GlobisImportBase#doPartition(eu.etaxonomy.cdm.io.common.ResultSetPartitioner, eu.etaxonomy.cdm.io.globis.GlobisImportState)
93 */
94 @Override
95 public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) {
96 boolean success = true;
97
98 Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
99
100 Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
101 // Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE);
102
103 ResultSet rs = partitioner.getResultSet();
104
105 try {
106
107 int i = 0;
108
109 //for each common name
110 while (rs.next()){
111
112 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
113
114 Integer taxonId = rs.getInt("IdCrrentSpec");
115
116
117 try {
118
119 //source ref
120 Reference<?> sourceRef = state.getTransactionalSourceReference();
121
122 //species
123 Taxon species = createObject(rs, state);
124
125
126 handleCountries(state, rs, species);
127
128 this.doIdCreatedUpdatedNotes(state, species, rs, taxonId, TAXON_NAMESPACE);
129
130 objectsToSave.add(species);
131
132
133 } catch (Exception e) {
134 logger.warn("Exception in current_species: IDcurrentspec " + taxonId + ". " + e.getMessage());
135 // e.printStackTrace();
136 }
137
138 }
139
140 // logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions );
141
142 logger.warn(pluralString + " to save: " + objectsToSave.size());
143 getTaxonService().save(objectsToSave);
144
145 return success;
146 } catch (SQLException e) {
147 logger.error("SQLException:" + e);
148 return false;
149 }
150 }
151
152 private void handleCountries(GlobisImportState state, ResultSet rs, Taxon species) throws SQLException {
153 String countriesStr = rs.getString("dtSpcCountries");
154 if (isBlank(countriesStr)){
155 return;
156 }
157 String[] countriesSplit = countriesStr.split(";");
158 for (String countryStr : countriesSplit){
159 if (isBlank(countryStr)){
160 continue;
161 }
162 countryStr = countryStr.trim();
163
164 //TODO use isComplete
165 boolean isComplete = countryStr.endsWith(".");
166 if (isComplete){
167 countryStr = countryStr.substring(0,countryStr.length() - 1).trim();
168 }
169 boolean isDoubtful = countryStr.endsWith("[?]");
170 if (isDoubtful){
171 countryStr = countryStr.substring(0,countryStr.length() - 3).trim();
172 }
173 if (countryStr.startsWith("?")){
174 isDoubtful = true;
175 countryStr = countryStr.substring(1).trim();
176 }
177
178
179
180 countryStr = normalizeCountry(countryStr);
181
182 WaterbodyOrCountry country = getCountry(state, countryStr);
183
184 PresenceTerm status;
185 if (isDoubtful){
186 status = PresenceTerm.PRESENT_DOUBTFULLY();
187 }else{
188 status = PresenceTerm.PRESENT();
189 }
190
191 if (country != null){
192 TaxonDescription desc = getTaxonDescription(species, state.getTransactionalSourceReference(), false, true);
193 Distribution distribution = Distribution.NewInstance(country, status);
194 desc.addElement(distribution);
195 }else{
196 logger.warn("Country string not recognized: " + countryStr);
197 }
198 }
199 }
200
201
202
203 /**
204 * @param countryStr
205 * @return
206 */
207 private String normalizeCountry(String countryStr) {
208 String result = countryStr.trim();
209 if (result.endsWith(".")){
210 result = result.substring(0,result.length() - 1);
211 }
212 return result;
213 }
214
215
216 /* (non-Javadoc)
217 * @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase)
218 */
219 public Taxon createObject(ResultSet rs, GlobisImportState state)
220 throws SQLException {
221 String speciesEpi = rs.getString("dtSpcSpcakt");
222 String subGenusEpi = rs.getString("dtSpcSubgenakt");
223 String genusEpi = rs.getString("dtSpcGenusakt");
224 String author = rs.getString("dtSpcAutor");
225
226
227 ZoologicalName zooName = ZoologicalName.NewInstance(Rank.SPECIES());
228 zooName.setSpecificEpithet(speciesEpi);
229 if (StringUtils.isNotBlank(subGenusEpi)){
230 zooName.setInfraGenericEpithet(subGenusEpi);
231 }
232 zooName.setGenusOrUninomial(genusEpi);
233 handleAuthorAndYear(author, zooName);
234
235 Taxon taxon = Taxon.NewInstance(zooName, state.getTransactionalSourceReference());
236
237 return taxon;
238 }
239
240
241
242
243
244 /* (non-Javadoc)
245 * @see eu.etaxonomy.cdm.io.berlinModel.in.IPartitionedIO#getRelatedObjectsForPartition(java.sql.ResultSet)
246 */
247 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs) {
248 String nameSpace;
249 Class cdmClass;
250 Set<String> idSet;
251 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>();
252 try{
253 Set<String> taxonIdSet = new HashSet<String>();
254
255 while (rs.next()){
256 // handleForeignKey(rs, taxonIdSet, "taxonId");
257 }
258
259 //taxon map
260 nameSpace = TAXON_NAMESPACE;
261 cdmClass = Taxon.class;
262 idSet = taxonIdSet;
263 Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace);
264 result.put(nameSpace, objectMap);
265
266
267 } catch (SQLException e) {
268 throw new RuntimeException(e);
269 }
270 return result;
271 }
272
273 /* (non-Javadoc)
274 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#doCheck(eu.etaxonomy.cdm.io.common.IImportConfigurator)
275 */
276 @Override
277 protected boolean doCheck(GlobisImportState state){
278 IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
279 return validator.validate(state);
280 }
281
282
283 /* (non-Javadoc)
284 * @see eu.etaxonomy.cdm.io.common.CdmIoBase#isIgnore(eu.etaxonomy.cdm.io.common.IImportConfigurator)
285 */
286 protected boolean isIgnore(GlobisImportState state){
287 return ! state.getConfig().isDoCurrentTaxa();
288 }
289
290
291
292
293
294 }