| 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;
|
| 5623 |
anupam.sin |
5 |
import in.shop2020.thrift.clients.UserClient;
|
| 1676 |
ankur.sing |
6 |
|
|
|
7 |
import java.io.ByteArrayOutputStream;
|
|
|
8 |
import java.io.IOException;
|
| 5623 |
anupam.sin |
9 |
import java.io.OutputStream;
|
| 4817 |
anupam.sin |
10 |
import java.text.DateFormat;
|
|
|
11 |
import java.text.ParseException;
|
|
|
12 |
import java.text.SimpleDateFormat;
|
| 5623 |
anupam.sin |
13 |
import java.util.ArrayList;
|
| 4817 |
anupam.sin |
14 |
import java.util.Date;
|
| 5623 |
anupam.sin |
15 |
import java.util.List;
|
| 4817 |
anupam.sin |
16 |
import java.util.TimeZone;
|
| 1676 |
ankur.sing |
17 |
|
| 1941 |
ankur.sing |
18 |
import javax.servlet.ServletContext;
|
| 1676 |
ankur.sing |
19 |
import javax.servlet.ServletOutputStream;
|
| 1941 |
ankur.sing |
20 |
import javax.servlet.http.HttpServletRequest;
|
|
|
21 |
import javax.servlet.http.HttpServletResponse;
|
|
|
22 |
import javax.servlet.http.HttpSession;
|
| 1676 |
ankur.sing |
23 |
|
| 1891 |
ankur.sing |
24 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
25 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3936 |
chandransh |
26 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
27 |
import org.apache.struts2.convention.annotation.Results;
|
| 1941 |
ankur.sing |
28 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
29 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
30 |
import org.apache.struts2.util.ServletContextAware;
|
| 3105 |
chandransh |
31 |
import org.slf4j.Logger;
|
|
|
32 |
import org.slf4j.LoggerFactory;
|
| 1676 |
ankur.sing |
33 |
|
| 5623 |
anupam.sin |
34 |
@InterceptorRefs({
|
|
|
35 |
@InterceptorRef("defaultStack"),
|
|
|
36 |
@InterceptorRef("login")
|
|
|
37 |
})
|
|
|
38 |
@Results({
|
|
|
39 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
40 |
})
|
| 1941 |
ankur.sing |
41 |
public class RegisteredUsersController implements ServletRequestAware, ServletResponseAware, ServletContextAware {
|
| 3105 |
chandransh |
42 |
|
|
|
43 |
private static Logger logger = LoggerFactory.getLogger(RegisteredUsersController.class);
|
| 1891 |
ankur.sing |
44 |
|
| 1941 |
ankur.sing |
45 |
private HttpServletRequest request;
|
|
|
46 |
private HttpSession session;
|
|
|
47 |
private HttpServletResponse response;
|
|
|
48 |
private ServletContext context;
|
|
|
49 |
|
| 1676 |
ankur.sing |
50 |
private final String authsuccess = "authsuccess";
|
|
|
51 |
private String message = "";
|
| 4817 |
anupam.sin |
52 |
|
|
|
53 |
private Date endDate;
|
|
|
54 |
private Date startDate;
|
|
|
55 |
|
|
|
56 |
private String errorMsg;
|
| 1676 |
ankur.sing |
57 |
|
|
|
58 |
public RegisteredUsersController() {
|
|
|
59 |
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
public String index() {
|
| 5623 |
anupam.sin |
63 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
|
|
64 |
return "authfail";
|
|
|
65 |
}
|
| 1891 |
ankur.sing |
66 |
return authsuccess;
|
| 1676 |
ankur.sing |
67 |
}
|
|
|
68 |
|
|
|
69 |
// Handles the POST request (Form Submission)
|
|
|
70 |
public String create() {
|
| 4817 |
anupam.sin |
71 |
String startDateStr = request.getParameter("startDate");
|
|
|
72 |
String endDateStr = request.getParameter("endDate");
|
|
|
73 |
|
|
|
74 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
75 |
df.setTimeZone(TimeZone.getTimeZone("IST"));
|
|
|
76 |
try {
|
|
|
77 |
startDate = df.parse(startDateStr);
|
|
|
78 |
endDate = df.parse(endDateStr);
|
|
|
79 |
} catch (ParseException pe) {
|
|
|
80 |
errorMsg = "Please enter start and end dates in format MM/dd/yyyy";
|
|
|
81 |
return authsuccess;
|
|
|
82 |
}
|
| 5623 |
anupam.sin |
83 |
|
|
|
84 |
UserClient usc;
|
|
|
85 |
in.shop2020.model.v1.user.UserContextService.Client uClient;
|
| 4817 |
anupam.sin |
86 |
|
| 5623 |
anupam.sin |
87 |
List<String> emails = new ArrayList<String>();
|
| 4817 |
anupam.sin |
88 |
|
| 5623 |
anupam.sin |
89 |
try {
|
|
|
90 |
usc = new UserClient();
|
|
|
91 |
uClient = usc.getClient();
|
|
|
92 |
|
|
|
93 |
emails = uClient.getUserEmails(startDate.getTime(), endDate.getTime());
|
|
|
94 |
} catch (Exception e) {
|
|
|
95 |
logger.error("Error initializing connection to user or order service", e);
|
| 1676 |
ankur.sing |
96 |
}
|
| 5623 |
anupam.sin |
97 |
|
| 1676 |
ankur.sing |
98 |
// Preparing XLS file for output
|
| 5623 |
anupam.sin |
99 |
response.setContentType("application/vnd.ms-excel");
|
|
|
100 |
response.setHeader("Content-disposition", "attachment; filename=email-addresses.xls");
|
|
|
101 |
|
| 1676 |
ankur.sing |
102 |
ServletOutputStream sos;
|
|
|
103 |
try {
|
| 5623 |
anupam.sin |
104 |
StringBuilder builder = new StringBuilder();
|
|
|
105 |
|
|
|
106 |
for (String email: emails) {
|
|
|
107 |
builder.append(email);
|
|
|
108 |
builder.append("\n");
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
sos = response.getOutputStream();
|
|
|
112 |
sos.write(builder.toString().getBytes());
|
|
|
113 |
sos.flush();
|
|
|
114 |
sos.close();
|
| 1676 |
ankur.sing |
115 |
} catch (IOException e) {
|
| 3105 |
chandransh |
116 |
logger.error("Error streaming registered users report");
|
| 1676 |
ankur.sing |
117 |
}
|
|
|
118 |
return authsuccess;
|
|
|
119 |
}
|
|
|
120 |
|
|
|
121 |
public String getMessage() {
|
|
|
122 |
return message;
|
|
|
123 |
}
|
| 1941 |
ankur.sing |
124 |
|
|
|
125 |
@Override
|
|
|
126 |
public void setServletRequest(HttpServletRequest req) {
|
|
|
127 |
this.request = req;
|
|
|
128 |
this.session = req.getSession();
|
|
|
129 |
}
|
|
|
130 |
|
|
|
131 |
@Override
|
|
|
132 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
133 |
this.response = res;
|
|
|
134 |
}
|
|
|
135 |
|
|
|
136 |
@Override
|
|
|
137 |
public void setServletContext(ServletContext context) {
|
|
|
138 |
this.context = context;
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
public String getServletContextPath() {
|
|
|
142 |
return context.getContextPath();
|
|
|
143 |
}
|
| 4817 |
anupam.sin |
144 |
|
|
|
145 |
public void setStartDate(Date startDate) {
|
|
|
146 |
this.startDate = startDate;
|
|
|
147 |
}
|
|
|
148 |
|
|
|
149 |
public String getErrorMsg() {
|
|
|
150 |
return errorMsg;
|
|
|
151 |
}
|
|
|
152 |
|
|
|
153 |
public void setErrorMsg(String errorMsg) {
|
|
|
154 |
this.errorMsg = errorMsg;
|
|
|
155 |
}
|
| 5623 |
anupam.sin |
156 |
}
|