package com.braimatics.training.controller;

import com.braimatics.training.dao.MateriDao;
import com.braimatics.training.entity.Materi;
import java.io.IOException;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import net.sf.jasperreports.engine.JRException;
import net.sf.jasperreports.engine.JasperCompileManager;
import net.sf.jasperreports.engine.JasperExportManager;
import net.sf.jasperreports.engine.JasperFillManager;
import net.sf.jasperreports.engine.JasperPrint;
import net.sf.jasperreports.engine.JasperReport;
import net.sf.jasperreports.engine.data.JRBeanCollectionDataSource;
import net.sf.jasperreports.engine.util.JRLoader;
import org.apache.commons.collections.IteratorUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/materi")
public class MateriController {
    
    @Autowired private MateriDao md;
    
    @Value("classpath:/report/materi.jrxml") 
    private Resource materiJrxml;
    private JasperReport materiJasper;
    
    private JasperReport getMateriJasper() throws IOException, JRException{
        if(materiJasper == null){
            materiJasper = JasperCompileManager.compileReport(materiJrxml.getInputStream());
        }
        return materiJasper;
    }
    
    @RequestMapping("/pdf")
    @ResponseBody
    public void generatePdf(HttpServletResponse response) throws JRException, IOException{
        Map<String, Object> reportParameters = new HashMap<>();
        reportParameters.put("tanggalCetak", new Date());
        
        JRBeanCollectionDataSource reportData 
                = new JRBeanCollectionDataSource(IteratorUtils.toList(md.findAll().iterator()));
        
        JasperPrint materiPdf = JasperFillManager.fillReport(getMateriJasper(), reportParameters, 
                reportData);
        response.setContentType("application/x-pdf");
        response.setHeader("Content-disposition", "inline; filename=materi.pdf");
        JasperExportManager.exportReportToPdfStream(materiPdf, response.getOutputStream());
    }
    
    @RequestMapping("list")
    public ModelMap daftarMateri(Pageable page){
        ModelMap mm = new ModelMap();
        
        mm.addAttribute("daftarMateri", md.findAll(page));
        
        return mm;
    }
    
    @RequestMapping("view")
    public void detailMateri(){}
    
    @RequestMapping(value = "form", method = RequestMethod.GET)
    public ModelMap tampilkanForm(@RequestParam(required = false) String id){
        ModelMap mm = new ModelMap();
        
        mm.addAttribute("materi", new Materi());
        
        if(StringUtils.hasText(id)){
            Materi m = md.findOne(id);
            if(m != null){
                mm.addAttribute("materi", m);
            }
        }
        
        return mm;
    }
    
    @RequestMapping(value = "form", method = RequestMethod.POST)
    public String prosesForm(@ModelAttribute @Valid Materi m, BindingResult hasilValidasi){
        
        if(hasilValidasi.hasErrors()) {
            return "/materi/form";
        }
        
        md.save(m);
        return "redirect:list";
    }
}

@@ -2157,106 +2157,55 @@
     
-@RequestMapping(%22/pdf%22)%0A    @ResponseBody%0A    public void generatePdf(HttpServletResponse response
+private JasperPrint generateMateriJasperReport(
 ) th
@@ -2225,32 +2225,33 @@
 ion, IOException
+ 
 %7B%0A        Map%3CSt
@@ -2354,33 +2354,24 @@
 ew Date());%0A
-        %0A
         JRBe
@@ -2498,33 +2498,24 @@
 erator()));%0A
-        %0A
         Jasp
@@ -2528,19 +2528,22 @@
 t materi
-Pdf
+Report
  = Jaspe
@@ -2633,16 +2633,258 @@
 tData);%0A
+        return materiReport;%0A    %7D%0A    %0A    @RequestMapping(%22/pdf%22)%0A    @ResponseBody%0A    public void generatePdf(HttpServletResponse response) throws JRException, IOException%7B%0A        JasperPrint materiReport = generateMateriJasperReport();%0A
         
@@ -2984,14 +2984,18 @@
 %22, %22
-inline
+attachment
 ; fi
@@ -3001,16 +3001,23 @@
 ilename=
+daftar-
 materi.p
@@ -3080,19 +3080,22 @@
 m(materi
-Pdf
+Report
 , respon
@@ -3115,36 +3115,32 @@
 tream());%0A    %7D%0A
-    
 %0A    @RequestMap

refactor supaya bisa reuse