@Override public void write(List<? extends T> items) throws Exception { if (!opened) { File file = setResourceToDelegate(); // create only if write is called file.createNewFile(); Assert.state(file.canWrite(), "Output resource " + file.getAbsolutePath() + " must be writable"); delegate.open(new ExecutionContext()); opened = true; } delegate.write(items); currentResourceItemCount += items.size(); if (currentResourceItemCount >= itemCountLimitPerResource) { delegate.close(); resourceIndex++; currentResourceItemCount = 0; setResourceToDelegate(); opened = false; } }
/** * Create output resource (if necessary) and point the delegate to it. */ private File setResourceToDelegate() throws IOException { String path = resource.getFile().getAbsolutePath() + suffixCreator.getSuffix(resourceIndex); File file = new File(path); delegate.setResource(new FileSystemResource(file)); return file; } }
@Override public void update(ExecutionContext executionContext) throws ItemStreamException { super.update(executionContext); if (saveState) { if (opened) { delegate.update(executionContext); } executionContext.putInt(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT), currentResourceItemCount); executionContext.putInt(getExecutionContextKey(RESOURCE_INDEX_KEY), resourceIndex); } }
@Override public void write(List<? extends ExcelRow> items) throws Exception { if (!initialized && items.size() > 0) { SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd"); String suffix = format.format(items.get(0).getDate()); File file = ExcelFileUtils.getFile(new File(directoryPath), fileName, suffix, fileExtension); delegate.setResource(new FileSystemResource(file)); delegate.open(executionContext); initialized = true; } delegate.write(items); }
@Override protected void doOpen() throws Exception { if (executionContext.containsKey(getExecutionContextKey(RESOURCE_KEY))) { resource = new FileSystemResource(executionContext.getString(getExecutionContextKey(RESOURCE_KEY), "")); // It's a restart delegate.open(executionContext); delegate.setResource(resource); // We don't have to create the resource opened = true; LOGGER.trace("Stream is opened"); } else { opened = false; LOGGER.trace("Stream is not opened yet"); } }
@Override public void close() throws ItemStreamException { super.close(); resourceIndex = 1; currentResourceItemCount = 0; if (opened) { delegate.close(); } }
@Override public void open(ExecutionContext executionContext) throws ItemStreamException { super.open(executionContext); resourceIndex = executionContext.getInt(getExecutionContextKey(RESOURCE_INDEX_KEY), 1); currentResourceItemCount = executionContext.getInt(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT), 0); try { setResourceToDelegate(); } catch (IOException e) { throw new ItemStreamException("Couldn't assign resource", e); } if (executionContext.containsKey(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT))) { // It's a restart delegate.open(executionContext); opened = true; } else { opened = false; } }
@Override public void close() throws ItemStreamException { delegate.close(); initialized = false; }
@Override public void open(ExecutionContext executionContext) throws ItemStreamException { super.open(executionContext); resourceIndex = executionContext.getInt(getExecutionContextKey(RESOURCE_INDEX_KEY), 1); currentResourceItemCount = executionContext.getInt(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT), 0); try { setResourceToDelegate(); } catch (IOException e) { throw new ItemStreamException("Couldn't assign resource", e); } if (executionContext.containsKey(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT))) { // It's a restart delegate.open(executionContext); opened = true; } else { opened = false; } }
@Override public void update(ExecutionContext executionContext) throws ItemStreamException { delegate.update(executionContext); }
@Override public void close() throws ItemStreamException { super.close(); resourceIndex = 1; currentResourceItemCount = 0; if (opened) { delegate.close(); } }
private void initializeStreams() throws Exception { if (createBackupFile) { outputResource = new FileSystemResource(File.createTempFile(resource.getFilename(), ".tmp", resource.getFile().getParentFile())); } else { outputResource = resource; } reader.setResource(resource); writer.setResource(outputResource); }
@Override public void write(List<? extends T> items) throws Exception { if (!opened) { File file = setResourceToDelegate(); // create only if write is called file.createNewFile(); Assert.state(file.canWrite(), "Output resource " + file.getAbsolutePath() + " must be writable"); delegate.open(new ExecutionContext()); opened = true; } delegate.write(items); currentResourceItemCount += items.size(); if (currentResourceItemCount >= itemCountLimitPerResource) { delegate.close(); resourceIndex++; currentResourceItemCount = 0; setResourceToDelegate(); opened = false; } }
@Override public void update(ExecutionContext executionContext) { if (isSaveState()) { if (opened) { delegate.update(executionContext); } try { if (resource != null) { executionContext.putString(getExecutionContextKey(RESOURCE_KEY), resource.getFile().getAbsolutePath()); LOGGER.trace("We store the resource={} into the execution context", resource.getFile().getAbsolutePath()); } } catch (IOException e) { throw new ItemStreamException("Cannot get resource's absolute path", e); } } }
@Override protected void doClose() throws Exception { resource = null; setCurrentItemIndex(0); if (opened) { delegate.close(); opened = false; LOGGER.trace("Stream is closed"); } }
/** * Create output resource (if necessary) and point the delegate to it. */ private File setResourceToDelegate() throws IOException { resource = resourceLocator.getResource(executionContext); delegate.setResource(resource); LOGGER.debug("Setting resource={} to delegate", resource != null ? resource.getFile().getAbsolutePath() : "null"); return resource.getFile(); }
@Override public void update(ExecutionContext executionContext) throws ItemStreamException { super.update(executionContext); if (saveState) { if (opened) { delegate.update(executionContext); } executionContext.putInt(getExecutionContextKey(CURRENT_RESOURCE_ITEM_COUNT), currentResourceItemCount); executionContext.putInt(getExecutionContextKey(RESOURCE_INDEX_KEY), resourceIndex); } }
/** * Create output resource (if necessary) and point the delegate to it. */ private File setResourceToDelegate() throws IOException { String path = resource.getFile().getAbsolutePath() + suffixCreator.getSuffix(resourceIndex); File file = new File(path); delegate.setResource(new FileSystemResource(file)); return file; } }