Rev 1632 | Rev 1891 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.support.services.PaymentDetailsGenerator;import in.shop2020.thrift.clients.HelperServiceClient;import in.shop2020.utils.StatisticsUser;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.text.DateFormat;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;public class PaymentDetailsController implements ServletResponseAware, ServletRequestAware {private HttpServletRequest request;private HttpServletResponse response;private HttpSession session;private String errorMsg = "";private final String authsuccess = "authsuccess";private final String authfail = "authfail";public PaymentDetailsController() {}@Overridepublic void setServletRequest(HttpServletRequest req) {this.request = req;this.session = req.getSession();}@Overridepublic void setServletResponse(HttpServletResponse res) {this.response = res;}public String index() {if (getSessionUserName() == null) {return authfail;} else {return authsuccess;}}// Handles the POST request (Form Submission)public String create() {String username = request.getParameter("username");String password = request.getParameter("password");if(username != null && password != null) {try{HelperServiceClient hsc = new HelperServiceClient();in.shop2020.utils.HelperService.Client client = hsc.getClient();StatisticsUser user = client.authenticateStatisticsUser(username, password);session.setAttribute("username", user.getUsername());return authsuccess;}catch(Exception e){e.printStackTrace();return authfail;}}if (getSessionUserName() == null) {return authfail;}// Formatting Form input parametersString startDateStr = request.getParameter("startDate");String endDateStr = request.getParameter("endDate");DateFormat df = new SimpleDateFormat("MM/dd/yyyy");Date startDate = null, endDate = null;try {startDate = df.parse(startDateStr);endDate = df.parse(endDateStr);Calendar cal = Calendar.getInstance();cal.setTime(endDate);cal.add(Calendar.DATE, 1);endDate.setTime(cal.getTimeInMillis());} catch (ParseException pe) {errorMsg = "Please enter start and end dates in format MM/dd/yyyy";return authsuccess;}PaymentDetailsGenerator paymentDetailGenerator = new PaymentDetailsGenerator();ByteArrayOutputStream baos = paymentDetailGenerator.generatePaymentDetailsReport(startDate, endDate);if (baos == null) {errorMsg = "No output for given date range";return authsuccess;} else {errorMsg = "";}// Preparing XLS file for outputresponse.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "inline; filename=payments-report" + ".xls");ServletOutputStream sos;try {sos = response.getOutputStream();baos.writeTo(sos);sos.flush();} catch (IOException e) {errorMsg = "Failed to write to response.";e.printStackTrace();}return authsuccess;}public String getErrorMsg() {return errorMsg;}public String getSessionUserName() {return (String) session.getAttribute("username");}}