| 1738 |
vikas |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
|
|
3 |
import in.shop2020.model.v1.user.User;
|
|
|
4 |
import in.shop2020.model.v1.user.UserCommunication;
|
| 3214 |
chandransh |
5 |
import in.shop2020.model.v1.user.UserCommunicationException;
|
| 1738 |
vikas |
6 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
7 |
import in.shop2020.model.v1.user.UserContextService.Client;
|
| 3125 |
rajveer |
8 |
import in.shop2020.thrift.clients.UserClient;
|
| 1738 |
vikas |
9 |
|
|
|
10 |
import java.io.ByteArrayOutputStream;
|
|
|
11 |
import java.io.IOException;
|
|
|
12 |
import java.util.Date;
|
|
|
13 |
import java.util.List;
|
|
|
14 |
|
|
|
15 |
import javax.servlet.ServletOutputStream;
|
|
|
16 |
import javax.servlet.http.HttpServletResponse;
|
|
|
17 |
|
|
|
18 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
19 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
20 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
21 |
import org.apache.poi.ss.usermodel.CreationHelper;
|
|
|
22 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
23 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
24 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
25 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
26 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
27 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
28 |
import org.apache.thrift.TException;
|
| 3214 |
chandransh |
29 |
import org.slf4j.Logger;
|
|
|
30 |
import org.slf4j.LoggerFactory;
|
| 1738 |
vikas |
31 |
|
| 1744 |
vikas |
32 |
public class UserCommunicationsController implements ServletResponseAware{
|
| 3214 |
chandransh |
33 |
|
|
|
34 |
private static Logger logger = LoggerFactory.getLogger(UserCommunicationsController.class);
|
| 1738 |
vikas |
35 |
|
|
|
36 |
private HttpServletResponse response;
|
|
|
37 |
|
|
|
38 |
private String errorMsg = "";
|
|
|
39 |
|
|
|
40 |
@Override
|
|
|
41 |
public void setServletResponse(HttpServletResponse res) {
|
|
|
42 |
this.response = res;
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
public String index() {
|
|
|
46 |
try {
|
| 3125 |
rajveer |
47 |
UserClient userContextServiceClient = new UserClient();
|
| 1738 |
vikas |
48 |
Client userClient = userContextServiceClient.getClient();
|
|
|
49 |
|
|
|
50 |
//Retrieving all user communications
|
|
|
51 |
List<UserCommunication> allCommunications = userClient.getAllUserCommunications();
|
|
|
52 |
|
|
|
53 |
// Preparing XLS file for output
|
|
|
54 |
response.setContentType("application/vnd.ms-excel");
|
|
|
55 |
|
|
|
56 |
response.setHeader("Content-disposition", "inline; filename=user-communications" + ".xls");
|
|
|
57 |
|
|
|
58 |
ServletOutputStream sos;
|
|
|
59 |
try {
|
|
|
60 |
ByteArrayOutputStream baos = getSpreadSheetData(allCommunications, userClient);
|
|
|
61 |
sos = response.getOutputStream();
|
|
|
62 |
baos.writeTo(sos);
|
|
|
63 |
sos.flush();
|
|
|
64 |
} catch (IOException e) {
|
| 3214 |
chandransh |
65 |
logger.error("Unable to stream the user communications report");
|
| 1738 |
vikas |
66 |
errorMsg = "Failed to write to response.";
|
|
|
67 |
}
|
|
|
68 |
|
| 3214 |
chandransh |
69 |
} catch (UserCommunicationException e) {
|
|
|
70 |
logger.error("Error while getting user communications", e);
|
| 1738 |
vikas |
71 |
errorMsg = e.getMessage();
|
| 3214 |
chandransh |
72 |
} catch (TException e) {
|
|
|
73 |
logger.error("Unable to get user communications", e);
|
|
|
74 |
errorMsg = e.getMessage();
|
|
|
75 |
} catch (Exception e) {
|
|
|
76 |
logger.error("Unexpected exception", e);
|
|
|
77 |
errorMsg = e.getMessage();
|
| 1738 |
vikas |
78 |
}
|
|
|
79 |
return null;
|
|
|
80 |
}
|
|
|
81 |
|
|
|
82 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
|
|
83 |
private ByteArrayOutputStream getSpreadSheetData(List<UserCommunication> allCommunications,
|
|
|
84 |
Client userClient)
|
|
|
85 |
{
|
|
|
86 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
87 |
|
|
|
88 |
Workbook wb = new HSSFWorkbook();
|
|
|
89 |
|
|
|
90 |
Font font = wb.createFont();
|
|
|
91 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
92 |
CellStyle style = wb.createCellStyle();
|
|
|
93 |
style.setFont(font);
|
|
|
94 |
|
|
|
95 |
CreationHelper createHelper = wb.getCreationHelper();
|
|
|
96 |
CellStyle dateCellStyle = wb.createCellStyle();
|
|
|
97 |
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
|
|
|
98 |
|
|
|
99 |
createAllUserCommunicationSheet(allCommunications, userClient, wb, style, dateCellStyle);
|
|
|
100 |
|
|
|
101 |
// Write the workbook to the output stream
|
|
|
102 |
try {
|
|
|
103 |
wb.write(baosXLS);
|
|
|
104 |
baosXLS.close();
|
|
|
105 |
} catch (IOException e) {
|
| 3214 |
chandransh |
106 |
logger.error("Error while converting the user communications report to a byte array", e);
|
| 1738 |
vikas |
107 |
}
|
|
|
108 |
return baosXLS;
|
|
|
109 |
}
|
|
|
110 |
|
|
|
111 |
private void createAllUserCommunicationSheet(
|
|
|
112 |
List<UserCommunication> allCommunications,
|
|
|
113 |
Client userClient,
|
|
|
114 |
Workbook wb,
|
|
|
115 |
CellStyle style, CellStyle dateCellStyle)
|
|
|
116 |
{
|
|
|
117 |
// USER COMMUNICATION SHEET
|
|
|
118 |
Sheet commSheet = wb.createSheet("All Users Communications");
|
|
|
119 |
short commSerialNo = 0;
|
|
|
120 |
|
|
|
121 |
Row commTitleRow = commSheet.createRow(commSerialNo ++);
|
|
|
122 |
Cell commTitleCell = commTitleRow.createCell(0);
|
|
|
123 |
commTitleCell.setCellValue("All Users Communications");
|
|
|
124 |
commTitleCell.setCellStyle(style);
|
|
|
125 |
|
|
|
126 |
commSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
127 |
|
|
|
128 |
commSheet.createRow(commSerialNo ++);
|
|
|
129 |
|
|
|
130 |
Row commHeaderRow = commSheet.createRow(commSerialNo++);
|
|
|
131 |
commHeaderRow.createCell(0).setCellValue("User Email");
|
|
|
132 |
commHeaderRow.createCell(1).setCellValue("User Name");
|
|
|
133 |
commHeaderRow.createCell(2).setCellValue("Communication Email");
|
|
|
134 |
commHeaderRow.createCell(3).setCellValue("Date of Birth");
|
|
|
135 |
commHeaderRow.createCell(4).setCellValue("Mobile Number");
|
|
|
136 |
commHeaderRow.createCell(5).setCellValue("User Id");
|
|
|
137 |
commHeaderRow.createCell(6).setCellValue("Sex");
|
|
|
138 |
commHeaderRow.createCell(7).setCellValue("Order Id");
|
|
|
139 |
commHeaderRow.createCell(8).setCellValue("Communication Type");
|
|
|
140 |
commHeaderRow.createCell(9).setCellValue("AirwayBill No");
|
|
|
141 |
commHeaderRow.createCell(10).setCellValue("TimeStamp");
|
|
|
142 |
commHeaderRow.createCell(11).setCellValue("Product Name");
|
|
|
143 |
commHeaderRow.createCell(12).setCellValue("Reply To");
|
|
|
144 |
commHeaderRow.createCell(13).setCellValue("Subject");
|
|
|
145 |
commHeaderRow.createCell(14).setCellValue("Message");
|
|
|
146 |
for (int i=0; i<15 ;i++) {
|
|
|
147 |
commHeaderRow.getCell(i).setCellStyle(style);
|
|
|
148 |
}
|
|
|
149 |
|
|
|
150 |
for( UserCommunication userComm : allCommunications) {
|
|
|
151 |
commSerialNo++;
|
|
|
152 |
Row commContentRow = commSheet.createRow(commSerialNo);
|
|
|
153 |
|
|
|
154 |
try {
|
|
|
155 |
User user = userClient.getUserById(userComm.getUserId());
|
| 1790 |
vikas |
156 |
if (user.getUserId() != -1) {
|
| 1738 |
vikas |
157 |
commContentRow.createCell(0).setCellValue(user.getEmail());
|
|
|
158 |
commContentRow.createCell(1).setCellValue(user.getName());
|
|
|
159 |
commContentRow.createCell(2).setCellValue(user.getCommunicationEmail());
|
|
|
160 |
commContentRow.createCell(3).setCellValue(user.getDateOfBirth());
|
|
|
161 |
commContentRow.createCell(4).setCellValue(user.getMobileNumber());
|
|
|
162 |
commContentRow.createCell(5).setCellValue(user.getUserId());
|
|
|
163 |
if (user.getSex() != null) {
|
|
|
164 |
commContentRow.createCell(6).setCellValue(user.getSex().name());
|
|
|
165 |
}
|
|
|
166 |
}
|
|
|
167 |
} catch (UserContextException e) {
|
| 3214 |
chandransh |
168 |
logger.error("Error while getting user data", e);
|
| 1738 |
vikas |
169 |
} catch (TException e) {
|
| 3214 |
chandransh |
170 |
logger.error("Unable to get user data", e);
|
| 1738 |
vikas |
171 |
}
|
|
|
172 |
commContentRow.createCell(7).setCellValue(userComm.getOrderId());
|
|
|
173 |
if (userComm.getCommunicationType() != null) {
|
|
|
174 |
commContentRow.createCell(8).setCellValue(
|
|
|
175 |
userComm.getCommunicationType().name());
|
|
|
176 |
}
|
|
|
177 |
commContentRow.createCell(9).setCellValue(userComm.getAirwaybillNo());
|
| 1744 |
vikas |
178 |
commContentRow.createCell(10).setCellValue(new Date(userComm.getCommunication_timestamp()));
|
| 1738 |
vikas |
179 |
commContentRow.getCell(10).setCellStyle(dateCellStyle);
|
|
|
180 |
commContentRow.createCell(11).setCellValue(userComm.getProductName());
|
|
|
181 |
commContentRow.createCell(12).setCellValue(userComm.getReplyTo());
|
|
|
182 |
commContentRow.createCell(13).setCellValue(userComm.getSubject());
|
|
|
183 |
commContentRow.createCell(14).setCellValue(userComm.getMessage());
|
|
|
184 |
}
|
|
|
185 |
}
|
|
|
186 |
|
|
|
187 |
public String getErrorMsg() {
|
|
|
188 |
return errorMsg;
|
|
|
189 |
}
|
|
|
190 |
}
|