/*
 * 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.dropUsers;

import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

import javax.swing.JOptionPane;

import fr.gouv.finances.cp.utils.PropertiesExpansion;
import fr.gouv.finances.dgfip.xemelios.common.Scramble;

public class DropThemAll {
    public static final transient String PROP_CONFIG_FILE_NAME = "xemelios.mysql.config.filename";
    
    /**
     * @param args
     */
    public DropThemAll() {
        String configFileName = System.getProperty(PROP_CONFIG_FILE_NAME);
        PropertiesExpansion props = new PropertiesExpansion();
        for(Object key:System.getProperties().keySet()) {
            props.put(key,System.getProperty((String)key));
        }
        try {
            props.load(new FileInputStream(configFileName));
            Class.forName(props.getProperty("driver.class"));
            String cryptedPass = props.getProperty("password");
            String clearPass = Scramble.unScramblePassword(cryptedPass);
            props.setProperty("password",clearPass);
            Connection con = DriverManager.getConnection(props.getProperty("database.url"),props);
            if(JOptionPane.showConfirmDialog(null,"Les mots de passes doivent impérativement être réinitialisés.\nC'est normalement le rôle de l'administrateur de faire ceci.\n\nVoulez-vous continuer ?","Confirmation",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) {
                Statement st = con.createStatement();
                st.executeUpdate("DROP TABLE IF EXISTS BASIC_USERS");
                st.executeUpdate("DROP TABLE IF EXISTS DGCP_USERS");
                st.close();
//                if(JOptionPane.showConfirmDialog(null,"Les tables existantes vont être supprimées. Vous devrez réimporter vos données.\nCependant, vous pouvez choisir de ne pas effectuer cette opération,\nmais vous risquez d'être limités dans vos recherches.\n\nVoulez-vous continuer ?","Confirmation",JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION) {
//                    st=con.createStatement();
//                    ResultSet rs = st.executeQuery("SELECT TABLE_NAME FROM TABLES_VERSIONS");
//                    while(rs.next()) {
//                        Statement st2 = con.createStatement();
//                        st2.executeUpdate("DROP TABLE IF EXISTS "+rs.getString(1));
//                        st2.close();
//                    }
//                    rs.close();
//                    st.executeUpdate("DELETE FROM TABLES_VERSIONS");
//                }
                System.out.println("Utilisateurs supprimés.");
            }
            con.close();
            
        } catch(Exception ex) {
            ex.printStackTrace();
            JOptionPane.showMessageDialog(null,ex.getMessage());
        }
    }
    
    public static void main(String args[]) {
        new DropThemAll();
    }
    
}