Subversion Repositories SmartDukaan

Rev

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