Subversion Repositories SmartDukaan

Rev

Rev 1744 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package in.shop2020.support.controllers;

import in.shop2020.model.v1.order.TransactionServiceException;
import in.shop2020.model.v1.user.User;
import in.shop2020.model.v1.user.UserCommunication;
import in.shop2020.model.v1.user.UserContextException;
import in.shop2020.model.v1.user.UserContextService.Client;
import in.shop2020.thrift.clients.UserContextServiceClient;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
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;

public class UserCommunicationsController implements ServletResponseAware{

        private HttpServletResponse response;
        
        private String errorMsg = "";
        
        @Override
        public void setServletResponse(HttpServletResponse res) {
                this.response = res;
        }
        
        public String index()   {
            try   {
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
            Client userClient = userContextServiceClient.getClient();

            //Retrieving all user communications
            List<UserCommunication> allCommunications = userClient.getAllUserCommunications();
            
            // Preparing XLS file for output
            response.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) {
                errorMsg = "Failed to write to response.";
                e.printStackTrace();
            }

        } catch (ParseException e)  {
            errorMsg = e.getMessage();
            e.printStackTrace();
        } catch (TransactionServiceException e) {
            errorMsg = e.getMessage();
            e.printStackTrace();
        } catch (Exception e)   {
            errorMsg = e.getMessage();
            e.printStackTrace();
        }
        return null;
        }
        
        // Prepares the XLS worksheet object and fills in the data with proper formatting
        private 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 stream
                try {
                        wb.write(baosXLS);
                        baosXLS.close();
                } catch (IOException e) {
                        e.printStackTrace();
                }               
                return baosXLS;
        }
        
        private void createAllUserCommunicationSheet(
                        List<UserCommunication> allCommunications, 
                        Client userClient,
                        Workbook wb, 
                        CellStyle style, CellStyle dateCellStyle) 
        {
                // USER COMMUNICATION SHEET
            Sheet 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) {
                                e.printStackTrace();
                        } catch (TException e) {
                                e.printStackTrace();
                        }
                        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;
        }
}