| 4141 |
chandransh |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.support.services.PaymentDetailsGenerator;
|
|
|
4 |
import in.shop2020.support.utils.ReportsUtils;
|
|
|
5 |
|
|
|
6 |
import java.io.ByteArrayOutputStream;
|
|
|
7 |
import java.io.IOException;
|
|
|
8 |
import java.text.DateFormat;
|
|
|
9 |
import java.text.ParseException;
|
|
|
10 |
import java.text.SimpleDateFormat;
|
|
|
11 |
import java.util.Calendar;
|
|
|
12 |
import java.util.Date;
|
|
|
13 |
|
|
|
14 |
import javax.servlet.ServletContext;
|
|
|
15 |
import javax.servlet.ServletOutputStream;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
|
|
18 |
import javax.servlet.http.HttpSession;
|
|
|
19 |
|
|
|
20 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
21 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
22 |
import org.apache.struts2.util.ServletContextAware;
|
|
|
23 |
import org.slf4j.Logger;
|
|
|
24 |
import org.slf4j.LoggerFactory;
|
|
|
25 |
|
|
|
26 |
public class PaymentReconciliationController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
|
|
|
27 |
|
|
|
28 |
private static Logger logger = LoggerFactory.getLogger(PaymentReconciliationController.class);
|
|
|
29 |
|
|
|
30 |
private HttpServletRequest request;
|
|
|
31 |
private HttpSession session;
|
|
|
32 |
private HttpServletResponse response;
|
|
|
33 |
private ServletContext context;
|
|
|
34 |
|
|
|
35 |
private String errorMsg = "";
|
|
|
36 |
private final String authsuccess = "authsuccess";
|
|
|
37 |
public PaymentReconciliationController() {
|
|
|
38 |
|
|
|
39 |
}
|
|
|
40 |
|
|
|
41 |
public String index() {
|
|
|
42 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
43 |
return "authfail";
|
|
|
44 |
}
|
|
|
45 |
return authsuccess;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
// Handles the POST request (Form Submission)
|
|
|
49 |
public String create() {
|
|
|
50 |
// Formatting Form input parameters
|
|
|
51 |
String startDateStr = request.getParameter("startDate");
|
|
|
52 |
String endDateStr = request.getParameter("endDate");
|
|
|
53 |
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
54 |
Date startDate = null, endDate = null;
|
|
|
55 |
try {
|
|
|
56 |
startDate = df.parse(startDateStr);
|
|
|
57 |
endDate = df.parse(endDateStr);
|
|
|
58 |
Calendar cal = Calendar.getInstance();
|
|
|
59 |
cal.setTime(endDate);
|
|
|
60 |
cal.add(Calendar.DATE, 1);
|
|
|
61 |
endDate.setTime(cal.getTimeInMillis());
|
|
|
62 |
} catch (ParseException pe) {
|
|
|
63 |
errorMsg = "Please enter start and end dates in format dd/MM/yyyy";
|
|
|
64 |
return authsuccess;
|
|
|
65 |
} catch (NumberFormatException nfe) {
|
|
|
66 |
errorMsg = "Please select payment status";
|
|
|
67 |
return authsuccess;
|
|
|
68 |
}
|
|
|
69 |
|
|
|
70 |
PaymentDetailsGenerator paymentDetailGenerator = new PaymentDetailsGenerator();
|
|
|
71 |
ByteArrayOutputStream baos = paymentDetailGenerator.generatePaymentReconciliationReport(startDate, endDate);
|
|
|
72 |
|
|
|
73 |
if (baos == null) {
|
|
|
74 |
errorMsg = "No payments were captured in the given date range";
|
|
|
75 |
return authsuccess;
|
|
|
76 |
} else {
|
|
|
77 |
errorMsg = "Generating report...";
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
// Preparing XLS file for output
|
|
|
81 |
DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
|
|
|
82 |
response.setContentType("application/vnd.ms-excel");
|
|
|
83 |
response.setHeader("Content-disposition", "inline; filename=payments-reconciliation-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
|
|
|
84 |
ServletOutputStream sos;
|
|
|
85 |
try {
|
|
|
86 |
sos = response.getOutputStream();
|
|
|
87 |
baos.writeTo(sos);
|
|
|
88 |
sos.flush();
|
|
|
89 |
errorMsg = "Report generated";
|
|
|
90 |
} catch (IOException e) {
|
|
|
91 |
errorMsg = "Failed to write to response.";
|
|
|
92 |
logger.error("Unable to stream the payment details report", e);
|
|
|
93 |
}
|
|
|
94 |
return authsuccess;
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
public String getErrorMsg() {
|
|
|
98 |
return errorMsg;
|
|
|
99 |
}
|
|
|
100 |
|
|
|
101 |
@Override
|
|
|
102 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
103 |
this.request = req;
|
|
|
104 |
this.session = req.getSession();
|
|
|
105 |
}
|
|
|
106 |
|
|
|
107 |
@Override
|
|
|
108 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
109 |
this.response = res;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
@Override
|
|
|
113 |
public void setServletContext(ServletContext context) {
|
|
|
114 |
this.context = context;
|
|
|
115 |
}
|
|
|
116 |
|
|
|
117 |
public String getServletContextPath() {
|
|
|
118 |
return context.getContextPath();
|
|
|
119 |
}
|
|
|
120 |
}
|