Revision 02153b5c
Added by Andreas Müller over 11 years ago
app-import/src/main/java/eu/etaxonomy/cdm/io/globis/GlobisImageImport.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.cdm.io.globis; |
11 | 11 |
|
12 |
import java.net.MalformedURLException; |
|
13 |
import java.net.URI; |
|
12 | 14 |
import java.sql.ResultSet; |
13 | 15 |
import java.sql.SQLException; |
14 | 16 |
import java.util.HashMap; |
... | ... | |
20 | 22 |
import org.apache.log4j.Logger; |
21 | 23 |
import org.springframework.stereotype.Component; |
22 | 24 |
|
25 |
import eu.etaxonomy.cdm.common.media.ImageInfo; |
|
23 | 26 |
import eu.etaxonomy.cdm.io.common.IOValidator; |
24 | 27 |
import eu.etaxonomy.cdm.io.common.ResultSetPartitioner; |
25 | 28 |
import eu.etaxonomy.cdm.io.globis.validation.GlobisCurrentSpeciesImportValidator; |
... | ... | |
97 | 100 |
public boolean doPartition(ResultSetPartitioner partitioner, GlobisImportState state) { |
98 | 101 |
boolean success = true; |
99 | 102 |
|
100 |
Set<TaxonBase> objectsToSave = new HashSet<TaxonBase>();
|
|
103 |
Set<Media> objectsToSave = new HashSet<Media>();
|
|
101 | 104 |
|
102 |
Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE); |
|
105 |
// Map<String, Taxon> taxonMap = (Map<String, Taxon>) partitioner.getObjectMap(TAXON_NAMESPACE);
|
|
103 | 106 |
// Map<String, DerivedUnit> ecoFactDerivedUnitMap = (Map<String, DerivedUnit>) partitioner.getObjectMap(ECO_FACT_DERIVED_UNIT_NAMESPACE); |
104 | 107 |
|
105 | 108 |
ResultSet rs = partitioner.getResultSet(); |
106 |
|
|
107 |
Classification classification = getClassification(state); |
|
108 | 109 |
|
109 | 110 |
try { |
110 | 111 |
|
... | ... | |
115 | 116 |
|
116 | 117 |
if ((i++ % modCount) == 0 && i!= 1 ){ logger.info(pluralString + " handled: " + (i-1));} |
117 | 118 |
|
118 |
Integer taxonId = rs.getInt("IDcurrentspec"); |
|
119 |
Integer bildID = rs.getInt("BildID"); |
|
120 |
|
|
121 |
Integer spectaxID = nullSafeInt(rs, "spectaxID"); |
|
119 | 122 |
|
123 |
//ignore: [file lab2], same as Dateiname04 but less data |
|
120 | 124 |
|
121 |
//TODO |
|
122 |
//String dtSpcJahr, |
|
123 |
//dtSpcFamakt,dtSpcSubfamakt,dtSpcTribakt, |
|
124 |
//fiSpcLiteratur, fiSpcspcgrptax, dtSpcCountries,vernacularnames |
|
125 | 125 |
|
126 |
try { |
|
126 |
|
|
127 |
try { |
|
127 | 128 |
|
128 |
//source ref
|
|
129 |
//source ref
|
|
129 | 130 |
Reference<?> sourceRef = state.getTransactionalSourceReference(); |
130 |
Taxon nextHigherTaxon = null; |
|
131 | 131 |
|
132 |
Taxon species = createObject(rs, state); |
|
132 |
//make image path |
|
133 |
String pathShort = rs.getString("Dateipfad_kurz"); |
|
134 |
String fileOS = rs.getString("file OS"); |
|
135 |
pathShort.replace(fileOS, ""); |
|
136 |
//TODO move to config |
|
137 |
String newPath = "http://globis-images.insects-online.de/images/"; |
|
138 |
String path = pathShort.replace("image:Webversionen", newPath); |
|
139 |
|
|
133 | 140 |
|
134 |
//subgenus |
|
135 |
String subGenusStr = rs.getString("dtSpcSubgenakt"); |
|
136 |
String subGenusAuthorStr = rs.getString("dtSpcSubgenaktauthor"); |
|
137 |
boolean hasSubgenus = StringUtils.isNotBlank(subGenusStr) || StringUtils.isNotBlank(subGenusAuthorStr); |
|
138 |
if (hasSubgenus){ |
|
139 |
Taxon subGenus = getTaxon(state, rs, subGenusStr, Rank.SUBGENUS(), subGenusAuthorStr, taxonMap); |
|
140 |
classification.addParentChild(subGenus, species, sourceRef, null); |
|
141 |
nextHigherTaxon = getParent(subGenus, classification); |
|
142 |
} |
|
143 | 141 |
|
144 |
//genus |
|
145 |
String genusStr = rs.getString("dtSpcGenusakt"); |
|
146 |
String genusAuthorStr = rs.getString("dtSpcGenusaktauthor"); |
|
147 |
Taxon genus = getTaxon(state, rs, genusStr, Rank.GENUS(), genusAuthorStr, taxonMap); |
|
148 |
if (nextHigherTaxon != null){ |
|
149 |
if (! compareTaxa(genus, nextHigherTaxon)){ |
|
150 |
logger.warn("Current genus and parent of subgenus are not equal: " + taxonId); |
|
151 |
} |
|
152 |
}else{ |
|
153 |
classification.addParentChild(genus, species, sourceRef, null); |
|
154 |
nextHigherTaxon = getParent(genus, classification); |
|
155 |
} |
|
142 |
Media media1 = makeMedia(state, rs, "file OS", "Legende 1", path ); |
|
143 |
Media media2 = makeMedia(state, rs, "Dateiname02", "Legende 2", path ); |
|
144 |
Media media3 = makeMedia(state, rs, "Dateiname03", "Legende 3", path ); |
|
145 |
Media media4 = makeMedia(state, rs, "Dateiname04", "Legende 4", path ); |
|
146 |
|
|
156 | 147 |
|
157 |
this.doIdCreatedUpdatedNotes(state, species, rs, taxonId, REFERENCE_NAMESPACE); |
|
148 |
//TODO |
|
149 |
this.doIdCreatedUpdatedNotes(state, media1, rs, bildID, IMAGE_NAMESPACE); |
|
158 | 150 |
|
159 |
objectsToSave.add(species);
|
|
151 |
save(objectsToSave, media1);
|
|
160 | 152 |
|
153 |
|
|
161 | 154 |
|
162 | 155 |
} catch (Exception e) { |
163 |
logger.warn("Exception in current_species: IDcurrentspec " + taxonId + ". " + e.getMessage());
|
|
156 |
logger.warn("Exception in Einzelbilder: bildID " + bildID + ". " + e.getMessage());
|
|
164 | 157 |
// e.printStackTrace(); |
165 | 158 |
} |
166 | 159 |
|
... | ... | |
169 | 162 |
// logger.warn("Specimen: " + countSpecimen + ", Descriptions: " + countDescriptions ); |
170 | 163 |
|
171 | 164 |
logger.warn(pluralString + " to save: " + objectsToSave.size()); |
172 |
getTaxonService().save(objectsToSave);
|
|
165 |
getMediaService().save(objectsToSave);
|
|
173 | 166 |
|
174 | 167 |
return success; |
175 | 168 |
} catch (SQLException e) { |
... | ... | |
178 | 171 |
} |
179 | 172 |
} |
180 | 173 |
|
181 |
/** |
|
182 |
* Compares 2 taxa, returns true of both taxa look similar |
|
183 |
* @param genus |
|
184 |
* @param nextHigherTaxon |
|
185 |
* @return |
|
186 |
*/ |
|
187 |
private boolean compareTaxa(Taxon taxon1, Taxon taxon2) { |
|
188 |
ZoologicalName name1 = CdmBase.deproxy(taxon1.getName(), ZoologicalName.class); |
|
189 |
ZoologicalName name2 = CdmBase.deproxy(taxon2.getName(), ZoologicalName.class); |
|
190 |
if (!name1.getRank().equals(name2.getRank())){ |
|
191 |
return false; |
|
192 |
} |
|
193 |
if (! name1.getTitleCache().equals(name2.getTitleCache())){ |
|
194 |
return false; |
|
174 |
private void save(Set<Media> objectsToSave, Media media) { |
|
175 |
if (media != null){ |
|
176 |
objectsToSave.add(media); |
|
195 | 177 |
} |
196 |
return true; |
|
197 | 178 |
} |
198 | 179 |
|
199 | 180 |
|
200 | 181 |
|
201 | 182 |
|
202 |
private Taxon getParent(Taxon subgenus, Classification classification) { |
|
203 |
for (TaxonNode node : subgenus.getTaxonNodes()){ |
|
204 |
if (node.getClassification().equals(classification)){ |
|
205 |
return node.getParent().getTaxon(); |
|
206 |
} |
|
183 |
private Media makeMedia(GlobisImportState state, ResultSet rs, String fileNameAttr, String legendAttr, String path) throws SQLException { |
|
184 |
Media media = null; |
|
185 |
String fileName = rs.getString(fileNameAttr); |
|
186 |
String legend = rs.getString(legendAttr); |
|
187 |
|
|
188 |
URI uri = URI.create(path+fileName); |
|
189 |
|
|
190 |
// Media media = ImageInfo.NewInstanceWithMetaData(uri, null); |
|
191 |
|
|
192 |
try { |
|
193 |
media = this.getImageMedia(uri.toString(), true, false); |
|
194 |
media.putTitle(Language.ENGLISH(), legend); |
|
195 |
} catch (MalformedURLException e) { |
|
196 |
e.printStackTrace(); |
|
207 | 197 |
} |
208 |
return null; |
|
198 |
|
|
199 |
return media; |
|
209 | 200 |
} |
210 | 201 |
|
211 | 202 |
|
... | ... | |
225 | 216 |
} |
226 | 217 |
|
227 | 218 |
|
228 |
//fast and dirty is enough here |
|
229 |
private Classification classification; |
|
230 |
|
|
231 |
private Classification getClassification(GlobisImportState state) { |
|
232 |
if (this.classification == null){ |
|
233 |
String name = state.getConfig().getClassificationName(); |
|
234 |
Reference<?> reference = state.getTransactionalSourceReference(); |
|
235 |
this.classification = Classification.NewInstance(name, reference, Language.DEFAULT()); |
|
236 |
classification.setUuid(state.getConfig().getClassificationUuid()); |
|
237 |
getClassificationService().save(classification); |
|
238 |
} |
|
239 |
return this.classification; |
|
240 |
|
|
241 |
} |
|
242 |
|
|
243 |
private INonViralNameParser parser = NonViralNameParserImpl.NewInstance(); |
|
244 |
|
|
245 | 219 |
|
246 |
/* (non-Javadoc) |
|
247 |
* @see eu.etaxonomy.cdm.io.common.mapping.IMappingImport#createObject(java.sql.ResultSet, eu.etaxonomy.cdm.io.common.ImportStateBase) |
|
248 |
*/ |
|
249 |
public Taxon createObject(ResultSet rs, GlobisImportState state) |
|
250 |
throws SQLException { |
|
251 |
String speciesEpi = rs.getString("dtSpcSpcakt"); |
|
252 |
String subGenusEpi = rs.getString("dtSpcSubgenakt"); |
|
253 |
String genusEpi = rs.getString("dtSpcGenusakt"); |
|
254 |
String author = rs.getString("dtSpcAutor"); |
|
255 |
|
|
256 |
|
|
257 |
ZoologicalName zooName = ZoologicalName.NewInstance(Rank.SPECIES()); |
|
258 |
zooName.setSpecificEpithet(speciesEpi); |
|
259 |
if (StringUtils.isNotBlank(subGenusEpi)){ |
|
260 |
zooName.setInfraSpecificEpithet(subGenusEpi); |
|
261 |
} |
|
262 |
zooName.setGenusOrUninomial(genusEpi); |
|
263 |
handleAuthorAndYear(author, zooName); |
|
264 |
|
|
265 |
Taxon taxon = Taxon.NewInstance(zooName, state.getTransactionalSourceReference()); |
|
266 |
|
|
267 |
return taxon; |
|
268 |
} |
|
269 | 220 |
|
270 | 221 |
|
271 | 222 |
/* (non-Javadoc) |
Also available in: Unified diff
update Globis import