I'm new in Spring-Batch world and I'm triying to do the following:
Read from a file that have inside something like this:
65198;4
65257;9
66745;14
67091;3
64206;10
112233;8
and one of those codes doesn't exist.
My goal is that write in a file with the name "Exist.txt" the codes that exist and in other file with the name "NoExist.txt" the other ones.
I have only one processor that do this work. Here the processor
@Bean
@StepScope
public ItemProcessor<HanaskaAssitedRequestSendedFileVO,AssitedRequestFileVO> processor(IHorecaAssistedRequestProcessorGestor horecaAssistedRequestProcessorGestor){
return new ItemProcessor<HanaskaAssitedRequestSendedFileVO,AssitedRequestFileVO>() {
@Override
public AssitedRequestFileVO process(HanaskaAssitedRequestSendedFileVO item) throws Exception {
AssitedRequestFileVO assitedRequestFileVO = new AssitedRequestFileVO();
Set<String> itemsBarCode = new HashSet<>();
BusquedaArticulosRequestVO busquedaArticulosRequestVO = new BusquedaArticulosRequestVO();
return horecaAssistedRequestProcessorGestor.getDataToWrite(item,assitedRequestFileVO, itemsBarCode,
busquedaArticulosRequestVO);
}
};
}
and her is the gestor that returns data to write in a file
@Override
public AssitedRequestFileVO getDataToWrite(HanaskaAssitedRequestSendedFileVO item,
AssitedRequestFileVO assitedRequestFileVO, Set<String> itemsBarCode,
BusquedaArticulosRequestVO busquedaArticulosRequestVO) {
this.validateData(busquedaArticulosRequestVO, item, itemsBarCode, assitedRequestFileVO);
return assitedRequestFileVO;
}
private void validateData(BusquedaArticulosRequestVO busquedaArticulosRequestVO,
HanaskaAssitedRequestSendedFileVO item, Set<String> itemsBarCode,
AssitedRequestFileVO assitedRequestFileVO) {
try {
this.setDataToBusquedaArticulosRequestVO(busquedaArticulosRequestVO, item, itemsBarCode);
Map<String, ArticuloVentaVO> mapItem = horecaAssistedRequestSpirngBatchService
.getDataItem(busquedaArticulosRequestVO).getMapArticuloVentaVO();
Optional<Entry<String, ArticuloVentaVO>> optItem = mapItem.entrySet().stream().findAny();
ArticuloVentaVO articuloVentaVO = null;
if (optItem.isPresent()) {
articuloVentaVO = optItem.get().getValue();
assitedRequestFileVO.setItemCode(this.addDigitsToData(articuloVentaVO.getCodigoBarras(),12));
assitedRequestFileVO.setItemPresent(true);
assitedRequestFileVO.setMeasureUnit(this.addDigitsToData(articuloVentaVO.getUnidadManejoVenta().toString(),3));
assitedRequestFileVO.setRequestedQuantity(this.addDigitsToData(item.getCantidadPedida(),3));
assitedRequestFileVO.setStoreCode("711");
assitedRequestFileVO.setStoreCode("096");
}
} catch (Exception e) {
assitedRequestFileVO.setItemCode(item.getCodigoBarras());
assitedRequestFileVO.setItemPresent(false);
logger.info("Error->"+e.getMessage());
}
}
The code above return if a code exists or not.
So how can I write two distincts files with different names, filtering and writing the codes that exists or not in their appropriate file in java-config?
Thanks in advance!!!
You need to use the ClassifierCompositeItemWriter in order to classify items and write each type in its corresponding file.
Here is a self contained example you can try:
This sample reads some
Personitems and writes those with namefoo*tofoos.txtand those with namebar*tobars.txt. You can do the same withExist.txtandNoExist.txtin your case.Hope this helps.