| 1676 |
ankur.sing |
1 |
package in.shop2020.support.services;
|
|
|
2 |
|
| 1879 |
ankur.sing |
3 |
import in.shop2020.model.v1.order.Order;
|
|
|
4 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
| 1676 |
ankur.sing |
5 |
import in.shop2020.model.v1.user.Address;
|
| 1879 |
ankur.sing |
6 |
import in.shop2020.model.v1.user.Cart;
|
|
|
7 |
import in.shop2020.model.v1.user.Line;
|
|
|
8 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 1676 |
ankur.sing |
9 |
import in.shop2020.model.v1.user.User;
|
| 1684 |
ankur.sing |
10 |
import in.shop2020.model.v1.user.UserContextException;
|
| 1676 |
ankur.sing |
11 |
import in.shop2020.model.v1.user.UserType;
|
| 4817 |
anupam.sin |
12 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 3125 |
rajveer |
13 |
import in.shop2020.thrift.clients.TransactionClient;
|
|
|
14 |
import in.shop2020.thrift.clients.UserClient;
|
| 1676 |
ankur.sing |
15 |
|
|
|
16 |
import java.io.ByteArrayOutputStream;
|
|
|
17 |
import java.io.FileNotFoundException;
|
|
|
18 |
import java.io.FileOutputStream;
|
|
|
19 |
import java.io.IOException;
|
| 1684 |
ankur.sing |
20 |
import java.text.DateFormat;
|
| 4817 |
anupam.sin |
21 |
import java.text.ParseException;
|
| 1684 |
ankur.sing |
22 |
import java.text.SimpleDateFormat;
|
| 1676 |
ankur.sing |
23 |
import java.util.Calendar;
|
| 1879 |
ankur.sing |
24 |
import java.util.Date;
|
| 1676 |
ankur.sing |
25 |
import java.util.List;
|
| 4817 |
anupam.sin |
26 |
import java.util.TimeZone;
|
| 1676 |
ankur.sing |
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.Font;
|
|
|
32 |
import org.apache.poi.ss.usermodel.Row;
|
|
|
33 |
import org.apache.poi.ss.usermodel.Sheet;
|
|
|
34 |
import org.apache.poi.ss.usermodel.Workbook;
|
|
|
35 |
import org.apache.poi.ss.util.CellRangeAddress;
|
|
|
36 |
import org.apache.thrift.TException;
|
| 3213 |
chandransh |
37 |
import org.slf4j.Logger;
|
|
|
38 |
import org.slf4j.LoggerFactory;
|
| 1676 |
ankur.sing |
39 |
|
|
|
40 |
public class RegisteredUsersGenerator {
|
| 3213 |
chandransh |
41 |
|
|
|
42 |
private static Logger logger = LoggerFactory.getLogger(RegisteredUsersGenerator.class);
|
|
|
43 |
|
| 3125 |
rajveer |
44 |
UserClient usc;
|
| 1676 |
ankur.sing |
45 |
in.shop2020.model.v1.user.UserContextService.Client uClient;
|
| 1879 |
ankur.sing |
46 |
|
| 3125 |
rajveer |
47 |
TransactionClient tsc;
|
| 1879 |
ankur.sing |
48 |
in.shop2020.model.v1.order.TransactionService.Client tClient;
|
|
|
49 |
|
| 1676 |
ankur.sing |
50 |
public RegisteredUsersGenerator() {
|
|
|
51 |
try {
|
| 3125 |
rajveer |
52 |
usc = new UserClient();
|
| 1676 |
ankur.sing |
53 |
uClient = usc.getClient();
|
| 1879 |
ankur.sing |
54 |
|
| 3125 |
rajveer |
55 |
tsc = new TransactionClient();
|
| 1879 |
ankur.sing |
56 |
tClient = tsc.getClient();
|
| 1676 |
ankur.sing |
57 |
} catch (Exception e) {
|
| 3213 |
chandransh |
58 |
logger.error("Error initializing connection to user or order service", e);
|
| 1676 |
ankur.sing |
59 |
}
|
|
|
60 |
}
|
|
|
61 |
|
|
|
62 |
/**
|
|
|
63 |
* This method is used in RegisteredUsersController.
|
|
|
64 |
* If any registered user(s) exist(s), then returns ByteArrayOutputStream containing user(s) data,
|
|
|
65 |
* otherwise returns null
|
|
|
66 |
* @param startDate -- inclusive
|
|
|
67 |
* @param endDate -- inclusive
|
|
|
68 |
* @return
|
|
|
69 |
*/
|
| 4817 |
anupam.sin |
70 |
public ByteArrayOutputStream generateRegisteredUsersReport(long startDate, long endDate) {
|
| 1676 |
ankur.sing |
71 |
List<User> users = null;
|
| 4817 |
anupam.sin |
72 |
|
| 1676 |
ankur.sing |
73 |
try {
|
| 4817 |
anupam.sin |
74 |
users = uClient.getAllUsers(UserType.USER, startDate, endDate);
|
| 1676 |
ankur.sing |
75 |
} catch (TException e) {
|
| 3213 |
chandransh |
76 |
logger.error("Unable to get all users", e);
|
| 1676 |
ankur.sing |
77 |
}
|
|
|
78 |
if(users == null || users.isEmpty()) {
|
|
|
79 |
return null;
|
|
|
80 |
}
|
|
|
81 |
// Preparing XLS file for output
|
|
|
82 |
return getSpreadSheetData(users);
|
|
|
83 |
|
|
|
84 |
}
|
|
|
85 |
|
|
|
86 |
// Prepares the XLS worksheet object and fills in the data with proper
|
|
|
87 |
// formatting
|
|
|
88 |
private ByteArrayOutputStream getSpreadSheetData(List<User> users) {
|
|
|
89 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
90 |
|
|
|
91 |
Workbook wb = new HSSFWorkbook();
|
|
|
92 |
|
|
|
93 |
Font font = wb.createFont();
|
|
|
94 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
95 |
CellStyle style = wb.createCellStyle();
|
|
|
96 |
style.setFont(font);
|
|
|
97 |
|
|
|
98 |
CellStyle styleWT = wb.createCellStyle();
|
|
|
99 |
styleWT.setWrapText(true);
|
|
|
100 |
|
| 4817 |
anupam.sin |
101 |
createRegisteredUserSheet(users, wb, style, styleWT);
|
|
|
102 |
// Write the workbook to the output stream
|
|
|
103 |
try {
|
|
|
104 |
wb.write(baosXLS);
|
|
|
105 |
baosXLS.close();
|
|
|
106 |
} catch (IOException e) {
|
|
|
107 |
logger.error("Unable to write the registered orders report to the byte array", e);
|
|
|
108 |
}
|
|
|
109 |
return baosXLS;
|
|
|
110 |
}
|
|
|
111 |
|
|
|
112 |
private void createRegisteredUserSheet(List<User> users, Workbook wb,
|
|
|
113 |
CellStyle style, CellStyle styleWT) {
|
|
|
114 |
Sheet registeredUserSheet = wb.createSheet("User");
|
| 1676 |
ankur.sing |
115 |
short userSerialNo = 0;
|
|
|
116 |
|
| 4817 |
anupam.sin |
117 |
Row titleRow = registeredUserSheet.createRow(userSerialNo++);
|
| 1676 |
ankur.sing |
118 |
Cell titleCell = titleRow.createCell(0);
|
|
|
119 |
titleCell.setCellValue("Registered Users");
|
|
|
120 |
titleCell.setCellStyle(style);
|
|
|
121 |
|
| 4817 |
anupam.sin |
122 |
registeredUserSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
| 1676 |
ankur.sing |
123 |
|
| 4817 |
anupam.sin |
124 |
Row headerRow = registeredUserSheet.createRow(userSerialNo++);
|
| 1676 |
ankur.sing |
125 |
headerRow.createCell(0).setCellValue("User Id");
|
|
|
126 |
headerRow.createCell(1).setCellValue("Email");
|
|
|
127 |
headerRow.createCell(2).setCellValue("Name");
|
|
|
128 |
headerRow.createCell(3).setCellValue("Communication Email");
|
|
|
129 |
headerRow.createCell(4).setCellValue("Date of Birth");
|
|
|
130 |
headerRow.createCell(5).setCellValue("Sex");
|
|
|
131 |
headerRow.createCell(6).setCellValue("Mobile");
|
| 1684 |
ankur.sing |
132 |
headerRow.createCell(7).setCellValue("Registered On");
|
|
|
133 |
headerRow.createCell(8).setCellValue("Last Login");
|
| 1879 |
ankur.sing |
134 |
headerRow.createCell(9).setCellValue("Successful Orders");
|
|
|
135 |
headerRow.createCell(10).setCellValue("Pending/Failed Orders");
|
|
|
136 |
headerRow.createCell(11).setCellValue("Items in Cart");
|
| 4817 |
anupam.sin |
137 |
registeredUserSheet.setColumnWidth(11, 20 * 256); // set width of items in cart column to 20 chars
|
| 1676 |
ankur.sing |
138 |
|
|
|
139 |
Calendar calendar = Calendar.getInstance();
|
|
|
140 |
Row contentRow;
|
| 4817 |
anupam.sin |
141 |
float defaultRowHeight = registeredUserSheet.getDefaultRowHeightInPoints();
|
| 1684 |
ankur.sing |
142 |
DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
| 1879 |
ankur.sing |
143 |
Date currentDate = new Date();
|
| 1676 |
ankur.sing |
144 |
for (User u : users) {
|
|
|
145 |
userSerialNo++;
|
| 4817 |
anupam.sin |
146 |
contentRow = registeredUserSheet.createRow(userSerialNo);
|
| 1676 |
ankur.sing |
147 |
contentRow.createCell(0).setCellValue(u.getUserId());
|
|
|
148 |
contentRow.createCell(1).setCellValue(u.getEmail());
|
|
|
149 |
contentRow.createCell(2).setCellValue(u.getName());
|
|
|
150 |
contentRow.createCell(3).setCellValue(u.getCommunicationEmail());
|
|
|
151 |
contentRow.createCell(4).setCellValue(u.getDateOfBirth());
|
|
|
152 |
// calendar.setTimeInMillis
|
|
|
153 |
contentRow.createCell(5).setCellValue(u.getSex().name());
|
|
|
154 |
contentRow.createCell(6).setCellValue(u.getMobileNumber());
|
| 5326 |
rajveer |
155 |
calendar.setTimeInMillis(u.getActiveSince());
|
|
|
156 |
contentRow.createCell(7).setCellValue(formatter.format(calendar.getTime()));
|
|
|
157 |
calendar.setTimeInMillis(u.getLastLogin());
|
|
|
158 |
contentRow.createCell(8).setCellValue(formatter.format(calendar.getTime()));
|
| 1676 |
ankur.sing |
159 |
|
| 1879 |
ankur.sing |
160 |
|
|
|
161 |
|
| 1684 |
ankur.sing |
162 |
try {
|
| 1879 |
ankur.sing |
163 |
List<Order> orders = tClient.getOrdersForCustomer(u.getUserId(), 0, currentDate.getTime(), null);
|
|
|
164 |
int noOfFailedOrders = 0, noOfValidOrders = 0;
|
|
|
165 |
for(Order order : orders) {
|
|
|
166 |
if(order.getStatus().getValue() < 3) {
|
|
|
167 |
noOfFailedOrders++;
|
|
|
168 |
} else {
|
|
|
169 |
noOfValidOrders++;
|
|
|
170 |
}
|
|
|
171 |
}
|
|
|
172 |
contentRow.createCell(9).setCellValue(noOfValidOrders);
|
|
|
173 |
contentRow.createCell(10).setCellValue(noOfFailedOrders);
|
|
|
174 |
} catch (TransactionServiceException e) {
|
| 3213 |
chandransh |
175 |
logger.error("Error while getting orders for the customer", e);
|
| 4817 |
anupam.sin |
176 |
e.printStackTrace();
|
| 1879 |
ankur.sing |
177 |
} catch (TException e) {
|
| 3213 |
chandransh |
178 |
logger.error("Unable to get orders for the customer", e);
|
| 4817 |
anupam.sin |
179 |
e.printStackTrace();
|
| 1879 |
ankur.sing |
180 |
}
|
|
|
181 |
|
|
|
182 |
String itemList = getCartItems(u.getUserId());
|
|
|
183 |
contentRow.createCell(11).setCellValue(itemList);
|
|
|
184 |
contentRow.getCell(11).setCellStyle(styleWT);
|
| 1684 |
ankur.sing |
185 |
|
| 1676 |
ankur.sing |
186 |
List<Address> addresses = u.getAddresses();
|
|
|
187 |
if(addresses == null || addresses.isEmpty()) {
|
|
|
188 |
continue;
|
|
|
189 |
}
|
| 1879 |
ankur.sing |
190 |
int i = 0, col, defaultAddrCol = 12, maxAddrLength = 0, len, addrColWidth = 40;
|
| 1676 |
ankur.sing |
191 |
String addressString;
|
|
|
192 |
for(Address a : addresses) {
|
|
|
193 |
if(a.getId() == u.getDefaultAddressId()) {
|
|
|
194 |
col = defaultAddrCol;
|
| 1684 |
ankur.sing |
195 |
headerRow.createCell(col).setCellValue("Default Address");
|
| 1676 |
ankur.sing |
196 |
} else {
|
|
|
197 |
i++;
|
|
|
198 |
col = defaultAddrCol + i;
|
|
|
199 |
headerRow.createCell(col).setCellValue("Address " + i);
|
|
|
200 |
}
|
| 4817 |
anupam.sin |
201 |
registeredUserSheet.setColumnWidth(col, addrColWidth * 256); // set width of address column to 40 characters
|
| 1676 |
ankur.sing |
202 |
addressString = generateAddressString(a);
|
|
|
203 |
contentRow.createCell(col).setCellValue(addressString);
|
|
|
204 |
contentRow.getCell(col).setCellStyle(styleWT);
|
|
|
205 |
len = addressString.length();
|
|
|
206 |
if(len > maxAddrLength)
|
|
|
207 |
maxAddrLength = len;
|
|
|
208 |
}
|
| 1879 |
ankur.sing |
209 |
if((len = itemList.length()) > maxAddrLength) {
|
|
|
210 |
maxAddrLength = len;
|
|
|
211 |
}
|
| 1676 |
ankur.sing |
212 |
contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
|
|
|
213 |
}
|
| 1879 |
ankur.sing |
214 |
for (int i = 0; i <= 10; i++) {
|
| 4817 |
anupam.sin |
215 |
registeredUserSheet.autoSizeColumn(i);
|
| 1676 |
ankur.sing |
216 |
}
|
|
|
217 |
}
|
|
|
218 |
|
| 1879 |
ankur.sing |
219 |
private String getCartItems(long userId) {
|
|
|
220 |
String itemList = "";
|
|
|
221 |
try {
|
|
|
222 |
Cart cart = uClient.getCurrentCart(userId);
|
|
|
223 |
if(cart == null) {
|
|
|
224 |
return "";
|
|
|
225 |
}
|
|
|
226 |
List<Line> lines = cart.getLines();
|
|
|
227 |
if(lines == null || lines.isEmpty()) {
|
|
|
228 |
return "";
|
|
|
229 |
}
|
|
|
230 |
boolean firstItem = true;
|
|
|
231 |
for(Line line : lines) {
|
|
|
232 |
if(firstItem) {
|
|
|
233 |
firstItem = false;
|
|
|
234 |
itemList += line.getItemId();
|
|
|
235 |
continue;
|
|
|
236 |
}
|
|
|
237 |
itemList += ", " + line.getItemId();;
|
|
|
238 |
}
|
|
|
239 |
} catch (ShoppingCartException e) {
|
| 3213 |
chandransh |
240 |
logger.error("Error while getting the cart of the user", e);
|
| 1879 |
ankur.sing |
241 |
} catch (TException e) {
|
| 3213 |
chandransh |
242 |
logger.error("Unable to get the cart of the user", e);
|
| 1879 |
ankur.sing |
243 |
}
|
|
|
244 |
return itemList;
|
|
|
245 |
}
|
|
|
246 |
|
| 1676 |
ankur.sing |
247 |
private String generateAddressString(Address a) {
|
|
|
248 |
String addrStr = "";
|
|
|
249 |
addrStr += "Name: " + getStringValue(a.getName());
|
|
|
250 |
addrStr += "; Address: " + getStringValue(a.getLine1());
|
|
|
251 |
addrStr += ", " + getStringValue(a.getLine2());
|
|
|
252 |
addrStr += "; City: " + getStringValue(a.getCity());
|
|
|
253 |
addrStr += "; State: " + getStringValue(a.getState());
|
|
|
254 |
addrStr += "; Pin: " + getStringValue(a.getPin());
|
|
|
255 |
addrStr += "; Phone: " + getStringValue(a.getPhone());
|
|
|
256 |
return addrStr;
|
|
|
257 |
}
|
|
|
258 |
|
|
|
259 |
private String getStringValue(String str) {
|
|
|
260 |
return str == null ? "" : str.trim();
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
public static void main(String[] args) {
|
|
|
264 |
RegisteredUsersGenerator rug = new RegisteredUsersGenerator();
|
|
|
265 |
try {
|
|
|
266 |
String userHome = System.getProperty("user.home");
|
|
|
267 |
FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");
|
| 4817 |
anupam.sin |
268 |
String startDateStr = "01/11/2011";
|
|
|
269 |
String endDateStr = "05/11/2011";
|
|
|
270 |
|
|
|
271 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
|
|
272 |
df.setTimeZone(TimeZone.getTimeZone("IST"));
|
|
|
273 |
Date startDate = null;
|
|
|
274 |
Date endDate = null;
|
|
|
275 |
try {
|
|
|
276 |
startDate = df.parse(startDateStr);
|
|
|
277 |
endDate = df.parse(endDateStr);
|
|
|
278 |
//Calendar cal = Calendar.getInstance();
|
|
|
279 |
//cal.setTime(endDate);
|
|
|
280 |
//endDate.setTime(cal.getTimeInMillis());
|
|
|
281 |
} catch (ParseException pe) {
|
|
|
282 |
System.out.println("Please enter start and end dates in format MM/dd/yyyy");
|
|
|
283 |
}
|
|
|
284 |
ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport(startDate.getTime(), endDate.getTime());
|
| 1676 |
ankur.sing |
285 |
if(baosXLS == null) {
|
|
|
286 |
System.out.println("No data");
|
|
|
287 |
return;
|
|
|
288 |
}
|
|
|
289 |
baosXLS.writeTo(f);
|
|
|
290 |
f.close();
|
|
|
291 |
} catch (FileNotFoundException e) {
|
| 3213 |
chandransh |
292 |
logger.error("Unable to create the registered users report", e);
|
| 1676 |
ankur.sing |
293 |
} catch (IOException e) {
|
| 3213 |
chandransh |
294 |
logger.error("IO Error while creating the registered users report", e);
|
| 1676 |
ankur.sing |
295 |
}
|
|
|
296 |
System.out.println("Successfully generated the registered users report");
|
|
|
297 |
}
|
|
|
298 |
}
|