Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.nkapps.billing.controllers; import java.io.OutputStream; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.core.env.Environment; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import com.nkapps.billing.models.BankStatement; import com.nkapps.billing.models.PrintClaimPojo; import com.nkapps.billing.models.PrintClaimRegisterPojo; import com.nkapps.billing.models.PrintRegisterPojo; import com.nkapps.billing.models.ReportClickListPojo; import com.nkapps.billing.models.ReportSmstListPojo; import com.nkapps.billing.models.Subject; import com.nkapps.billing.services.AuthService; import com.nkapps.billing.services.ReportPrintService; import com.nkapps.billing.services.ReportService; import com.nkapps.billing.services.SearchService; /** * * @author nuraddin */ @Controller @RequestMapping("/report") public class ReportController { @Autowired private MessageSource messageSource; @Autowired private AuthService authService; @Autowired private SearchService searchService; @Autowired private Environment environment; @Autowired private ReportService reportService; @Autowired private ReportPrintService reportPrintService; Logger logger = LoggerFactory.getLogger(ReportController.class); @RequestMapping(value = "/click", method = RequestMethod.GET) public String click(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception { Subject subject = authService.getSubject(); map.put("userName", subject.getName()); map.put("userRole", subject.getRole()); map.put("ns10Code", subject.getNs10Code()); map.put("ns11Code", subject.getNs11Code()); map.put("searchBy", searchService.execSearchBy(request, response)); map.put("searchWithinDate", searchService.execSearchWithinDate(request, response)); map.put("searchByDate", searchService.execSearchByDate(request, response)); return "report/click"; } @RequestMapping(value = "/click-list", method = RequestMethod.POST) public @ResponseBody HashMap<String, Object> clickList(HttpServletRequest request, HttpServletResponse response) { HashMap<String, Object> resMap = new LinkedHashMap<String, Object>(); try { // for saving search parameters searchService.execSearchBy(request, response); searchService.execSearchWithinDate(request, response); searchService.execSearchByDate(request, response); Map<String, String[]> map = request.getParameterMap(); HashMap<String, String> parameters = new HashMap<>(); for (String key : map.keySet()) { String[] mapValue = map.get(key); parameters.put(key, mapValue[0]); } resMap.put("success", true); resMap.put("draw", request.getParameter("draw")); resMap.put("data", reportService.getClickList(parameters)); } catch (Exception e) { logger.error(e.getMessage()); resMap.put("success", false); resMap.put("reason", e.getMessage()); } return resMap; } @RequestMapping(value = "/print-click", method = RequestMethod.POST) public void printClick(HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFWorkbook workbook = null; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Date periodStart = sdf.parse(request.getParameter("periodStart")); Date periodEnd = sdf.parse(request.getParameter("periodEnd")); List<ReportClickListPojo> listPojo = reportService.getPrintClickList(periodStart, periodEnd); workbook = reportPrintService.printClick(listPojo); String fileName = new SimpleDateFormat("dd.MM.yyyy HH-mm-ss").format(Calendar.getInstance().getTime()); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\""); response.addHeader("Cache-Control", "max-age=1, must-revalidate"); response.addHeader("Pragma", "no-cache"); OutputStream stream = response.getOutputStream(); workbook.write(stream); } @RequestMapping(value = "/smst", method = RequestMethod.GET) public String smst(ModelMap map, HttpServletRequest request, HttpServletResponse response) throws Exception { Subject subject = authService.getSubject(); map.put("userName", subject.getName()); map.put("userRole", subject.getRole()); map.put("ns10Code", subject.getNs10Code()); map.put("ns11Code", subject.getNs11Code()); map.put("searchBy", searchService.execSearchBy(request, response)); map.put("searchWithinDate", searchService.execSearchWithinDate(request, response)); map.put("searchByDate", searchService.execSearchByDate(request, response)); return "report/smst"; } @RequestMapping(value = "/smst-list", method = RequestMethod.POST) public @ResponseBody HashMap<String, Object> smstList(HttpServletRequest request, HttpServletResponse response) { HashMap<String, Object> resMap = new LinkedHashMap<String, Object>(); try { // for saving search parameters searchService.execSearchBy(request, response); searchService.execSearchWithinDate(request, response); searchService.execSearchByDate(request, response); Map<String, String[]> map = request.getParameterMap(); HashMap<String, String> parameters = new HashMap<>(); for (String key : map.keySet()) { String[] mapValue = map.get(key); parameters.put(key, mapValue[0]); } resMap.put("success", true); resMap.put("draw", request.getParameter("draw")); resMap.put("data", reportService.getSmstList(parameters)); } catch (Exception e) { logger.error(e.getMessage()); resMap.put("success", false); resMap.put("reason", e.getMessage()); } return resMap; } @RequestMapping(value = "/print-smst", method = RequestMethod.POST) public void printSmst(HttpServletRequest request, HttpServletResponse response) throws Exception { HSSFWorkbook workbook = null; SimpleDateFormat sdf = new SimpleDateFormat("dd.MM.yyyy"); Date periodStart = sdf.parse(request.getParameter("periodStart")); Date periodEnd = sdf.parse(request.getParameter("periodEnd")); List<ReportSmstListPojo> listPojo = reportService.getPrintSmstList(periodStart, periodEnd); workbook = reportPrintService.printSmst(listPojo); String fileName = new SimpleDateFormat("dd.MM.yyyy HH-mm-ss").format(Calendar.getInstance().getTime()); response.setContentType("application/vnd.ms-excel"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".xls\""); response.addHeader("Cache-Control", "max-age=1, must-revalidate"); response.addHeader("Pragma", "no-cache"); OutputStream stream = response.getOutputStream(); workbook.write(stream); } }