Subversion Repositories SmartDukaan

Rev

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

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