package com.reallinkbilling.download;

import static java.awt.Color.WHITE;
import static org.apache.pdfbox.pdmodel.font.PDType1Font.HELVETICA;
import static org.vandeseer.easytable.settings.HorizontalAlignment.CENTER;
import static org.vandeseer.easytable.settings.HorizontalAlignment.LEFT;
import static org.vandeseer.easytable.settings.HorizontalAlignment.RIGHT;

import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.pdmodel.PDPage;
import org.apache.pdfbox.pdmodel.PDPageContentStream;
import org.apache.pdfbox.pdmodel.common.PDRectangle;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.pdfbox.pdmodel.graphics.image.PDImageXObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;
import org.krysalis.barcode4j.BarcodeDimension;
import org.krysalis.barcode4j.BarcodeGenerator;
import org.krysalis.barcode4j.impl.code128.Code128Bean;
import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
import org.krysalis.barcode4j.output.java2d.Java2DCanvasProvider;
import org.krysalis.barcode4j.tools.UnitConv;
import org.vandeseer.easytable.TableDrawer;
import org.vandeseer.easytable.structure.Row;
import org.vandeseer.easytable.structure.Table;
import org.vandeseer.easytable.structure.Table.TableBuilder;
import org.vandeseer.easytable.structure.cell.TextCell;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.ProductBarcodeDetailsDAO;
import com.reallinkbilling.DAO.SupplierDebtReportDAO;
import com.reallinkbilling.db.GetDBConnection;

