| Line 8... |
Line 8... |
| 8 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
8 |
import in.shop2020.model.v1.user.ShoppingCartException;
|
| 9 |
import in.shop2020.model.v1.user.User;
|
9 |
import in.shop2020.model.v1.user.User;
|
| 10 |
import in.shop2020.model.v1.user.UserContextException;
|
10 |
import in.shop2020.model.v1.user.UserContextException;
|
| 11 |
import in.shop2020.model.v1.user.UserState;
|
11 |
import in.shop2020.model.v1.user.UserState;
|
| 12 |
import in.shop2020.model.v1.user.UserType;
|
12 |
import in.shop2020.model.v1.user.UserType;
|
| - |
|
13 |
import in.shop2020.thrift.clients.CatalogClient;
|
| 13 |
import in.shop2020.thrift.clients.TransactionClient;
|
14 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 14 |
import in.shop2020.thrift.clients.UserClient;
|
15 |
import in.shop2020.thrift.clients.UserClient;
|
| 15 |
|
16 |
|
| 16 |
import java.io.ByteArrayOutputStream;
|
17 |
import java.io.ByteArrayOutputStream;
|
| 17 |
import java.io.FileNotFoundException;
|
18 |
import java.io.FileNotFoundException;
|
| 18 |
import java.io.FileOutputStream;
|
19 |
import java.io.FileOutputStream;
|
| 19 |
import java.io.IOException;
|
20 |
import java.io.IOException;
|
| 20 |
import java.text.DateFormat;
|
21 |
import java.text.DateFormat;
|
| - |
|
22 |
import java.text.ParseException;
|
| 21 |
import java.text.SimpleDateFormat;
|
23 |
import java.text.SimpleDateFormat;
|
| 22 |
import java.util.Calendar;
|
24 |
import java.util.Calendar;
|
| 23 |
import java.util.Date;
|
25 |
import java.util.Date;
|
| 24 |
import java.util.List;
|
26 |
import java.util.List;
|
| - |
|
27 |
import java.util.TimeZone;
|
| 25 |
|
28 |
|
| 26 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
29 |
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
|
| 27 |
import org.apache.poi.ss.usermodel.Cell;
|
30 |
import org.apache.poi.ss.usermodel.Cell;
|
| 28 |
import org.apache.poi.ss.usermodel.CellStyle;
|
31 |
import org.apache.poi.ss.usermodel.CellStyle;
|
| 29 |
import org.apache.poi.ss.usermodel.Font;
|
32 |
import org.apache.poi.ss.usermodel.Font;
|
| Line 63... |
Line 66... |
| 63 |
* otherwise returns null
|
66 |
* otherwise returns null
|
| 64 |
* @param startDate -- inclusive
|
67 |
* @param startDate -- inclusive
|
| 65 |
* @param endDate -- inclusive
|
68 |
* @param endDate -- inclusive
|
| 66 |
* @return
|
69 |
* @return
|
| 67 |
*/
|
70 |
*/
|
| 68 |
public ByteArrayOutputStream generateRegisteredUsersReport() {
|
71 |
public ByteArrayOutputStream generateRegisteredUsersReport(long startDate, long endDate) {
|
| 69 |
List<User> users = null;
|
72 |
List<User> users = null;
|
| - |
|
73 |
|
| 70 |
try {
|
74 |
try {
|
| 71 |
users = uClient.getAllUsers(UserType.USER, -1, -1);
|
75 |
users = uClient.getAllUsers(UserType.USER, startDate, endDate);
|
| 72 |
} catch (TException e) {
|
76 |
} catch (TException e) {
|
| 73 |
logger.error("Unable to get all users", e);
|
77 |
logger.error("Unable to get all users", e);
|
| 74 |
}
|
78 |
}
|
| 75 |
if(users == null || users.isEmpty()) {
|
79 |
if(users == null || users.isEmpty()) {
|
| 76 |
return null;
|
80 |
return null;
|
| Line 93... |
Line 97... |
| 93 |
style.setFont(font);
|
97 |
style.setFont(font);
|
| 94 |
|
98 |
|
| 95 |
CellStyle styleWT = wb.createCellStyle();
|
99 |
CellStyle styleWT = wb.createCellStyle();
|
| 96 |
styleWT.setWrapText(true);
|
100 |
styleWT.setWrapText(true);
|
| 97 |
|
101 |
|
| - |
|
102 |
createRegisteredUserSheet(users, wb, style, styleWT);
|
| - |
|
103 |
// Write the workbook to the output stream
|
| - |
|
104 |
try {
|
| - |
|
105 |
wb.write(baosXLS);
|
| - |
|
106 |
baosXLS.close();
|
| - |
|
107 |
} catch (IOException e) {
|
| - |
|
108 |
logger.error("Unable to write the registered orders report to the byte array", e);
|
| - |
|
109 |
}
|
| - |
|
110 |
return baosXLS;
|
| - |
|
111 |
}
|
| - |
|
112 |
|
| - |
|
113 |
private void createRegisteredUserSheet(List<User> users, Workbook wb,
|
| - |
|
114 |
CellStyle style, CellStyle styleWT) {
|
| 98 |
Sheet userSheet = wb.createSheet("User");
|
115 |
Sheet registeredUserSheet = wb.createSheet("User");
|
| 99 |
short userSerialNo = 0;
|
116 |
short userSerialNo = 0;
|
| 100 |
|
117 |
|
| 101 |
Row titleRow = userSheet.createRow(userSerialNo++);
|
118 |
Row titleRow = registeredUserSheet.createRow(userSerialNo++);
|
| 102 |
Cell titleCell = titleRow.createCell(0);
|
119 |
Cell titleCell = titleRow.createCell(0);
|
| 103 |
titleCell.setCellValue("Registered Users");
|
120 |
titleCell.setCellValue("Registered Users");
|
| 104 |
titleCell.setCellStyle(style);
|
121 |
titleCell.setCellStyle(style);
|
| 105 |
|
122 |
|
| 106 |
userSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
123 |
registeredUserSheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
| 107 |
|
124 |
|
| 108 |
Row headerRow = userSheet.createRow(userSerialNo++);
|
125 |
Row headerRow = registeredUserSheet.createRow(userSerialNo++);
|
| 109 |
headerRow.createCell(0).setCellValue("User Id");
|
126 |
headerRow.createCell(0).setCellValue("User Id");
|
| 110 |
headerRow.createCell(1).setCellValue("Email");
|
127 |
headerRow.createCell(1).setCellValue("Email");
|
| 111 |
headerRow.createCell(2).setCellValue("Name");
|
128 |
headerRow.createCell(2).setCellValue("Name");
|
| 112 |
headerRow.createCell(3).setCellValue("Communication Email");
|
129 |
headerRow.createCell(3).setCellValue("Communication Email");
|
| 113 |
headerRow.createCell(4).setCellValue("Date of Birth");
|
130 |
headerRow.createCell(4).setCellValue("Date of Birth");
|
| Line 116... |
Line 133... |
| 116 |
headerRow.createCell(7).setCellValue("Registered On");
|
133 |
headerRow.createCell(7).setCellValue("Registered On");
|
| 117 |
headerRow.createCell(8).setCellValue("Last Login");
|
134 |
headerRow.createCell(8).setCellValue("Last Login");
|
| 118 |
headerRow.createCell(9).setCellValue("Successful Orders");
|
135 |
headerRow.createCell(9).setCellValue("Successful Orders");
|
| 119 |
headerRow.createCell(10).setCellValue("Pending/Failed Orders");
|
136 |
headerRow.createCell(10).setCellValue("Pending/Failed Orders");
|
| 120 |
headerRow.createCell(11).setCellValue("Items in Cart");
|
137 |
headerRow.createCell(11).setCellValue("Items in Cart");
|
| 121 |
userSheet.setColumnWidth(11, 20 * 256); // set width of items in cart column to 20 chars
|
138 |
registeredUserSheet.setColumnWidth(11, 20 * 256); // set width of items in cart column to 20 chars
|
| 122 |
|
139 |
|
| 123 |
Calendar calendar = Calendar.getInstance();
|
140 |
Calendar calendar = Calendar.getInstance();
|
| 124 |
Row contentRow;
|
141 |
Row contentRow;
|
| 125 |
float defaultRowHeight = userSheet.getDefaultRowHeightInPoints();
|
142 |
float defaultRowHeight = registeredUserSheet.getDefaultRowHeightInPoints();
|
| 126 |
UserState uState;
|
143 |
UserState uState;
|
| 127 |
DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
144 |
DateFormat formatter = new SimpleDateFormat("EEE, dd-MMM-yyyy hh:mm a");
|
| 128 |
Date currentDate = new Date();
|
145 |
Date currentDate = new Date();
|
| 129 |
for (User u : users) {
|
146 |
for (User u : users) {
|
| 130 |
userSerialNo++;
|
147 |
userSerialNo++;
|
| 131 |
contentRow = userSheet.createRow(userSerialNo);
|
148 |
contentRow = registeredUserSheet.createRow(userSerialNo);
|
| 132 |
contentRow.createCell(0).setCellValue(u.getUserId());
|
149 |
contentRow.createCell(0).setCellValue(u.getUserId());
|
| 133 |
contentRow.createCell(1).setCellValue(u.getEmail());
|
150 |
contentRow.createCell(1).setCellValue(u.getEmail());
|
| 134 |
contentRow.createCell(2).setCellValue(u.getName());
|
151 |
contentRow.createCell(2).setCellValue(u.getName());
|
| 135 |
contentRow.createCell(3).setCellValue(u.getCommunicationEmail());
|
152 |
contentRow.createCell(3).setCellValue(u.getCommunicationEmail());
|
| 136 |
contentRow.createCell(4).setCellValue(u.getDateOfBirth());
|
153 |
contentRow.createCell(4).setCellValue(u.getDateOfBirth());
|
| Line 164... |
Line 181... |
| 164 |
}
|
181 |
}
|
| 165 |
contentRow.createCell(9).setCellValue(noOfValidOrders);
|
182 |
contentRow.createCell(9).setCellValue(noOfValidOrders);
|
| 166 |
contentRow.createCell(10).setCellValue(noOfFailedOrders);
|
183 |
contentRow.createCell(10).setCellValue(noOfFailedOrders);
|
| 167 |
} catch (TransactionServiceException e) {
|
184 |
} catch (TransactionServiceException e) {
|
| 168 |
logger.error("Error while getting orders for the customer", e);
|
185 |
logger.error("Error while getting orders for the customer", e);
|
| - |
|
186 |
e.printStackTrace();
|
| 169 |
} catch (TException e) {
|
187 |
} catch (TException e) {
|
| 170 |
logger.error("Unable to get orders for the customer", e);
|
188 |
logger.error("Unable to get orders for the customer", e);
|
| - |
|
189 |
e.printStackTrace();
|
| 171 |
}
|
190 |
}
|
| 172 |
|
191 |
|
| 173 |
String itemList = getCartItems(u.getUserId());
|
192 |
String itemList = getCartItems(u.getUserId());
|
| 174 |
contentRow.createCell(11).setCellValue(itemList);
|
193 |
contentRow.createCell(11).setCellValue(itemList);
|
| 175 |
contentRow.getCell(11).setCellStyle(styleWT);
|
194 |
contentRow.getCell(11).setCellStyle(styleWT);
|
| Line 187... |
Line 206... |
| 187 |
} else {
|
206 |
} else {
|
| 188 |
i++;
|
207 |
i++;
|
| 189 |
col = defaultAddrCol + i;
|
208 |
col = defaultAddrCol + i;
|
| 190 |
headerRow.createCell(col).setCellValue("Address " + i);
|
209 |
headerRow.createCell(col).setCellValue("Address " + i);
|
| 191 |
}
|
210 |
}
|
| 192 |
userSheet.setColumnWidth(col, addrColWidth * 256); // set width of address column to 40 characters
|
211 |
registeredUserSheet.setColumnWidth(col, addrColWidth * 256); // set width of address column to 40 characters
|
| 193 |
addressString = generateAddressString(a);
|
212 |
addressString = generateAddressString(a);
|
| 194 |
contentRow.createCell(col).setCellValue(addressString);
|
213 |
contentRow.createCell(col).setCellValue(addressString);
|
| 195 |
contentRow.getCell(col).setCellStyle(styleWT);
|
214 |
contentRow.getCell(col).setCellStyle(styleWT);
|
| 196 |
len = addressString.length();
|
215 |
len = addressString.length();
|
| 197 |
if(len > maxAddrLength)
|
216 |
if(len > maxAddrLength)
|
| Line 201... |
Line 220... |
| 201 |
maxAddrLength = len;
|
220 |
maxAddrLength = len;
|
| 202 |
}
|
221 |
}
|
| 203 |
contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
|
222 |
contentRow.setHeightInPoints((maxAddrLength / addrColWidth + 1) * defaultRowHeight); // Setting Row Height
|
| 204 |
}
|
223 |
}
|
| 205 |
for (int i = 0; i <= 10; i++) {
|
224 |
for (int i = 0; i <= 10; i++) {
|
| 206 |
userSheet.autoSizeColumn(i);
|
225 |
registeredUserSheet.autoSizeColumn(i);
|
| 207 |
}
|
226 |
}
|
| 208 |
|
- |
|
| 209 |
// Write the workbook to the output stream
|
- |
|
| 210 |
try {
|
- |
|
| 211 |
wb.write(baosXLS);
|
- |
|
| 212 |
baosXLS.close();
|
- |
|
| 213 |
} catch (IOException e) {
|
- |
|
| 214 |
logger.error("Unable to write the registered orders report to the byte array", e);
|
- |
|
| 215 |
}
|
- |
|
| 216 |
return baosXLS;
|
- |
|
| 217 |
}
|
227 |
}
|
| 218 |
|
228 |
|
| 219 |
private String getCartItems(long userId) {
|
229 |
private String getCartItems(long userId) {
|
| 220 |
String itemList = "";
|
230 |
String itemList = "";
|
| 221 |
try {
|
231 |
try {
|
| Line 263... |
Line 273... |
| 263 |
public static void main(String[] args) {
|
273 |
public static void main(String[] args) {
|
| 264 |
RegisteredUsersGenerator rug = new RegisteredUsersGenerator();
|
274 |
RegisteredUsersGenerator rug = new RegisteredUsersGenerator();
|
| 265 |
try {
|
275 |
try {
|
| 266 |
String userHome = System.getProperty("user.home");
|
276 |
String userHome = System.getProperty("user.home");
|
| 267 |
FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");
|
277 |
FileOutputStream f = new FileOutputStream(userHome + "/registered-users.xls");
|
| - |
|
278 |
String startDateStr = "01/11/2011";
|
| - |
|
279 |
String endDateStr = "05/11/2011";
|
| - |
|
280 |
|
| - |
|
281 |
DateFormat df = new SimpleDateFormat("MM/dd/yyyy");
|
| - |
|
282 |
df.setTimeZone(TimeZone.getTimeZone("IST"));
|
| - |
|
283 |
Date startDate = null;
|
| - |
|
284 |
Date endDate = null;
|
| - |
|
285 |
try {
|
| - |
|
286 |
startDate = df.parse(startDateStr);
|
| - |
|
287 |
endDate = df.parse(endDateStr);
|
| - |
|
288 |
//Calendar cal = Calendar.getInstance();
|
| - |
|
289 |
//cal.setTime(endDate);
|
| - |
|
290 |
//endDate.setTime(cal.getTimeInMillis());
|
| - |
|
291 |
} catch (ParseException pe) {
|
| - |
|
292 |
System.out.println("Please enter start and end dates in format MM/dd/yyyy");
|
| - |
|
293 |
}
|
| 268 |
ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport();
|
294 |
ByteArrayOutputStream baosXLS = rug.generateRegisteredUsersReport(startDate.getTime(), endDate.getTime());
|
| 269 |
if(baosXLS == null) {
|
295 |
if(baosXLS == null) {
|
| 270 |
System.out.println("No data");
|
296 |
System.out.println("No data");
|
| 271 |
return;
|
297 |
return;
|
| 272 |
}
|
298 |
}
|
| 273 |
baosXLS.writeTo(f);
|
299 |
baosXLS.writeTo(f);
|