Subversion Repositories SmartDukaan

Rev

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

Rev Author Line No. Line
1901 vikas 1
package in.shop2020.support.controllers;
2
 
2340 vikas 3
import in.shop2020.model.v1.order.LineItem;
4
import in.shop2020.model.v1.order.Order;
1901 vikas 5
import in.shop2020.model.v1.order.TransactionServiceException;
6
import in.shop2020.model.v1.user.Affiliate;
7
import in.shop2020.model.v1.user.MasterAffiliate;
8
import in.shop2020.model.v1.user.TrackLog;
9
import in.shop2020.model.v1.user.UserContextService.Client;
2340 vikas 10
import in.shop2020.payments.Payment;
11
import in.shop2020.thrift.clients.PaymentServiceClient;
12
import in.shop2020.thrift.clients.TransactionServiceClient;
1901 vikas 13
import in.shop2020.thrift.clients.UserContextServiceClient;
14
 
15
import java.io.ByteArrayOutputStream;
16
import java.io.IOException;
17
import java.text.ParseException;
18
import java.util.Date;
19
import java.util.HashMap;
20
import java.util.LinkedList;
21
import java.util.List;
22
import java.util.Map;
23
 
24
import javax.servlet.ServletOutputStream;
25
import javax.servlet.http.HttpServletRequest;
26
import javax.servlet.http.HttpServletResponse;
27
 
28
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
29
import org.apache.poi.ss.usermodel.Cell;
30
import org.apache.poi.ss.usermodel.CellStyle;
31
import org.apache.poi.ss.usermodel.CreationHelper;
32
import org.apache.poi.ss.usermodel.Font;
33
import org.apache.poi.ss.usermodel.Row;
34
import org.apache.poi.ss.usermodel.Sheet;
35
import org.apache.poi.ss.usermodel.Workbook;
36
import org.apache.poi.ss.util.CellRangeAddress;
37
import org.apache.struts2.interceptor.ServletRequestAware;
38
import org.apache.struts2.interceptor.ServletResponseAware;
39
 
40
public class AffiliateController implements ServletResponseAware, ServletRequestAware{
41
 
42
	private HttpServletResponse response;
43
	private HttpServletRequest request;
44
 
45
	private String errorMsg = "";
46
	private List<MasterAffiliate> masterAffiliates;
47
 
48
	@Override
49
	public void setServletResponse(HttpServletResponse res) {
50
		this.response = res;
51
	}
52
 
53
	@Override
54
    public void setServletRequest(HttpServletRequest req) {
55
	    this.request = req;
56
    }
57
 
58
	public String index()  {
59
 
60
        try {
61
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
62
            Client userClient = userContextServiceClient.getClient();
63
            masterAffiliates = userClient.getAllMasterAffiliates();
64
        } catch (Exception e) {
65
            e.printStackTrace();
66
        }
67
        return "report";
68
    }
69
 
70
	public String create()	{
71
	    try   {
72
	        long mAfId = Long.parseLong(request.getParameter("masterAffiliate"));
73
            UserContextServiceClient userContextServiceClient = new UserContextServiceClient();
2340 vikas 74
            PaymentServiceClient paymentServiceClient = new PaymentServiceClient();
75
            TransactionServiceClient transactionServiceClient = new TransactionServiceClient();
76
 
1901 vikas 77
            Client userClient = userContextServiceClient.getClient();
2340 vikas 78
            in.shop2020.payments.PaymentService.Client paymentClient = paymentServiceClient.getClient();
79
            in.shop2020.model.v1.order.TransactionService.Client transactionClient = transactionServiceClient.getClient();
80
 
81
 
1901 vikas 82
            List<Map<String, String>> contentRows = new LinkedList<Map<String,String>>(); 
83
            MasterAffiliate mAffiliate = userClient.getMasterAffiliateById(mAfId);
84
            for (Affiliate aff : userClient.getAffiliatesByMasterAffiliate(mAfId)) {
2000 vikas 85
                for (TrackLog tracklog : userClient.getTrackLogsByAffiliate(aff.getId())) {
86
                    Map<String, String> contentRow = new HashMap<String, String>();
87
                    contentRow.put("mAffiliate", mAffiliate.getName());
88
                    contentRow.put("affiliate", aff.getName());
89
                    contentRow.put("affiliateUrl", aff.getUrl());
90
                    contentRow.put("date",String.valueOf(tracklog.getAddedOn()));
91
                    contentRow.put("event", tracklog.getEvent());
92
                    contentRow.put("url", tracklog.getUrl());
2345 vikas 93
                    if (tracklog.getEvent().equals("payment success")) {
2340 vikas 94
                        try {
2345 vikas 95
                            long paymentId = Long.parseLong(tracklog.getData().replaceAll("\\(*\\)", ""));
2340 vikas 96
                            Payment payment = paymentClient
97
                                    .getPayment(paymentId);
98
                            List<Order> orders = transactionServiceClient
99
                                    .getClient().getOrdersForTransaction(
100
                                            payment.getMerchantTxnId(),
101
                                            payment.getUserId());
102
                            contentRow.put("amount",
103
                                    Double.toString(payment.getAmount()));
2345 vikas 104
                            String itemString = "";
2340 vikas 105
                            for (Order order : orders) {
106
                                List<LineItem> items = order.getLineitems();
107
                                for (LineItem item : items) {
108
                                    itemString += item.getBrand() + " "
109
                                            + item.getModel_name() + " "
110
                                            + item.getModel_number() + "("
111
                                            + item.getQuantity() + ", "
112
                                            + item.getTotal_price() + "), ";
113
                                }
2345 vikas 114
                                contentRow.put("items", itemString.replaceAll("null", ""));
2340 vikas 115
                            }
116
                        } catch (Exception e) {
117
                            e.printStackTrace();
118
                        }
119
                    }
2000 vikas 120
                    contentRow.put("data", tracklog.getData());
121
                    contentRows.add(contentRow);
1901 vikas 122
                }
123
            }
124
 
125
            // Preparing XLS file for output
126
            response.setContentType("application/vnd.ms-excel");
127
 
2058 vikas 128
            response.setHeader("Content-disposition", "inline; filename=affiliate-report" + ".xls");
1901 vikas 129
 
130
            ServletOutputStream sos;
131
            try {
132
                ByteArrayOutputStream baos = getSpreadSheetData(contentRows, mAffiliate);
133
                sos = response.getOutputStream();
134
                baos.writeTo(sos);
135
                sos.flush();
136
            } catch (IOException e) {
137
                errorMsg = "Failed to write to response.";
138
                e.printStackTrace();
139
            }
140
 
141
        } catch (ParseException e)  {
142
            errorMsg = e.getMessage();
143
            e.printStackTrace();
144
        } catch (TransactionServiceException e) {
145
            errorMsg = e.getMessage();
146
            e.printStackTrace();
147
        } catch (Exception e)   {
148
            errorMsg = e.getMessage();
149
            e.printStackTrace();
150
        }
151
        return null;
152
	}
153
 
154
	// Prepares the XLS worksheet object and fills in the data with proper formatting
155
	private ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows, MasterAffiliate mAffiliate)
