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