Rev 919 | Rev 3062 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.services;import in.shop2020.logistics.LogisticsServiceException;import in.shop2020.logistics.Provider;import in.shop2020.model.v1.catalog.InventoryServiceException;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.LogisticsServiceClient;import in.shop2020.thrift.clients.TransactionServiceClient;import java.io.ByteArrayOutputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.util.Date;import java.util.List;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.CreationHelper;import org.apache.poi.ss.usermodel.Row;import org.apache.poi.ss.usermodel.Sheet;import org.apache.poi.ss.usermodel.Workbook;import org.apache.thrift.TException;public class CourierDetailsGenerator {private TransactionServiceClient tsc = null;private CatalogServiceClient csc = null;private LogisticsServiceClient lsc = null;public CourierDetailsGenerator(){try {tsc = new TransactionServiceClient();csc = new CatalogServiceClient();lsc = new LogisticsServiceClient();} catch (Exception e) {e.printStackTrace();}}public ByteArrayOutputStream generateCourierDetails(long warehouseId, long providerId){ByteArrayOutputStream baosXLS = new ByteArrayOutputStream();in.shop2020.model.v1.order.TransactionService.Client txnClient = tsc.getClient();in.shop2020.model.v1.catalog.InventoryService.Client inventoryClient = csc.getClient();in.shop2020.logistics.LogisticsService.Client logisticsClient = lsc.getClient();List<Order> orders = null;Warehouse warehouse = null;Provider provider = null;try {orders = txnClient.getAllOrders(OrderStatus.BILLED, 0L, new Date().getTime(), warehouseId);warehouse = inventoryClient.getWarehouse(warehouseId);provider = logisticsClient.getProvider(providerId);} catch (TException e1) {e1.printStackTrace();return baosXLS;} catch (InventoryServiceException e) {e.printStackTrace();return baosXLS;} catch (LogisticsServiceException e) {e.printStackTrace();return baosXLS;} catch (TransactionServiceException e) {e.printStackTrace();return baosXLS;}Workbook wb = new HSSFWorkbook();CreationHelper createHelper = wb.getCreationHelper();Sheet sheet = wb.createSheet("new sheet");CellStyle dateCellStyle = wb.createCellStyle();dateCellStyle.setDataFormat(createHelper.createDataFormat().getFormat("d/m/yyyy"));CellStyle weightStyle = wb.createCellStyle();weightStyle.setDataFormat(createHelper.createDataFormat().getFormat("0.000"));// Create the header row and put all the titles in it. Rows are 0 based.Row headerRow = sheet.createRow((short)0);headerRow.createCell(0).setCellValue("Sl No");headerRow.createCell(1).setCellValue("AWB No");headerRow.createCell(2).setCellValue("AWB Date");headerRow.createCell(3).setCellValue("Order No");headerRow.createCell(4).setCellValue("Name");headerRow.createCell(5).setCellValue("Address 1");headerRow.createCell(6).setCellValue("Address 2");headerRow.createCell(7).setCellValue("City");headerRow.createCell(8).setCellValue("State");headerRow.createCell(9).setCellValue("Pin Code");headerRow.createCell(10).setCellValue("Telephone No 1");headerRow.createCell(11).setCellValue("Telephone No 2");headerRow.createCell(12).setCellValue("Paymode");headerRow.createCell(13).setCellValue("Amount to be Collected");headerRow.createCell(14).setCellValue("Shipment Value");headerRow.createCell(15).setCellValue("Item ID");headerRow.createCell(16).setCellValue("Packet Weight(in Kg)");headerRow.createCell(17).setCellValue("Product Name");headerRow.createCell(18).setCellValue("Pickup Location");headerRow.createCell(19).setCellValue("Customer A/C Code");Date awbDate = new Date();int serialNo = 0;for(int i = 0; i<orders.size(); i++){Order order = orders.get(i);if(order.getLogistics_provider_id()!=providerId)continue;serialNo++;Row contentRow = sheet.createRow((short)serialNo);contentRow.createCell(0).setCellValue(serialNo);contentRow.createCell(1).setCellValue(order.getAirwaybill_no());Cell awbDateCell = contentRow.createCell(2);awbDateCell.setCellValue(awbDate);awbDateCell.setCellStyle(dateCellStyle);contentRow.createCell(3).setCellValue(order.getId());contentRow.createCell(4).setCellValue(getValueForEmptyString(order.getCustomer_name()));contentRow.createCell(5).setCellValue(getValueForEmptyString(order.getCustomer_address1()));contentRow.createCell(6).setCellValue(getValueForEmptyString(order.getCustomer_address2()));contentRow.createCell(7).setCellValue(getValueForEmptyString(order.getCustomer_city()));contentRow.createCell(8).setCellValue(getValueForEmptyString(order.getCustomer_state()));contentRow.createCell(9).setCellValue(getValueForEmptyString(order.getCustomer_pincode()));contentRow.createCell(10).setCellValue(getValueForEmptyString(order.getCustomer_mobilenumber()));contentRow.createCell(11).setCellValue("-");contentRow.createCell(12).setCellValue("Prepaid");contentRow.createCell(13).setCellValue(0);List<LineItem> lineItems = order.getLineitems();LineItem lineItem = lineItems.get(0);contentRow.createCell(14).setCellValue(order.getTotal_amount());contentRow.createCell(15).setCellValue(lineItem.getId());Cell weightCell = contentRow.createCell(16);weightCell.setCellValue(lineItem.getTotal_weight());weightCell.setCellStyle(weightStyle);contentRow.createCell(17).setCellValue(lineItem.getBrand() + " " + lineItem.getModel_number() + " " + lineItem.getModel_name() + " " + lineItem.getColor());contentRow.createCell(18).setCellValue(warehouse.getLocation());contentRow.createCell(19).setCellValue(provider.getAccountNo());}// Write the workbook to the output streamtry {wb.write(baosXLS);baosXLS.close();} catch (IOException e) {e.printStackTrace();}return baosXLS;}private String getValueForEmptyString(String s){if(s==null || s.equals(""))return "-";elsereturn s;}/*** @param args*/public static void main(String[] args) {System.out.println("Hey There");CourierDetailsGenerator g = new CourierDetailsGenerator();try {FileOutputStream f = new FileOutputStream("/home/ashish/Downloads/courier-details.xls");ByteArrayOutputStream baosXLS = g.generateCourierDetails(1, 1);baosXLS.writeTo(f);f.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}System.out.println("Successfully generated the detailed courier report");}}