Subversion Repositories SmartDukaan

Rev

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