package polizaServlet;

import java.io.BufferedInputStream;
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.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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

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


public class m05bImpBajaSeguroServlet extends javax.servlet.http.HttpServlet
{
		java.sql.Connection conn=null;
		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);
//        }

   public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException
   {
	   java.sql.Connection conn=null;
	   PreparedStatement pstmt = null;
	   ResultSet rs = null;
	   PreparedStatement pstmt2 = null;
	   ResultSet rs2 = null;
      try
      {  HttpSession session = request.getSession(true);
			m05bConexion miConex = new m05bConexion();
			conn = miConex.getConnection();
      
      	 // Obtención de información de BD
      	 	 	// Vehículo
         String sCIA=request.getParameter("CIA");
      	 String fecha="";
      	 String matricula="";
      	 String poliza="";
      	 String tipo="";
      	 String modalidad="";
	 	 m05bComun miComun = new m05bComun();
      	 m05bCamposInformes miVehiculo = new m05bCamposInformes();
      	 String nombre = (String) session.getAttribute("PDFNombre");  // Se pone en la sesion en JSP Asignaciones
      	 String firma = miVehiculo.getFirma(m05bConstantesInformes.DET7_C_ACTA);
      	 //Preparamos la Sentencia de recuperación
      	 String sCompania="";
      	 int numerolineas=0;
      	 String sql = "Update "+m05bConstantes.TAB_HIST_SEGUROS+ " set " + m05bConstantes.COL_13_IMPRESO + 
		        " = 'S'," +m05bConstantes.COL_13_FIMPRESION + " = " + 
		        " TO_DATE(TO_CHAR(SYSDATE,'dd/mm/yyyy'),'dd/mm/yyyy')"+						
				" where " + m05bConstantes.COL_13_POLIZA + " = '" +sCIA + "' AND "+ 
				m05bConstantes.COL_13_ESTADO + " ='C' AND "  +  m05bConstantes.COL_13_IMPRESO + " = 'N'";

      	 String sSentenciaCIA = " SELECT " + m05bConstantes.COL_28_CIA +
      	 	                    " FROM " + m05bConstantes.TAB_SEGUROS +
      	 	                    " WHERE " + m05bConstantes.COL_28_POLIZA +"='" + sCIA +"'";
      	 
         String sSentencia = "select " + m05bConstantes.COL_13_CIA + ",to_char(sysdate,'dd/mm/yyyy')," + m05bConstantes.COL_13_MATRICULA +
         	                 "," + m05bConstantes.COL_99_DESCRIPCION + "||'   '||" + m05bConstantes.COL_17_DESCRIP +
                             " from " + m05bConstantes.TAB_HIST_SEGUROS + 
                             "," + m05bConstantes.TAB_VEHICULOS + 
                             "," + m05bConstantes.TAB_DESCRIPCIONES + 
                             "," + m05bConstantes.TAB_MODELOS +
                             " WHERE " + m05bConstantes.COL_13_MATRICULA + " = " + m05bConstantes.COL_34_MATRICULA +
                             " AND " + m05bConstantes.COL_99_CODTABLA + " = '7'"  + 
                             " AND " + m05bConstantes.COL_99_CODDESC + " = " + m05bConstantes.COL_34_MARCA +
                             " AND " + m05bConstantes.COL_17_CODMARCA + " = " + m05bConstantes.COL_34_MARCA +
                             " AND " + m05bConstantes.COL_17_CODMOD + " = " + m05bConstantes.COL_34_MODELO +
                             " AND " + m05bConstantes.COL_13_ESTADO + " = 'C'" +
                             " AND " + m05bConstantes.COL_13_IMPRESO + " = 'N'" +
                             " AND " + m05bConstantes.COL_13_POLIZA + " = '" +sCIA + "'" +
                             " ORDER BY 1" ;
			 
         //System.out.println(" m05b - ImpBajaSeguroServlet: " + sSentencia);
		    pstmt2 = conn.prepareStatement(sSentenciaCIA);
		   	rs2 = pstmt2.executeQuery();
		    if (rs2.next()){
	       	    	sCompania = rs2.getString(1)==null ? "":rs2.getString(1);}
	       	    	sCompania=sCompania.trim();
		    pstmt = conn.prepareStatement(sSentencia);
		   	rs = pstmt.executeQuery();

         //String foParam = request.getParameter(FO_REQUEST_PARAM);
         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=\"33cm\" page-width=\"21cm\" margin-top=\"2cm\" margin-bottom=\"2cm\" margin-left=\"2cm\" margin-right=\"2.5cm\">\n";
			       tmpFoFileStream.write(sLinea.getBytes());
			       //*  sLinea = "		<fo:region-before extent=\"0cm\"/>\n";
			       //*  tmpFoFileStream.write(sLinea.getBytes());
			       //*  sLinea = "		<fo:region-body margin-bottom=\"0cm\" margin-top=\"0cm\"/>\n";
			       //*  tmpFoFileStream.write(sLinea.getBytes());
			       //*  sLinea = "		<fo:region-after extent=\"0cm\"/>\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:region-before extent=\"2cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-body  margin-top=\"2.8cm\" margin-bottom=\"10cm\" />\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-after  extent=\"10cm\"/>\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());
		 // PAGE-SEQUENCE DEL LISTADO 
	         sLinea = "<fo:page-sequence master-reference=\"general\">\n";
	         tmpFoFileStream.write(sLinea.getBytes());
	         
	         
//REGION BEFORE
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-before\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
		           // --------------    CABECERA DEL LISTADO    --------------
                        //Cabecera 1
		            sLinea = "		<fo:block space-after.optimum=\"12pt\" text-align=\"center\" position=\"absolute\" top=\"0cm\" left=\"3.4cm\" width=\"10.5cm\" 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=\"10.5cm\"/>\n";
			            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());

        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());

	         
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-after\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      <fo:block space-after.optimum=\"70pt\" >\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Adeitasunez
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " + m05bConstantesInformes.DET3_E_POLIZA_DUPLI + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// DET1 - Atentamente
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"10pt\">\n " + m05bConstantesInformes.DET3_C_POLIZA_DUPLI +  "</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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



		            
                        //PIE DE PAGINA FIRMA
		         // --------------   EN   --------------
				   sLinea = "      <fo:block   space-after.optimum=\"70pt\">\n";
					
		            tmpFoFileStream.write(sLinea.getBytes());
		            
		            
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"40mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// En Castellano 
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\"  font-weight=\"bold\">\n" +  m05bConstantesInformes.EN_E_ASIGNACION + "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Intermedio
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" >\n </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Puntos
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block>\n  </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());
			            			// En Euskera 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n" + m05bConstantesInformes.EN_C_ASIGNACION + "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Intermedio
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n "+ miComun.getDetFechaActual(m05bConstantesInformes.IDIOMA_C)+ " </fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Puntos
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block>\n </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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            
//FIRMADO:
					sLinea = "      <fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"12mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"15mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			                sLinea = "	    <fo:table-column column-width=\"60mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                              // FIRMA CASTELLANO
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
                                          //Fiema en Castellano
		            			sLinea = "	    <fo:table-cell >\n";
		            			tmpFoFileStream.write(sLinea.getBytes());          			
		            			    sLinea = "      <fo:block>\n";	
						            tmpFoFileStream.write(sLinea.getBytes());
						            sLinea =       "</fo:block>";
						            tmpFoFileStream.write(sLinea.getBytes());	
					     		sLinea = "	    </fo:table-cell>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());							            
                                    //Fiema en Castellano
		            			sLinea = "	    <fo:table-cell >\n";
		            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\">\n" + m05bConstantesInformes.FIR_E_ASIGNACION+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
		            			// Firma en euskera
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"left\">\n" + " / " + m05bConstantesInformes.FIR_C_ASIGNACION+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			// Responsable
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n" + firma + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());		
			                       // jna.
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\" font-weight=\"bold\">\n" + "jna." + "</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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            
		            sLinea = "      <fo:block   space-after.optimum=\"20pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"80mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"80mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Responsable parque movil
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"right\" >\n " + m05bConstantesInformes.DET7_E_ACTA+ "  " + "</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\">\n " +" "+ m05bConstantesInformes.DET7_C_ACTA +"</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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



                        //PIE DE PAGINA Pie
					sLinea = "      <fo:block  space-after.optimum=\"140pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"160mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                              // Donostia SS
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
                                          //Donostias SS
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block text-align=\"center\">" + m05bConstantesInformes.PIE_ASIGNACION+ "</fo:block>";
							            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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());
