| 992 |
varun.gupt |
1 |
package in.shop2020.support.controllers;
|
|
|
2 |
|
| 3213 |
chandransh |
3 |
import in.shop2020.model.v1.catalog.InventoryServiceException;
|
| 992 |
varun.gupt |
4 |
import in.shop2020.model.v1.catalog.Warehouse;
|
|
|
5 |
import in.shop2020.model.v1.order.LineItem;
|
|
|
6 |
import in.shop2020.model.v1.order.Order;
|
|
|
7 |
import in.shop2020.model.v1.order.TransactionServiceException;
|
| 2492 |
ankur.sing |
8 |
import in.shop2020.support.utils.ReportsUtils;
|
| 3125 |
rajveer |
9 |
import in.shop2020.thrift.clients.CatalogClient;
|
|
|
10 |
import in.shop2020.thrift.clients.TransactionClient;
|
| 992 |
varun.gupt |
11 |
|
|
|
12 |
import java.text.DateFormat;
|
|
|
13 |
import java.text.SimpleDateFormat;
|
|
|
14 |
import java.text.ParseException;
|
|
|
15 |
|
|
|
16 |
import java.io.ByteArrayOutputStream;
|
|
|
17 |
import java.io.IOException;
|
|
|
18 |
import java.util.ArrayList;
|
|
|
19 |
import java.util.Date;
|
|
|
20 |
import java.util.List;
|
|
|
21 |
|
| 2492 |
ankur.sing |
22 |
import javax.servlet.ServletContext;
|
| 992 |
varun.gupt |
23 |
import javax.servlet.ServletOutputStream;
|
|
|
24 |
import javax.servlet.http.HttpServletRequest;
|
|
|
25 |
import javax.servlet.http.HttpServletResponse;
|
| 2492 |
ankur.sing |
26 |
import javax.servlet.http.HttpSession;
|
| 992 |
varun.gupt |
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;
|
| 2492 |
ankur.sing |
36 |
import org.apache.struts2.convention.annotation.InterceptorRef;
|
|
|
37 |
import org.apache.struts2.convention.annotation.InterceptorRefs;
|
| 3936 |
chandransh |
38 |
import org.apache.struts2.convention.annotation.Result;
|
|
|
39 |
import org.apache.struts2.convention.annotation.Results;
|
| 992 |
varun.gupt |
40 |
import org.apache.struts2.interceptor.ServletRequestAware;
|
|
|
41 |
import org.apache.struts2.interceptor.ServletResponseAware;
|
|
|
42 |
import org.apache.struts2.rest.DefaultHttpHeaders;
|
|
|
43 |
import org.apache.struts2.rest.HttpHeaders;
|
| 2492 |
ankur.sing |
44 |
import org.apache.struts2.util.ServletContextAware;
|
| 3213 |
chandransh |
45 |
import org.apache.thrift.TException;
|
|
|
46 |
import org.slf4j.Logger;
|
|
|
47 |
import org.slf4j.LoggerFactory;
|
| 992 |
varun.gupt |
48 |
|
|
|
49 |
/**
|
|
|
50 |
*
|
|
|
51 |
* @author Varun Gupta
|
|
|
52 |
* @version 1.0
|
|
|
53 |
* @description HotspotReconciliationController handles requests to generate a reconciliation
|
|
|
54 |
* report in XLS format for all the transactions with HotSpot within a given date range.
|
|
|
55 |
*
|
|
|
56 |
*/
|
|
|
57 |
|
| 2492 |
ankur.sing |
58 |
@InterceptorRefs({
|
|
|
59 |
@InterceptorRef("defaultStack"),
|
|
|
60 |
@InterceptorRef("login")
|
|
|
61 |
})
|
| 3936 |
chandransh |
62 |
@Results({
|
|
|
63 |
@Result(name="authfail", type="redirectAction", params = {"actionName" , "reports"})
|
|
|
64 |
})
|
| 2492 |
ankur.sing |
65 |
public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware, ServletContextAware {
|
|
|
66 |
|
| 3213 |
chandransh |
67 |
private static Logger logger = LoggerFactory.getLogger(HotspotReconciliationController.class);
|
|
|
68 |
|
| 2008 |
chandransh |
69 |
private enum ReportColumn{
|
| 2360 |
ankur.sing |
70 |
ORDER_ID(0),
|
|
|
71 |
BILLING_NUMBER(1),
|
|
|
72 |
BILLING_DATE(2),
|
|
|
73 |
CUSTOMER_NAME(3),
|
|
|
74 |
BRAND(4),
|
|
|
75 |
MODEL_NAME(5),
|
|
|
76 |
MODEL_NUMBER(6),
|
|
|
77 |
COLOR(7),
|
|
|
78 |
XFER_PRICE(8),
|
|
|
79 |
SELLING_PRICE(9);
|
| 2008 |
chandransh |
80 |
|
|
|
81 |
private int value;
|
|
|
82 |
|
|
|
83 |
ReportColumn(int value) {
|
|
|
84 |
this.value = value;
|
|
|
85 |
}
|
|
|
86 |
public int getValue(){
|
|
|
87 |
return this.value;
|
|
|
88 |
}
|
|
|
89 |
}
|
|
|
90 |
|
| 1075 |
chandransh |
91 |
//FIXME: Read this configuration from the config server
|
| 1884 |
chandransh |
92 |
//private String hotspotReconciliationReportPath = "/HotspotReports";
|
| 992 |
varun.gupt |
93 |
|
|
|
94 |
private HttpServletRequest request;
|
|
|
95 |
private HttpServletResponse response;
|
| 2492 |
ankur.sing |
96 |
private HttpSession session;
|
|
|
97 |
private ServletContext context;
|
| 992 |
varun.gupt |
98 |
private String id;
|
|
|
99 |
|
|
|
100 |
public HotspotReconciliationController(){
|
|
|
101 |
|
|
|
102 |
}
|
|
|
103 |
|
| 2492 |
ankur.sing |
104 |
public String index() {
|
|
|
105 |
if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {
|
| 3936 |
chandransh |
106 |
return "authfail";
|
| 2492 |
ankur.sing |
107 |
}
|
|
|
108 |
return "report";
|
| 992 |
varun.gupt |
109 |
}
|
|
|
110 |
|
|
|
111 |
// Handles the POST request (Form Submission)
|
|
|
112 |
public HttpHeaders create(){
|
|
|
113 |
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
| 3213 |
chandransh |
114 |
|
| 992 |
varun.gupt |
115 |
try {
|
|
|
116 |
//Formatting Form input parameters
|
| 3213 |
chandransh |
117 |
Date startDate = dateFormat.parse(request.getParameter("start"));
|
|
|
118 |
Date endDate = dateFormat.parse(request.getParameter("end"));
|
| 992 |
varun.gupt |
119 |
|
| 3125 |
rajveer |
120 |
CatalogClient csc = new CatalogClient();
|
| 992 |
varun.gupt |
121 |
in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();
|
|
|
122 |
List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);
|
|
|
123 |
|
| 3125 |
rajveer |
124 |
TransactionClient transactionServiceClient = new TransactionClient();
|
| 992 |
varun.gupt |
125 |
in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();
|
|
|
126 |
|
|
|
127 |
List <Order> orders = new ArrayList<Order>();
|
|
|
128 |
|
|
|
129 |
//Retrieving all the orders across all the warehouses
|
|
|
130 |
for(Warehouse warehouse : warehouses) {
|
| 2008 |
chandransh |
131 |
orders.addAll(client.getOrdersByBillingDate(null, startDate.getTime(), endDate.getTime(), warehouse.getId()));
|
| 992 |
varun.gupt |
132 |
}
|
| 3213 |
chandransh |
133 |
logger.debug("Total number of Orders: " + orders.size());
|
| 992 |
varun.gupt |
134 |
|
|
|
135 |
// Preparing XLS file for output
|
|
|
136 |
response.setContentType("application/vnd.ms-excel");
|
|
|
137 |
|
|
|
138 |
|
| 2008 |
chandransh |
139 |
DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");
|
|
|
140 |
response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");
|
|
|
141 |
|
| 992 |
varun.gupt |
142 |
ServletOutputStream sos;
|
|
|
143 |
try {
|
|
|
144 |
ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);
|
|
|
145 |
sos = response.getOutputStream();
|
|
|
146 |
baos.writeTo(sos);
|
|
|
147 |
sos.flush();
|
|
|
148 |
} catch (IOException e) {
|
| 3213 |
chandransh |
149 |
logger.error("Error while streaming the hotspot reconciliation report", e);
|
| 992 |
varun.gupt |
150 |
}
|
|
|
151 |
|
|
|
152 |
} catch (ParseException e) {
|
| 3213 |
chandransh |
153 |
logger.error("Unable to parse the start or end date", e);
|
| 992 |
varun.gupt |
154 |
} catch (TransactionServiceException e) {
|
| 3213 |
chandransh |
155 |
logger.error("Error while getting order information from the transaction service", e);
|
|
|
156 |
} catch (InventoryServiceException e) {
|
|
|
157 |
logger.error("Error while getting the list of warehouses from the catalog service", e);
|
|
|
158 |
} catch (TException e) {
|
|
|
159 |
logger.error("Unable to get the orders or the warehouses", e);
|
|
|
160 |
} catch (Exception e) {
|
|
|
161 |
logger.error("Unexpected exception", e);
|
|
|
162 |
}
|
| 992 |
varun.gupt |
163 |
return new DefaultHttpHeaders("report");
|
|
|
164 |
}
|
|
|
165 |
|
|
|
166 |
// Prepares the XLS worksheet object and fills in the data with proper formatting
|
|
|
167 |
private ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate) {
|
|
|
168 |
ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();
|
|
|
169 |
|
|
|
170 |
DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
|
|
|
171 |
|
|
|
172 |
Workbook wb = new HSSFWorkbook();
|
|
|
173 |
Sheet sheet = wb.createSheet("new sheet");
|
|
|
174 |
short serialNo = 0;
|
|
|
175 |
|
|
|
176 |
// Create the header row and put all the titles in it. Rows are 0 based.
|
|
|
177 |
Font font = wb.createFont();
|
|
|
178 |
font.setBoldweight(Font.BOLDWEIGHT_BOLD);
|
|
|
179 |
CellStyle style = wb.createCellStyle();
|
|
|
180 |
style.setFont(font);
|
|
|
181 |
|
|
|
182 |
Row titleRow = sheet.createRow(serialNo ++);
|
|
|
183 |
Cell titleCell = titleRow.createCell(0);
|
|
|
184 |
titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");
|
|
|
185 |
titleCell.setCellStyle(style);
|
|
|
186 |
|
|
|
187 |
sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));
|
|
|
188 |
|
|
|
189 |
sheet.createRow(serialNo ++);
|
|
|
190 |
|
|
|
191 |
Row headerRow = sheet.createRow(serialNo ++);
|
| 2360 |
ankur.sing |
192 |
headerRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue("Order Id");
|
| 2008 |
chandransh |
193 |
headerRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue("Billing Number");
|
|
|
194 |
headerRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue("Billing Date");
|
|
|
195 |
headerRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue("Customer Name");
|
|
|
196 |
headerRow.createCell(ReportColumn.BRAND.getValue()).setCellValue("Brand");
|
|
|
197 |
headerRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue("Model Name");
|
|
|
198 |
headerRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue("Model Number");
|
|
|
199 |
headerRow.createCell(ReportColumn.COLOR.getValue()).setCellValue("Color");
|
|
|
200 |
headerRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue("Transfer Price");
|
| 2011 |
rajveer |
201 |
headerRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue("Selling Price");
|
| 992 |
varun.gupt |
202 |
|
| 2014 |
rajveer |
203 |
sheet.createRow(serialNo ++);
|
| 992 |
varun.gupt |
204 |
double totalTransferPrice = 0.0;
|
| 2011 |
rajveer |
205 |
double totalSellingPrice = 0.0;
|
| 992 |
varun.gupt |
206 |
|
|
|
207 |
for(int i = 0; i < orders.size(); i ++) {
|
|
|
208 |
Order order = orders.get(i);
|
| 2014 |
rajveer |
209 |
|
|
|
210 |
Row contentRow = sheet.createRow(serialNo++);
|
| 992 |
varun.gupt |
211 |
|
|
|
212 |
LineItem lineItem = order.getLineitems().get(0);
|
|
|
213 |
double transferPrice = lineItem.getTransfer_price();
|
|
|
214 |
totalTransferPrice += transferPrice;
|
|
|
215 |
|
| 2014 |
rajveer |
216 |
double sellingPrice = lineItem.getTotal_price();
|
|
|
217 |
totalSellingPrice += sellingPrice;
|
| 2011 |
rajveer |
218 |
|
| 2360 |
ankur.sing |
219 |
contentRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());
|
| 2008 |
chandransh |
220 |
contentRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());
|
|
|
221 |
contentRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));
|
|
|
222 |
contentRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue(order.getCustomer_name());
|
|
|
223 |
contentRow.createCell(ReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));
|
|
|
224 |
contentRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));
|
|
|
225 |
contentRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));
|
|
|
226 |
contentRow.createCell(ReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));
|
|
|
227 |
contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(transferPrice);
|
| 2011 |
rajveer |
228 |
contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(sellingPrice);
|
| 992 |
varun.gupt |
229 |
}
|
|
|
230 |
sheet.createRow(serialNo ++);
|
|
|
231 |
Row contentRow = sheet.createRow(serialNo);
|
|
|
232 |
contentRow.createCell(0).setCellValue("Total Transfer Price");
|
| 2008 |
chandransh |
233 |
contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(totalTransferPrice);
|
| 2011 |
rajveer |
234 |
contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);
|
| 992 |
varun.gupt |
235 |
sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));
|
|
|
236 |
|
|
|
237 |
// Write the workbook to the output stream
|
|
|
238 |
try {
|
|
|
239 |
wb.write(baosXLS);
|
|
|
240 |
baosXLS.close();
|
|
|
241 |
} catch (IOException e) {
|
| 3213 |
chandransh |
242 |
logger.error("Unable to write the hotspot reconciliation report to the byte array", e);
|
| 992 |
varun.gupt |
243 |
}
|
|
|
244 |
return baosXLS;
|
|
|
245 |
}
|
|
|
246 |
|
|
|
247 |
public String getId(){
|
|
|
248 |
return id;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
public void setId(String id){
|
|
|
252 |
this.id = id;
|
|
|
253 |
}
|
|
|
254 |
|
|
|
255 |
@Override
|
|
|
256 |
public void setServletRequest(HttpServletRequest request) {
|
|
|
257 |
this.request = request;
|
| 2492 |
ankur.sing |
258 |
this.session = request.getSession();
|
| 992 |
varun.gupt |
259 |
}
|
|
|
260 |
|
|
|
261 |
@Override
|
|
|
262 |
public void setServletResponse(HttpServletResponse response) {
|
|
|
263 |
this.response = response;
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
private String getValueForEmptyString(String s){
|
|
|
267 |
if(s==null || s.equals(""))
|
|
|
268 |
return "-";
|
|
|
269 |
else
|
|
|
270 |
return s;
|
|
|
271 |
}
|
| 2492 |
ankur.sing |
272 |
@Override
|
|
|
273 |
public void setServletContext(ServletContext context) {
|
|
|
274 |
this.context = context;
|
|
|
275 |
}
|
|
|
276 |
|
|
|
277 |
public String getServletContextPath() {
|
|
|
278 |
return context.getContextPath();
|
|
|
279 |
}
|
| 2011 |
rajveer |
280 |
}
|