/*
 * Copyright
 *   2009 axYus - www.axyus.com
 *   2009 C.Marchand - christophe.marchand@axyus.com
 *
 * This file is part of XEMELIOS.
 *
 * XEMELIOS 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 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; 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.xemelios.data.impl.MySqlDataLayer;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.log4j.Logger;

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

    public static final transient String PARAM_REPOSITORY_PATCH = "repository.patch.applied";

    private MySqlDataLayer impl = null;

    public RepositoryPatch(MySqlDataLayer impl) {
        super();
        this.impl = impl;
    }

    public void run() {
        Connection con = null;
        try {
            con = impl.getConnection();
            applyPatch(con,"PES_CARTEACHAT_REPOSITORY");
            applyPatch(con,"PAYE_REPOSITORY");
            applyPatch(con,"EDMN_REPOSITORY");
            applyPatch(con,"FACTURERECETTE_REPOSITORY");
            applyPatch(con,"FACTUREDEPENSE_REPOSITORY");
            applyPatch(con,"PES_ALLER_REPOSITORY");
            applyPatch(con,"PES_FACTURE_REPOSITORY");
            applyPatch(con,"QUITTANCEMENT_REPOSITORY");
            applyPatch(con,"VERSEMENT_REPOSITORY");

        } catch(SQLException sqlEx) {
            // TODO
        } finally {
            if(con!=null) impl.releaseConnection(con);
        }
        impl.setParameterValue(PARAM_REPOSITORY_PATCH, "applied");
    }

    protected void applyPatch(Connection con, String tbName) {
        boolean tableExists = false;
        try {
            con.createStatement().executeQuery("SELECT 1 FROM "+tbName);
            tableExists = true;
        } catch(SQLException sqlEx) {
            // rien a faire
        }
        if(tableExists) {
            try {
                con.createStatement().executeQuery("SELECT ID,LIBELLE FROM "+tbName);
            } catch(SQLException sqlEx) {
                try {
                    con.createStatement().executeUpdate("ALTER TABLE "+tbName+" ADD ID VARCHAR(25");
                } catch(Throwable t) {}
                try {
                    con.createStatement().executeUpdate("ALTER TABLE "+tbName+" ADD LIBELLE VARCHAR(100");
                } catch(Throwable t) {}
            }
        }
    }

}
