@Override public Void call() throws Exception { try { state.reset(); List<CollectRecord> recordSummaries = loadAllSummaries(); if (recordSummaries != null && stepNumbers != null) { state.setRunning(true); String fileName = FILE_NAME; File file = new File(directory, fileName); if (file.exists()) { file.delete(); } FileOutputStream fileOutputStream = new FileOutputStream(file); BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(fileOutputStream); ZipOutputStream zipOutputStream = new ZipOutputStream(bufferedOutputStream); backup(zipOutputStream, recordSummaries); zipOutputStream.flush(); zipOutputStream.close(); if (!state.isCancelled()) { state.setComplete(true); } } } catch (Exception e) { state.setError(true); LOG.error("Error during data export", e); } finally { state.setRunning(false); } return null; }
private void backup(ZipOutputStream zipOutputStream, List<CollectRecord> recordSummaries) { int total = calculateTotal(recordSummaries); state.setTotal(total); includeIdml(zipOutputStream); for (CollectRecord summary : recordSummaries) { if (!state.isCancelled()) { int recordStepNumber = summary.getStep().getStepNumber(); for (int stepNum : stepNumbers) { if (stepNum <= recordStepNumber) { backup(zipOutputStream, summary, stepNum); state.incrementCount(); } } } else { state.setRunning(false); break; } } }
@Override public void cancel() { state.setCancelled(true); state.setRunning(false); }