public class DownloadBarcode extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -9001030197827726107L;
	public HttpServletRequest request;
	public HttpServletResponse response;

	private final static Color BLUE_DARK = new Color(76, 129, 190);
    private final static Color BLUE_LIGHT_1 = new Color(186, 206, 230);
    private final static Color BLUE_LIGHT_2 = new Color(218, 230, 242);

    private final static Color GRAY_LIGHT_1 = new Color(245, 245, 245);
    private final static Color GRAY_LIGHT_2 = new Color(240, 240, 240);
    private final static Color GRAY_LIGHT_3 = new Color(216, 216, 216);
    private static final float PADDING10 = 10f;
    private static final float PADDING20 = 20f;
    private static final float PADDING30 = 30f;
    private static final float PADDING40 = 40f;
    private static final float PADDING50 = 50f;

	private HttpSession session;
	private int quantity;

	private String productDetails;
	private String size;
	
	
	private SessionMap<String,Object> sessionMap;  
	
	
	private boolean addCodedPrice;
	private boolean addSupplierId;
	private boolean addPurchaseDate;
	private boolean addMrp;
	
	public void printBarcodeOnly()
	{
		session=ServletActionContext.getRequest().getSession(false); 
		 
        if(session==null || session.getAttribute("login")==null){ 
        	addActionMessage("Please Login First !!!");
        	
        }  
        else{
        	
        	response = ServletActionContext.getResponse();
			request = ServletActionContext.getRequest();

		
			String shopId = (String) this.getSessionMap().get("shopId");
    		String shopCode = (String) this.getSessionMap().get("shopCode");
        	
        
        	String productDet [] = this.getProductDetails().split("-");
        	
        	Connection connection = null;
    		PreparedStatement preparedStatement= null;
    		ResultSet resultSet = null;
    	 
    		String sql = "";
    		String db = (String) this.getSessionMap().get("db");
    		String productCode = "";
    		try
    		{
    			
    		
    			
    			connection = GetDBConnection.getDBConnection(db);
    			
    			sql = "SELECT product_short_code "
    					+ "FROM "
    					+ "shop_product_master "
    					+ "WHERE "
    					+ "product_id = ? "
    					+ "AND "
    					+ "shop_id = ? "
    					+ "AND "
    					+ "shop_code = ?";
    			
    			preparedStatement = connection.prepareStatement(sql);
    			preparedStatement.setString(1, productDet[0]);
    			preparedStatement.setString(2, shopId);
    			preparedStatement.setString(3, shopCode);
    			
    			resultSet = preparedStatement.executeQuery();
    			
    			if(resultSet.next())
    			{
    				
    				productCode = resultSet.getString(1);
    				
    			}
    			
    			HashMap<String,ProductBarcodeDetailsDAO> productBarcodeDetailsMap = (HashMap<String, ProductBarcodeDetailsDAO>) this.getSessionMap().get("barcodeProductDetails");
	        	
    			ProductBarcodeDetailsDAO  barcodeDetailsDAO = productBarcodeDetailsMap.get(productCode);
    	
    			
    			if(resultSet != null)
    			{
    				resultSet.close();
    				resultSet = null;
    			}
    			
    			if(preparedStatement != null)
    			{
    				preparedStatement.close();
    				preparedStatement = null;
    			}
    			String supplierId="";
    			String purDate = "";
    			
    			sql = "SELECT "
    					+ "pinfo.supplier_id,"
    					+ "DATE_FORMAT(pinfo.purchase_date,'%d%m%Y') "
    					+ "FROM "
    					+ "shop_purchase_info_master AS pinfo INNER JOIN shop_purchase_product_info_master AS ppinfo "
    					+ "ON "
    					+ "pinfo.purchase_number = ppinfo.purchase_number "
    					+ "WHERE "
    					+ "pinfo.shop_id = ? "
    					+ "AND "
    					+ "pinfo.shop_code = ? "
    					+ "AND "
    					+ "ppinfo.shop_id = ? "
    					+ "AND "
    					+ "ppinfo.shop_code = ? "
    					+ "AND "
    					+ "ppinfo.product_id = ? "
    					+ "ORDER BY pinfo.purchase_date DESC LIMIT 1";
    			
    			preparedStatement = connection.prepareStatement(sql);
    			
    			preparedStatement.setString(1, shopId);
    			preparedStatement.setString(2, shopCode);
    			preparedStatement.setString(3, shopId);
    			preparedStatement.setString(4, shopCode);
    			
    			
    			preparedStatement.setString(5, productDet[0]);
    			
    			resultSet = preparedStatement.executeQuery();
    			
    			while(resultSet.next())
				{
    				supplierId = resultSet.getString(1);
    				purDate = resultSet.getString(2);
				}
    					
    			
    			String path=	request.getSession().getServletContext().getRealPath("/images/");
    			PDDocument document = new PDDocument(); 
				PDPage page = null;
				
				
				Date date = new Date();
				
				float margin = 3.0F;
				float height = 0.0F;
				
				  if(this.getSize().equals("80X25X2"))
			         {
					  
					  height =  (this.getQuantity()/2 *70)+(this.getQuantity()*margin);
					  page = new PDPage(new PDRectangle(240F, height+100));
			         }
				
				  if(this.getSize().equals("50X25X1"))
			         {
					  
					  height =  (this.getQuantity() * 30)+(this.getQuantity()*margin);
					  page = new PDPage(new PDRectangle(200F, height));
			         }
					
				  if(this.getSize().equals("38X25X1"))
			         {
					  margin = 2.5F;
					  
					  height =  (this.getQuantity() * 90)+(this.getQuantity()*margin);
					  page = new PDPage(new PDRectangle(118F, height));
			         }
				  
				  if(this.getSize().equals("38X15X1"))
			         {
					  margin = 2.5F;
					  
					  height =  (this.getQuantity() * 45)+(this.getQuantity()*margin);
					  page = new PDPage(new PDRectangle(118F, height));
			         }
				
				
					 document.addPage(page);
			         PDPageContentStream contentStream = new PDPageContentStream(document, page);
			         int pageCount = 1;
			         
			         float startY = page.getMediaBox().getHeight();
		    			 
			         Code128Bean bean = new Code128Bean();
			         final int dpi = 300;

			         //Configure the barcode generator
			         bean.setModuleWidth(UnitConv.in2mm(2.8f / dpi));

			         bean.doQuietZone(false);

			         //Open output file
			         String imagePath=	request.getSession().getServletContext().getRealPath("/images/");
			         File outputFile = new File(imagePath+"/"+ productCode + ".png");

			         FileOutputStream out2 = new FileOutputStream(outputFile);
			       
			         BitmapCanvasProvider canvas = new BitmapCanvasProvider(
			        		 out2, "image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);

			         //Generate the barcode
			         bean.generateBarcode(canvas, productCode);
			      
			      
			         //Signal end of generation
			         canvas.finish();
			         out2.close();
			         PDImageXObject pdImage = PDImageXObject.createFromFile(imagePath+"/"+productCode + ".png", document);
					DecimalFormat df = new DecimalFormat("##");
					
				       
			         if(this.getSize().equals("50X25X1"))
			         {
			        	 String coddedPrice = "";
			        	 if(addCodedPrice)
			        	 {
			        		 coddedPrice = "0"+ df.format(barcodeDetailsDAO.getCustomerSalePrice())
			        		 	+"3"+ df.format(barcodeDetailsDAO.getPurchasePrice())+"7";
			        	 }
			        	 
			        	 Table header;
		    			 
			    		 Table bottom;
			    			 
			    			
			    			  TableBuilder tableHeaderBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
			    			  Row shopName = Row.builder()
						                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).padding(0).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).build())				               
						                    .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .height(3F)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          
					          
					        
					          
					          tableHeaderBuilder.addRow(shopName);
					         
					        
					          header = tableHeaderBuilder.build();
					          
					          
					          TableBuilder tableBottomBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK);
					          
						      Row productNameRow = Row.builder()
						                .add(TextCell.builder().text(barcodeDetailsDAO.getProductNameEn()).horizontalAlignment(CENTER).borderWidth(0.0F).padding(2F).lineSpacing(0.1F).padding(0).build())				               
						               .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .height(6F)
						                .build();
						     
						      tableBottomBuilder.addRow(productNameRow);
						      
						      
						      if(addSupplierId && addPurchaseDate)
						      {
						    	  Row addSupIdDate = Row.builder()  
							                .add(TextCell.builder().text(supplierId+"                                        "+purDate).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(6F)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
							                .horizontalAlignment(CENTER)
							                .build();
						          
						          tableBottomBuilder.addRow(addSupIdDate);
						      }
					          
						      
						      
						      if(addCodedPrice)
				        	 {
						          Row codedPrice = Row.builder()
							                .add(TextCell.builder().text(coddedPrice).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .height(3F)
							                .build();
						          
						          tableBottomBuilder.addRow(codedPrice);
				        	 }
						      
						     
					       
					       
					        
					          bottom = tableBottomBuilder.build();
				         
					         
					          
					          
					          float currentY = startY;
					          float barcodepositionY = startY-38;
					         
					          
					          for(int count = 1; count<=this.getQuantity();count++)
					         {
					          
					          TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(header)
				                .startX(5)
				                .startY(currentY-5)
				                .build()
				                .draw();
					      
					          
					          
					          contentStream.drawImage(pdImage, 20, barcodepositionY, 95, 25);
					         barcodepositionY = barcodepositionY - 80;
					          
					       
					         currentY = currentY-20;
					          TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(bottom)
				                .startX(0)
				                .startY(currentY-20)
				                .build()
				                .draw();
					         
					          currentY = currentY-60;
					         }
			        	 
			         }
			         
			         if(this.getSize().equals("38X25X1"))
			         {

			        	 String coddedPrice = "";
			        	 if(addCodedPrice)
			        	 {
			        		 coddedPrice = "0"+ df.format(barcodeDetailsDAO.getCustomerSalePrice())
			        		 	+"3"+ df.format(barcodeDetailsDAO.getPurchasePrice())+"7";
			        	 }
			        	 
			        	 Table header;
		    			 
			    		 Table bottom;
			    			 
			    			
			    			  TableBuilder tableHeaderBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
			    			  Row shopName = Row.builder()
						                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).padding(0).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).build())				               
						                    .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .height(3F)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          
					          
					        
					          
					          tableHeaderBuilder.addRow(shopName);
					         
					        
					          header = tableHeaderBuilder.build();
					          
					          
					          TableBuilder tableBottomBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK);
					          
						      Row productNameRow = Row.builder()
						                .add(TextCell.builder().text(barcodeDetailsDAO.getProductNameEn()).horizontalAlignment(CENTER).borderWidth(0.0F).padding(2F).lineSpacing(0.1F).padding(0).build())				               
						               .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .height(6F)
						                .build();
						     
						      tableBottomBuilder.addRow(productNameRow);
						      
						      
						      if(addSupplierId && addPurchaseDate)
						      {
						    	  Row addSupIdDate = Row.builder()  
							                .add(TextCell.builder().text(supplierId+"                                    "+purDate).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(6F)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
							                .horizontalAlignment(CENTER)
							                .build();
						          
						          tableBottomBuilder.addRow(addSupIdDate);
						      }
					          
						      
						      
						      if(addCodedPrice)
				        	 {
						          Row codedPrice = Row.builder()
							                .add(TextCell.builder().text(coddedPrice).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .height(3F)
							                .build();
						          
						          tableBottomBuilder.addRow(codedPrice);
				        	 }
						      
						      if(addMrp)
					        	 {
							          Row mrPrice = Row.builder()
								                .add(TextCell.builder().text("MRP: "+barcodeDetailsDAO.getBarcodeMRP()).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
								               .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .height(3F)
								                .build();
							          
							          tableBottomBuilder.addRow(mrPrice);
					        	 }
					       
					       
					        
					          bottom = tableBottomBuilder.build();
				         
					         
					          
					          
					          float currentY = startY;
					          float barcodepositionY = startY-42;
					         
					          
					          for(int count = 1; count<=this.getQuantity();count++)
					         {
					          
					          TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(header)
				                .startX(5)
				                .startY(currentY-8)
				                .build()
				                .draw();
					      
					          if(count == 3)
					          {
					        	  barcodepositionY = barcodepositionY + 2; 
					          }
					          
					          contentStream.drawImage(pdImage, 30, barcodepositionY, 90, 25);
					         barcodepositionY = barcodepositionY - 80;
					          
					       
					         currentY = currentY-20;
					          TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(bottom)
				                .startX(5)
				                .startY(currentY-21)
				                .build()
				                .draw();
					         
					          currentY = currentY-58;
					         }
			        	 
			         
			         }
			         
			         
			         if(this.getSize().equals("38X15X1"))
			         {

			        	 String coddedPrice = "";
			        	 if(addCodedPrice)
			        	 {
			        		 coddedPrice = "0"+ df.format(barcodeDetailsDAO.getCustomerSalePrice())
			        		 	+"3"+ df.format(barcodeDetailsDAO.getPurchasePrice())+"7";
			        	 }
			        	 
			        	 Table header;
		    			 
			    		 Table bottom;
			    			 
			    			
			    			  TableBuilder tableHeaderBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK)
						                .horizontalAlignment(CENTER);
			    			  Row shopName = Row.builder()
						                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).padding(0).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).build())				               
						                    .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .height(3F)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .build();
					          
					          
					          
					        
					          
					          tableHeaderBuilder.addRow(shopName);
					         
					        
					          header = tableHeaderBuilder.build();
					          
					          
					          TableBuilder tableBottomBuilder = Table.builder()
						                .addColumnsOfWidth(135)				                
						                .font(HELVETICA)
						                .borderColor(Color.BLACK);
					          
						      Row productNameRow = Row.builder()
						                .add(TextCell.builder().text(barcodeDetailsDAO.getProductNameEn()).horizontalAlignment(CENTER).borderWidth(0.0F).padding(2F).lineSpacing(0.1F).padding(0).build())				               
						               .backgroundColor(WHITE)
						                .textColor(Color.BLACK)
						                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
						                .horizontalAlignment(CENTER)
						                .height(6F)
						                .build();
						     
						      tableBottomBuilder.addRow(productNameRow);
						      
						      
						      if(addSupplierId && addPurchaseDate)
						      {
						    	  Row addSupIdDate = Row.builder()  
							                .add(TextCell.builder().text(supplierId+"                                    "+purDate).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .height(6F)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
							                .horizontalAlignment(CENTER)
							                .build();
						          
						          tableBottomBuilder.addRow(addSupIdDate);
						      }
					          
						      
						      
						      if(addCodedPrice)
				        	 {
						          Row codedPrice = Row.builder()
							                .add(TextCell.builder().text(coddedPrice).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
							               .backgroundColor(WHITE)
							                .textColor(Color.BLACK)
							                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
							                .horizontalAlignment(CENTER)
							                .height(3F)
							                .build();
						          
						          tableBottomBuilder.addRow(codedPrice);
				        	 }
						      
						      if(addMrp)
					        	 {
							          Row mrPrice = Row.builder()
								                .add(TextCell.builder().text("MRP: "+barcodeDetailsDAO.getBarcodeMRP()).horizontalAlignment(CENTER).borderWidth(0.0F).lineSpacing(0.1F).padding(0F).build())				               
								               .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(9)				              
								                .horizontalAlignment(CENTER)
								                .height(3F)
								                .build();
							          
							          tableBottomBuilder.addRow(mrPrice);
					        	 }
					       
					       
					        
					          bottom = tableBottomBuilder.build();
				         
					         
					          
					          
					          float currentY = startY;
					          float barcodepositionY = startY-42;
					         
					          
					          for(int count = 1; count<=this.getQuantity();count++)
					         {
					          
					          
					      
					          if(count == 3)
					          {
					        	  barcodepositionY = barcodepositionY + 2; 
					          }
					          
					          contentStream.drawImage(pdImage, 30, barcodepositionY, 90, 20);
					         barcodepositionY = barcodepositionY - 50;
					          
					       
					        
					         
					          currentY = currentY-58;
					         }
			        	 
			         
			         }
			         
			         if(this.getSize().equals("80X25X2"))
			         {
			         Table header;
		    			 
		    		 Table bottom;
		    			 
		    			
		    			  TableBuilder tableHeaderBuilder = Table.builder()
					                .addColumnsOfWidth(120,120)				                
					                .font(HELVETICA)
					                .borderColor(Color.BLACK)
					                .horizontalAlignment(CENTER);
		    			  Row shopName = Row.builder()
					                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.15F).build())				               
					                .add(TextCell.builder().text((String)this.getSessionMap().get("shopnameenglish")).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.1F).build())				               
						               .backgroundColor(WHITE)
					                .textColor(Color.BLACK)
					                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
					                .horizontalAlignment(CENTER)
					                .build();
				          
				          
				          
				        
				          
				          tableHeaderBuilder.addRow(shopName);
				         
				        
				          header = tableHeaderBuilder.build();
				          
				          
				          TableBuilder tableBottomBuilder = Table.builder()
					                .addColumnsOfWidth(120,120)				                
					                .font(HELVETICA)
					                .borderColor(Color.BLACK);
				          
					      Row productNameRow = Row.builder()
					                .add(TextCell.builder().text(barcodeDetailsDAO.getProductNameEn()).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.1F).padding(2).build())				               
					                .add(TextCell.builder().text(barcodeDetailsDAO.getProductNameEn()).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.1F).padding(2).build())				               
						            .backgroundColor(WHITE)
					                .textColor(Color.BLACK)
					                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
					                .horizontalAlignment(CENTER)
					                .build();
				          
				          
				          Row mrpRow = Row.builder()
					                .add(TextCell.builder().text("MRP:"+barcodeDetailsDAO.getBarcodeMRP()+"  Price:"+barcodeDetailsDAO.getSalesPrice()).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.1F).padding(0).build())				               
					                .add(TextCell.builder().text("MRP:"+barcodeDetailsDAO.getBarcodeMRP()+"  Price:"+barcodeDetailsDAO.getSalesPrice()).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.1F).padding(0).build())				               
					                .backgroundColor(WHITE)
					                .textColor(Color.BLACK)
					                .font(PDType1Font.HELVETICA_BOLD).fontSize(7)				              
					                .horizontalAlignment(CENTER)
					                .build();
				          
				          tableBottomBuilder.addRow(productNameRow);
				          tableBottomBuilder.addRow(mrpRow);
				        
				          bottom = tableBottomBuilder.build();
			         
				         
				          
				          
				          float currentY = startY;
				          float barcodepositionY = startY-30;
				         
				          
				          for(int count = 1; count<=this.getQuantity()/2;count++)
				         {
				          
				          TableDrawer.builder()
			                .contentStream(contentStream)
			                .table(header)
			                .startX(15)
			                .startY(currentY+2)
			                .build()
			                .draw();
				          currentY = currentY - 80;
				          
				          
				          contentStream.drawImage(pdImage, 50, barcodepositionY, 65, 20);
				          contentStream.drawImage(pdImage, 170, barcodepositionY, 65, 20);
				          barcodepositionY = barcodepositionY - 80;
				          
				       
				          
				          TableDrawer.builder()
			                .contentStream(contentStream)
			                .table(bottom)
			                .startX(20)
			                .startY(currentY+50)
			                .build()
			                .draw();
				         
				         
				         }
			         }
				         
				          contentStream.close();
					          
					          
					          
					          document.save(path+"/barcode-"+productCode+".pdf");
						        document.close();
						        
						        response.setHeader("Content-disposition", "inline; filename=barcode-"+productCode+".pdf");
								response.setContentType("application/pdf");
								
								FileInputStream fileInputStream = new FileInputStream(path+"/barcode-"+productCode+".pdf");
								PrintWriter out = response.getWriter();  
								int bytes;
								while ((bytes = fileInputStream.read()) != -1) {
									out.write(bytes);
								}
								fileInputStream.close();
								out.flush();
								out.close();
    			
    		}
    		catch (SQLException e) 
    		{
	    			e.printStackTrace();
	    				    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong!");
	    			
	    	} catch (IOException e) {
	    		
	    				e.printStackTrace();    			
	    			addActionError(e.getMessage()+" || "+e.getCause()+" || Something Went Wrong!");
	    	}
    			finally {
	    			if(resultSet != null)
					{
						try {
							resultSet.close();
						} catch (SQLException e) {
							
						}
					}
	    			if(preparedStatement != null)
	    			{
	    				try {preparedStatement.close();} catch (SQLException e) {}
	    			}
	    			if(connection != null)
	    			{
	    				
	    				try {connection.close();}catch (SQLException e) {}
	    			}
	    			
	    		}
        }
	}


	public HttpSession getSession() {
		return session;
	}


	public void setSession(HttpSession session) {
		this.session = session;
	}


	


	public HttpServletRequest getRequest() {
		return request;
	}


	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}


	public HttpServletResponse getResponse() {
		return response;
	}


	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}



	public int getQuantity() {
		return quantity;
	}


	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}


	public String getProductDetails() {
		return productDetails;
	}


	public void setProductDetails(String productDetails) {
		this.productDetails = productDetails;
	}


	public String getSize() {
		return size;
	}


	public void setSize(String size) {
		this.size = size;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}
	
	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}
	
	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}
	

	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}


	public boolean isAddCodedPrice() {
		return addCodedPrice;
	}


	public void setAddCodedPrice(boolean addCodedPrice) {
		this.addCodedPrice = addCodedPrice;
	}


	public boolean isAddSupplierId() {
		return addSupplierId;
	}


	public void setAddSupplierId(boolean addSupplierId) {
		this.addSupplierId = addSupplierId;
	}


	public boolean isAddPurchaseDate() {
		return addPurchaseDate;
	}


	public void setAddPurchaseDate(boolean addPurchaseDate) {
		this.addPurchaseDate = addPurchaseDate;
	}


	public boolean isAddMrp() {
		return addMrp;
	}


	public void setAddMrp(boolean addMrp) {
		this.addMrp = addMrp;
	}
	
	
	
	

}

	