/*
 * Copyright
 *  2010 axYus - http://www.axyus.com
 *  2010 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS_NB.
 *
 * XEMELIOS_NB is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * XEMELIOS_NB is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with XEMELIOS_NB; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 * 
 */


package fr.gouv.finances.cp.xemelios.data.impl.mysql;

import fr.gouv.finances.cp.utils.xml.marshal.XmlOutputter;
import fr.gouv.finances.dgfip.xemelios.common.config.ConfigModelPatcher;
import fr.gouv.finances.dgfip.xemelios.common.config.PersonnalConfigParser;
import fr.gouv.finances.dgfip.xemelios.common.config.RechercheModel;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import org.apache.log4j.Logger;

/**
 *
 * @author cmarchand
 */
public class SavedRequestsPatch {
    private static final Logger logger = Logger.getLogger(SavedRequestsPatch.class);

    public SavedRequestsPatch() {
        super();
    }
    public void run() {
        Connection con1 = null;
        Connection con2 = null;
        try {
            con1 = PoolManager.getInstance().getConnection();
            con2 = PoolManager.getInstance().getConnection();
            PreparedStatement updSt = con2.prepareStatement("UPDATE SAVED_REQUESTS SET REQUEST=? WHERE ID=?");
            ResultSet rs = con1.createStatement().executeQuery("SELECT ID, REQUEST FROM SAVED_REQUESTS");
            while(rs.next()) {
                PersonnalConfigParser pcp = new PersonnalConfigParser();
                String id = rs.getString(1);
                InputStream is = rs.getBinaryStream(2);
                pcp.parse(is);
                RechercheModel rm = (RechercheModel)pcp.getMarshallable();
                ConfigModelPatcher.patchRechercheModel(rm);
                
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                Charset cs = Charset.forName("ISO-8859-1");
                OutputStreamWriter out = new OutputStreamWriter(baos,cs);
                XmlOutputter outputter = new XmlOutputter(out, cs.name());
                rm.marshall(outputter);
                out.flush(); out.close();
                InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
                updSt.setBinaryStream(1, is2, baos.size());
                updSt.setString(2,id);
//                logger.debug("patching "+id+"\n"+baos.toString());
                updSt.executeUpdate();
            }
        } catch(Exception ex) {
            logger.error("run()",ex);
        } finally {
            if(con1!=null) PoolManager.getInstance().releaseConnection(con1);
            if(con2!=null) PoolManager.getInstance().releaseConnection(con2);
        }
    }

}
