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.math.BigDecimal; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.HashMap; import java.util.LinkedHashMap; import java.util.Map; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.MessageSource; import org.springframework.context.i18n.LocaleContextHolder; 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.Subject; import com.nkapps.billing.services.AuthService; import com.nkapps.billing.services.OverpaymentService; import com.nkapps.billing.services.SearchService; /** * * @author nuraddin */ @Controller @RequestMapping("/overpayment") public class OverpaymentController { @Autowired private MessageSource messageSource; @Autowired private AuthService authService; @Autowired private SearchService searchService; @Autowired private OverpaymentService overpaymentService; Logger logger = LoggerFactory.getLogger(OverpaymentController.class); @RequestMapping(value = "/index", method = RequestMethod.GET) public String index(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 "overpayment/index"; } @RequestMapping(value = "/list", method = RequestMethod.POST) public @ResponseBody HashMap<String, Object> list(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", overpaymentService.getList(parameters)); } catch (Exception e) { logger.error(e.getMessage()); resMap.put("success", false); resMap.put("reason", e.getMessage()); } return resMap; } @RequestMapping(value = "/return-save", method = RequestMethod.POST) public @ResponseBody HashMap<String, Object> returnSave(HttpServletRequest request, HttpServletResponse response) { HashMap<String, Object> resMap = new LinkedHashMap<String, Object>(); try { String bankStatementId = request.getParameter("bankStatementId"); Short returnState = Short.parseShort(request.getParameter("returnState")); BigDecimal returnSum = new BigDecimal(request.getParameter("returnSum")); Long issuerSerialNumber = authService.getCertificateInfo().getSerialNumber().longValue(); String issuerIp = authService.getClientIp(request); overpaymentService.saveReturnState(bankStatementId, returnState, returnSum, issuerSerialNumber, issuerIp); resMap.put("success", true); } catch (Exception e) { logger.error(e.getMessage()); resMap.put("success", false); resMap.put("reason", e.getMessage()); } return resMap; } @RequestMapping(value = "/return-text-file", method = RequestMethod.POST) public void returnTextFile(HttpServletRequest request, HttpServletResponse response) throws Exception { String fileName; String fileContent; Long issuerSerialNumber = authService.getCertificateInfo().getSerialNumber().longValue(); String issuerIp = authService.getClientIp(request); if ("single".equals(request.getParameter("type"))) { fileName = "Vozvrat - " + new SimpleDateFormat("dd.MM.yyyy HH-mm-ss").format(Calendar.getInstance().getTime()); String bankStatementId = request.getParameter("bankStatementId"); fileContent = overpaymentService.singleReturnBankStatementText(bankStatementId, issuerSerialNumber, issuerIp); } else { fileName = "Vozvrati_v_den - " + request.getParameter("paymentDate"); Date paymentDate = new SimpleDateFormat("dd.MM.yyyy").parse(request.getParameter("paymentDate")); fileContent = overpaymentService.allReturnBankStatementText(paymentDate, issuerSerialNumber, issuerIp); } response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + ".txt\""); response.addHeader("Cache-Control", "max-age=1, must-revalidate"); response.addHeader("Pragma", "no-cache"); ServletOutputStream stream = response.getOutputStream(); stream.write(fileContent.getBytes()); stream.close(); } }