Rev 1075 | Rev 2011 | 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.OrderStatus;import in.shop2020.model.v1.order.TransactionServiceException;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.Calendar;import java.util.Date;import java.util.GregorianCalendar;import java.util.List;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;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.interceptor.ServletRequestAware;import org.apache.struts2.interceptor.ServletResponseAware;import org.apache.struts2.rest.DefaultHttpHeaders;import org.apache.struts2.rest.HttpHeaders;/**** @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.**/public class HotspotReconciliationController implements ServletResponseAware, ServletRequestAware {//FIXME: Read this configuration from the config server//private String hotspotReconciliationReportPath = "/HotspotReports";private HttpServletRequest request;private HttpServletResponse response;private String id;public HotspotReconciliationController(){}public HttpHeaders index() {return new DefaultHttpHeaders("report");}public HttpHeaders show(){return new DefaultHttpHeaders("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(OrderStatus.BILLED, 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");Calendar date = new GregorianCalendar();int year = date.get(Calendar.YEAR);int month = date.get(Calendar.MONTH) +1;int day = date.get(Calendar.DAY_OF_MONTH);response.setHeader("Content-disposition", "inline; filename=hotspot-reconciliation-" + "-" + year + "-" + month + "-" + day + ".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(0).setCellValue("Billing Number");headerRow.createCell(1).setCellValue("Billing Date");headerRow.createCell(2).setCellValue("Brand");headerRow.createCell(3).setCellValue("Model Name");headerRow.createCell(4).setCellValue("Model Number");headerRow.createCell(5).setCellValue("Color");headerRow.createCell(6).setCellValue("Transfer Price");double totalTransferPrice = 0.0;for(int i = 0; i < orders.size(); i ++) {Order order = orders.get(i);serialNo ++;Row contentRow = sheet.createRow(serialNo);LineItem lineItem = order.getLineitems().get(0);double transferPrice = lineItem.getTransfer_price();totalTransferPrice += transferPrice;contentRow.createCell(0).setCellValue(order.getInvoice_number());contentRow.createCell(1).setCellValue(dateFormat.format(new Date(order.getBilling_timestamp())));contentRow.createCell(2).setCellValue(getValueForEmptyString(lineItem.getBrand()));contentRow.createCell(3).setCellValue(getValueForEmptyString(lineItem.getModel_name()));contentRow.createCell(4).setCellValue(getValueForEmptyString(lineItem.getModel_number()));contentRow.createCell(5).setCellValue(getValueForEmptyString(lineItem.getColor()));contentRow.createCell(6).setCellValue(transferPrice);}sheet.createRow(serialNo ++);Row contentRow = sheet.createRow(serialNo);contentRow.createCell(0).setCellValue("Total Transfer Price");contentRow.createCell(6).setCellValue(totalTransferPrice);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;}@Overridepublic void setServletResponse(HttpServletResponse response) {this.response = response;}private String getValueForEmptyString(String s){if(s==null || s.equals(""))return "-";elsereturn s;}}