//BODY
		         sLinea = "	<fo:flow flow-name=\"xsl-region-body\">\n";
		         tmpFoFileStream.write(sLinea.getBytes());
		         Context ctx =  new InitialContext(System.getProperties());



						// Cuerpo
		           // sLinea = "      <fo:block-container position=\"absolute\" top=\"9cm\" left=\"0.69cm\" width=\"15cm\" height=\"5.5cm\">\n";
		            //tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      <fo:block space-after.optimum=\"12pt\" position=\"absolute\" top=\"4cm\" left=\"0.69cm\" width=\"15cm\" height=\"2.8cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"55mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"50mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"66mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			//  Blanco1
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"7pt\">\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 font-weight=\"bold\" padding-top=\"7pt\">\n " + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			//  Cuadro vacío
			            			sLinea = "	    <fo:table-cell  border-style=\"solid\" border-width=\"0.1pt\" height=\"2.7cm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"7pt\">\n <![CDATA["+ sCompania ;
							            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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());



      	         // --------------    DETALLE DEL LISTADO    --------------

		            sLinea = "      <fo:block space-after.optimum=\"12pt\" position=\"absolute\"  left=\"0.69cm\" width=\"15cm\" height=\"5.5cm\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"10mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"75mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"10pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Jaun horiek
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_E_TARJETA_DUPLI + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// DET1 - Muy señores Nuestros
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_C_TARJETA_DUPLI +  "</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());
			            			// Ondoren zehazten - Euskera
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_E_POLIZA_BAJA + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " +  "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());

			            			// De conformidad - Castellano
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET2_C_POLIZA_BAJA +  "</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>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            
		           		sLinea= "       <fo:table space-after.optimum=\"12pt\" table-layout=\"fixed\">\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=\"100mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		                    sLinea = "	    <fo:table-header font-family=\"Helvetica\" font-size=\"7pt\" >\n";
			                tmpFoFileStream.write(sLinea.getBytes());
			            		sLinea = "	    <fo:table-row>\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// DATA 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n DATA</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// MATRIKULA
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n MATRIKULA</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			   // IBILGAILU MOTA
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n IBILGAILU MOTA</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());
			            			// FECHA 
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n FECHA</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// MATRICULA
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n MATRICULA</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// TIPO DE VEHICULO
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n TIPO DE VEHICULO</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());
			            		///DETALLE
			            	sLinea = "	    </fo:table-header>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		
		            		sLinea = "	    <fo:table-body  font-family=\"Helvetica\" font-size=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            		
		   		    while (rs.next()){
	       	    			fecha = rs.getString(2)==null ? "":rs.getString(2);
	       	    			matricula= rs.getString(3)==null ? "":rs.getString(3);
	       	    			tipo = rs.getString(4)==null ? "":rs.getString(4);
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Fecha 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n <![CDATA["+ fecha +"]]></fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Matricula
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n <![CDATA["+ matricula +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tipo
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block line-height=\"15pt\" text-align=\"center\">\n <![CDATA["+ tipo +"]]></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();
      			pstmt = conn.prepareStatement(sql);
				pstmt.executeUpdate();	
			    conn.commit();
			    

			            	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: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);
         generarPDFdesdeFO(sFoFileDir + tmpFoFile.getName(),sFoFileDir,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 (pstmt != null)
	            pstmt.close();
	        if (pstmt2 != null)
	            pstmt2.close();
	        if (rs != null)
	            rs.close();
	        if (rs2 != null)
	            rs2.close();
	        if (conn != null)
	            conn.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);
        }
    }
