package asignacionServlet;

import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import m05bClasses.m05bComun;
import m05bClasses.m05bConexion;
import m05bClasses.m05bConstantes;
import m05bClasses.m05bConstantesInformes;
import m05bClasses.m05bFileLog;
import m05bClasses.m05bInformes;

import org.apache.fop.apps.Driver;
import org.xml.sax.InputSource;


public class m05bImpCLibresServlet extends javax.servlet.http.HttpServlet
{
		java.sql.Connection conn=null;
		m05bConexion miConex = new m05bConexion();
	 	m05bComun miComun = new m05bComun();
	 	m05bInformes miInforme = new m05bInformes();
	 	m05bFileLog miLog = new m05bFileLog();
   public static final String FO_REQUEST_PARAM = "fo";
//    Logger log = null;

   public void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, java.io.IOException
   {
 	   try{
		      doGet(request, response);
		    }catch (Exception e){
		    	miLog.addTraza(e);
		    }
   }

   public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException
   {
      performTask(request, response);
   }
//        if(log == null)
//        {
//            Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
//            log = hierarchy.getLoggerFor("fop");
//            log.setPriority(Priority.WARN);
//        }

/**
 * Initializes the servlet.
 */
public void init() {
	// insert code to initialize the servlet here
		
		// Conexion
			try {
					conn = miConex.getConnection();
					conn.setAutoCommit(false);  // Al final si todo ha ido bien se hace commit
   			} catch (Exception SQLException) {
					try {
			  			conn.rollback();  // Ha habido problemas, se deshacen todos los cambios
			  		} catch (SQLException e2){
			  			System.err.println("Exception: OutletMaintenanceS.service: " + e2);
			  		}
			  		miLog.addTraza(SQLException);
			} 
}
   public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException
   {
	   java.sql.Statement pstmt = null;
	   ResultSet rs = null;

       try {

    	   String sSentencia="", order="";
    	   String matricula="", marca="", color="", kmact="", modelo="", ubicacion="", rango="";

    	   rango = request.getParameter("Rango")==null ? "":request.getParameter("Rango");
    	   /*if (!(rango.equals("")))
				condRango= " AND RANGO_96 = '"  + rango + "'";*/
    	   String descRango = request.getParameter("DescRango");
    	   String hoy = request.getParameter("Hoy");
    	   String desde = request.getParameter("Desde");
    	   String hasta = request.getParameter("Hasta");
    	   String horadesde = request.getParameter("HoraDesde");
    	   String horahasta = request.getParameter("HoraHasta");
    	   String tipo = request.getParameter("Tipo")==null?"":request.getParameter("Tipo");
    	   String Rango = request.getParameter("Rango")==null?"":request.getParameter("Rango");
    	   String conRango="";
    	   
    	   String vTipo="";
    	   if (tipo.equals("L"))
    		   vTipo="Libres";
    	   else
    		   vTipo="Todos";
    		
    		
    	   HttpSession session = request.getSession(true);
    	   Locale idiomaLocal = (Locale)session.getAttribute("Local");
   		String idiomaSes = ((idiomaLocal.getLanguage()==null)?"es":idiomaLocal.getLanguage());	
   		String campoTexto="";		
   	String strIdioma=m05bConstantesInformes.IDIOMA_C;
   		ResourceBundle res =  (ResourceBundle)session.getAttribute("resTit");
   		if(!idiomaSes.equals("es")){
   			campoTexto="E"+campoTexto;
   			strIdioma=m05bConstantesInformes.IDIOMA_E;
   			
   		}
    	   String persona = (String) session.getAttribute("persona");
    	   sSentencia = "select MATRIC_96, KMACT_96, MARCA_96, NVL("+campoTexto+"DESCRIP_99,DESCRIP_99), UBICACION_96, MODELO_96, RANGO_96 " +
    	   				"from " + m05bConstantes.TAB_COCHESLIBRES +
    	   				" left JOIN M0599T00 ON cod_tab_99    =1 AND IDCOLOR_96   =cod_desc_99 "+
	   					" where USUARIO_96 = '" + persona + "'";
    	   if (tipo.equals("L"))
    		   sSentencia = sSentencia + " AND LIBRE_96 = 'S'";

    	   order = " order  by  MATRIC_96";
    	   if (rango.equals(""))
    		   conRango="";
    	   else
    		   conRango=" AND RANGO_96 = '" + rango + "' ";

		 sSentencia += conRango + order;

		 pstmt = conn.createStatement();
		 pstmt.execute(sSentencia);
		 rs = pstmt.getResultSet();

         String sFoFileDir = "/datos/m05b/tmp/";
         File foFileDir = new File(sFoFileDir);
         File tmpFoFile = File.createTempFile("m05b", "Fo", foFileDir);

         FileOutputStream tmpFoFileStream = new FileOutputStream(tmpFoFile);

         String sLinea;

         sLinea = "<?xml version=\"1.0\" encoding=\"windows-1252\"?>\n";
         tmpFoFileStream.write(sLinea.getBytes());

         sLinea = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n";
         tmpFoFileStream.write(sLinea.getBytes());
		 // LAYOUT DEL LISTADO
	         sLinea = "<fo:layout-master-set>\n";
	         tmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "<fo:simple-page-master  master-name=\"general\" page-height=\"29.5cm\" page-width=\"21.5cm\" margin-top=\"0cm\" margin-bottom=\"3cm\" margin-left=\"2cm\" margin-right=\"1cm\">\n";
			     tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-before extent=\"7cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-body margin-bottom=\"1cm\" margin-top=\"4cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-after extent=\"0.5cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-start extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-end extent=\"0cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "	</fo:simple-page-master>\n";
		         tmpFoFileStream.write(sLinea.getBytes());
		         
	         sLinea = "</fo:layout-master-set>\n";
	         tmpFoFileStream.write(sLinea.getBytes());
	         
//------------------------------------------------------------------------------------------------------------------------
 	sLinea = "<fo:page-sequence master-reference=\"general\">\n";
        tmpFoFileStream.write(sLinea.getBytes());
		//CABECERA
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-before\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
	  		sLinea = "		<fo:block-container position=\"absolute\" top=\"0cm\" left=\"2.4cm\" width=\"12cm\" height=\"1.7cm\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
			//  Escudo
        	sLinea = "			<fo:external-graphic src=\"" + "/datos/m05b/file/images/fop/cabeceraHacFin.jpg\" height=\"1.7cm\" width=\"12cm\"/>\n";
       		tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = "      </fo:block-container>\n";
        	tmpFoFileStream.write(sLinea.getBytes());
       		sLinea = "		<fo:block-container position=\"absolute\" top=\"2.5cm\" left=\"2.4cm\" width=\"12cm\" height=\"1.7cm\">\n";
        	tmpFoFileStream.write(sLinea.getBytes()); 	
        	// Titulo
	 	    if (tipo.equals("T")) 
       			sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\" text-align=\"center\"  >\n " + res.getString("infCocheslibres.titulo1") + "</fo:block>\n";
	 	    else
       			sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\" text-align=\"center\"  >\n " + res.getString("infCocheslibres.titulo2") + "</fo:block>\n";
	        tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = "      </fo:block-container>\n";
        	tmpFoFileStream.write(sLinea.getBytes());


		sLinea = "      <fo:block-container position=\"absolute\" top=\"3.5cm\" left=\"0cm\" width=\"12cm\" height=\"7cm\">\n";
		tmpFoFileStream.write(sLinea.getBytes());
		      	// Detalle-1
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"178mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            	/*	sLinea = "	    <fo:table-column column-width=\"178mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());*/
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	if (hoy.equals("true")) {
										//Fecha
					            		sLinea = "	    <fo:table-row>\n";
					            		tmpFoFileStream.write(sLinea.getBytes());
					            			//  Blanco1
					            			sLinea = "	    <fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					                                sLinea = "			<fo:block text-align=\"left\"  font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\"   >\n " + res.getString("infCocheslibres.texto1") +":"+ miComun.getFechaActual(strIdioma);
					                                tmpFoFileStream.write(sLinea.getBytes());
		                   			              sLinea = "	    	</fo:block>\n";
				                                      tmpFoFileStream.write(sLinea.getBytes());
					            			sLinea = "	    </fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					            		sLinea = "	    </fo:table-row>\n";
		  			            		tmpFoFileStream.write(sLinea.getBytes());
										sLinea = "	    <fo:table-row>\n";
					            		tmpFoFileStream.write(sLinea.getBytes());	
											sLinea = "	    <fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					                                sLinea = "			<fo:block text-align=\"left\"  font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\"   >\n " + res.getString("infCocheslibres.texto2") +":"+ vTipo;
					                                tmpFoFileStream.write(sLinea.getBytes());
		                   			              sLinea = "	    	</fo:block>\n";
				                                      tmpFoFileStream.write(sLinea.getBytes());
					            			sLinea = "	    </fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					            		sLinea = "	    </fo:table-row>\n";
		  			            		tmpFoFileStream.write(sLinea.getBytes());
			            	 } else {
										//Desde-Hasta
					            		sLinea = "	    <fo:table-row>\n";
					            		tmpFoFileStream.write(sLinea.getBytes());
					            			//  Blanco1
					            			sLinea = "	    <fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					                        sLinea = "			<fo:block text-align=\"left\"  font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\"   >\n " + res.getString("infCocheslibres.texto3")+": " + desde + " " + horadesde + "  -  "+res.getString("infCocheslibres.texto4")+": " +  hasta + " " + horahasta ;
					                        tmpFoFileStream.write(sLinea.getBytes());
		                   			        sLinea = "	    	</fo:block>\n";
				                            tmpFoFileStream.write(sLinea.getBytes());
					            			sLinea = "	    </fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					            			/*sLinea = "	    <fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());
					                        sLinea = "			<fo:block text-align=\"left\"  font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\"   >\n ";
					                        tmpFoFileStream.write(sLinea.getBytes());
		                   			        sLinea = "	    	</fo:block>\n";
				                            tmpFoFileStream.write(sLinea.getBytes());
					            			sLinea = "	    </fo:table-cell>\n";
					            			tmpFoFileStream.write(sLinea.getBytes());*/
					            		sLinea = "	    </fo:table-row>\n";
		  			            		tmpFoFileStream.write(sLinea.getBytes());
			            	} 
						//Rango
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco1
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
                  		            	
			                                sLinea = "			<fo:block text-align=\"left\"  font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\"   >\n " + res.getString("infCocheslibres.texto5") +  descRango ;
			                                tmpFoFileStream.write(sLinea.getBytes());
                   			              sLinea = "	    	</fo:block>\n";
		                                      tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
  			            		tmpFoFileStream.write(sLinea.getBytes());
								//Linea en negrita
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco1
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
                  		            	// Linea
			                                sLinea = "			<fo:block font-family=\"Helvetica\" font-weight=\"bold\" font-size=\"8pt\" text-align=\"left\"  >\n "  ;
			                                tmpFoFileStream.write(sLinea.getBytes());
                   			              	sLinea = "	    	</fo:block>\n";
		                                    tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
  			            		tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    </fo:table-body>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());			            								            		
			            sLinea= "       </fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block-container>\n";
		            tmpFoFileStream.write(sLinea.getBytes());

        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());
