Revision 3443c007
Added by Andreas Müller about 7 years ago
cdmlib-io/src/main/java/eu/etaxonomy/cdm/io/common/CdmApplicationAwareDefaultExport.java | ||
---|---|---|
9 | 9 |
|
10 | 10 |
package eu.etaxonomy.cdm.io.common; |
11 | 11 |
|
12 |
import java.util.ArrayList; |
|
12 | 13 |
import java.util.HashMap; |
14 |
import java.util.List; |
|
13 | 15 |
import java.util.Map; |
14 | 16 |
|
15 | 17 |
import org.apache.log4j.Logger; |
... | ... | |
19 | 21 |
import org.springframework.stereotype.Component; |
20 | 22 |
|
21 | 23 |
import eu.etaxonomy.cdm.api.service.IService; |
24 |
import eu.etaxonomy.cdm.common.monitor.IProgressMonitor; |
|
25 |
import eu.etaxonomy.cdm.common.monitor.SubProgressMonitor; |
|
22 | 26 |
import eu.etaxonomy.cdm.io.common.events.IIoObserver; |
23 | 27 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
24 | 28 |
|
... | ... | |
208 | 212 |
state.initialize(config); |
209 | 213 |
state.setResult(result); |
210 | 214 |
|
215 |
List<ICdmExport> ioList = makeIoList(state, config); |
|
216 |
|
|
217 |
List<Integer> stepCounts = countSteps(state, ioList); |
|
218 |
Integer totalCount = stepCounts.get(stepCounts.size()-1); |
|
219 |
config.getProgressMonitor().worked(1); |
|
220 |
IProgressMonitor parentMonitor = SubProgressMonitor |
|
221 |
.NewStarted(config.getProgressMonitor(), 99, "Process data", totalCount); |
|
222 |
|
|
211 | 223 |
//do invoke for each class |
212 |
for (Class<ICdmExport> ioClass: config.getIoClassList()){ |
|
224 |
for (int i = 0; i< ioList.size(); i++){ |
|
225 |
ICdmExport export = ioList.get(i); |
|
226 |
Integer counts = stepCounts.get(i); |
|
213 | 227 |
try { |
214 |
String ioBeanName = getComponentBeanName(ioClass); |
|
215 |
ICdmExport cdmIo = applicationContext.getBean(ioBeanName, ICdmExport.class); |
|
216 |
if (cdmIo != null){ |
|
217 |
state.setCurrentIO(cdmIo); |
|
218 |
cdmIo.invoke(state); |
|
219 |
}else{ |
|
220 |
String message = "cdmIO was null"; |
|
221 |
logger.error(message); |
|
222 |
result.addError(message); |
|
223 |
} |
|
228 |
String ioName = export.getClass().getSimpleName(); |
|
229 |
SubProgressMonitor ioMonitor = SubProgressMonitor |
|
230 |
.NewStarted(parentMonitor, counts, ioName, counts ); |
|
231 |
state.setCurrentMonitor(ioMonitor); |
|
232 |
state.setCurrentIO(export); |
|
233 |
export.invoke(state); |
|
234 |
ioMonitor.done(); |
|
224 | 235 |
} catch (Exception e) { |
225 |
String message = "Unexpected exception in " + ioClass.getSimpleName()+ ": " + e.getMessage();
|
|
226 |
logger.error(message);
|
|
227 |
e.printStackTrace();
|
|
228 |
result.addException(e, message);
|
|
236 |
String message = "Unexpected exception in " + export.getClass().getSimpleName()+ ": " + e.getMessage();
|
|
237 |
logger.error(message); |
|
238 |
e.printStackTrace(); |
|
239 |
result.addException(e, message); |
|
229 | 240 |
} |
230 | 241 |
} |
231 | 242 |
|
... | ... | |
236 | 247 |
return result; |
237 | 248 |
} |
238 | 249 |
|
239 |
private String getComponentBeanName(Class<ICdmExport> ioClass){ |
|
250 |
/** |
|
251 |
* @param state |
|
252 |
* @param ioList |
|
253 |
* @return |
|
254 |
*/ |
|
255 |
private List<Integer> countSteps(ExportStateBase state, List<ICdmExport> ioList) { |
|
256 |
//do invoke for each class |
|
257 |
List<Integer> result = new ArrayList<>(); |
|
258 |
int sum = 0; |
|
259 |
for (ICdmExport export: ioList){ |
|
260 |
int count = 1; |
|
261 |
try { |
|
262 |
// state.setCurrentIO(export); |
|
263 |
count = ((Long)export.countSteps(state)).intValue(); |
|
264 |
} catch (Exception e) { |
|
265 |
String message = "Unexpected exception when count steps for progress monitoring " + export.getClass().getSimpleName()+ ": " + e.getMessage(); |
|
266 |
logger.error(message); |
|
267 |
e.printStackTrace(); |
|
268 |
state.getResult().addException(e, message); |
|
269 |
} |
|
270 |
result.add(count); |
|
271 |
sum += count; |
|
272 |
} |
|
273 |
result.add(sum); |
|
274 |
return result; |
|
275 |
} |
|
276 |
|
|
277 |
|
|
278 |
/** |
|
279 |
* @param state |
|
280 |
* @param config |
|
281 |
* @return |
|
282 |
*/ |
|
283 |
private <CONFIG extends T> List<ICdmExport> makeIoList(ExportStateBase state, CONFIG config) { |
|
284 |
|
|
285 |
List<ICdmExport> result = new ArrayList<>(); |
|
286 |
|
|
287 |
for (Class<ICdmExport> ioClass: config.getIoClassList()){ |
|
288 |
try { |
|
289 |
String ioBeanName = getComponentBeanName(ioClass); |
|
290 |
ICdmExport cdmIo = applicationContext.getBean(ioBeanName, ICdmExport.class); |
|
291 |
if (cdmIo != null){ |
|
292 |
result.add(cdmIo); |
|
293 |
}else{ |
|
294 |
String message = "cdmIO was null: " + ioBeanName; |
|
295 |
logger.error(message); |
|
296 |
state.getResult().addError(message); |
|
297 |
} |
|
298 |
} catch (Exception e) { |
|
299 |
String message = "Unexpected exception in " + ioClass.getSimpleName()+ ": " + e.getMessage(); |
|
300 |
logger.error(message); |
|
301 |
e.printStackTrace(); |
|
302 |
state.getResult().addException(e, message); |
|
303 |
} |
|
304 |
} |
|
305 |
return result; |
|
306 |
} |
|
307 |
|
|
308 |
|
|
309 |
private String getComponentBeanName(Class<ICdmExport> ioClass){ |
|
240 | 310 |
Component component = ioClass.getAnnotation(Component.class); |
241 | 311 |
String ioBean = component.value(); |
242 | 312 |
if ("".equals(ioBean)){ |
Also available in: Unified diff
ref #6754 ref #6755 improve filtering and progress monitoring for DwCA export