I have a odd problem with my code. I have implemented the template method pattern
to provide different implementation of the same alghoritm. So I have created the abstract class:
@Service
public abstract class ExcelRdi_Iupr_Sl {
@Autowired
private Environment env;
private static String PROPERTY_NAME_FILESYSTEM_EXCELPATH = "temporary.excelpath";
private XSSFWorkbook workbook;
private XSSFCellStyle unlockedNumericStyle;
private XSSFSheet sheet;
/**
* Create the excel file with the RDI,IUPR or SL
* @param car
* @param fileName
* @return
* @throws Exception
*/
public final String retrieve(Car car, String fileName) throws Exception{
writeInitialize();
Map<Integer,Integer> defMap = firstTwoRow(car, sheet);
elaboration(car, sheet, unlockedNumericStyle, defMap);
return writeFile(car, fileName);
}
/**
* Import the excel file with the RDI,IUPR or SL
* @param file
* @param car
* @throws Exception
*/
public final int update(MultipartFile file, Car car) throws Exception{
String filePath = saveFile(file, car);
readInitialize(filePath);
return updateRdi(car, sheet);
}
/**
* Save the imported file into file system inside a temporary folder
* @param file
* @param car
* @return
* @throws Exception
*/
private String saveFile(MultipartFile multipartFile, Car car) throws Exception {
String path = env.getRequiredProperty(PROPERTY_NAME_FILESYSTEM_EXCELPATH) + File.separator + String.valueOf(System.currentTimeMillis());
File file = new File (path+ File.separator + multipartFile.getOriginalFilename());
file.getParentFile().mkdirs();
multipartFile.transferTo(file);
return file.getAbsolutePath();
}
/**
* Initialize the object for the creating procedure
*/
protected void writeInitialize(){
//initialize class variable
}
/**
* Initialize the object for the reading procedure
* @param filePath
* @throws Exception
*/
protected void readInitialize(String filePath) throws Exception{
//read ile and initialize class variable
}
/**
* Write the created file inside the file system
* @param car
* @param fileName
* @return
* @throws Exception
*/
private String writeFile(Car car, String fileName) throws Exception{
//write on file
}
/**
* This excel creating method has to be implemented by the concrete class
* @param car
* @param sheet
* @throws Exception
*/
protected abstract Map<Integer,Integer> firstTwoRow(Car car, XSSFSheet sheet) throws Exception;
/**
* This excel creating method has to be implemented by the concrete class
* @param car
* @param sheet
* @param defMap
* @throws Exception
*/
protected abstract void elaboration(Car car, XSSFSheet sheet, XSSFCellStyle unlockedNumericStyle, Map<Integer,Integer> defMap);
/**
* This excel import method has to be implemented by the concrete class
* @param car
* @param sheet
* @return number of elaborated row
* @throws Exception
*/
protected abstract int updateRdi(Car car, XSSFSheet sheet) throws Exception;
}
One of the classes that extend the above class has this implementation:
@Component
public class ExcelRdi extends ExcelRdi_Iupr_Sl {
@Autowired
private RdiServices rdiServices;
@Autowired
private AcquisitionServices acquisitionServices;
static final Logger LOG = LoggerFactory.getLogger(ExcelRdi.class);
/**
* Create the first two row of excel file
*/
@Override
protected Map<Integer,Integer> firstTwoRow(Car car, XSSFSheet sheet) throws Exception {
//code
}
/**
* Create the row with RDI for excel file
*/
@Override
protected void elaboration(Car car, XSSFSheet sheet, XSSFCellStyle unlockedNumericStyle, Map<Integer,Integer> defRdiMap) {
//code
}
/**
* Import the Excel file
* @throws Exception
*/
@Override
@Transactional(rollbackFor= Exception.class)
protected int updateRdi(Car car, XSSFSheet sheet) throws Exception{
//COde
}
}
So from the services that recieves the request through the controller I have a simple switch:
@Autowired
private ExcelRdi excelRdi;
@Autowired
private ExcelIupr excelIupr;
@Autowired
private ExcelSl excelSl;
@Override
public String getExcel(Car car, String type) throws Exception {
String fileName = type + "$"+car.getFleet().getFleetName().getFleetName() +"$"+ car.getFleet().getApplication()+"$"+ car.getCarType().getIdCarType()+car.getId() +"$"+car.getIdCar()+".xlsx";
switch (type){
case "RDI": return excelRdi.retrieve(car, fileName);
case "IUPR": return excelIupr.retrieve(car, fileName);
case "SL": return excelSl.retrieve(car, fileName);
default: throw new FileFormatException("You can create only RDI, SL or IUPR file!");
}
}
@Override
public int importExcel(MultipartFile file, Car car, String type) throws Exception {
//Check if the file has the correct name before import
String fileName = type + "$"+car.getFleet().getFleetName().getFleetName() +"$"+ car.getFleet().getApplication()+"$"+ car.getCarType().getIdCarType()+car.getId() +"$"+car.getIdCar()+".xlsx";
if (!file.getOriginalFilename().equals(fileName))
throw new FileFormatException("The file is not for this car");
switch (type){
case "RDI": return excelRdi.update(file, car);
case "IUPR": return excelIupr.update(file, car);
case "SL": return excelSl.update(file, car);
default: throw new FileFormatException("You can create only RDI, SL or IUPR file!");
}
}
So this is the problem:
- If I make
protected
theupdateRdi
all work fine but@Transactional
annotation needs public method otherwise it doesn't work - _If I make
public
theupdateRdi
I receivenullPointerException
onsheet
object offirstTwoRow
and the same for theenv
variable. Do you know why I have this behavior and how I can fix that? Many thanks
Aucun commentaire:
Enregistrer un commentaire