Rev 3125 | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.user.User;import in.shop2020.model.v1.user.UserCommunication;import in.shop2020.model.v1.user.UserCommunicationException;import in.shop2020.model.v1.user.UserContextException;import in.shop2020.model.v1.user.UserContextService.Client;import in.shop2020.thrift.clients.UserClient;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.Date;import java.util.List;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.CreationHelper;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.thrift.TException;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class UserCommunicationsController implements ServletResponseAware{private static Logger logger = LoggerFactory.getLogger(UserCommunicationsController.class);private HttpServletResponse response;private String errorMsg = "";@Overridepublic void setServletResponse(HttpServletResponse res) {this.response = res;}public String index() {try {UserClient userContextServiceClient = new UserClient();Client userClient = userContextServiceClient.getClient();//Retrieving all user communicationsList<UserCommunication> allCommunications = userClient.getAllUserCommunications();// Preparing XLS file for outputresponse.setContentType("application/vnd.ms-excel");response.setHeader("Content-disposition", "inline; filename=user-communications" + ".xls");ServletOutputStream sos;try {ByteArrayOutputStream baos = getSpreadSheetData(allCommunications, userClient);sos = response.getOutputStream();baos.writeTo(sos);sos.flush();} catch (IOException e) {logger.error("Unable to stream the user communications report");errorMsg = "Failed to write to response.";}} catch (UserCommunicationException e) {logger.error("Error while getting user communications", e);errorMsg = e.getMessage();} catch (TException e) {logger.error("Unable to get user communications", e);errorMsg = e.getMessage();} catch (Exception e) {logger.error("Unexpected exception", e);errorMsg = e.getMessage();}return null;}// Prepares the XLS worksheet object and fills in the data with proper formattingprivate ByteArrayOutputStream getSpreadSheetData(List<UserCommunication> allCommunications,Client userClient){ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();Workbook wb = new HSSFWorkbook();Font font = wb.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle style = wb.createCellStyle();style.setFont(font);CreationHelper createHelper = wb.getCreationHelper();CellStyle dateCellStyle = wb.createCellStyle();dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));createAllUserCommunicationSheet(allCommunications, userClient, wb, style, dateCellStyle);// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {logger.error("Error while converting the user communications report to a byte array", e);}return baosXLS;}private void createAllUserCommunicationSheet(List<UserCommunication> allCommunications,Client userClient,Workbook wb,CellStyle style, CellStyle dateCellStyle){// USER COMMUNICATION SHEETSheet commSheet = wb.createSheet("All Users Communications");short commSerialNo = 0;Row commTitleRow = commSheet.createRow(commSerialNo ++);Cell commTitleCell = commTitleRow.createCell(0);commTitleCell.setCellValue("All Users Communications");commTitleCell.setCellStyle(style);commSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));commSheet.createRow(commSerialNo ++);Row commHeaderRow = commSheet.createRow(commSerialNo++);commHeaderRow.createCell(0).setCellValue("User Email");commHeaderRow.createCell(1).setCellValue("User Name");commHeaderRow.createCell(2).setCellValue("Communication Email");commHeaderRow.createCell(3).setCellValue("Date of Birth");commHeaderRow.createCell(4).setCellValue("Mobile Number");commHeaderRow.createCell(5).setCellValue("User Id");commHeaderRow.createCell(6).setCellValue("Sex");commHeaderRow.createCell(7).setCellValue("Order Id");commHeaderRow.createCell(8).setCellValue("Communication Type");commHeaderRow.createCell(9).setCellValue("AirwayBill No");commHeaderRow.createCell(10).setCellValue("TimeStamp");commHeaderRow.createCell(11).setCellValue("Product Name");commHeaderRow.createCell(12).setCellValue("Reply To");commHeaderRow.createCell(13).setCellValue("Subject");commHeaderRow.createCell(14).setCellValue("Message");for (int i=0; i<15 ;i++) {commHeaderRow.getCell(i).setCellStyle(style);}for( UserCommunication userComm : allCommunications) {commSerialNo++;Row commContentRow = commSheet.createRow(commSerialNo);try {User user = userClient.getUserById(userComm.getUserId());if (user.getUserId() != -1) {commContentRow.createCell(0).setCellValue(user.getEmail());commContentRow.createCell(1).setCellValue(user.getName());commContentRow.createCell(2).setCellValue(user.getCommunicationEmail());commContentRow.createCell(3).setCellValue(user.getDateOfBirth());commContentRow.createCell(4).setCellValue(user.getMobileNumber());commContentRow.createCell(5).setCellValue(user.getUserId());if (user.getSex() != null) {commContentRow.createCell(6).setCellValue(user.getSex().name());}}} catch (UserContextException e) {logger.error("Error while getting user data", e);} catch (TException e) {logger.error("Unable to get user data", e);}commContentRow.createCell(7).setCellValue(userComm.getOrderId());if (userComm.getCommunicationType() != null) {commContentRow.createCell(8).setCellValue(userComm.getCommunicationType().name());}commContentRow.createCell(9).setCellValue(userComm.getAirwaybillNo());commContentRow.createCell(10).setCellValue(new Date(userComm.getCommunication_timestamp()));commContentRow.getCell(10).setCellStyle(dateCellStyle);commContentRow.createCell(11).setCellValue(userComm.getProductName());commContentRow.createCell(12).setCellValue(userComm.getReplyTo());commContentRow.createCell(13).setCellValue(userComm.getSubject());commContentRow.createCell(14).setCellValue(userComm.getMessage());}}public String getErrorMsg() {return errorMsg;}}