| 1586 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
| 1631 |
ankur.sing |
3 |
import in.shop2020.support.services.PaymentDetailsGenerator;
|
| 1677 |
ankur.sing |
4 |
import in.shop2020.thrift.clients.HelperServiceClient;
|
|
|
5 |
import in.shop2020.utils.StatisticsUser;
|
| 1586 |
ankur.sing |
6 |
|
|
|
7 |
import java.io.ByteArrayOutputStream;
|
|
|
8 |
import java.io.IOException;
|
|
|
9 |
import java.text.DateFormat;
|
|
|
10 |
import java.text.ParseException;
|
|
|
11 |
import java.text.SimpleDateFormat;
|
|
|
12 |
import java.util.Calendar;
|
|
|
13 |
import java.util.Date;
|
|
|
14 |
|
|
|
15 |
import javax.servlet.ServletOutputStream;
|
|
|
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
| 1677 |
ankur.sing |
18 |
import javax.servlet.http.HttpSession;
|
| 1586 |
ankur.sing |
19 |
|
|
|
20 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
21 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
22 |
|
| 1677 |
ankur.sing |
23 |
public class PaymentDetailsController implements ServletResponseAware, ServletRequestAware {
|
| 1586 |
ankur.sing |
24 |
|
| 1677 |
ankur.sing |
25 |
private HttpServletRequest request;
|
|
|
26 |
private HttpServletResponse response;
|
|
|
27 |
private HttpSession session;
|
| 1631 |
ankur.sing |
28 |
|
| 1677 |
ankur.sing |
29 |
private String errorMsg = "";
|
|
|
30 |
private final String authsuccess = "authsuccess";
|
|
|
31 |
private final String authfail = "authfail";
|
|
|
32 |
public PaymentDetailsController() {
|
| 1631 |
ankur.sing |
33 |
|
| 1677 |
ankur.sing |
34 |
}
|
| 1631 |
ankur.sing |
35 |
|
| 1677 |
ankur.sing |
36 |
@Override
|
|
|
37 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
38 |
this.request = req;
|
|
|
39 |
this.session = req.getSession();
|
|
|
40 |
}
|
| 1631 |
ankur.sing |
41 |
|
| 1677 |
ankur.sing |
42 |
@Override
|
|
|
43 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
44 |
this.response = res;
|
|
|
45 |
}
|
| 1586 |
ankur.sing |
46 |
|
| 1677 |
ankur.sing |
47 |
public String index() {
|
|
|
48 |
if (getSessionUserName() == null) {
|
|
|
49 |
return authfail;
|
|
|
50 |
} else {
|
|
|
51 |
return authsuccess;
|
|
|
52 |
}
|
|
|
53 |
}
|
| 1631 |
ankur.sing |
54 |
|
| 1677 |
ankur.sing |
55 |
// Handles the POST request (Form Submission)
|
|
|
56 |
public String create() {
|
|
|
57 |
String username = request.getParameter("username");
|
|
|
58 |
String password = request.getParameter("password");
|
|
|
59 |
if(username != null && password != null) {
|
|
|
60 |
try{
|
|
|
61 |
HelperServiceClient hsc = new HelperServiceClient();
|
|
|
62 |
in.shop2020.utils.HelperService.Client client = hsc.getClient();
|
|
|
63 |
StatisticsUser user = client.authenticateStatisticsUser(username, password);
|
|
|
64 |
session.setAttribute("username", user.getUsername());
|
|
|
65 |
return authsuccess;
|
|
|
66 |
}catch(Exception e){
|
|
|
67 |
e.printStackTrace();
|
|
|
68 |
return authfail;
|
|
|
69 |
}
|
|
|
70 |
}
|
|
|
71 |
if (getSessionUserName() == null) {
|
|
|
72 |
return authfail;
|
|
|
73 |
}
|
|
|
74 |
|
|
|
75 |
// Formatting Form input parameters
|
|
|
76 |
String startDateStr = request.getParameter("startDate");
|
|
|
77 |
String endDateStr = request.getParameter("endDate");
|
| 1881 |
ankur.sing |
78 |
String statusStr = request.getParameter("status");
|
|
|
79 |
String filenameStr = "";
|
|
|
80 |
int status = 1;
|
| 1677 |
ankur.sing |
81 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
82 |
Date startDate = null, endDate = null;
|
|
|
83 |
try {
|
|
|
84 |
startDate = df.parse(startDateStr);
|
|
|
85 |
endDate = df.parse(endDateStr);
|
|
|
86 |
Calendar cal = Calendar.getInstance();
|
|
|
87 |
cal.setTime(endDate);
|
|
|
88 |
cal.add(Calendar.DATE, 1);
|
|
|
89 |
endDate.setTime(cal.getTimeInMillis());
|
| 1881 |
ankur.sing |
90 |
status = Integer.parseInt(statusStr);
|
|
|
91 |
filenameStr = (status == 0 ? "success" : "failed");
|
| 1677 |
ankur.sing |
92 |
} catch (ParseException pe) {
|
|
|
93 |
errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
|
|
|
94 |
return authsuccess;
|
| 1881 |
ankur.sing |
95 |
} catch (NumberFormatException nfe) {
|
|
|
96 |
errorMsg = "Please select payment status";
|
|
|
97 |
return authsuccess;
|
| 1677 |
ankur.sing |
98 |
}
|
| 1631 |
ankur.sing |
99 |
|
| 1677 |
ankur.sing |
100 |
PaymentDetailsGenerator paymentDetailGenerator = new PaymentDetailsGenerator();
|
| 1881 |
ankur.sing |
101 |
ByteArrayOutputStream baos = paymentDetailGenerator.generatePaymentDetailsReport(startDate, endDate, status);
|
| 1631 |
ankur.sing |
102 |
|
| 1677 |
ankur.sing |
103 |
if (baos == null) {
|
|
|
104 |
errorMsg = "No output for given date range";
|
|
|
105 |
return authsuccess;
|
|
|
106 |
} else {
|
|
|
107 |
errorMsg = "";
|
|
|
108 |
}
|
| 1586 |
ankur.sing |
109 |
|
| 1677 |
ankur.sing |
110 |
// Preparing XLS file for output
|
|
|
111 |
response.setContentType("application/vnd.ms-excel");
|
| 1881 |
ankur.sing |
112 |
response.setHeader("Content-disposition", "inline; filename=payments-report-" + filenameStr + ".xls");
|
| 1677 |
ankur.sing |
113 |
ServletOutputStream sos;
|
|
|
114 |
try {
|
|
|
115 |
sos = response.getOutputStream();
|
|
|
116 |
baos.writeTo(sos);
|
|
|
117 |
sos.flush();
|
|
|
118 |
} catch (IOException e) {
|
|
|
119 |
errorMsg = "Failed to write to response.";
|
|
|
120 |
e.printStackTrace();
|
|
|
121 |
}
|
|
|
122 |
return authsuccess;
|
|
|
123 |
}
|
| 1586 |
ankur.sing |
124 |
|
| 1677 |
ankur.sing |
125 |
public String getErrorMsg() {
|
|
|
126 |
return errorMsg;
|
|
|
127 |
}
|
| 1586 |
ankur.sing |
128 |
|
| 1677 |
ankur.sing |
129 |
public String getSessionUserName() {
|
|
|
130 |
return (String) session.getAttribute("username");
|
|
|
131 |
}
|
| 1586 |
ankur.sing |
132 |
}
|