/*
 * 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 Lesser General Public License as published by
 * the Free Software Foundation; either version 3 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 Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser 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.common.config.DocumentModel;
import fr.gouv.finances.cp.xemelios.data.DataConfigurationException;
import fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer;
import fr.gouv.finances.cp.xemelios.data.impl.sqlconfig.TDocument;
import fr.gouv.finances.cp.xemelios.data.impl.sqlconfig.TEtat;
import fr.gouv.finances.cp.xemelios.data.impl.sqlconfig.TPersistenceConfig;
import fr.gouv.finances.cp.xemelios.data.impl.sqlconfig.TTable;
import fr.gouv.finances.cp.xemelios.ui.MainWindow;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;

/**
 *
 * @author chm
 */
public class PesAllerPatch33 {
    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 = MainWindow.instance.getDocModels().getDocumentById("PES_Aller");
        if(dm==null) return;
        Connection con = null;
        try {
            con = impl.getConnection();
            TPersistenceConfig cfg = impl.getPersistenceConfig(dm);
            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) {
            
        } catch(SQLException 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) {}
    }
    
}
