package denunciaServlet;

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.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.m05bConstantesInformes;
import m05bClasses.m05bFileLog;

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


public class m05bImpDenunciaSinNombrePltServlet 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;

      try
      {  HttpSession session = request.getSession(true);
			m05bConexion miConex = new m05bConexion();
			conn = miConex.getConnection();
      
      	 // Obtención de información de BD
      	 	 	// Vehículo

      	 m05bFileLog miLog = new m05bFileLog(); 
	 	 m05bComun miComun = new m05bComun();
      	 m05bCamposInformes miVehiculo = new m05bCamposInformes();

      	 String firma = miVehiculo.getFirma(m05bConstantesInformes.DET7_C_PLTDENSINNOMBRE);

      	 String sDepartamento="";
      	 String [][] Matriculas = new String[6][2]; 
      	 //Preparamos la Sentencia de recuperación

      	 int nCont=0;
      	 int nCont1=0;
      	 String sMatricula=request.getParameter("Matricula1")==null?"":request.getParameter("Matricula1");
      	 String sFecha=request.getParameter("Fecha1")==null?"":request.getParameter("Fecha1");
     	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 }
      	 sMatricula=request.getParameter("Matricula2")==null?"":request.getParameter("Matricula2");
      	 sFecha=request.getParameter("Fecha2")==null?"":request.getParameter("Fecha2");
      	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 }      	 
      	 sMatricula=request.getParameter("Matricula3")==null?"":request.getParameter("Matricula3");
      	 sFecha=request.getParameter("Fecha3")==null?"":request.getParameter("Fecha3");
      	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 }      	 			 
      	 sMatricula=request.getParameter("Matricula4")==null?"":request.getParameter("Matricula4");
      	 sFecha=request.getParameter("Fecha4")==null?"":request.getParameter("Fecha4");
      	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 }
      	 sMatricula=request.getParameter("Matricula5")==null?"":request.getParameter("Matricula5");
      	 sFecha=request.getParameter("Fecha5")==null?"":request.getParameter("Fecha5");
      	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 }
      	 sMatricula=request.getParameter("Matricula6")==null?"":request.getParameter("Matricula6");
      	 sFecha=request.getParameter("Fecha6")==null?"":request.getParameter("Fecha6");
      	 if (!(sMatricula.equals(""))){
     		Matriculas[nCont][0]=sMatricula;
     		Matriculas[nCont][1]=sFecha;
     		nCont++;
     	 } 
     	 sDepartamento=request.getParameter("Departamento")==null?"":request.getParameter("Departamento");
         
         //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=\"2cm\"/>\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-body  margin-top=\"2.8cm\" margin-bottom=\"3cm\" />\n";
			         tmpFoFileStream.write(sLinea.getBytes());
			         sLinea = "		<fo:region-after  extent=\"1cm\"/>\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




		            
                        //PIE DE PAGINA FIRMA
		         // --------------   EN   --------------


		            
