Rev 919 | Rev 3125 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.support.services;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.net.MalformedURLException;import java.text.DateFormat;import java.text.DecimalFormat;import java.util.Date;import java.util.List;import org.apache.thrift.TException;import com.itextpdf.text.Document;import com.itextpdf.text.DocumentException;import com.itextpdf.text.Element;import com.itextpdf.text.Font;import com.itextpdf.text.FontFactory;import com.itextpdf.text.Image;import com.itextpdf.text.Paragraph;import com.itextpdf.text.Phrase;import com.itextpdf.text.Rectangle;import com.itextpdf.text.Font.FontFamily;import com.itextpdf.text.pdf.PdfPCell;import com.itextpdf.text.pdf.PdfPTable;import com.itextpdf.text.pdf.PdfWriter;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;public class ManifestGenerator {private TransactionServiceClient tsc = null;private CatalogServiceClient csc = null;private LogisticsServiceClient lsc = null;private DecimalFormat weightFormat = new DecimalFormat("0.000");public ManifestGenerator() {try {tsc = new TransactionServiceClient();csc = new CatalogServiceClient();lsc = new LogisticsServiceClient();} catch (Exception e) {e.printStackTrace();}}public ByteArrayOutputStream generateManifestFile(long warehouseId, long providerId) {ByteArrayOutputStream baosPDF = null;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 baosPDF;} catch (InventoryServiceException e) {e.printStackTrace();return baosPDF;} catch (LogisticsServiceException e) {e.printStackTrace();return baosPDF;} catch (TransactionServiceException e) {e.printStackTrace();return baosPDF;}try {baosPDF = new ByteArrayOutputStream();Font helvetica8 = FontFactory.getFont(FontFactory.HELVETICA, 8);Document document = new Document();PdfWriter.getInstance(document, baosPDF);document.addAuthor("shop2020");document.addTitle("Manifest for warehouse " + warehouseId + " provider " + providerId);document.open();PdfPTable table = new PdfPTable(1);table.getDefaultCell().setBorder(Rectangle.NO_BORDER);table.getDefaultCell().setPaddingBottom(10.0f);String logoPath = ManifestGenerator.class.getResource("/logo.jpg").getPath();PdfPCell logoCell = new PdfPCell(Image.getInstance(logoPath), false);logoCell.setBorder(Rectangle.NO_BORDER);String addressString = warehouse.getLocation() + "\nPIN " + warehouse.getPincode()+ "\n\n";Paragraph addressParagraph = new Paragraph(addressString, new Font(FontFamily.TIMES_ROMAN,8f));PdfPCell addressCell = new PdfPCell();addressCell.addElement(addressParagraph);addressCell.setHorizontalAlignment(Element.ALIGN_LEFT);addressCell.setBorder(Rectangle.NO_BORDER);PdfPTable ordersTable = new PdfPTable(8);ordersTable.addCell(new Phrase("Sl No", helvetica8));ordersTable.addCell(new Phrase("Order No", helvetica8));ordersTable.addCell(new Phrase("AWB No", helvetica8));ordersTable.addCell(new Phrase("Packet Wt.", helvetica8));ordersTable.addCell(new Phrase("Name", helvetica8));ordersTable.addCell(new Phrase("Destination City", helvetica8));ordersTable.addCell(new Phrase("Pincode", helvetica8));ordersTable.addCell(new Phrase("State", helvetica8));int serialNo = 0;for(int i=0; i < orders.size();i++){Order order = orders.get(i);if(order.getLogistics_provider_id()!=providerId)continue;//TODO: These are exactly the orders which will be shipped now. Shouldn't we change their status to be SHIPPED?serialNo++;List<LineItem> lineItems = order.getLineitems();double weight = 0;for(LineItem lineItem: lineItems)weight += lineItem.getTotal_weight();ordersTable.addCell(new Phrase(serialNo + "", helvetica8));ordersTable.addCell(new Phrase(order.getId() + "", helvetica8));ordersTable.addCell(new Phrase(order.getAirwaybill_no(), helvetica8));ordersTable.addCell(new Phrase(weightFormat.format(weight), helvetica8));ordersTable.addCell(new Phrase(order.getCustomer_name(), helvetica8));ordersTable.addCell(new Phrase(order.getCustomer_city(), helvetica8));ordersTable.addCell(new Phrase(order.getCustomer_pincode(), helvetica8));ordersTable.addCell(new Phrase(order.getCustomer_state(), helvetica8));}table.addCell(logoCell);table.addCell(addressCell);table.addCell(new Phrase("PAYMODE: Prepaid", helvetica8));table.addCell(new Phrase("Courier Name: " + provider.getName(), helvetica8));table.addCell(new Phrase("Pick up Date: " + DateFormat.getDateInstance(DateFormat.MEDIUM).format(new Date()), helvetica8));table.addCell(ordersTable);table.setWidthPercentage(90.0f);document.add(table);document.close();baosPDF.close();} catch (DocumentException e) {e.printStackTrace();} catch (MalformedURLException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}return baosPDF;}public static void main(String[] args) throws IOException {ManifestGenerator manifestGenerator = new ManifestGenerator();ByteArrayOutputStream baos = manifestGenerator.generateManifestFile(1, 1);File f = new File("/home/ashish/Downloads/manifest-1-2.pdf");FileOutputStream fos = new FileOutputStream(f);baos.writeTo(fos);}}