I have a project where I am currently loading the records from the excel file to the database using from org.apache.poi
I have 3 kinds of files that I am loading into different Dto classes. Two of these dtos shares the same base class (they have common attributes)
@Getter
@Setter
@ToString
public class PpyRecordDTO {
private String units;
private Double quantity;
}
@Getter
@Setter
@ToString
public class FirstRecordDTO extends RecordDTO {
private String normDescription;
private String relatedUnits;
}
@Getter
@Setter
@ToString
public class SecondRecordDTO extends RecordDTO{
private String normName;
}
@Getter
@Setter
@ToString
public class ThirdRecordDTO {
private String code;
}
The ThirdRecordDto class has a unique attribute and no shared attributes with the base dto class RecordDTO
I want to return from this method the base class : RecordDto (But ThirdRecordDTO cannot extend it since there are no common fields)
public static List<?extends RecordDTO> readPpyExcelFile(MultipartFile file, SourceType sourceType){
//TODO: making readPpyExcelFile generic
try {
Workbook workbook = WorkbookFactory.create(new BufferedInputStream(file.getInputStream()));
Sheet sheet = workbook.getSheetAt(0);
Iterator<Row> rows = sheet.iterator();
List<? extends RecordDTO> lstRecords = processRecords(rows, sourceType);
// Close WorkBook
workbook.close();
return lstRecords;
} catch(ApiGenericException apiException){
throw new ApiGenericException(apiException.getStatus(), apiException.getMessage(), apiException.getErrorType());
} catch (Exception e) {
logger.error(e.getMessage());
throw new ApiGenericException(HttpStatus.INTERNAL_SERVER_ERROR,"Enable while loading the file", ApiErrorType.Unhandled
);
}
}
Is there a way to make the dto ThirdRecordDto also be returned or inherit from an abstract class which is shared by the other dtos in order to return the type <? extends List from this method?
interfaceto define conformanceSerializable- it defines conformance, but no actual functionality