public void generarPDFdesdeFO(String ficheroFO, String rutaPDF, HttpServletResponse response) throws FOPException {
        try {

            Driver driver = new Driver();

            driver.setRenderer(Driver.RENDER_PDF);

            //Si no existe el directrio lo crea
            File f = new File(rutaPDF);

            if (!f.exists()) {

                f.mkdir();


            }
            //Cargar los datos del .FO
            FileInputStream file = new FileInputStream(ficheroFO);

            BufferedInputStream bfile=new BufferedInputStream(file);

            driver.setInputSource(new InputSource(bfile));


            //Crear un temporal que va a ser el .pdf
            String sPdfFileDir = rutaPDF;
            File   pdfFileDir = new File(sPdfFileDir);
            File   tmpPdfFile = File.createTempFile("m05b", ".pdf", pdfFileDir);


            FileOutputStream fileout = new FileOutputStream(tmpPdfFile);

            BufferedOutputStream bfileout=new BufferedOutputStream(fileout);

            driver.setOutputStream(bfileout);

            driver.run();


 

            bfile.close();

            bfileout.close();
            //Longitus del Fichero PDF.
            Long longitud = new Long(tmpPdfFile.length());            
            String sLongitud=""+ longitud;
            int iLongitud=Integer.parseInt(sLongitud);

            response.setContentLength((int)iLongitud);
	        response.setContentType("application/pdf");
	        
            byte[] fileBytes = new byte[longitud.intValue()];
            FileInputStream fisficheroPdf = new FileInputStream(tmpPdfFile);
            fisficheroPdf.read(fileBytes);
            ServletOutputStream os = response.getOutputStream();
            ((OutputStream)os).write(fileBytes);
            ((OutputStream)os).flush();
            ((OutputStream)os).close();

            // BORRAR EL FICHERO FO

            File fichero = new File(ficheroFO);
            

            boolean borrado = fichero.delete();
            boolean borrado2 = tmpPdfFile.delete();

        } catch (Exception e) {

            //System.out.println("EXCEPCION !!! : Error pasando de FO a PDF: " + e);

        }


    }    
}