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.io.File;
import java.io.FileInputStream;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletOutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.fontbox.ttf.TTFParser;
import org.apache.fontbox.ttf.TrueTypeFont;
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.PDFont;
import org.apache.pdfbox.pdmodel.font.PDType0Font;
import org.apache.pdfbox.pdmodel.font.PDType1Font;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
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.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.ItemWiseStockDetailsDAO;
import com.reallinkbilling.DAO.ProductInfoDAO;
import com.reallinkbilling.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

import com.reallinkbilling.utils.UnicodeToKrutiDev;

public class DownloadProductDetails extends ActionSupport implements SessionAware,ServletRequestAware,ServletResponseAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3514661918497611795L;
	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 SessionMap<String,Object> sessionMap;

private HttpServletRequest request = null;
private HttpServletResponse response = null;

private List<ProductInfoDAO> productList = new ArrayList<ProductInfoDAO>();
public void downloadProductDetails() throws Exception {
	
	String shopId = (String) this.getSessionMap().get("shopId");
	String shopCode = (String) this.getSessionMap().get("shopCode");
	

	if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
    	addActionMessage("Please Login First !!!");
      
    }  
    else
    {
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet resultSet = null;
		
		String sql = "";
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		String db = (String) session.get("db");
		try
		{
			
		sql = "SELECT "
					+ "DATE_FORMAT(add_date, '%d-%m-%Y'),"
					+ "product_name_eng,"
					+ "product_name_mar,"
					+ "product_alias,"
					+ "product_short_code,"
					+ "customer_sales_price,"
					+ "dealer_sales_price,"
					+ "retailer_sales_price,"
					+ "tax_percentage,"
					+ "product_id,"
					+ "in_stock_quantity,"
					+ "rack_number,"
					+ "product_short_code "
					+ "FROM "
					+ "shop_product_master "
					+ "WHERE "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code= ? ";
		
		
		connection = GetDBConnection.getDBConnection(db);
		preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, shopId);
		preparedStatement.setString(2, shopCode);
		
		resultSet = preparedStatement.executeQuery();
		resultSet.last();
		int rowCount = resultSet.getRow();
		resultSet.beforeFirst();
		
		if(rowCount>0)
		{
			while(resultSet.next())
			{
				
				ProductInfoDAO productInfoDAO = new ProductInfoDAO();
				
				productInfoDAO.setAddDate(resultSet.getString(1));
				productInfoDAO.setProductName(resultSet.getString(2));
				productInfoDAO.setMarathiName(resultSet.getString(3));
				
				//System.out.println("product marathi name:"+ resultSet.getString(3) );
				
				productInfoDAO.setAlias(resultSet.getString(4));
				productInfoDAO.setProductCode(resultSet.getString(5));
				productInfoDAO.setCustomerSalesRate(resultSet.getString(6));
				productInfoDAO.setDealerSalesRate(resultSet.getString(7));
				productInfoDAO.setRetailerSaleRate(resultSet.getString(8));
				productInfoDAO.setTaxPercentage(resultSet.getString(9));
				productInfoDAO.setProductId(EncryptionDecryption.encrypt(resultSet.getString(10)));
				productInfoDAO.setInStockQuantity(resultSet.getString(11));
				productInfoDAO.setRackNumber(resultSet.getString(12));
				
				this.getProductList().add(productInfoDAO);
				
			}
			
			
			
			
			
			
			if(this.getProductList().size()>0)
			{

				
				
				try
				{
					String name = (String) this.getSessionMap().get("shopnameenglish");
					String address = (String) this.getSessionMap().get("shopaddresseng");
					String path= this.getRequest().getSession().getServletContext().getRealPath("/images/");
					
					
					 java.util.Date date = new java.util.Date();
		        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		        	String reportDate = dateFormat.format(date);
			
					 
		       	     long t = date.getTime();	        	     
		       	     java.sql.Time time = new java.sql.Time(t);
		       	     
					PDDocument document = new PDDocument(); 
					PDPage page = null;
					
					 page = new PDPage(PDRectangle.A4);
						

File fontFileKrutiBold = new File(
    request.getSession().getServletContext()
        .getRealPath("/fonts/Kruti_Dev_100__Bold.ttf")
);

TrueTypeFont ttfKrutiBold = new TTFParser().parse(fontFileKrutiBold);

PDFont fontMarathiKrutiBold =
    PDType0Font.load(document, ttfKrutiBold, true);

					 
					 document.addPage(page);
					 
					 
			         PDPageContentStream contentStream = new PDPageContentStream(document, page);
					 
			         int pageCount = 1;
			         
			         Table header;
			         
			         
			         
			         
			         TableBuilder tableHeaderBuilder = Table.builder()
				                .addColumnsOfWidth(550)				                
				                .font(HELVETICA)
				                .borderColor(Color.BLACK)
				                .horizontalAlignment(CENTER);
			         
			         Row dairyNameHeader = Row.builder()
				                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         Row addressRow = Row.builder()
				                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         
			         Row reportInfo1 = Row.builder()
				                .add(TextCell.builder()
				                		.text("Product Details On : "+reportDate+" "+time)
				                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         
			         
			         
			         tableHeaderBuilder.addRow(dairyNameHeader);
			         tableHeaderBuilder.addRow(addressRow);
			         tableHeaderBuilder.addRow(reportInfo1);
			        
			         
			         header = tableHeaderBuilder.build();
			         
			         float startY = page.getMediaBox().getHeight() - PADDING20;
			         TableDrawer.builder()
		                .contentStream(contentStream)
		                .table(header)
		                .startX(PADDING30)
		                .startY(startY)
		                .build()
		                .draw();
					
			         
			         Table stockInfoRowTable = null;
			         
			         TableBuilder tableStockInfoBuilder = Table.builder()
				                .addColumnsOfWidth(40,140,140,60,60,60,60)				                
				                .font(HELVETICA)
				                .borderWidth(0.5F)
				                .borderColor(Color.BLACK)
				                .horizontalAlignment(CENTER);
			         
			         
			         Row stockTableHeader = Row.builder()
			        		  	.add(TextCell.builder().text("SrNo").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())					              
				                .add(TextCell.builder().text("Product Name").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Product Name MR").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
					            .add(TextCell.builder().text("ALIAS").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("Product Code").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("Product Id").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("In Stock").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .backgroundColor(Color.LIGHT_GRAY)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         tableStockInfoBuilder.addRow(stockTableHeader);
			        
			         stockInfoRowTable = tableStockInfoBuilder.build();
			        		
			        		
			        		 float currentY = startY;
			        		 
			        		 int srNo = 1;
			        		 
			        		 for(ProductInfoDAO productInfo :  this.getProductList())
			        		 {

					        	 
						         if(currentY <100)
						         {
						        	 
						        	 stockInfoRowTable = tableStockInfoBuilder.build();
				        			 
				        			 
				        			 TableDrawer.builder()
						                .contentStream(contentStream)
						                .table(stockInfoRowTable)
						                .startX(PADDING20)
						                .startY(startY-50)
						                .build()
						                .draw();
					         
				        			 stockInfoRowTable = null;
				        			 tableStockInfoBuilder = null;
				        			 tableStockInfoBuilder = Table.builder()
				        					 	.addColumnsOfWidth(40,140,140,60,60,60,60)			                
								                .font(HELVETICA)
								                .borderWidth(0.5F)
								                .borderColor(Color.BLACK)
								                .horizontalAlignment(CENTER);
				        			 
				        			 
						        	 
						        	 pageCount++;
						        	 contentStream.close();
						        	PDPage newPage = new PDPage(PDRectangle.A4);
						        	 contentStream = new PDPageContentStream(document,newPage);
						        	currentY = startY;
						             document.addPage( newPage );
						             
						             
						             
						             Table headerOtherPage;
						             
						             
						             TableBuilder tableHeaderBuilderOtherPage = Table.builder()
								                .addColumnsOfWidth(550)				                
								                .font(HELVETICA)
								                .borderColor(Color.BLACK)
								                .horizontalAlignment(CENTER);
							         
							         Row dairyNameHeaderOtherPage = Row.builder()
								                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         Row addressRowOtherPage = Row.builder()
								                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         
							         Row reportInfo1OtherPage = Row.builder()
								                .add(TextCell.builder()
								                		.text("Product Details On : "+reportDate+" "+time)
								                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         
							         
							         
							         tableHeaderBuilderOtherPage.addRow(dairyNameHeaderOtherPage);
							         tableHeaderBuilderOtherPage.addRow(addressRowOtherPage);
							         tableHeaderBuilderOtherPage.addRow(reportInfo1OtherPage);
							        
							         
							         headerOtherPage = tableHeaderBuilderOtherPage.build();
							         
							         TableDrawer.builder()
						                .contentStream(contentStream)
						                .table(headerOtherPage)
						                .startX(PADDING30)
						                .startY(startY)
						                .build()
						                .draw();
							         
							         tableStockInfoBuilder.addRow(stockTableHeader);
							         
									
						         }
			        			 
						       

					        	 Row collectionTableInfoRow = Row.builder()
							                .add(TextCell.builder().text(""+srNo).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())				               
							                .add(TextCell.builder().text(productInfo.getProductName()).fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())


.add(TextCell.builder()
    .text(formatMarathiName(productInfo.getMarathiName(),
            HELVETICA, fontMarathiKrutiBold))
    .font(productInfo.getMarathiName() != null
            && productInfo.getMarathiName().matches("(?s).*\\p{IsDevanagari}.*")
            ? fontMarathiKrutiBold : HELVETICA)
    .fontSize(productInfo.getMarathiName() != null
            && productInfo.getMarathiName().matches("(?s).*\\p{IsDevanagari}.*")
            ? 13 : 9)
    .horizontalAlignment(LEFT)
    .borderWidth(0.5F)
    .lineSpacing(0.5F)
    .build())

							                .add(TextCell.builder().text(productInfo.getAlias()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getProductCode()).fontSize(7).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getProductId()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getInStockQuantity()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
								            .backgroundColor(Color.WHITE)
							                .textColor(Color.BLACK)
							                .fontSize(10)				              
							                .horizontalAlignment(CENTER)
							                .height(25F)
							                .build();
						         
					        	 tableStockInfoBuilder.addRow(collectionTableInfoRow);
						         
						       
						         currentY = (float) (currentY - 25.0F);
						         
						         srNo++;
						         
			        		 }
			        		 
			        		 stockInfoRowTable = tableStockInfoBuilder.build();

			        		 TableDrawer.builder()
				                .contentStream(contentStream)
				                .table(stockInfoRowTable)
				                .startX(PADDING20)
				                .startY(startY-50)
				                .build()
				                .draw();
				        	
			        		 
			        		 contentStream.close();
							 	document.save(path+"/product-details.pdf");
						        document.close();
						        
						        


response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
        "inline; filename=\"product-details.pdf\"");

FileInputStream fileInputStream =
        new FileInputStream(path + "/product-details.pdf");

ServletOutputStream out = response.getOutputStream();

int bytes;
while ((bytes = fileInputStream.read()) != -1) {
    out.write(bytes);
}

out.flush();
fileInputStream.close();
ttfKrutiBold.close();

				}
				catch(Exception e)
				{
					  e.printStackTrace();
				}
			
			}
			
			
		}
				
	}catch (SQLException e) {
		e.printStackTrace();
	}
	catch (Exception e) {
		e.printStackTrace();
	}finally {
		if(resultSet != null)
		{
			try {
				resultSet.close();resultSet = null;
			} catch (SQLException e) {
				
			}
		}
		if(preparedStatement != null)
		{
			try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
		}
		if(connection != null)
		{
			
			try {connection.close(); connection = null;}catch (SQLException e) {}
		}
		
		
	}

    }
 }
	
	
	

public void downloadProductDetailsAliasWise() throws Exception {
	
	String shopId = (String) this.getSessionMap().get("shopId");
	String shopCode = (String) this.getSessionMap().get("shopCode");
	

	if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
    	addActionMessage("Please Login First !!!");
      
    }  
    else
    {
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet resultSet = null;
		
		String sql = "";
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		String db = (String) session.get("db");
		try
		{
			
		sql = "SELECT "
					+ "DATE_FORMAT(add_date, '%d-%m-%Y'),"
					+ "product_name_eng,"
					+ "product_name_mar,"
					+ "product_alias,"
					+ "product_short_code,"
					+ "customer_sales_price,"
					+ "dealer_sales_price,"
					+ "retailer_sales_price,"
					+ "tax_percentage,"
					+ "product_id,"
					+ "in_stock_quantity,"
					+ "rack_number,"
					+ "product_short_code "
					+ "FROM "
					+ "shop_product_master "
					+ "WHERE "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code= ? "
					+ "ORDER BY product_alias  ASC";
		
		
		connection = GetDBConnection.getDBConnection(db);
		preparedStatement = connection.prepareStatement(sql);
		preparedStatement.setString(1, shopId);
		preparedStatement.setString(2, shopCode);
		
		resultSet = preparedStatement.executeQuery();
		resultSet.last();
		int rowCount = resultSet.getRow();
		resultSet.beforeFirst();
		
		if(rowCount>0)
		{
			while(resultSet.next())
			{
				
				ProductInfoDAO productInfoDAO = new ProductInfoDAO();
				
				productInfoDAO.setAddDate(resultSet.getString(1));
				productInfoDAO.setProductName(resultSet.getString(2));
				productInfoDAO.setMarathiName(resultSet.getString(3));
				
				productInfoDAO.setAlias(resultSet.getString(4));
				productInfoDAO.setProductCode(resultSet.getString(5));
				productInfoDAO.setCustomerSalesRate(resultSet.getString(6));
				productInfoDAO.setDealerSalesRate(resultSet.getString(7));
				productInfoDAO.setRetailerSaleRate(resultSet.getString(8));
				productInfoDAO.setTaxPercentage(resultSet.getString(9));
				productInfoDAO.setProductId(EncryptionDecryption.encrypt(resultSet.getString(10)));
				productInfoDAO.setInStockQuantity(resultSet.getString(11));
				productInfoDAO.setRackNumber(resultSet.getString(12));
				
				this.getProductList().add(productInfoDAO);
				
			}
			
			
			
			
			if(this.getProductList().size()>0)
			{

				
				
				try
				{
					String name = (String) this.getSessionMap().get("shopnameenglish");
					String address = (String) this.getSessionMap().get("shopaddresseng");
					String path= this.getRequest().getSession().getServletContext().getRealPath("/images/");
					
					
					 java.util.Date date = new java.util.Date();
		        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
		        	String reportDate = dateFormat.format(date);
			
					 
		       	     long t = date.getTime();	        	     
		       	     java.sql.Time time = new java.sql.Time(t);
		       	     
					PDDocument document = new PDDocument(); 
					PDPage page = null;
					
					 page = new PDPage(PDRectangle.A4);
						

File fontFileKrutiBold = new File(
    request.getSession().getServletContext()
        .getRealPath("/fonts/Kruti_Dev_100__Bold.ttf")
);

TrueTypeFont ttfKrutiBold = new TTFParser().parse(fontFileKrutiBold);

PDFont fontMarathiKrutiBold =
    PDType0Font.load(document, ttfKrutiBold, true);

					 
					 document.addPage(page);
					 
					 
			         PDPageContentStream contentStream = new PDPageContentStream(document, page);
					 
			         int pageCount = 1;
			         
			         Table header;
			         
			         
			         
			         
			         TableBuilder tableHeaderBuilder = Table.builder()
				                .addColumnsOfWidth(550)				                
				                .font(HELVETICA)
				                .borderColor(Color.BLACK)
				                .horizontalAlignment(CENTER);
			         
			         Row dairyNameHeader = Row.builder()
				                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         Row addressRow = Row.builder()
				                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         
			         Row reportInfo1 = Row.builder()
				                .add(TextCell.builder()
				                		.text("Product Details On : "+reportDate+" "+time)
				                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
				                .backgroundColor(WHITE)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         
			         
			         
			         tableHeaderBuilder.addRow(dairyNameHeader);
			         tableHeaderBuilder.addRow(addressRow);
			         tableHeaderBuilder.addRow(reportInfo1);
			        
			         
			         header = tableHeaderBuilder.build();
			         
			         float startY = page.getMediaBox().getHeight() - PADDING20;
			         TableDrawer.builder()
		                .contentStream(contentStream)
		                .table(header)
		                .startX(PADDING30)
		                .startY(startY)
		                .build()
		                .draw();
					
			         
			         Table stockInfoRowTable = null;
			         
			         TableBuilder tableStockInfoBuilder = Table.builder()
				                .addColumnsOfWidth(40,140,140,60,60,60,60)				                
				                .font(HELVETICA)
				                .borderWidth(0.5F)
				                .borderColor(Color.BLACK)
				                .horizontalAlignment(CENTER);
			         
			         
			         Row stockTableHeader = Row.builder()
			        		  	.add(TextCell.builder().text("SrNo").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())					              
				                .add(TextCell.builder().text("Product Name").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())				               
				                .add(TextCell.builder().text("Product Name MR").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
					            .add(TextCell.builder().text("ALIAS").horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("Product Code").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("Product Id").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .add(TextCell.builder().text("In Stock").horizontalAlignment(CENTER).borderWidth(0.5F).lineSpacing(1F).build())
				                .backgroundColor(Color.LIGHT_GRAY)
				                .textColor(Color.BLACK)
				                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
				                .horizontalAlignment(CENTER)
				                .build();
			         
			         tableStockInfoBuilder.addRow(stockTableHeader);
			        
			         stockInfoRowTable = tableStockInfoBuilder.build();
			        		
			        		
			        		 float currentY = startY;
			        		 
			        		 int srNo = 1;
			        		 
			        		 for(ProductInfoDAO productInfo :  this.getProductList())
			        		 {

					        	 
						         if(currentY <100)
						         {
						        	 
						        	 stockInfoRowTable = tableStockInfoBuilder.build();
				        			 
				        			 
				        			 TableDrawer.builder()
						                .contentStream(contentStream)
						                .table(stockInfoRowTable)
						                .startX(PADDING20)
						                .startY(startY-50)
						                .build()
						                .draw();
					         
				        			 stockInfoRowTable = null;
				        			 tableStockInfoBuilder = null;
				        			 tableStockInfoBuilder = Table.builder()
				        					 	.addColumnsOfWidth(40,140,140,60,60,60,60)			                
								                .font(HELVETICA)
								                .borderWidth(0.5F)
								                .borderColor(Color.BLACK)
								                .horizontalAlignment(CENTER);
				        			 
				        			 
						        	 
						        	 pageCount++;
						        	 contentStream.close();
						        	PDPage newPage = new PDPage(PDRectangle.A4);
						        	 contentStream = new PDPageContentStream(document,newPage);
						        	currentY = startY;
						             document.addPage( newPage );
						             
						             
						             
						             Table headerOtherPage;
						             
						             
						             TableBuilder tableHeaderBuilderOtherPage = Table.builder()
								                .addColumnsOfWidth(550)				                
								                .font(HELVETICA)
								                .borderColor(Color.BLACK)
								                .horizontalAlignment(CENTER);
							         
							         Row dairyNameHeaderOtherPage = Row.builder()
								                .add(TextCell.builder().text(name).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(16)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         Row addressRowOtherPage = Row.builder()
								                .add(TextCell.builder().text(address).horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(8)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         
							         Row reportInfo1OtherPage = Row.builder()
								                .add(TextCell.builder()
								                		.text("Product Details On : "+reportDate+" "+time)
								                		.horizontalAlignment(CENTER).borderWidth(0).lineSpacing(0.5F).build())				               
								                .backgroundColor(WHITE)
								                .textColor(Color.BLACK)
								                .font(PDType1Font.HELVETICA_BOLD).fontSize(12)				              
								                .horizontalAlignment(CENTER)
								                .build();
							         
							         
							         
							         
							         tableHeaderBuilderOtherPage.addRow(dairyNameHeaderOtherPage);
							         tableHeaderBuilderOtherPage.addRow(addressRowOtherPage);
							         tableHeaderBuilderOtherPage.addRow(reportInfo1OtherPage);
							        
							         
							         headerOtherPage = tableHeaderBuilderOtherPage.build();
							         
							         TableDrawer.builder()
						                .contentStream(contentStream)
						                .table(headerOtherPage)
						                .startX(PADDING30)
						                .startY(startY)
						                .build()
						                .draw();
							         
							         tableStockInfoBuilder.addRow(stockTableHeader);
							         
									
						         }
			        			 
						         

					        	 Row collectionTableInfoRow = Row.builder()
							                .add(TextCell.builder().text(""+srNo).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())				               
							                .add(TextCell.builder().text(productInfo.getProductName()).fontSize(9).horizontalAlignment(LEFT).borderWidth(0.5F).lineSpacing(0.5F).build())


							                .add(TextCell.builder()
							                	    .text(formatMarathiName(productInfo.getMarathiName(),
							                	            HELVETICA, fontMarathiKrutiBold))
							                	    .font(productInfo.getMarathiName() != null
							                	            && productInfo.getMarathiName().matches("(?s).*\\p{IsDevanagari}.*")
							                	            ? fontMarathiKrutiBold : HELVETICA)
							                	    .fontSize(productInfo.getMarathiName() != null
							                	            && productInfo.getMarathiName().matches("(?s).*\\p{IsDevanagari}.*")
							                	            ? 13 : 9)
							                	    .horizontalAlignment(LEFT)
							                	    .borderWidth(0.5F)
							                	    .lineSpacing(0.5F)
							                	    .build())

							                .add(TextCell.builder().text(productInfo.getAlias()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getProductCode()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getProductId()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
							                .add(TextCell.builder().text(productInfo.getInStockQuantity()).horizontalAlignment(RIGHT).borderWidth(0.5F).lineSpacing(0.5F).build())
								            .backgroundColor(Color.WHITE)
							                .textColor(Color.BLACK)
							                .fontSize(10)				              
							                .horizontalAlignment(CENTER)
							                .height(25F)
							                .build();
						         
					        	 tableStockInfoBuilder.addRow(collectionTableInfoRow);
						         
						       
						         currentY = (float) (currentY - 25.0F);
						         
						         srNo++;
						         
			        		 }
			        		 

				        	
			        		 
			        		 contentStream.close();
							 	document.save(path+"/product-details.pdf");
						        document.close();
						        
						        


response.setContentType("application/pdf");
response.setHeader("Content-Disposition",
        "inline; filename=\"product-details.pdf\"");

FileInputStream fileInputStream =
        new FileInputStream(path + "/product-details.pdf");

ServletOutputStream out = response.getOutputStream();

int bytes;
while ((bytes = fileInputStream.read()) != -1) {
    out.write(bytes);
}

out.flush();
fileInputStream.close();
ttfKrutiBold.close();

				}
				catch(Exception e)
				{
					  e.printStackTrace();
				}
			
			}
			
			
		}
				
	}catch (SQLException e) {
		e.printStackTrace();
	}
	catch (Exception e) {
		e.printStackTrace();
	}finally {
		if(resultSet != null)
		{
			try {
				resultSet.close();resultSet = null;
			} catch (SQLException e) {
				
			}
		}
		if(preparedStatement != null)
		{
			try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
		}
		if(connection != null)
		{
			
			try {connection.close(); connection = null;}catch (SQLException e) {}
		}
		
		
	}
}
}



private String formatMarathiName(String name, PDFont englishFont, PDFont marathiFont)
        throws Exception {

    if (name == null || name.trim().isEmpty()) {
        return "";
    }

    // Check whether the value contains any Devanagari characters.
    if (name.matches("(?s).*\\p{IsDevanagari}.*")) {
        return UnicodeToKrutiDev.convert(name);
    }

    // English-only names remain unchanged.
    return name;
}


public void setSession(Map<String, Object> map) {  
    sessionMap=(SessionMap<String, Object>)map;  
}

public SessionMap<String, Object> getSessionMap() {
	return sessionMap;
}

public void setSessionMap(SessionMap<String, Object> sessionMap) {
	this.sessionMap = sessionMap;
}
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 void setServletResponse(HttpServletResponse httpServletResponse) {
	this.response = httpServletResponse;
}


public void setServletRequest(HttpServletRequest httpServletRequest) {
	this.request = httpServletRequest;
}




public List<ProductInfoDAO> getProductList() {
	return productList;
}




public void setProductList(List<ProductInfoDAO> productList) {
	this.productList = productList;
}



}
