Revision 8422c0cd
Added by Andreas Müller almost 8 years ago
app-import/src/main/java/eu/etaxonomy/cdm/io/globis/GlobisCommonNameImport.java | ||
---|---|---|
1 | 1 |
/** |
2 | 2 |
* Copyright (C) 2007 EDIT |
3 |
* European Distributed Institute of Taxonomy
|
|
3 |
* European Distributed Institute of Taxonomy |
|
4 | 4 |
* http://www.e-taxonomy.eu |
5 |
*
|
|
5 |
* |
|
6 | 6 |
* The contents of this file are subject to the Mozilla Public License Version 1.1 |
7 | 7 |
* See LICENSE.TXT at the top of this package for the full license terms. |
8 | 8 |
*/ |
... | ... | |
29 | 29 |
import eu.etaxonomy.cdm.model.common.TimePeriod; |
30 | 30 |
import eu.etaxonomy.cdm.model.description.CommonTaxonName; |
31 | 31 |
import eu.etaxonomy.cdm.model.description.TaxonDescription; |
32 |
import eu.etaxonomy.cdm.model.location.NamedArea; |
|
33 | 32 |
import eu.etaxonomy.cdm.model.location.Country; |
33 |
import eu.etaxonomy.cdm.model.location.NamedArea; |
|
34 | 34 |
import eu.etaxonomy.cdm.model.reference.Reference; |
35 | 35 |
import eu.etaxonomy.cdm.model.reference.ReferenceFactory; |
36 | 36 |
import eu.etaxonomy.cdm.model.taxon.Taxon; |
... | ... | |
40 | 40 |
/** |
41 | 41 |
* @author a.mueller |
42 | 42 |
* @created 20.02.2010 |
43 |
*
|
|
43 |
* |
|
44 | 44 |
* OPEN ISSUES: |
45 |
*
|
|
45 |
* |
|
46 | 46 |
* ... |
47 | 47 |
*/ |
48 | 48 |
@Component |
49 | 49 |
public class GlobisCommonNameImport extends GlobisImportBase<Taxon> { |
50 | 50 |
private static final Logger logger = Logger.getLogger(GlobisCommonNameImport.class); |
51 |
|
|
51 |
|
|
52 | 52 |
private int modCount = 10000; |
53 | 53 |
private static final String pluralString = "common names"; |
54 | 54 |
private static final String dbTableName = "species_language"; |
55 | 55 |
private static final Class<?> cdmTargetClass = Taxon.class; //not needed |
56 |
|
|
56 |
|
|
57 | 57 |
public GlobisCommonNameImport(){ |
58 | 58 |
super(pluralString, dbTableName, cdmTargetClass); |
59 | 59 |
} |
60 | 60 |
|
61 | 61 |
//dirty but acceptable for globis environment |
62 | 62 |
private Map<Integer,Reference> refMap = new HashMap<Integer,Reference>(); |
63 |
|
|
63 |
|
|
64 | 64 |
@Override |
65 | 65 |
protected String getIdQuery() { |
66 |
String strRecordQuery =
|
|
67 |
" SELECT ID " +
|
|
68 |
" FROM " + dbTableName;
|
|
69 |
return strRecordQuery;
|
|
66 |
String strRecordQuery = |
|
67 |
" SELECT ID " + |
|
68 |
" FROM " + dbTableName; |
|
69 |
return strRecordQuery; |
|
70 | 70 |
} |
71 | 71 |
|
72 | 72 |
|
73 | 73 |
@Override |
74 | 74 |
protected String getRecordQuery(GlobisImportConfigurator config) { |
75 |
String strRecordQuery =
|
|
76 |
" SELECT * " +
|
|
75 |
String strRecordQuery = |
|
76 |
" SELECT * " + |
|
77 | 77 |
" FROM " + getTableName() + " sl " + |
78 | 78 |
" WHERE ( sl.ID IN (" + ID_LIST_TOKEN + ") )"; |
79 | 79 |
return strRecordQuery; |
80 | 80 |
} |
81 |
|
|
81 |
|
|
82 | 82 |
@Override |
83 | 83 |
public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) { |
84 | 84 |
boolean success = true; |
85 |
|
|
85 |
|
|
86 | 86 |
Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>(); |
87 |
|
|
88 |
Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
|
|
89 |
|
|
87 |
|
|
88 |
Map<String, Taxon> taxonMap = partitioner.getObjectMap(TAXON_NAMESPACE); |
|
89 |
|
|
90 | 90 |
ResultSet rs = partitioner.getResultSet(); |
91 | 91 |
|
92 | 92 |
try { |
93 |
|
|
93 |
|
|
94 | 94 |
int i = 0; |
95 | 95 |
|
96 | 96 |
//for each common name |
97 | 97 |
while (rs.next()){ |
98 |
|
|
98 |
|
|
99 | 99 |
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));} |
100 |
|
|
100 |
|
|
101 | 101 |
Integer idTaxon = nullSafeInt(rs,"IDCurrentSpec"); |
102 |
|
|
102 |
|
|
103 | 103 |
try { |
104 |
|
|
104 |
|
|
105 | 105 |
//source ref |
106 |
Reference<?> sourceRef = state.getTransactionalSourceReference();
|
|
107 |
|
|
106 |
Reference sourceRef = state.getTransactionalSourceReference(); |
|
107 |
|
|
108 | 108 |
//common names |
109 | 109 |
Integer id = nullSafeInt(rs,"ID"); |
110 | 110 |
String isoLang = rs.getString("ISO"); |
111 | 111 |
String strCommonName = rs.getString("commonname"); |
112 | 112 |
Integer refID = nullSafeInt(rs,"ReferenceID"); |
113 | 113 |
String strCountryCode = rs.getString("Code2"); |
114 |
|
|
115 |
|
|
114 |
|
|
115 |
|
|
116 | 116 |
Taxon taxon = taxonMap.get(String.valueOf(idTaxon)); |
117 | 117 |
if (taxon == null){ |
118 | 118 |
logger.warn("No taxon found for taxonId " + idTaxon); |
... | ... | |
131 | 131 |
TaxonDescription taxonDescription = getTaxonDescription(taxon, sourceRef, ! IMAGE_GALLERY, CREATE); |
132 | 132 |
CommonTaxonName commonName = CommonTaxonName.NewInstance(strCommonName, language, area); |
133 | 133 |
taxonDescription.addElement(commonName); |
134 |
|
|
135 |
Reference<?> ref = handleReference(state, refID);
|
|
134 |
|
|
135 |
Reference ref = handleReference(state, refID); |
|
136 | 136 |
if (ref == null && refID != null){ |
137 | 137 |
logger.warn("No reference found for common name ID: " + id); |
138 | 138 |
}else{ |
139 | 139 |
commonName.addSource(OriginalSourceType.Import, String.valueOf(refID), "reference", sourceRef, null); |
140 | 140 |
} |
141 |
|
|
142 |
objectsToSave.add(taxon);
|
|
141 |
|
|
142 |
objectsToSave.add(taxon); |
|
143 | 143 |
} |
144 | 144 |
|
145 | 145 |
} catch (Exception e) { |
146 | 146 |
logger.warn("Exception in current_species: IDcurrentspec " + idTaxon + ". " + e.getMessage()); |
147 | 147 |
e.printStackTrace(); |
148 |
}
|
|
149 |
|
|
148 |
} |
|
149 |
|
|
150 | 150 |
} |
151 |
|
|
151 |
|
|
152 | 152 |
logger.warn(pluralString + " to save: " + objectsToSave.size()); |
153 |
getTaxonService().save(objectsToSave);
|
|
154 |
|
|
153 |
getTaxonService().save(objectsToSave); |
|
154 |
|
|
155 | 155 |
return success; |
156 | 156 |
} catch (SQLException e) { |
157 | 157 |
logger.error("SQLException:" + e); |
... | ... | |
159 | 159 |
} |
160 | 160 |
} |
161 | 161 |
|
162 |
|
|
162 |
|
|
163 | 163 |
private Map<String,Language> languageMap = new HashMap<String,Language>(); |
164 | 164 |
private Language getLanguage(String isoLang) { |
165 | 165 |
Language result = languageMap.get(isoLang); |
166 | 166 |
if (result == null){ |
167 |
|
|
167 |
|
|
168 | 168 |
result = getTermService().getLanguageByIso(isoLang); |
169 | 169 |
if (result == null){ |
170 | 170 |
logger.warn("No language found for iso code: " + isoLang); |
... | ... | |
173 | 173 |
return result; |
174 | 174 |
|
175 | 175 |
} |
176 |
|
|
177 |
private Reference<?> handleReference(GlobisImportState state, Integer refId){
|
|
176 |
|
|
177 |
private Reference handleReference(GlobisImportState state, Integer refId){ |
|
178 | 178 |
if (refId == null){ |
179 | 179 |
return null; |
180 | 180 |
} |
181 |
Reference<?> result = refMap.get(refId);
|
|
182 |
|
|
181 |
Reference result = refMap.get(refId); |
|
182 |
|
|
183 | 183 |
if (result == null){ |
184 | 184 |
try { |
185 | 185 |
String sql = "SELECT * FROM [references] WHERE ReferenceID = " + refId; |
186 | 186 |
ResultSet rs = state.getConfig().getSource().getResultSet(sql); |
187 | 187 |
rs.next(); |
188 |
|
|
188 |
|
|
189 | 189 |
String authors = rs.getString("Author(s)"); |
190 | 190 |
String title = rs.getString("Title"); |
191 | 191 |
String details = rs.getString("Details"); |
... | ... | |
207 | 207 |
}else{ |
208 | 208 |
author = makeSingleAuthor(authors); |
209 | 209 |
} |
210 |
|
|
210 |
|
|
211 | 211 |
result.setAuthorship(author); |
212 | 212 |
refMap.put(refId,result); |
213 | 213 |
rs.close(); |
... | ... | |
215 | 215 |
e.printStackTrace(); |
216 | 216 |
} |
217 | 217 |
|
218 |
|
|
218 |
|
|
219 | 219 |
} |
220 |
|
|
220 |
|
|
221 | 221 |
return result; |
222 | 222 |
} |
223 | 223 |
|
... | ... | |
234 | 234 |
Map<Object, Map<String, ? extends CdmBase>> result = new HashMap<Object, Map<String, ? extends CdmBase>>(); |
235 | 235 |
try{ |
236 | 236 |
Set<String> taxonIdSet = new HashSet<String>(); |
237 |
|
|
237 |
|
|
238 | 238 |
while (rs.next()){ |
239 | 239 |
handleForeignKey(rs, taxonIdSet, "IDCurrentSpec"); |
240 | 240 |
} |
241 |
|
|
241 |
|
|
242 | 242 |
//taxon map |
243 | 243 |
nameSpace = TAXON_NAMESPACE; |
244 | 244 |
cdmClass = Taxon.class; |
... | ... | |
246 | 246 |
Map<String, Taxon> objectMap = (Map<String, Taxon>)getCommonService().getSourcedObjectsByIdInSource(cdmClass, idSet, nameSpace); |
247 | 247 |
result.put(nameSpace, objectMap); |
248 | 248 |
|
249 |
|
|
249 |
|
|
250 | 250 |
} catch (SQLException e) { |
251 | 251 |
throw new RuntimeException(e); |
252 | 252 |
} |
253 | 253 |
return result; |
254 | 254 |
} |
255 |
|
|
255 |
|
|
256 | 256 |
|
257 | 257 |
@Override |
258 | 258 |
protected boolean doCheck(GlobisImportState state){ |
259 | 259 |
// IOValidator<GlobisImportState> validator = new GlobisCurrentSpeciesImportValidator(); |
260 | 260 |
return true; |
261 | 261 |
} |
262 |
|
|
262 |
|
|
263 | 263 |
@Override |
264 | 264 |
protected boolean isIgnore(GlobisImportState state){ |
265 | 265 |
return ! state.getConfig().isDoCommonNames(); |
Also available in: Unified diff
Remove generics from Reference in cdmlib-app #5830