I’m trying to make a jasperreport, here’s the following code
Controller:
@GetMapping("getX")
public ResponseEntity<?> getX() throws IllegalAccessException, InvocationTargetException{
return ResponseEntity.status(HttpStatus.OK).body(s.getX());
}
@PostMapping("saveY")
public ResponseEntity<?> saveY(YDTO y){
if(y.getName()==""||y.getSurname()=="") {
throw new CustomException("custom");
}
if(y.getNumb().length()!=someNumb) {
throw new CustomException2("custom2",y.getNumb().length());
}
Integer idNewY = s.saveY(y);
if(idNewY == -1) {
throw new CustomException3("custom3");
}
return ResponseEntity.ok("Success:" +idNewY);
}
@GetMapping("reportI")
public ResponseEntity<?> getReport(String z) throws JRException, IOException{
if(!s.existZ(z)) {
throw new CustomException4("custom4", z);
}
byte[] report = s.createReport(z);
if(report==null) {
throw new CustomException4("custom4", z);
}
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.parseMediaType("application/pdf"));
headers.setContentDispositionFormData("attachment", "pdfName.pdf");
return ResponseEntity.ok().headers(headers).body(report);
}
@ExceptionHandler(RuntimeException.class)
public ResponseEntity<String> handleNotFoundZException(RuntimeException e){
return ResponseEntity.badRequest().body("Error: "+e.getMessage());
}
Service:
public List<XDTO> getX() throws IllegalAccessException, InvocationTargetException {
List<X> x = xr.findAll();
List<XDTO> xDTO = new LinkedList<>();
for(X p: x) {
XDTO dto = new XDTO();
BeanUtils.copyProperties(p, dto);
xDTO.add(dto);
}
return xDTO;
}
public int saveY(YDTO y) {
Y newY = new Y();
newY.setName(y.getName());
newY.setSurname(y.getSurname());
try {
Y yc = yr.save(newY);
return yc.getIdY();
}catch(Exception e) {
e.printStackTrace();
return -1;
}
}
public boolean existZ(String z) {
Z newZ = zr.findByName(z);
if(newZ!=null) {
return true;
}
return false;
}
public byte[] createReport(String z, and should be date) throws JRException, IOException {
System.out.println("Number : "+ir.getIZ(z).size());
if (ir.getIZ(z).size() > 0) {
JRBeanCollectionDataSource dataSource = new JRBeanCollectionDataSource(ir.getIZ(z));
java.io.InputStream inputStream = this.getClass().getResourceAsStream("/jasperreports/nameOfJrxml.jrxml");
JasperReport jasperReport = JasperCompileManager.compileReport(inputStream);
Map<String, Object> params = new HashMap<String, Object>();
params.put("z", z);
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, params, dataSource);
inputStream.close();
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
JRPdfExporter exporter = new JRPdfExporter();
exporter.setExporterInput(new SimpleExporterInput(jasperPrint));
exporter.setExporterOutput(new SimpleOutputStreamExporterOutput(byteArrayOutputStream));
exporter.exportReport();
return byteArrayOutputStream.toByteArray();
}
return null;
}
Exception:
public class CustomException4 extends RuntimeException {
private static final long serialVersionUID = 1L;
public CustomException4(String message, String value) {
super(message+", Value: "+value);
}
}
I also have regular repo’s and dto’s
The report template is created, and the main issue is the date, i dont know how to use it in the report
Date needs to be parsed in some sort of way, do not know how
It needs the strict date format, i think its yyyy-mm-dd, MySQL Database is being used