/*
 * Copyright
 *   2007 axYus - www.axyus.com
 *   2007 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.dgfip.xemelios.common.config.DocumentModel;
import fr.gouv.finances.dgfip.xemelios.data.DataConfigurationException;
import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.dgfip.xemelios.common.config.Loader;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TDocument;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TEtat;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TPersistenceConfig;
import fr.gouv.finances.dgfip.xemelios.data.impl.sqlconfig.TTable;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import org.apache.log4j.Logger;
/**
 *
 * @author chm
 */
public class PesAllerPatch33 {
     private static final Logger logger = Logger.getLogger(PesAllerPatch33.class);
    public static final transient String PARAM_PESV2_PATCH_33 = "pesv2.patch.3.3.applied";
    private MySqlDataLayer impl;

    
    /** Creates a new instance of PesAllerPatch33 */
    public PesAllerPatch33(MySqlDataLayer impl) {
        super();
        this.impl=impl;
    }
    public void run() {
         DocumentModel dm = null;
        try {
            dm = Loader.getDocumentsInfos(null).getDocumentById("PES_Aller");
        } catch(Throwable t) {
            logger.error("while getting config",t);
        }
        if(dm==null) return;
        Connection con = null;
        try {
            con = impl.getConnection();
            TPersistenceConfig cfg = impl.getPersistenceConfig(dm,null);
            TDocument td = cfg.getLayer("mysql").getDocument("PES_Aller");
            TTable tt = td.getListBcTable(); if(tt!=null) deleteFrom(tt.getBaseName(),con);
            tt = td.getRepositoryTable(); if(tt!=null) deleteFrom(tt.getBaseName(),con);
            tt = td.getSpecialKeyTable(); if(tt!=null) deleteFrom(tt.getBaseName(),con);
            for(TEtat te:td.getEtats()) {
                tt = te.getDocumentTable();  if(tt!=null) deleteFrom(tt.getBaseName(),con);
                tt = te.getMaintable();  if(tt!=null) deleteFrom(tt.getBaseName(),con);
                for(TTable tti: te.getIndexTables()) {
                    deleteFrom(tti.getBaseName(),con);
                }
            }
            impl.setParameterValue(PARAM_PESV2_PATCH_33,"applied");
        } catch(DataConfigurationException dcEx) {
               logger.error(" ", dcEx);
        } catch(SQLException sqlEx) {
            logger.error(" ", sqlEx);
            
        } finally {
            if(con!=null) impl.releaseConnection(con);
        }
    }
    
    private void deleteFrom(String table, Connection con) {
        try {
            Statement st = con.createStatement();
            st.execute("DELETE FROM "+table);
            st.close();
        } catch(SQLException sqlEx) {}
    }
    
}
