| 1891 |
ankur.sing |
1 |
package in.shop2020.support.services;
|
|
|
2 |
|
| 5945 |
mandeep.dh |
3 |
import in.shop2020.model.v1.catalog.CatalogServiceException;
|
| 1891 |
ankur.sing |
4 |
import in.shop2020.model.v1.catalog.Item;
|
|
|
5 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
6 |
import in.shop2020.model.v1.order.Order;
|
|
|
7 |
import in.shop2020.model.v1.order.Transaction;
|
|
|
8 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
|
|
9 |
import in.shop2020.model.v1.user.Address;
|
|
|
10 |
import in.shop2020.model.v1.user.Cart;
|
|
|
11 |
import in.shop2020.model.v1.user.Line;
|
|
|
12 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
|
|
13 |
import in.shop2020.model.v1.user.User;
|
|
|
14 |
import in.shop2020.model.v1.user.UserContextException;
|
|
|
15 |
import in.shop2020.model.v1.user.UserType;
|
| 3125 |
rajveer |
16 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
17 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
18 |
import in.shop2020.thrift.clients.UserClient;
|
| 1891 |
ankur.sing |
19 |
|
|
|
20 |
import java.io.ByteArrayOutputStream;
|
|
|
21 |
import java.io.FileNotFoundException;
|
|
|
22 |
import java.io.FileOutputStream;
|
|
|
23 |
import java.io.IOException;
|
|
|
24 |
import java.text.DateFormat;
|
|
|
25 |
import java.text.ParseException;
|
|
|
26 |
import java.text.SimpleDateFormat;
|
|
|
27 |
import java.util.Calendar;
|
|
|
28 |
import java.util.Date;
|
|
|
29 |
import java.util.List;
|
|
|
30 |
|
|
|
31 |
import org.apache.poi.hssf.usermodel.HSSFFont;
|
|
|
32 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
|
|
33 |
import org.apache.poi.hssf.util.HSSFColor;
|
|
|
34 |
import org.apache.poi.ss.usermodel.Cell;
|
|
|
35 |
import org.apache.poi.ss.usermodel.CellStyle;
|
|
|
36 |
import org.apache.poi.ss.usermodel.Font;
|
|
|
37 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
38 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
39 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
40 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
41 |
import org.apache.thrift.TException;
|
| 3213 |
chandransh |
42 |
import org.slf4j.Logger;
|
|
|
43 |
import org.slf4j.LoggerFactory;
|
| 1891 |
ankur.sing |
44 |
|
|
|
45 |
public class AnalysisDataGenerator {
|
| 3213 |
chandransh |
46 |
|
|
|
47 |
private static Logger logger = LoggerFactory.getLogger(AnalysisDataGenerator.class);
|
|
|
48 |
|
| 3125 |
rajveer |
49 |
UserClient usc;
|
| 1891 |
ankur.sing |
50 |
in.shop2020.model.v1.user.UserContextService.Client uClient;
|
|
|
51 |
|
| 3125 |
rajveer |
52 |
CatalogClient csc;
|
| 5945 |
mandeep.dh |
53 |
in.shop2020.model.v1.catalog.CatalogService.Client cClient;
|
| 1891 |
ankur.sing |
54 |
|
| 3125 |
rajveer |
55 |
TransactionClient tsc;
|
| 1891 |
ankur.sing |
56 |
in.shop2020.model.v1.order.TransactionService.Client tClient;
|
|
|
57 |
|
|
|
58 |
public AnalysisDataGenerator() {
|
|
|
59 |
try {
|
| 3125 |
rajveer |
60 |
usc = new UserClient();
|
| 1891 |
ankur.sing |
61 |
uClient = usc.getClient();
|
|
|
62 |
|
| 3125 |
rajveer |
63 |
csc = new CatalogClient();
|
| 1891 |
ankur.sing |
64 |
cClient = csc.getClient();
|
|
|
65 |
|
| 3125 |
rajveer |
66 |
tsc = new TransactionClient();
|
| 1891 |
ankur.sing |
67 |
tClient = tsc.getClient();
|
|
|
68 |
} catch (Exception e) {
|
| 3213 |
chandransh |
69 |
logger.error("Unable to initialize connection with one of user, catalog or txn client", e);
|
| 1891 |
ankur.sing |
70 |
}
|
|
|
71 |
}
|
|
|
72 |
|
|
|
73 |
public ByteArrayOutputStream generateAnalysisReport(long startDate, long endDate) throws ShoppingCartException {
|
|
|
74 |
List<User> users = null;
|
|
|
75 |
try {
|
|
|
76 |
users = uClient.getAllUsers(UserType.USER, startDate, endDate);
|
|
|
77 |
} catch (TException e) {
|
| 3213 |
chandransh |
78 |
logger.error("Unable to get all users", e);
|
| 1891 |
ankur.sing |
79 |
}
|
|
|
80 |
if (users == null || users.isEmpty()) {
|
|
|
81 |
return null;
|
|
|
82 |
}
|
|
|
83 |
// Preparing XLS file for output
|
|
|
84 |
return getSpreadSheetData(users);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
// Prepares the XLS worksheet object and fills in the data with proper
|
|
|
88 |
// formatting
|
|
|
89 |
private ByteArrayOutputStream getSpreadSheetData(List<User> users) {
|
|
|
90 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
91 |
|
|
|
92 |
Workbook wb = new HSSFWorkbook();
|
|
|
93 |
|
|
|
94 |
Font font = wb.createFont();
|
|
|
95 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
96 |
CellStyle style = wb.createCellStyle();
|
|
|
97 |
style.setFont(font);
|
|
|
98 |
|
|
|
99 |
CellStyle styleWT = wb.createCellStyle();
|
|
|
100 |
styleWT.setWrapText(true);
|
|
|
101 |
styleWT.setFillBackgroundColor((short)1);
|
|
|
102 |
|
|
|
103 |
CellStyle styleUser = wb.createCellStyle();
|
|
|
104 |
styleUser.setFillBackgroundColor(HSSFColor.LIGHT_BLUE.index);
|
|
|
105 |
|
|
|
106 |
CellStyle styleItem = wb.createCellStyle();
|
|
|
107 |
styleItem.setFillBackgroundColor(HSSFColor.LIGHT_ORANGE.index);
|
|
|
108 |
|
|
|
109 |
CellStyle styleOrder = wb.createCellStyle();
|
|
|
110 |
styleOrder.setFillBackgroundColor(HSSFFont.COLOR_RED);
|
|
|
111 |
styleOrder.setHidden(true);
|
|
|
112 |
|
|
|
113 |
Sheet analysisSheet = wb.createSheet("User");
|
|
|
114 |
short rowNo = 0;
|
|
|
115 |
|
|
|
116 |
Row titleRow = analysisSheet.createRow(rowNo++);
|
|
|
117 |
Cell titleCell = titleRow.createCell(0);
|
|
|
118 |
titleCell.setCellValue("User Details");
|
|
|
119 |
titleCell.setCellStyle(style);
|
|
|
120 |
|
|
|
121 |
analysisSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
122 |
|
|
|
123 |
Row headerRow = analysisSheet.createRow(rowNo++);
|
|
|
124 |
headerRow.createCell(0).setCellValue("S.No.");
|
|
|
125 |
headerRow.createCell(1).setCellValue("User Id");
|
|
|
126 |
headerRow.createCell(2).setCellValue("Email");
|
|
|
127 |
headerRow.createCell(3).setCellValue("Name");
|
|
|
128 |
headerRow.createCell(4).setCellValue("Communication Email");
|
|
|
129 |
headerRow.createCell(5).setCellValue("Date of Birth");
|
|
|
130 |
headerRow.createCell(6).setCellValue("Sex");
|
|
|
131 |
headerRow.createCell(7).setCellValue("Mobile");
|
|
|
132 |
headerRow.createCell(8).setCellValue("Registered On");
|
|
|
133 |
headerRow.createCell(9).setCellValue("Last Login");
|
|
|
134 |
|
|
|
135 |
headerRow.createCell(10).setCellValue("Cart Id");
|
|
|
136 |
headerRow.createCell(11).setCellValue("Cart/Default Address");
|
|
|
137 |
|
|
|
138 |
headerRow.createCell(12).setCellValue("Item Id");
|
|
|
139 |
headerRow.createCell(13).setCellValue("Brand");
|
|
|
140 |
headerRow.createCell(14).setCellValue("Model Name");
|
|
|
141 |
headerRow.createCell(15).setCellValue("Model Number");
|
|
|
142 |
|
|
|
143 |
headerRow.createCell(16).setCellValue("Order Id");
|
|
|
144 |
headerRow.createCell(17).setCellValue("Order Status");
|
|
|
145 |
headerRow.createCell(18).setCellValue("Status Description");
|
|
|
146 |
headerRow.createCell(19).setCellValue("Order Amount");
|
|
|
147 |
headerRow.createCell(20).setCellValue("Item Brand");
|
|
|
148 |
headerRow.createCell(21).setCellValue("Model Name");
|
|
|
149 |
headerRow.createCell(22).setCellValue("Model Number");
|
|
|
150 |
|
|
|
151 |
Calendar calendar = Calendar.getInstance();
|
|
|
152 |
Row contentRow;
|
|
|
153 |
float defaultRowHeight = analysisSheet.getDefaultRowHeightInPoints();
|
|
|
154 |
DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
|
|
155 |
int sNo = 0;
|
|
|
156 |
int addrCol = 11, itemOffset = addrCol + 1, orderOffset = itemOffset + 4;
|
|
|
157 |
for (User u : users) {
|
|
|
158 |
rowNo++;
|
|
|
159 |
sNo++;
|
|
|
160 |
contentRow = analysisSheet.createRow(rowNo);
|
|
|
161 |
contentRow.createCell(0).setCellValue(sNo);
|
|
|
162 |
contentRow.createCell(1).setCellValue(u.getUserId());
|
|
|
163 |
contentRow.createCell(2).setCellValue(u.getEmail());
|
|
|
164 |
contentRow.createCell(3).setCellValue(u.getName());
|
|
|
165 |
contentRow.createCell(4).setCellValue(u.getCommunicationEmail());
|
|
|
166 |
contentRow.createCell(5).setCellValue(u.getDateOfBirth());
|
|
|
167 |
// calendar.setTimeInMillis
|
|
|
168 |
contentRow.createCell(6).setCellValue(u.getSex().name());
|
|
|
169 |
contentRow.createCell(7).setCellValue(u.getMobileNumber());
|
| 5326 |
rajveer |
170 |
calendar.setTimeInMillis(u.getActiveSince());
|
|
|
171 |
contentRow.createCell(8).setCellValue(formatter.format(calendar.getTime()));
|
|
|
172 |
calendar.setTimeInMillis(u.getLastLogin());
|
|
|
173 |
contentRow.createCell(9).setCellValue(formatter.format(calendar.getTime()));
|
|
|
174 |
|
| 1891 |
ankur.sing |
175 |
Cart cart = null;
|
|
|
176 |
try {
|
|
|
177 |
System.out.println("User Id : " + u.getUserId());
|
|
|
178 |
cart = uClient.getCurrentCart(u.getUserId());
|
|
|
179 |
} catch (TException e) {
|
| 3213 |
chandransh |
180 |
logger.error("Error while getting the cart of the user:" + u.getUserId(), e);
|
| 1891 |
ankur.sing |
181 |
} catch (ShoppingCartException e) {
|
| 3213 |
chandransh |
182 |
logger.error("Unable to get the cart for the user:" + u.getUserId());
|
| 1891 |
ankur.sing |
183 |
}
|
|
|
184 |
contentRow.createCell(10).setCellValue((cart != null) ? cart.getId() + "" : "");
|
|
|
185 |
for(int i = 0; i <= addrCol - 1; i++) {
|
|
|
186 |
contentRow.getCell(i).setCellStyle(styleUser);
|
|
|
187 |
}
|
|
|
188 |
List<Address> addresses = u.getAddresses();
|
|
|
189 |
int len, addrColWidth = 40;
|
|
|
190 |
analysisSheet.setColumnWidth(addrCol, addrColWidth * 256); /* set width of address column to 40 characters */
|
|
|
191 |
String addressString = getAddress(addresses, u.getDefaultAddressId(), (cart != null ? cart.getAddressId() : 0));
|
|
|
192 |
contentRow.createCell(addrCol).setCellValue(addressString);
|
|
|
193 |
contentRow.getCell(addrCol).setCellStyle(styleWT);
|
|
|
194 |
len = addressString.length();
|
|
|
195 |
if (len > 40) {
|
|
|
196 |
contentRow.setHeightInPoints((len / addrColWidth + 1) * defaultRowHeight); /* Setting Row Height */
|
|
|
197 |
}
|
|
|
198 |
|
|
|
199 |
if (cart != null) {
|
|
|
200 |
List<Line> lines = cart.getLines();
|
|
|
201 |
if (lines != null && !lines.isEmpty()) {
|
|
|
202 |
Item catalogItem;
|
|
|
203 |
for (Line line : lines) {
|
|
|
204 |
try {
|
|
|
205 |
catalogItem = cClient.getItem(line.getItemId());
|
|
|
206 |
if (catalogItem == null) {
|
|
|
207 |
continue;
|
|
|
208 |
}
|
|
|
209 |
rowNo++;
|
|
|
210 |
contentRow = analysisSheet.createRow(rowNo);
|
|
|
211 |
contentRow.createCell(itemOffset + 0).setCellValue(catalogItem.getId());
|
|
|
212 |
contentRow.createCell(itemOffset + 1).setCellValue(catalogItem.getBrand());
|
|
|
213 |
contentRow.createCell(itemOffset + 2).setCellValue(catalogItem.getModelName());
|
|
|
214 |
contentRow.createCell(itemOffset + 3).setCellValue(catalogItem.getModelNumber());
|
|
|
215 |
// linesCount++;
|
|
|
216 |
// rowNo++;
|
| 5945 |
mandeep.dh |
217 |
} catch (CatalogServiceException e) {
|
| 3213 |
chandransh |
218 |
logger.error("Unable to get the item:" + line.getItemId(), e);
|
| 1891 |
ankur.sing |
219 |
} catch (TException e) {
|
| 3213 |
chandransh |
220 |
logger.error("Error while getting the item:" + line.getItemId(), e);
|
| 1891 |
ankur.sing |
221 |
}
|
|
|
222 |
for(int i = itemOffset; i < orderOffset; i++) {
|
|
|
223 |
contentRow.getCell(i).setCellStyle(styleItem);
|
|
|
224 |
}
|
|
|
225 |
}
|
|
|
226 |
}
|
|
|
227 |
|
|
|
228 |
List<Transaction> transactions;
|
|
|
229 |
try {
|
|
|
230 |
transactions = tClient.getTransactionsForShoppingCartId(cart.getId());
|
|
|
231 |
if (transactions != null && !transactions.isEmpty()) {
|
|
|
232 |
List<Order> orders;
|
|
|
233 |
// int ordersRowNo = rowNo - linesCount;
|
|
|
234 |
for (Transaction txn : transactions) {
|
|
|
235 |
orders = txn.getOrders();
|
|
|
236 |
if (orders == null || orders.isEmpty()) {
|
|
|
237 |
continue;
|
|
|
238 |
}
|
|
|
239 |
LineItem lineItem;
|
|
|
240 |
for (Order o : orders) {
|
|
|
241 |
lineItem = o.getLineitems().get(0);
|
|
|
242 |
rowNo++;
|
|
|
243 |
contentRow = analysisSheet.createRow(rowNo);
|
|
|
244 |
contentRow.createCell(orderOffset + 0).setCellValue(o.getId());
|
|
|
245 |
contentRow.createCell(orderOffset + 1).setCellValue(o.getStatus().name());
|
|
|
246 |
contentRow.createCell(orderOffset + 2).setCellValue(o.getStatusDescription());
|
|
|
247 |
contentRow.createCell(orderOffset + 3).setCellValue(o.getTotal_amount());
|
|
|
248 |
contentRow.createCell(orderOffset + 4).setCellValue(lineItem.getBrand());
|
|
|
249 |
contentRow.createCell(orderOffset + 5).setCellValue(lineItem.getModel_name());
|
|
|
250 |
contentRow.createCell(orderOffset + 6).setCellValue(lineItem.getModel_number());
|
|
|
251 |
for(int i = orderOffset; i < orderOffset + 6; i++) {
|
|
|
252 |
contentRow.getCell(i).setCellStyle(styleOrder);
|
|
|
253 |
}
|
|
|
254 |
}
|
|
|
255 |
}
|
|
|
256 |
}
|
|
|
257 |
} catch (TransactionServiceException e) {
|
| 3213 |
chandransh |
258 |
logger.error("Unable to get the details of the orders", e);
|
| 1891 |
ankur.sing |
259 |
} catch (TException e) {
|
| 3213 |
chandransh |
260 |
logger.error("Error while getting the details of the orders", e);
|
| 1891 |
ankur.sing |
261 |
}
|
|
|
262 |
}
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
for (int i = 0; i <= 22; i++) {
|
|
|
266 |
if (i == addrCol) {
|
|
|
267 |
continue;
|
|
|
268 |
}
|
|
|
269 |
analysisSheet.autoSizeColumn(i);
|
|
|
270 |
}
|
|
|
271 |
|
|
|
272 |
// Write the workbook to the output stream
|
|
|
273 |
try {
|
|
|
274 |
wb.write(baosXLS);
|
|
|
275 |
baosXLS.close();
|
|
|
276 |
} catch (IOException e) {
|
| 3213 |
chandransh |
277 |
logger.error("Erro while writing to the byte array", e);
|
| 1891 |
ankur.sing |
278 |
}
|
|
|
279 |
return baosXLS;
|
|
|
280 |
}
|
|
|
281 |
|
|
|
282 |
private String getAddress(List<Address> addresses, long defaultAddrId, long cartAddrId) {
|
|
|
283 |
String addressString = null;
|
|
|
284 |
if (addresses == null || addresses.isEmpty()) {
|
|
|
285 |
return "";
|
|
|
286 |
}
|
|
|
287 |
for (Address a : addresses) {
|
|
|
288 |
if (a.getId() == cartAddrId) {
|
|
|
289 |
addressString = generateAddressString(a);
|
|
|
290 |
break;
|
|
|
291 |
}
|
|
|
292 |
}
|
|
|
293 |
if (addressString == null) {
|
|
|
294 |
for (Address a : addresses) {
|
|
|
295 |
if (a.getId() == defaultAddrId) {
|
|
|
296 |
addressString = generateAddressString(a);
|
|
|
297 |
break;
|
|
|
298 |
}
|
|
|
299 |
}
|
|
|
300 |
}
|
|
|
301 |
return addressString == null ? "" : addressString;
|
|
|
302 |
}
|
|
|
303 |
|
|
|
304 |
private String generateAddressString(Address a) {
|
|
|
305 |
String addrStr = "";
|
|
|
306 |
addrStr += "Name: " + getStringValue(a.getName());
|
|
|
307 |
addrStr += "; Address: " + getStringValue(a.getLine1());
|
|
|
308 |
addrStr += ", " + getStringValue(a.getLine2());
|
|
|
309 |
addrStr += "; City: " + getStringValue(a.getCity());
|
|
|
310 |
addrStr += "; State: " + getStringValue(a.getState());
|
|
|
311 |
addrStr += "; Pin: " + getStringValue(a.getPin());
|
|
|
312 |
addrStr += "; Phone: " + getStringValue(a.getPhone());
|
|
|
313 |
return addrStr;
|
|
|
314 |
}
|
|
|
315 |
|
|
|
316 |
private String getStringValue(String str) {
|
|
|
317 |
return str == null ? "" : str.trim();
|
|
|
318 |
}
|
|
|
319 |
|
|
|
320 |
public static void main(String[] args) throws ShoppingCartException {
|
|
|
321 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
322 |
Date startDate = null, endDate = null;
|
|
|
323 |
try {
|
|
|
324 |
startDate = df.parse("05/07/2011");
|
|
|
325 |
endDate = df.parse("05/09/2011");
|
|
|
326 |
} catch (ParseException pe) {
|
| 3213 |
chandransh |
327 |
logger.error("Error while parsing date", pe);
|
| 1891 |
ankur.sing |
328 |
}
|
|
|
329 |
AnalysisDataGenerator adg = new AnalysisDataGenerator();
|
|
|
330 |
try {
|
|
|
331 |
String userHome = System.getProperty("user.home");
|
|
|
332 |
FileOutputStream f = new FileOutputStream(userHome + "/analysis-data.xls");
|
|
|
333 |
ByteArrayOutputStream baosXLS = adg.generateAnalysisReport(startDate.getTime(), endDate.getTime());
|
|
|
334 |
if (baosXLS == null) {
|
|
|
335 |
System.out.println("No data");
|
|
|
336 |
return;
|
|
|
337 |
}
|
|
|
338 |
baosXLS.writeTo(f);
|
|
|
339 |
f.close();
|
|
|
340 |
} catch (FileNotFoundException e) {
|
| 3213 |
chandransh |
341 |
logger.error("Unable to create file", e);
|
| 1891 |
ankur.sing |
342 |
} catch (IOException e) {
|
| 3213 |
chandransh |
343 |
logger.error("Error while creating the analysis report", e);
|
| 1891 |
ankur.sing |
344 |
}
|
|
|
345 |
System.out.println("Successfully generated the analysis report");
|
|
|
346 |
}
|
|
|
347 |
}
|