| 1676 |
ankur.sing |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.support.services.RegisteredUsersGenerator;
|
| 1941 |
ankur.sing |
4 |
import in.shop2020.support.utils.ReportsUtils;
|
| 1676 |
ankur.sing |
5 |
|
|
|
6 |
import java.io.ByteArrayOutputStream;
|
|
|
7 |
import java.io.IOException;
|
| 4817 |
anupam.sin |
8 |
import java.text.DateFormat;
|
|
|
9 |
import java.text.ParseException;
|
|
|
10 |
import java.text.SimpleDateFormat;
|
|
|
11 |
import java.util.Date;
|
|
|
12 |
import java.util.TimeZone;
|
| 1676 |
ankur.sing |
13 |
|
| 1941 |
ankur.sing |
14 |
import javax.servlet.ServletContext;
|
| 1676 |
ankur.sing |
15 |
import javax.servlet.ServletOutputStream;
|
| 1941 |
ankur.sing |
16 |
import javax.servlet.http.HttpServletRequest;
|
|
|
17 |
import javax.servlet.http.HttpServletResponse;
|
|
|
18 |
import javax.servlet.http.HttpSession;
|
| 1676 |
ankur.sing |
19 |
|
| 1891 |
ankur.sing |
20 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
21 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3936 |
chandransh |
22 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
23 |
import org.apache.struts2.convention.annotation.Results;
|
| 1941 |
ankur.sing |
24 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
25 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
26 |
import org.apache.struts2.util.ServletContextAware;
|
| 3105 |
chandransh |
27 |
import org.slf4j.Logger;
|
|
|
28 |
import org.slf4j.LoggerFactory;
|
| 1676 |
ankur.sing |
29 |
|
| 4600 |
varun.gupt |
30 |
//
|
|
|
31 |
//@InterceptorRefs({
|
|
|
32 |
// @InterceptorRef("defaultStack"),
|
|
|
33 |
// @InterceptorRef("login")
|
|
|
34 |
//})
|
|
|
35 |
//@Results({
|
|
|
36 |
// @Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
37 |
//})
|
| 1941 |
ankur.sing |
38 |
public class RegisteredUsersController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
|
| 3105 |
chandransh |
39 |
|
|
|
40 |
private static Logger logger = LoggerFactory.getLogger(RegisteredUsersController.class);
|
| 1891 |
ankur.sing |
41 |
|
| 1941 |
ankur.sing |
42 |
private HttpServletRequest request;
|
|
|
43 |
private HttpSession session;
|
|
|
44 |
private HttpServletResponse response;
|
|
|
45 |
private ServletContext context;
|
|
|
46 |
|
| 1676 |
ankur.sing |
47 |
private final String authsuccess = "authsuccess";
|
|
|
48 |
private String message = "";
|
| 4817 |
anupam.sin |
49 |
|
|
|
50 |
private Date endDate;
|
|
|
51 |
private Date startDate;
|
|
|
52 |
|
|
|
53 |
private String errorMsg;
|
| 1676 |
ankur.sing |
54 |
|
|
|
55 |
public RegisteredUsersController() {
|
|
|
56 |
|
|
|
57 |
}
|
|
|
58 |
|
|
|
59 |
public String index() {
|
| 4600 |
varun.gupt |
60 |
// if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
61 |
// return "authfail";
|
|
|
62 |
// }
|
| 1891 |
ankur.sing |
63 |
return authsuccess;
|
| 1676 |
ankur.sing |
64 |
}
|
|
|
65 |
|
|
|
66 |
// Handles the POST request (Form Submission)
|
|
|
67 |
public String create() {
|
| 4817 |
anupam.sin |
68 |
String startDateStr = request.getParameter("startDate");
|
|
|
69 |
String endDateStr = request.getParameter("endDate");
|
|
|
70 |
|
|
|
71 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
72 |
df.setTimeZone(TimeZone.getTimeZone("IST"));
|
|
|
73 |
try {
|
|
|
74 |
startDate = df.parse(startDateStr);
|
|
|
75 |
endDate = df.parse(endDateStr);
|
|
|
76 |
} catch (ParseException pe) {
|
|
|
77 |
errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
|
|
|
78 |
return authsuccess;
|
|
|
79 |
}
|
|
|
80 |
|
|
|
81 |
|
| 1676 |
ankur.sing |
82 |
RegisteredUsersGenerator usersReportGenerator = new RegisteredUsersGenerator();
|
| 4817 |
anupam.sin |
83 |
ByteArrayOutputStream baos = usersReportGenerator.generateRegisteredUsersReport(startDate.getTime(), endDate.getTime());
|
| 1676 |
ankur.sing |
84 |
|
|
|
85 |
if(baos == null) {
|
| 4600 |
varun.gupt |
86 |
message = "No registered user exists.";
|
| 1676 |
ankur.sing |
87 |
return authsuccess;
|
|
|
88 |
}
|
|
|
89 |
// Preparing XLS file for output
|
| 1941 |
ankur.sing |
90 |
response.setContentType("application/vnd.ms-excel");
|
| 4817 |
anupam.sin |
91 |
response.setHeader("Content-disposition", "inline; filename=registered-users" + ".xls");
|
| 1676 |
ankur.sing |
92 |
ServletOutputStream sos;
|
|
|
93 |
try {
|
| 1941 |
ankur.sing |
94 |
sos = response.getOutputStream();
|
| 1676 |
ankur.sing |
95 |
baos.writeTo(sos);
|
|
|
96 |
sos.flush();
|
|
|
97 |
} catch (IOException e) {
|
| 3105 |
chandransh |
98 |
logger.error("Error streaming registered users report");
|
| 1676 |
ankur.sing |
99 |
}
|
|
|
100 |
return authsuccess;
|
|
|
101 |
}
|
|
|
102 |
|
|
|
103 |
public String getMessage() {
|
|
|
104 |
return message;
|
|
|
105 |
}
|
| 1941 |
ankur.sing |
106 |
|
|
|
107 |
@Override
|
|
|
108 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
109 |
this.request = req;
|
|
|
110 |
this.session = req.getSession();
|
|
|
111 |
}
|
|
|
112 |
|
|
|
113 |
@Override
|
|
|
114 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
115 |
this.response = res;
|
|
|
116 |
}
|
|
|
117 |
|
|
|
118 |
@Override
|
|
|
119 |
public void setServletContext(ServletContext context) {
|
|
|
120 |
this.context = context;
|
|
|
121 |
}
|
|
|
122 |
|
|
|
123 |
public String getServletContextPath() {
|
|
|
124 |
return context.getContextPath();
|
|
|
125 |
}
|
| 4817 |
anupam.sin |
126 |
|
|
|
127 |
public void setStartDate(Date startDate) {
|
|
|
128 |
this.startDate = startDate;
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
public String getErrorMsg() {
|
|
|
132 |
return errorMsg;
|
|
|
133 |
}
|
|
|
134 |
|
|
|
135 |
public void setErrorMsg(String errorMsg) {
|
|
|
136 |
this.errorMsg = errorMsg;
|
|
|
137 |
}
|
| 1676 |
ankur.sing |
138 |
}
|