156
	{
157
	    ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
158
 
159
		Workbook wb = new HSSFWorkbook();
160
 
161
	    Font font = wb.createFont();
162
	    font.setBoldweight(Font.BOLDWEIGHT_BOLD);
163
	    CellStyle style = wb.createCellStyle();
164
	    style.setFont(font);
165
 
166
	    CreationHelper createHelper = wb.getCreationHelper();
167
	    CellStyle dateCellStyle = wb.createCellStyle();
168
	    dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
169
 
170
	    createAffiliateSheet(contentRows, mAffiliate, wb, style, dateCellStyle);
171
 
172
		// Write the workbook to the output stream
173
		try {
174
			wb.write(baosXLS);
175
			baosXLS.close();
176
		} catch (IOException e) {
177
			e.printStackTrace();
178
		}		
179
		return baosXLS;
180
	}
181
 
182
	private void createAffiliateSheet(
183
	        List<Map<String, String>> contentRows, 
184
			MasterAffiliate mAffiliate, Workbook wb, 
185
			CellStyle style, CellStyle dateCellStyle) 
186
	{
187
		// Affiliate SHEET
188
	    Sheet affSheet = wb.createSheet("Affiliates Report");
189
	    short affSerialNo = 0;
190
 
191
	    Row affTitleRow = affSheet.createRow(affSerialNo ++);
192
	    Cell affTitleCell = affTitleRow.createCell(0);
193
	    affTitleCell.setCellValue(mAffiliate.getName() + " Affiliates Report");
194
	    affTitleCell.setCellStyle(style);
195
 
196
	    affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
197
 
198
	    affSheet.createRow(affSerialNo ++);
199
 
200
	    Row affHeaderRow = affSheet.createRow(affSerialNo++);
201
	    affHeaderRow.createCell(0).setCellValue("Affiliate");
202
	    affHeaderRow.createCell(1).setCellValue("Affiliate Url");
203
	    affHeaderRow.createCell(2).setCellValue("Date");
204
	    affHeaderRow.createCell(3).setCellValue("Event");
205
	    affHeaderRow.createCell(4).setCellValue("Url");
206
	    affHeaderRow.createCell(5).setCellValue("Data");
2340 vikas 207
	    affHeaderRow.createCell(6).setCellValue("Items");
208
	    affHeaderRow.createCell(7).setCellValue("Amount");
209
	    for (int i=0; i<8 ;i++) {
1901 vikas 210
	        affHeaderRow.getCell(i).setCellStyle(style);
211
	    }
212
 
213
		for( Map<String, String> contentRow : contentRows) {
214
			affSerialNo++;
215
			Row commContentRow = affSheet.createRow(affSerialNo);
216
 
217
			commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));
218
			commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));
219
			commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));
220
			commContentRow.getCell(2).setCellStyle(dateCellStyle);
221
            commContentRow.createCell(3).setCellValue(contentRow.get("event"));
222
			commContentRow.createCell(4).setCellValue(contentRow.get("url"));
223
			commContentRow.createCell(5).setCellValue(contentRow.get("data"));
2340 vikas 224
			if (contentRow.containsKey("items")) {
225
			    commContentRow.createCell(6).setCellValue(contentRow.get("items"));
226
			}
227
			if (contentRow.containsKey("amount")) {
228
	            commContentRow.createCell(7).setCellValue(contentRow.get("amount"));
229
			}
1901 vikas 230
		}
2340 vikas 231
        for (int i = 0; i<8; i++) {
1901 vikas 232
            affSheet.autoSizeColumn(i);
233
        }
234
	}
235
 
236
	public String getErrorMsg() {
237
		return errorMsg;
238
	}
239
 
240
	public List<MasterAffiliate> getMasterAffiliates() {
241
        return masterAffiliates;
242
    }
2340 vikas 243
}