package tarjetasServlet;

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 java.util.Locale;
import java.util.ResourceBundle;

import javax.naming.Context;
import javax.naming.InitialContext;
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.m05bFileLog;

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


public class m05bImpCambioCentroServlet extends javax.servlet.http.HttpServlet
{
	   java.sql.Connection conn=null;
	   m05bComun miComun = new m05bComun();
	   m05bConexion miConex = new m05bConexion();
       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);
   }

   public void performTask(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws ServletException
   {
   	   try {
	   PreparedStatement pstmt = null, pstmt2 = null;
	   ResultSet rs = null, rs2 = null;
	   
	   //m05bAttachExample miMail = new m05bAttachExample();
       FileOutputStream FileStreamSalida=null;
       File FileSalida=null ;
       String sFileName="";
       String sNomFich="";
	   
      try
      {     HttpSession session = request.getSession(true);
  	/*Iker. idioma*/
 		Locale idiomaLocal = (Locale)session.getAttribute("Local");
 		String idiomaSes = ((idiomaLocal.getLanguage()==null)?"es":idiomaLocal.getLanguage());	
 		 ResourceBundle res =  (ResourceBundle)session.getAttribute("resTit");
         String campoTexto="C";
         String formatoFecha="dd/MM/yyyy";
         if(!idiomaSes.equals("es")){
 			campoTexto="E";
 			formatoFecha="yyyy/MM/dd";
         }
 			
		    String persona = (String) session.getAttribute("persona");
			conn = miConex.getConnection();
	        conn.setAutoCommit(false);  // Al final si todo ha ido bien se hace commit
			
			sNomFich=miComun.leerPropiedad("Fich.CambiosCentro");
      
      	 // Obtención de información de BD
      	 	 	// Vehículo
         String sCount="";
         String sSituacion=request.getParameter("Situacion");
            	if ((sSituacion==null)||(sSituacion.equals(""))){
					sSituacion="";}
			    else {
                    sSituacion=miComun.ListaCaracteres(sSituacion);} 
      	 String matricula="";
		 String sCta="";
	     String sMatricula = "";
	     String sMege = "";
	     String sTarjeta = "";
	     String sUnidad = "";
	     String sCentro= ""; 
	     String sFecha= ""; 
	     String sDesCta="";  
	     String sCtaAnt="";  
	     String sSentenciaUpdate= " UPDATE "+
	     	    m05bConstantes.TAB_TARJETAS+
	     	    " SET "+ m05bConstantes.COL_31_CAMBIO+"='S',"+ m05bConstantes.COL_31_FCAMBIO+
                " = TO_DATE(TO_CHAR(SYSDATE,'dd/mm/yyyy'),'dd/mm/yyyy')"+	     	 
                " WHERE (" + "  ("+
                   m05bConstantes.COL_31_SITUACION+"='A') AND ("+
                   m05bConstantes.COL_31_CAMBIO+"='N'))";
    	 miLog.addTraza(persona , "m05bImpCambiocentroServlet" , "sSentenciaUpdate: " + sSentenciaUpdate, m05bConstantes.LOG_NIVEL_BAJO);
         
         String sSentencia = " SELECT REPLACE("+
            	   m05bConstantes.COL_03_NOMBRE + ",'.',''),"+
            	   m05bConstantes.COL_31_TARJETA + ","+
            	   m05bConstantes.COL_31_MATRICULA+",'888888','01', "+
            	   m05bConstantes.COL_31_CENTRO+",TO_CHAR("+
            	   m05bConstantes.COL_31_FCAMBIO+" ,'"+formatoFecha+"') ,"+ 
            	   m05bConstantes.COL_31_CUENTA+
                   " FROM " +
                   m05bConstantes.TAB_TARJETAS+" ,"+
                   m05bConstantes.TAB_BANCOS +
                   " WHERE ((" +
                   m05bConstantes.COL_31_CUENTA +"="+ 
                   m05bConstantes.COL_03_NCUENTA+") and ("+
                   m05bConstantes.COL_31_SITUACION+"='A') AND ("+
                   m05bConstantes.COL_31_CAMBIO+"='N'))"+
                   " order by " +
                   m05bConstantes.COL_03_NCUENTA ;
    	 miLog.addTraza("Bego" , "m05bImpCambiocentroServlet" , "sSentencia: " + sSentencia, m05bConstantes.LOG_NIVEL_BAJO);
         
         
		     
		 pstmt = conn.prepareStatement(sSentencia);
		 rs = pstmt.executeQuery();
			 
         //String foParam = request.getParameter(FO_REQUEST_PARAM);
         String sFoFileDir = "/datos/m05b/tmp/";
         String sFileDir="/datos/m05b/file/";
         File foFileDir = new File(sFoFileDir);
         File tmpFoFile = File.createTempFile("m05b", "Fo", foFileDir);

         FileOutputStream tmpFoFileStream = new FileOutputStream(tmpFoFile);


         String sLinea;
         String sLinea2;
         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=\"21cm\" page-width=\"29.5cm\" margin-top=\"2cm\" margin-bottom=\"2cm\" margin-left=\"2cm\" margin-right=\"2cm\">\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=\"1.5cm\" />\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 text-align=\"end\">\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            sLinea = "      </fo:block>\n";
		            tmpFoFileStream.write(sLinea.getBytes());
                    sLinea = "      <fo:block font-weight=\"bold\" padding-top=\"10pt\">\n " +res.getString("infCambCtr.titulo")+ "</fo:block>\n";
			        tmpFoFileStream.write(sLinea.getBytes());
        	sLinea = " 		</fo:static-content>\n"; 
        	tmpFoFileStream.write(sLinea.getBytes());

            //PIE DE PAGINA Pie
        	sLinea = "		<fo:static-content flow-name=\"xsl-region-after\">\n";
        	tmpFoFileStream.write(sLinea.getBytes());
                        //PIE DE PAGINA Pie
					sLinea = "      <fo:block >\n";
		            tmpFoFileStream.write(sLinea.getBytes());
		            	sLinea= "       <fo:table>\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"127mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"125mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-body font-family=\"Helvetica\" font-size=\"6pt\">\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\">" + miComun.getDetFechaActual(campoTexto)+ "</fo:block>";
							            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=\"right\">" ; 
                       		            tmpFoFileStream.write(sLinea.getBytes());
		                                sLinea = res.getString("pagina")+" <fo:page-number/> \n";
                        	            tmpFoFileStream.write(sLinea.getBytes());
							            sLinea=  "</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());
      	         // --------------    DETALLE DEL LISTADO    --------------
		           		sLinea= "        <fo:table space-after.optimum=\"12pt\">\n";
		            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"30mm\"/>\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
		            		sLinea = "	    <fo:table-column column-width=\"50mm\"/>\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=\"30mm\"/>\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=\"8pt\">\n";
			            	tmpFoFileStream.write(sLinea.getBytes());
			            	int cont=0;
		   		            while (rs.next()){
		   		    	    sTarjeta=rs.getString(2)==null ? "":rs.getString(2);
	       	    			sMatricula = rs.getString(3)==null ? "":rs.getString(3);
	       	    			sMege= rs.getString(4)==null ? "":rs.getString(4);
	       	    			sUnidad = rs.getString(5)==null ? "":rs.getString(5);
	       	    			sCta=rs.getString(8)==null ? "":rs.getString(8);
	       	    			sDesCta = rs.getString(1)==null ? "":rs.getString(1);
	       	    			sFecha = rs.getString(7)==null ? "":rs.getString(7);
	       	    			sCentro = rs.getString(6)==null ? "":rs.getString(6);
	       	    			
	       	    			    if (sCtaAnt==""){
	       	    			    sFileName=sFileDir+sNomFich+sDesCta+".txt";
	       	    			    FileSalida= new File(sFileName);
	       	    			    FileStreamSalida = new FileOutputStream(FileSalida);
	       	    			    cont=0;
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n <![CDATA[" + sDesCta + "]]></fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// NTarjeta
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n " + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Matricula
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Marca
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Modelo
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// CodigoCentro
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\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());
     			                   // Matrícula
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col1")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            			// Tarjeta
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n "+res.getString("infCambCtr.col2")+ "</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Mege
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col3")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			   // Unidad
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col4")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   //Centro
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col5")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // Fec.Cambio
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col6")+"</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());}
	       	    			   else if (((!(sCta.equals(sCtaAnt))) && (sCtaAnt!=""))||(cont==20)){
	       	    			   	if ((!(sCta.equals(sCtaAnt))) && (sCtaAnt!="")){
	       	    			   	   FileStreamSalida.close();
	       	    			       sFileName=sFileDir+sNomFich+sDesCta+".txt";
	       	    			       FileSalida= new File(sFileName);
	       	    			   	   FileStreamSalida = new FileOutputStream(FileSalida);}

	       	    			    cont=0;
	       	                    sLinea = "	    <fo:table-row keep-with-previous=\"always\" break-before=\"page\">\n";
			            		tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro 
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n <![CDATA[" + sDesCta + "]]></fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"left\">\n "  + "</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Matricula
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Marca
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n" +"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Modelo
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n"+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// CodigoCentro
			            			sLinea = "	    <fo:table-cell border-width=\"0.1mm\" >\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\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());
     			                   // Matricula
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col1")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
			            			// Tarjeta
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\" text-align=\"center\">\n "+res.getString("infCambCtr.col2")+"</fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Mege
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col3")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
		            			   // Unidad
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col4")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // Centro
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col5")+"</fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());	
     			                   // Fecha
			            			sLinea = "	    <fo:table-cell padding-top=\"1mm\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block font-weight=\"bold\"  text-align=\"center\">\n "+res.getString("infCambCtr.col6")+"</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());
			            		
			            		}else{
	       	    			   	cont=cont+1;   
   		                        sLinea = "	    <fo:table-row >\n";
			            		tmpFoFileStream.write(sLinea.getBytes());}
	       	    			    sCtaAnt=sCta;
	       	    			    sLinea2= sTarjeta +sMege+sUnidad+sCentro+"\r\n";
	       	    			    FileStreamSalida.write(sLinea2.getBytes());
			            			// Matricula
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\"><![CDATA[" +sMatricula+"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Tarjeta 
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n <![CDATA[" + sTarjeta +"]]></fo:block>";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Mege
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n <![CDATA["+ sMege +"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Unidad
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sUnidad+"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Centro
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sCentro+"]]></fo:block>\n";
							            tmpFoFileStream.write(sLinea.getBytes());
			            			sLinea = "	    </fo:table-cell>\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
			            			// Fecha
			            			sLinea = "	    <fo:table-cell padding-top=\"2mm\" border-width=\"0.1mm\" border-style=\"solid\">\n";
			            			tmpFoFileStream.write(sLinea.getBytes());
							            sLinea = "      <fo:block  text-align=\"center\">\n<![CDATA["+sFecha+"]]></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.close();
                    pstmt2 = conn.prepareStatement(sSentenciaUpdate);
                    rs2 = pstmt2.executeQuery();
                   	conn.commit();
				    if (rs2 != null)
					    rs2.close();
				    if (pstmt2 != null)
					    pstmt2.close();
                   	
			 	      	    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();
         FileStreamSalida.close();
         //miMail.SendAttach("CO00544R@ejgva2","CO00544R@ejgva2","/datos/m05b/tmp/mensa.txt");
         FileInputStream file = new FileInputStream(sFoFileDir + tmpFoFile.getName());

         renderFO(new InputSource(file), response);

         tmpFoFile.delete();

         response.flushBuffer();

	    } catch (SQLException ex1) {
    	        conn.rollback();
				miLog.addTraza(ex1);
				response.sendRedirect("m05bError.jsp");
		} 	finally{
				if (rs != null)
					rs.close();
				if (pstmt != null)
					pstmt.close();
				if (rs2 != null)
					rs2.close();
				if (pstmt2 != null)
					pstmt2.close();
				if (conn != null)
					conn.close();
		}
	}
	catch(Throwable theException)
	{
		// uncomment the following line when unexpected exceptions
		// are occuring to aid in debugging the problem.
		miLog.addTraza(theException);
		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);
        }
    }
}