| 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());
|
| 2340 |
vikas |
93 |
if (tracklog.getEvent() == "payment success") {
|
|
|
94 |
try {
|
|
|
95 |
long paymentId = Long.parseLong(tracklog.getData()
|
|
|
96 |
.substring(0,
|
|
|
97 |
tracklog.getData().indexOf('(')));
|
|
|
98 |
Payment payment = paymentClient
|
|
|
99 |
.getPayment(paymentId);
|
|
|
100 |
List<Order> orders = transactionServiceClient
|
|
|
101 |
.getClient().getOrdersForTransaction(
|
|
|
102 |
payment.getMerchantTxnId(),
|
|
|
103 |
payment.getUserId());
|
|
|
104 |
contentRow.put("amount",
|
|
|
105 |
Double.toString(payment.getAmount()));
|
|
|
106 |
for (Order order : orders) {
|
|
|
107 |
List<LineItem> items = order.getLineitems();
|
|
|
108 |
String itemString = "";
|
|
|
109 |
for (LineItem item : items) {
|
|
|
110 |
itemString += item.getBrand() + " "
|
|
|
111 |
+ item.getModel_name() + " "
|
|
|
112 |
+ item.getModel_number() + "("
|
|
|
113 |
+ item.getQuantity() + ", "
|
|
|
114 |
+ item.getTotal_price() + "), ";
|
|
|
115 |
}
|
|
|
116 |
contentRow.put("items", itemString);
|
|
|
117 |
}
|
|
|
118 |
} catch (Exception e) {
|
|
|
119 |
e.printStackTrace();
|
|
|
120 |
}
|
|
|
121 |
}
|
| 2000 |
vikas |
122 |
contentRow.put("data", tracklog.getData());
|
|
|
123 |
contentRows.add(contentRow);
|
| 1901 |
vikas |
124 |
}
|
|
|
125 |
}
|
|
|
126 |
|
|
|
127 |
// Preparing XLS file for output
|
|
|
128 |
response.setContentType("application/vnd.ms-excel");
|
|
|
129 |
|
| 2058 |
vikas |
130 |
response.setHeader("Content-disposition", "inline; filename=affiliate-report" + ".xls");
|
| 1901 |
vikas |
131 |
|
|
|
132 |
ServletOutputStream sos;
|
|
|
133 |
try {
|
|
|
134 |
ByteArrayOutputStream baos = getSpreadSheetData(contentRows, mAffiliate);
|
|
|
135 |
sos = response.getOutputStream();
|
|
|
136 |
baos.writeTo(sos);
|
|
|
137 |
sos.flush();
|
|
|
138 |
} catch (IOException e) {
|
|
|
139 |
errorMsg = "Failed to write to response.";
|
|
|
140 |
e.printStackTrace();
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
} catch (ParseException e) {
|
|
|
144 |
errorMsg = e.getMessage();
|
|
|
145 |
e.printStackTrace();
|
|
|
146 |
} catch (TransactionServiceException e) {
|
|
|
147 |
errorMsg = e.getMessage();
|
|
|
148 |
e.printStackTrace();
|
|
|
149 |
} catch (Exception e) {
|
|
|
150 |
errorMsg = e.getMessage();
|
|
|
151 |
e.printStackTrace();
|
|
|
152 |
}
|
|
|
153 |
return null;
|
|
|
154 |
}
|
|
|
155 |
|
|
|
156 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
|
|
157 |
private ByteArrayOutputStream getSpreadSheetData(List<Map<String, String>> contentRows, MasterAffiliate mAffiliate)
|
|
|
158 |
{
|
|
|
159 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
160 |
|
|
|
161 |
Workbook wb = new HSSFWorkbook();
|
|
|
162 |
|
|
|
163 |
Font font = wb.createFont();
|
|
|
164 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
165 |
CellStyle style = wb.createCellStyle();
|
|
|
166 |
style.setFont(font);
|
|
|
167 |
|
|
|
168 |
CreationHelper createHelper = wb.getCreationHelper();
|
|
|
169 |
CellStyle dateCellStyle = wb.createCellStyle();
|
|
|
170 |
dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("DD/MM/YYYY HH:MM"));
|
|
|
171 |
|
|
|
172 |
createAffiliateSheet(contentRows, mAffiliate, wb, style, dateCellStyle);
|
|
|
173 |
|
|
|
174 |
// Write the workbook to the output stream
|
|
|
175 |
try {
|
|
|
176 |
wb.write(baosXLS);
|
|
|
177 |
baosXLS.close();
|
|
|
178 |
} catch (IOException e) {
|
|
|
179 |
e.printStackTrace();
|
|
|
180 |
}
|
|
|
181 |
return baosXLS;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
private void createAffiliateSheet(
|
|
|
185 |
List<Map<String, String>> contentRows,
|
|
|
186 |
MasterAffiliate mAffiliate, Workbook wb,
|
|
|
187 |
CellStyle style, CellStyle dateCellStyle)
|
|
|
188 |
{
|
|
|
189 |
// Affiliate SHEET
|
|
|
190 |
Sheet affSheet = wb.createSheet("Affiliates Report");
|
|
|
191 |
short affSerialNo = 0;
|
|
|
192 |
|
|
|
193 |
Row affTitleRow = affSheet.createRow(affSerialNo ++);
|
|
|
194 |
Cell affTitleCell = affTitleRow.createCell(0);
|
|
|
195 |
affTitleCell.setCellValue(mAffiliate.getName() + " Affiliates Report");
|
|
|
196 |
affTitleCell.setCellStyle(style);
|
|
|
197 |
|
|
|
198 |
affSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
199 |
|
|
|
200 |
affSheet.createRow(affSerialNo ++);
|
|
|
201 |
|
|
|
202 |
Row affHeaderRow = affSheet.createRow(affSerialNo++);
|
|
|
203 |
affHeaderRow.createCell(0).setCellValue("Affiliate");
|
|
|
204 |
affHeaderRow.createCell(1).setCellValue("Affiliate Url");
|
|
|
205 |
affHeaderRow.createCell(2).setCellValue("Date");
|
|
|
206 |
affHeaderRow.createCell(3).setCellValue("Event");
|
|
|
207 |
affHeaderRow.createCell(4).setCellValue("Url");
|
|
|
208 |
affHeaderRow.createCell(5).setCellValue("Data");
|
| 2340 |
vikas |
209 |
affHeaderRow.createCell(6).setCellValue("Items");
|
|
|
210 |
affHeaderRow.createCell(7).setCellValue("Amount");
|
|
|
211 |
for (int i=0; i<8 ;i++) {
|
| 1901 |
vikas |
212 |
affHeaderRow.getCell(i).setCellStyle(style);
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
for( Map<String, String> contentRow : contentRows) {
|
|
|
216 |
affSerialNo++;
|
|
|
217 |
Row commContentRow = affSheet.createRow(affSerialNo);
|
|
|
218 |
|
|
|
219 |
commContentRow.createCell(0).setCellValue(contentRow.get("affiliate"));
|
|
|
220 |
commContentRow.createCell(1).setCellValue(contentRow.get("affiliateUrl"));
|
|
|
221 |
commContentRow.createCell(2).setCellValue(new Date(Long.parseLong(contentRow.get("date"))));
|
|
|
222 |
commContentRow.getCell(2).setCellStyle(dateCellStyle);
|
|
|
223 |
commContentRow.createCell(3).setCellValue(contentRow.get("event"));
|
|
|
224 |
commContentRow.createCell(4).setCellValue(contentRow.get("url"));
|
|
|
225 |
commContentRow.createCell(5).setCellValue(contentRow.get("data"));
|
| 2340 |
vikas |
226 |
if (contentRow.containsKey("items")) {
|
|
|
227 |
commContentRow.createCell(6).setCellValue(contentRow.get("items"));
|
|
|
228 |
}
|
|
|
229 |
if (contentRow.containsKey("amount")) {
|
|
|
230 |
commContentRow.createCell(7).setCellValue(contentRow.get("amount"));
|
|
|
231 |
}
|
| 1901 |
vikas |
232 |
}
|
| 2340 |
vikas |
233 |
for (int i = 0; i<8; i++) {
|
| 1901 |
vikas |
234 |
affSheet.autoSizeColumn(i);
|
|
|
235 |
}
|
|
|
236 |
}
|
|
|
237 |
|
|
|
238 |
public String getErrorMsg() {
|
|
|
239 |
return errorMsg;
|
|
|
240 |
}
|
|
|
241 |
|
|
|
242 |
public List<MasterAffiliate> getMasterAffiliates() {
|
|
|
243 |
return masterAffiliates;
|
|
|
244 |
}
|
| 2340 |
vikas |
245 |
}
|