/*
 * 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.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 RapportPatch33 {
    public static final transient String PARAM_RAPPORT_PATCH_33 = "rapport.patch.3.3.applied";
    private MySqlDataLayer impl;

    
    /** Creates a new instance of PesAllerPatch33 */
    public RapportPatch33(MySqlDataLayer impl) {
        super();
        this.impl=impl;
    }
    public void run() {
        DocumentModel dm = MainWindow.instance.getDocModels().getDocumentById("DocumentRapport");
        if(dm==null) return;
        try {
            Connection con = impl.getConnection();
            TPersistenceConfig cfg = impl.getPersistenceConfig(dm);
            TDocument td = cfg.getLayer("mysql").getDocument("DocumentRapport");
            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_RAPPORT_PATCH_33,"applied");
        } catch(DataConfigurationException dcEx) {
            
        } catch(SQLException sqlEx) {
            
        }
    }
    
    private void deleteFrom(String table, Connection con) {
        try {
            Statement st = con.createStatement();
            st.execute("DELETE FROM "+table);
            st.close();
        } catch(SQLException sqlEx) {}
    }
    
}