//PIE
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-after\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
                sLinea = "                 <fo:block line-height=\"14pt\" font-size= \"8pt\" text-align=\"end\">"+res.getString("pagina")+" <fo:page-number/></fo:block>\n";
        	tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());
//------------------------------------------------------------------------------------------------------------------------------

		 // PAGE-SEQUENCE DEL LISTADO 
	         //bbsLinea = "<fo:page-sequence master-reference=\"general\">\n";
	         //bbtmpFoFileStream.write(sLinea.getBytes());
		         sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
		         tmpFoFileStream.write(sLinea.getBytes());

		         // --------------    CABECERA DEL LISTADO    --------------
                        //Cabecera 1
		        //bb    sLinea = "		<fo:block-container position=\"absolute\" top=\"0cm\" left=\"3.4cm\" width=\"12cm\" height=\"1.7cm\">\n";
		        //bb    tmpFoFileStream.write(sLinea.getBytes());
		        //bb    	//  Escudo
			//bb            sLinea = "			<fo:external-graphic src=\"" + urlprin + "/parquemovil/images/fop/cabeceraHacFin.jpg\" height=\"1.7cm\" width=\"12cm\"/>\n";
			 //bb           tmpFoFileStream.write(sLinea.getBytes());
		         //bb   sLinea = "      </fo:block-container>\n";
		         //bb   tmpFoFileStream.write(sLinea.getBytes());
		         // --------------    LISTADO    --------------
		         // Definicion de la tabla de descripciones.
		            sLinea= "       <fo:table>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "	    <fo:table-column column-width=\"22mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        if ( (hoy.equals("true")) && ( descRango.equals("")) ) {
			            sLinea = "	    <fo:table-column column-width=\"18mm\"/>\n";
				        tmpFoFileStream.write(sLinea.getBytes());
				    }
			    	sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			    	sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			    	sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			    	sLinea = "	    <fo:table-column column-width=\"25mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			    	sLinea = "	    <fo:table-column column-width=\"40mm\"/>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			        

