Subversion Repositories SmartDukaan

Rev

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