//FIRMADO:

		            




                        //PIE DE PAGINA Pie
					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=\"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: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());



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

		            sLinea = "      <fo:block  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=\"12pt\">\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.DET0_E_PLTDENSINNOMBRE + "</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.DET0_C_PLTDENSINNOMBRE +  "</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 text-align=\"justify\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_E_PLTDENSINNOMBRE + "</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  text-align=\"justify\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  padding-top=\"8pt\">\n " + m05bConstantesInformes.DET1_C_PLTDENSINNOMBRE +  "</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  padding-top=\"20pt\">\n " ;
						            tmpFoFileStream.write(sLinea.getBytes());	
						            sLinea = "      	<fo:inline  font-weight=\"bold\" text-decoration=\"underline\">\n" +m05bConstantesInformes.DET3_E_PLTDENSINNOMBRE + " / </fo:inline>\n" ;							            
						            tmpFoFileStream.write(sLinea.getBytes());	
						            sLinea = "      	<fo:inline text-decoration=\"underline\">\n" +m05bConstantesInformes.DET3_C_PLTDENSINNOMBRE + "</fo:inline>\n" +   "</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=\"50pt\">\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=\"20pt\">\n " ;
							            tmpFoFileStream.write(sLinea.getBytes());	
							            sLinea = "      	<fo:inline  font-weight=\"bold\" text-decoration=\"underline\">\n" +m05bConstantesInformes.DET4_E_PLTDENSINNOMBRE + " / </fo:inline>\n" ;							            
							            tmpFoFileStream.write(sLinea.getBytes());	
							            sLinea = "      	<fo:inline text-decoration=\"underline\">\n" +m05bConstantesInformes.DET4_C_PLTDENSINNOMBRE + "</fo:inline>\n" +   "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());							            
							            //sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"50pt\">\n " + m05bConstantesInformes.DET4_C_PLTDENSINNOMBRE + "</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());
                                nCont1=0;
			            		while (nCont1 < nCont){
				            		sLinea = "	    <fo:table-row>\n";
				            		tmpFoFileStream.write(sLinea.getBytes());
			            				sLinea = "	    <fo:table-cell>\n";
			            				 tmpFoFileStream.write(sLinea.getBytes());
			         		            sLinea = "      <fo:block  padding-top=\"1pt\">\n " + Matriculas[nCont1][1]+ "</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:table-cell>\n";
			            				tmpFoFileStream.write(sLinea.getBytes());				            				 
			            				sLinea = "	    <fo:table-cell>\n";
			            				 tmpFoFileStream.write(sLinea.getBytes());	
			            				 sLinea = "      <fo:block  padding-top=\"1pt\">\n " + Matriculas[nCont1][0]+ "</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());
			            			nCont1++;
			            		}
			            		
			            		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=\"5pt\">\n " + m05bConstantesInformes.DET2_E_PLTDENSINNOMBRE + "</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=\"5pt\">\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=\"5pt\">\n " + m05bConstantesInformes.DET2_C_PLTDENSINNOMBRE +  "</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 padding-top=\"50pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table 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=\"100mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"12pt\">\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 " + "</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 " + m05bConstantesInformes.DET5_E_PLTDENSINNOMBRE +" "+  miComun.getDetFechaActual(m05bConstantesInformes.IDIOMA_E)+  "</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 >\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());
			            			// DET1 - Jaun horiek
			            			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());
			            			// DET1 - Intermedia
			            			sLinea = "	    <fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  >\n " + m05bConstantesInformes.DET5_C_PLTDENSINNOMBRE +" "+  miComun.getDetFechaActual(m05bConstantesInformes.IDIOMA_C)+  "</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  >\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());


		    		sLinea = "      <fo:block  padding-top=\"50pt\" >\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=\"70mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"12pt\">\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\"  text-align=\"left\">\n" + m05bConstantesInformes.DET6_E_PLTDENSINNOMBRE+ "</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.DET6_C_PLTDENSINNOMBRE+ "</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 >\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"90mm\"/>\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=\"12pt\">\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_PLTDENSINNOMBRE+ "  " + "</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_PLTDENSINNOMBRE +"</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    padding-top=\"130pt\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	// Detalle-1
		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"70mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"90mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"12pt\">\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.DET8_E_PLTDENSINNOMBRE+ "  " + "</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.DET8_C_PLTDENSINNOMBRE +"</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    >\n";
			   		            tmpFoFileStream.write(sLinea.getBytes());
			   		            	// Detalle-1
			   		            	sLinea= "       <fo:table table-layout=\"fixed\">\n";
			   		            	tmpFoFileStream.write(sLinea.getBytes());
			   		       		    sLinea = "	    <fo:table-column column-width=\"7mm\"/>\n";
				            	    tmpFoFileStream.write(sLinea.getBytes());
				            		sLinea = "	    <fo:table-column column-width=\"11mm\"/>\n";
					            	tmpFoFileStream.write(sLinea.getBytes());
				            		sLinea = "	    <fo:table-column column-width=\"52mm\"/>\n";
					            	tmpFoFileStream.write(sLinea.getBytes());					            	
				            		sLinea = "	    <fo:table-column column-width=\"90mm\"/>\n";
					            	tmpFoFileStream.write(sLinea.getBytes());
			   		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"12pt\">\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:table-cell >\n";
		            			tmpFoFileStream.write(sLinea.getBytes());		            			
			            			sLinea = "	    <fo:table-cell >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block padding-top=\"10pt\" font-weight=\"bold\" text-align=\"right\" >\n " + m05bConstantesInformes.DET9_E_PLTDENSINNOMBRE+ "  " + "</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 padding-top=\"10pt\" text-align=\"left\">\n " +" "+ m05bConstantesInformes.DET9_C_PLTDENSINNOMBRE +"</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 padding-top=\"10pt\" text-align=\"left\">\n " +" "+ sDepartamento +"</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: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{
		if (conn != null)
			try {
				conn.close();
			} catch (SQLException 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);

        }


    }
    
}