/*
 * 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.dgfip.xemelios.auth;

import fr.gouv.finances.cp.utils.PropertiesExpansion;
import fr.gouv.finances.dgfip.xemelios.auth.impl.std.db.ui.DlgConfigAuthentication;
import fr.gouv.finances.dgfip.xemelios.common.Constants;
import fr.gouv.finances.dgfip.xemelios.data.DataLayerManager;
import fr.gouv.finances.dgfip.xemelios.data.impl.pool.PoolManager;
import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.ResultSet;
import java.util.Properties;

/**
 * Permet de configurer l'authentification
 * @author chm
 */
public class CfgAuth {

    public static void main(String args[]) {
        try {
            File localDir = new File(System.getProperty("user.dir"));
            File propertyFile = new File(localDir.getParentFile(),"mysql.properties");
            if(!propertyFile.exists()) throw new RuntimeException("Can not find "+propertyFile.getAbsolutePath());
            File xemeliosProperties = new File(localDir.getParent(),"xemelios.properties");
            if(!xemeliosProperties.exists()) throw new RuntimeException("Can not find "+xemeliosProperties.getAbsolutePath());
            Properties props = new Properties();
            Properties xemProps = new PropertiesExpansion();
            Connection con = null;
            try {
                props.load(new FileInputStream(propertyFile));
//                System.out.println(props);
                xemProps.load(new FileInputStream(xemeliosProperties));
//                System.out.println(xemProps);
                PoolManager.getInstance().init(props);
                Class.forName("fr.gouv.finances.cp.xemelios.data.impl.MySqlDataLayer");
                DataLayerManager.setDataImpl(xemProps.getProperty(Constants.SYS_PROP_DATA_IMPL));
                con = PoolManager.getInstance().getConnection();
                ResultSet rs = con.createStatement().executeQuery("SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='authEngineClassName'");
                String initialEngineName = null;
                if(rs.next()) {
                    initialEngineName = rs.getString(1);
                    System.out.println("Current authentication engine is "+initialEngineName);
                } else
                    System.out.println("unable to read authentiation engine");
                DlgConfigAuthentication dlg = new DlgConfigAuthentication(null, true,initialEngineName);
                dlg.run();
                dlg.dispose();
                rs = con.createStatement().executeQuery("SELECT PARAM_VALUE FROM PARAMETERS WHERE PARAM_NAME='authEngineClassName'");
                if(rs.next()) {
                    System.out.println("Current authentication engine is now "+rs.getString(1));
                } else
                    System.out.println("unable to read authentiation engine");
            } catch(Exception ex) {
                throw new RuntimeException(ex);
            } finally {
                if(con!=null) PoolManager.getInstance().releaseConnection(con);
//                PoolManager.getInstance().shutdown();
            }
        } catch(Throwable t) {
            t.printStackTrace();
            System.exit(1);
        }
    }

}
