ref #10432 finishing Cora import
[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.logging.log4j.LogManager;
20 import org.apache.logging.log4j.Logger;
21 import org.springframework.stereotype.Component;
22
23 import eu.etaxonomy.cdm.io.common.ResultSetPartitioner;
24 import eu.etaxonomy.cdm.model.agent.Person;
25 import eu.etaxonomy.cdm.model.agent.Team;
26 import eu.etaxonomy.cdm.model.agent.TeamOrPersonBase;
27 import eu.etaxonomy.cdm.model.common.CdmBase;
28 import eu.etaxonomy.cdm.model.common.Language;
29 import eu.etaxonomy.cdm.model.common.VerbatimTimePeriod;
30 import eu.etaxonomy.cdm.model.description.CommonTaxonName;
31 import eu.etaxonomy.cdm.model.description.TaxonDescription;
32 import eu.etaxonomy.cdm.model.location.Country;
33 import eu.etaxonomy.cdm.model.location.NamedArea;
34 import eu.etaxonomy.cdm.model.reference.OriginalSourceType;
35 import eu.etaxonomy.cdm.model.reference.Reference;
36 import eu.etaxonomy.cdm.model.reference.ReferenceFactory;
37 import eu.etaxonomy.cdm.model.taxon.Taxon;
38 import eu.etaxonomy.cdm.model.taxon.TaxonBase;
39
40
41 /**
42 * @author a.mueller
43 * @since 20.02.2010
44 *
45 * OPEN ISSUES:
46 *
47 * ...
48 */
49 @Component
50 public class GlobisCommonNameImport extends GlobisImportBase<Taxon> {
51
52 private static final long serialVersionUID = 2462539699442149049L;
53 private static final Logger logger = LogManager.getLogger();
54
55 private int modCount = 10000;
56 private static final String pluralString = "common names";
57 private static final String dbTableName = "species_language";
58 private static final Class<?> cdmTargetClass = Taxon.class; //not needed
59
60 public GlobisCommonNameImport(){
61 super(pluralString, dbTableName, cdmTargetClass);
62 }
63
64 //dirty but acceptable for globis environment
65 private Map<Integer,Reference> refMap = new HashMap<Integer,Reference>();
66
67 @Override
68 protected String getIdQuery() {
69 String strRecordQuery =
70 " SELECT ID " +
71 " FROM " + dbTableName;
72 return strRecordQuery;
73 }
74
75
76 @Override
77 protected String getRecordQuery(GlobisImportConfigurator config) {
78 String strRecordQuery =
79 " SELECT * " +
80 " FROM " + getTableName() + " sl " +
81 " WHERE ( sl.ID IN (" + ID_LIST_TOKEN + ") )";
82 return strRecordQuery;
83 }
84
85 @Override
86 public boolean doPartition(@SuppressWarnings("rawtypes") ResultSetPartitioner partitioner, GlobisImportState state) {
87 boolean success = true;
88
89 @SuppressWarnings("rawtypes")
90 Set<TaxonBase> objectsToSave = new HashSet<>();
91
92 @SuppressWarnings("unchecked")
93 Map<String, Taxon> taxonMap = partitioner.getObjectMap(TAXON_NAMESPACE);
94
95 ResultSet rs = partitioner.getResultSet();
96
97 try {
98
99 int i = 0;
100
101 //for each common name
102 while (rs.next()){
103
104 if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));}
105
106 Integer idTaxon = nullSafeInt(rs,"IDCurrentSpec");
107
108 try {
109
110 //source ref
111 Reference sourceRef = state.getTransactionalSourceReference();
112
113 //common names
114 Integer id = nullSafeInt(rs,"ID");
115 String isoLang = rs.getString("ISO");
116 String strCommonName = rs.getString("commonname");
117 Integer refID = nullSafeInt(rs,"ReferenceID");
118 String strCountryCode = rs.getString("Code2");
119
120
121 Taxon taxon = taxonMap.get(String.valueOf(idTaxon));
122 if (taxon == null){
123 logger.warn("No taxon found for taxonId " + idTaxon);
124 }else if (isBlank(strCommonName)){
125 logger.warn("No common name string defined for common name ID: " + id);
126 }else{
127 Language language = getLanguage(isoLang);
128 if (language == null){
129 logger.warn("No language found for common name ID: " + id);
130 }
131 NamedArea area = Country.getCountryByIso3166A2(strCountryCode);
132 if (area == null){
133 logger.warn("No country found for common name ID: " + id);
134 }
135
136 TaxonDescription taxonDescription = getTaxonDescription(taxon, sourceRef, ! IMAGE_GALLERY, CREATE);
137 CommonTaxonName commonName = CommonTaxonName.NewInstance(strCommonName, language, area);
138 taxonDescription.addElement(commonName);
139
140 Reference ref = handleReference(state, refID);
141 if (ref == null && refID != null){
142 logger.warn("No reference found for common name ID: " + id);
143 }else{
144 commonName.addSource(OriginalSourceType.Import, String.valueOf(refID), "reference", sourceRef, null);
145 }
146
147 objectsToSave.add(taxon);
148 }
149
150 } catch (Exception e) {
151 logger.warn("Exception in current_species: IDcurrentspec " + idTaxon + ". " + e.getMessage());
152 e.printStackTrace();
153 }
154
155 }
156
157 logger.warn(pluralString + " to save: " + objectsToSave.size());
158 getTaxonService().save(objectsToSave);
159
160 return success;
161 } catch (SQLException e) {
162 logger.error("SQLException:" + e);
163 return false;
164 }
165 }
166
167 private Map<String,Language> languageMap = new HashMap<>();
168 private Language getLanguage(String isoLang) {
169 Language result = languageMap.get(isoLang);
170 if (result == null){
171
172 result = getTermService().getLanguageByIso(isoLang);
173 if (result == null){
174 logger.warn("No language found for iso code: " + isoLang);
175 }
176 }
177 return result;
178
179 }
180
181 private Reference handleReference(GlobisImportState state, Integer refId){
182 if (refId == null){
183 return null;
184 }
185 Reference result = refMap.get(refId);
186
187 if (result == null){
188 try {
189 String sql = "SELECT * FROM [references] WHERE ReferenceID = " + refId;
190 ResultSet rs = state.getConfig().getSource().getResultSet(sql);
191 rs.next();
192
193 String authors = rs.getString("Author(s)");
194 String title = rs.getString("Title");
195 String details = rs.getString("Details");
196 Integer year = nullSafeInt(rs, "year");
197 result = ReferenceFactory.newGeneric();
198 result.setTitleCache(details, true);
199 result.setTitle(title);
200 result.setDatePublished(VerbatimTimePeriod.NewVerbatimInstance(year));
201
202 TeamOrPersonBase<?> author;
203 String[] authorSplit = authors.split("&");
204 if (authorSplit.length > 1){
205 Team team = Team.NewInstance();
206 author = team;
207 for (String singleAuthor : authorSplit){
208 Person person = makeSingleAuthor(singleAuthor);
209 team.addTeamMember(person);
210 }
211 }else{
212 author = makeSingleAuthor(authors);
213 }
214
215 result.setAuthorship(author);
216 refMap.put(refId,result);
217 rs.close();
218 } catch (SQLException e) {
219 e.printStackTrace();
220 }
221
222
223 }
224
225 return result;
226 }
227
228 private Person makeSingleAuthor(String authors) {
229 Person result = Person.NewTitledInstance(authors);
230 return result;
231 }
232
233 @Override
234 public Map<Object, Map<String, ? extends CdmBase>> getRelatedObjectsForPartition(ResultSet rs, GlobisImportState state) {
235 String nameSpace;
236 Set<String> idSet;
237 Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<>();
238 try{
239 Set<String> taxonIdSet = new HashSet<>();
240
241 while (rs.next()){
242 handleForeignKey(rs, taxonIdSet, "IDCurrentSpec");
243 }
244
245 //taxon map
246 nameSpace = TAXON_NAMESPACE;
247 idSet = taxonIdSet;
248 Map<String, Taxon> objectMap = getCommonService().getSourcedObjectsByIdInSourceC(Taxon.class, idSet, nameSpace);
249 result.put(nameSpace, objectMap);
250
251
252 } catch (SQLException e) {
253 throw new RuntimeException(e);
254 }
255 return result;
256 }
257
258 @Override
259 protected boolean doCheck(GlobisImportState state){
260 // IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator();
261 return true;
262 }
263
264 @Override
265 protected boolean isIgnore(GlobisImportState state){
266 return ! state.getConfig().isDoCommonNames();
267 }
268 }