Rev 2360 | Rev 3213 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.controllers;import in.shop2020.model.v1.catalog.Warehouse;import in.shop2020.model.v1.order.LineItem;import in.shop2020.model.v1.order.Order;import in.shop2020.model.v1.order.TransactionServiceException;import in.shop2020.support.utils.ReportsUtils;import in.shop2020.thrift.clients.CatalogServiceClient;import in.shop2020.thrift.clients.TransactionServiceClient;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.text.ParseException;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.util.ArrayList;import java.util.Date;import java.util.List;import javax.servlet.ServletContext;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import org.apache.poi.hssf.usermodel.HSSFWorkbook;import org.apache.poi.ss.usermodel.Cell;import org.apache.poi.ss.usermodel.CellStyle;import org.apache.poi.ss.usermodel.Font;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.poi.ss.util.CellRangeAddress;import org.apache.struts2.convention.annotation.InterceptorRef;import org.apache.struts2.convention.annotation.InterceptorRefs;import org.apache.struts2.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;import org.apache.struts2.util.ServletContextAware;/**** @author Varun Gupta* @version 1.0* @description HotspotReconciliationController handles requests to generate a reconciliation* report in XLS format for all the transactions with HotSpot within a given date range.**/@InterceptorRefs({@InterceptorRef("defaultStack"),@InterceptorRef("login")})public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware, ServletContextAware {private enum ReportColumn{ORDER_ID(0),BILLING_NUMBER(1),BILLING_DATE(2),CUSTOMER_NAME(3),BRAND(4),MODEL_NAME(5),MODEL_NUMBER(6),COLOR(7),XFER_PRICE(8),SELLING_PRICE(9);private int value;ReportColumn(int value) {this.value = value;}public int getValue(){return this.value;}}//FIXME: Read this configuration from the config server//private String hotspotReconciliationReportPath = "/HotspotReports";private HttpServletRequest request;private HttpServletResponse response;private HttpSession session;private ServletContext context;private String id;public HotspotReconciliationController(){}public String index() {if(!ReportsUtils.canAccessReport((Long)session.getAttribute(ReportsUtils.ROLE), request.getServletPath())) {return "exception";}return "report";}// Handles the POST request (Form Submission)public HttpHeaders create(){DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");Date startDate = null;Date endDate = null;try {//Formatting Form input parametersstartDate = dateFormat.parse(request.getParameter("start"));endDate = dateFormat.parse(request.getParameter("end"));CatalogServiceClient csc = new CatalogServiceClient();in.shop2020.model.v1.catalog.InventoryService.Client catalogClient= csc.getClient();List<Warehouse> warehouses = catalogClient.getAllWarehouses(true);TransactionServiceClient transactionServiceClient = new TransactionServiceClient();in.shop2020.model.v1.order.TransactionService.Client client = transactionServiceClient.getClient();List <Order> orders = new ArrayList<Order>();//Retrieving all the orders across all the warehousesfor(Warehouse warehouse : warehouses) {orders.addAll(client.getOrdersByBillingDate(null, startDate.getTime(), endDate.getTime(), warehouse.getId()));}System.out.println("Total number of Orders: " + orders.size());// Preparing XLS file for outputresponse.setContentType("application/vnd.ms-excel");DateFormat dateFormatForFile = new SimpleDateFormat("dd.MM.yyyy");response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-from-" + dateFormatForFile.format(startDate) + "-" + dateFormatForFile.format(endDate) + ".xls");ServletOutputStream sos;try {ByteArrayOutputStream baos = getSpreadSheetData(orders, startDate, endDate);sos = response.getOutputStream();baos.writeTo(sos);sos.flush();} catch (IOException e) {e.printStackTrace();}} catch (ParseException e) {e.printStackTrace();} catch (TransactionServiceException e) {e.printStackTrace();} catch (Exception e) {e.printStackTrace();} finally {System.out.println(startDate.getTime() + " | " + endDate.getTime());}return new DefaultHttpHeaders("report");}// Prepares the XLS worksheet object and fills in the data with proper formattingprivate ByteArrayOutputStream getSpreadSheetData(List <Order> orders, Date startDate, Date endDate) {ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");Workbook wb = new HSSFWorkbook();Sheet sheet = wb.createSheet("new sheet");short serialNo = 0;// Create the header row and put all the titles in it. Rows are 0 based.Font font = wb.createFont();font.setBoldweight(Font.BOLDWEIGHT_BOLD);CellStyle style = wb.createCellStyle();style.setFont(font);Row titleRow = sheet.createRow(serialNo ++);Cell titleCell = titleRow.createCell(0);titleCell.setCellValue("HotSpot Reconciliation Report (" + dateFormat.format(startDate) + " - " + dateFormat.format(endDate) + ")");titleCell.setCellStyle(style);sheet.addMergedRegion(new CellRangeAddress(0, 0, 0, 6));sheet.createRow(serialNo ++);Row headerRow = sheet.createRow(serialNo ++);headerRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue("Order Id");headerRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue("Billing Number");headerRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue("Billing Date");headerRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue("Customer Name");headerRow.createCell(ReportColumn.BRAND.getValue()).setCellValue("Brand");headerRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue("Model Name");headerRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue("Model Number");headerRow.createCell(ReportColumn.COLOR.getValue()).setCellValue("Color");headerRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue("Transfer Price");headerRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue("Selling Price");sheet.createRow(serialNo ++);double totalTransferPrice = 0.0;double totalSellingPrice = 0.0;for(int i = 0; i < orders.size(); i ++) {Order order = orders.get(i);Row contentRow = sheet.createRow(serialNo++);LineItem lineItem = order.getLineitems().get(0);double transferPrice = lineItem.getTransfer_price();totalTransferPrice += transferPrice;double sellingPrice = lineItem.getTotal_price();totalSellingPrice += sellingPrice;contentRow.createCell(ReportColumn.ORDER_ID.getValue()).setCellValue(order.getId());contentRow.createCell(ReportColumn.BILLING_NUMBER.getValue()).setCellValue(order.getInvoice_number());contentRow.createCell(ReportColumn.BILLING_DATE.getValue()).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));contentRow.createCell(ReportColumn.CUSTOMER_NAME.getValue()).setCellValue(order.getCustomer_name());contentRow.createCell(ReportColumn.BRAND.getValue()).setCellValue(getValueForEmptyString(lineItem.getBrand()));contentRow.createCell(ReportColumn.MODEL_NAME.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_name()));contentRow.createCell(ReportColumn.MODEL_NUMBER.getValue()).setCellValue(getValueForEmptyString(lineItem.getModel_number()));contentRow.createCell(ReportColumn.COLOR.getValue()).setCellValue(getValueForEmptyString(lineItem.getColor()));contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(transferPrice);contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(sellingPrice);}sheet.createRow(serialNo ++);Row contentRow = sheet.createRow(serialNo);contentRow.createCell(0).setCellValue("Total Transfer Price");contentRow.createCell(ReportColumn.XFER_PRICE.getValue()).setCellValue(totalTransferPrice);contentRow.createCell(ReportColumn.SELLING_PRICE.getValue()).setCellValue(totalSellingPrice);sheet.addMergedRegion(new CellRangeAddress(serialNo, serialNo, 0, 5));// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {e.printStackTrace();}return baosXLS;}public String getId(){return id;}public void setId(String id){this.id = id;}@Overridepublic void setServletRequest(HttpServletRequest request) {this.request = request;this.session = request.getSession();}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}private String getValueForEmptyString(String s){if(s==null || s.equals(""))return "-";elsereturn s;}@Overridepublic void setServletContext(ServletContext context) {this.context = context;}public String getServletContextPath() {return context.getContextPath();}}