//TABLE HEADER
	
		            sLinea = "	    <fo:table-header font-family=\"Helvetica\" font-size=\"10pt\" position=\"relative\" top=\"14cm\" left=\"4.4cm\" width=\"12cm\" height=\"3cm\">\n";
			        tmpFoFileStream.write(sLinea.getBytes());
			            	// Titulos tabla
		            		sLinea = "	    <fo:table-row>\n";
		            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Matrícula
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col1") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "--------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Rango
			        				if ( (hoy.equals("true")) && ( descRango.equals("")) ) {
				            			sLinea = "	    <fo:table-cell>\n";
				            			tmpFoFileStream.write(sLinea.getBytes());
								            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col2")+ "</fo:block>\n";
								            tmpFoFileStream.write(sLinea.getBytes());
								            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "----------" + "</fo:block>\n";
								            tmpFoFileStream.write(sLinea.getBytes());
				            			sLinea = "	    </fo:table-cell>\n";
				            			tmpFoFileStream.write(sLinea.getBytes());
				            		}
			            			//  Marca
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col3") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "-----------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Color
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col4") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Km. Actuales
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col5")+ "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            			
			            			//  Ubicación
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col6") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco1
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"20pt\" text-align=\"left\"  >\n " + res.getString("infCocheslibres.col7") + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"0pt\" text-align=\"left\"  >\n " + "------------" + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());			            						            			
			            			//  Blanco2
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block border-width=\"0.05mm\" border-style=\"solid\" > </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            		
			        sLinea = "	    </fo:table-header>\n";
			        tmpFoFileStream.write(sLinea.getBytes());

		            sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\" position=\"relative\" top=\"14cm\" left=\"4.4cm\" width=\"12cm\" height=\"1.7cm\">\n";
			        tmpFoFileStream.write(sLinea.getBytes());

		   		    while (rs.next()){
	       	    			matricula = rs.getString(1)==null ? "":rs.getString(1);
                            
	       	    			rango= rs.getString(7)==null ? "":rs.getString(7);
	       	    			marca = rs.getString(3)==null ? "":rs.getString(3);
	       	    			color = rs.getString(4)==null ? "":rs.getString(4);
	       	    			kmact = rs.getString(2)==null ? "":rs.getString(2);
	       	    			ubicacion = rs.getString(5)==null ? "":rs.getString(5);
	       	    			modelo = rs.getString(6)==null ? "":rs.getString(6);


		            		sLinea = "	    <fo:table-row>\n";
		            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  matricula
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"6pt\" text-align=\"left\"  >\n <![CDATA[" + matricula + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  rango
			        				if ( (hoy.equals("true")) && ( descRango.equals("")) ) {
				            			sLinea = "	    <fo:table-cell>\n";
				            			tmpFoFileStream.write(sLinea.getBytes());
								            sLinea = "      <fo:block text-align=\"left\"  padding-top=\"6pt\">\n <![CDATA[" + rango + "]]></fo:block>\n";
								            tmpFoFileStream.write(sLinea.getBytes());
				            			sLinea = "	    </fo:table-cell>\n";
				            			tmpFoFileStream.write(sLinea.getBytes());
				            		}				            			
			            			//  marca
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\"  padding-top=\"6pt\">\n<![CDATA[ " + marca + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  color
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\"  padding-top=\"6pt\">\n <![CDATA[" + color + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  km. actuales
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"right\"  padding-top=\"6pt\">\n<![CDATA[ " + miComun.formatearStrMiles(kmact) + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  ubicacion
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\"  padding-top=\"6pt\">\n <![CDATA[" + ubicacion + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  modelo
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\"  padding-top=\"6pt\">\n <![CDATA[" + modelo + "]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			
			            		sLinea = "	    </fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());

				}
           		rs.close();
          		sLinea = "	    </fo:table-body>\n";
            	tmpFoFileStream.write(sLinea.getBytes());
            sLinea= "       </fo:table>\n";
           	tmpFoFileStream.write(sLinea.getBytes());
 
         sLinea = "      </fo:flow>\n";
         tmpFoFileStream.write(sLinea.getBytes());
   	 sLinea = "   </fo:page-sequence>\n";
     tmpFoFileStream.write(sLinea.getBytes());
   sLinea = "</fo:root>\n";
   tmpFoFileStream.write(sLinea.getBytes());
  tmpFoFileStream.close();

         FileInputStream file = new FileInputStream(sFoFileDir + tmpFoFile.getName());

         renderFO(new InputSource(file), response);

         tmpFoFile.delete();

         response.flushBuffer();


      }
      catch (Exception ex)
      {
      	try {
			response.sendRedirect("m05bError.jsp");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			miLog.addTraza(e);
		}
      }
      finally{
	   	    try {
				if (rs != null)
        			rs.close();
				if (pstmt != null)
					pstmt.close();
			} catch (SQLException e2) {
				try {
					response.sendRedirect("m05bError.jsp");
				} catch (IOException e) {
					// TODO Auto-generated catch block
					miLog.addTraza(e);
				}
	  		}
	   }
    }

    /**
     * renders an FO inputsource into a PDF file which is rendered
     * directly to the response object's OutputStream
     */
    public void renderFO(InputSource foFile, HttpServletResponse response) throws ServletException
    {
        try
        {
            ByteArrayOutputStream out = new ByteArrayOutputStream();

	    	response.reset();
            OutputStream oOut=response.getOutputStream();
            BufferedOutputStream bOut=new BufferedOutputStream(oOut);
            Driver driver = new Driver(foFile, out);
            driver.setRenderer(Driver.RENDER_PDF);
            driver.run();

            response.setContentLength(out.size());
	    response.setContentType("application/pdf");


            bOut.write(out.toByteArray());
	    out.close();
	    bOut.flush();
            bOut.close();
        
//	    response.flushBuffer();
        }
        catch (Exception ex)
        {
            throw new ServletException(ex);
        }
    }
}