import java.math.BigDecimal;
import java.sql.*;
import com.softwareag.entirex.aci.Broker;
import com.softwareag.entirex.aci.BrokerException;
import com.softwareag.entirex.aci.EntireXSecurity;

/**
 * <p>Titre : JxweblogBw</p>
 * <p>Description : Classe de connexion et de fermeture de connexion au broker</p>
 * <p>Copyright : Copyright (c) 2013</p>
 * <p>Societe : ENSOR</p>
 * 
 * @author AR & DR <i>11/10/2013</i>
 * @version 1.0
 */
public class JxweblogBw {

	private static String UTILISATEUR;
	private static String PASSWORD;

	/**
	 * <p>Titre : setServerAddress et getServerAddress</p>
	 * <p> Description : Adresse du Serveur Broker</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static void setUtilisateur(String p) {
		UTILISATEUR = p;
	}

	public static String getUtilisateur() {
		return UTILISATEUR;
	}

	public static void setPassword(String p) {
		PASSWORD = p;
	}

	public static String getPassword() {
		return PASSWORD;
	}

	/**
	 * <p>Titre : logonBroker</p>
	 * <p>Description : Connexion au broker</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static Broker logonBroker() throws BrokerException {
		System.out.println("logonBroker : " + getUtilisateur() + "----"
				+ getPassword());
		Broker broker = new Broker(Constantes.BROKER_ID_PORT,
				Constantes.BROKER_IS_SECURE ? getUtilisateur()
						: Constantes.USER_APPLI_NOT_SECURE);
		// Broker broker = new Broker(Constantes.BROKER_ID_PORT,
		// Constantes.BROKER_IS_SECURE?"RPCB014W":Constantes.USER_APPLI_NOT_SECURE);
		if (Constantes.BROKER_IS_SECURE) {
			broker.setSecurity(new EntireXSecurity(), false);
		}
		Broker.setTrace(Constantes.TRACE);
		boolean brokerIsLogon = false;

		if (Constantes.BROKER_IS_SECURE) {
			broker.logon(getPassword());
		} else {
			broker.logon();
		}
		brokerIsLogon = true;
		System.out.println("Connexion Broker OK");
		return broker;
	} // End logonBroker

	/**
	 * <p>Titre : closeBroker</p>
	 * <p>Description : Fermeture du broker</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static void closeBroker(Broker broker) throws BrokerException {
		// BROKER LOGOFF
		broker.logoff();
		System.out.println("Deconnexion Broker OK");
	} // End closeBroker

	/**
	 * <p>Titre : logonBroker</p>
	 * <p>Description : Connexion au broker avec changement de mot de passe</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static Broker setBrokerPasswordAndLogon(String newpassword)
			throws BrokerException {
		System.out.println("logonBroker : " + getUtilisateur() + "----"
				+ getPassword());
		Broker broker = new Broker(Constantes.BROKER_ID_PORT,
				Constantes.BROKER_IS_SECURE ? getUtilisateur()
						: Constantes.USER_APPLI_NOT_SECURE);
		if (Constantes.BROKER_IS_SECURE) {
			broker.setSecurity(new EntireXSecurity(), false);
		}
		Broker.setTrace(Constantes.TRACE);
		boolean brokerIsLogon = false;

		if (Constantes.BROKER_IS_SECURE) {
			broker.logon(getPassword(), newpassword);
		} else {
			broker.logon();
		}
		brokerIsLogon = true;
		System.out.println("Changement mot de passe Broker OK");
		return broker;
	} // End logonBroker

	/*************************************************************
	 * <p>Titre : getConnexion</p>
	 * <p>Description : Connexion a Loggia et recuperation de la WebEnt</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */

	 public static Jxweblog getConnexion(
		String cd_cible,
		String dbid,
		String rpc_name,
		Broker broker) throws BrokerException {
		// Initialisation des variables en entree
		Jxweblog jxweblog                 = new Jxweblog(broker, Utils.formatSrvAddr (rpc_name));
		Jxweblog.NxweblogAxwebent vWebent = jxweblog.new NxweblogAxwebent();

		// Initialiser la vWebent
		vWebent.dbid                      = new BigDecimal(dbid);
		vWebent.utilisateur               = getUtilisateur();
		vWebent.fonction                  = "LOGIN";
		
		// vWebent.transaction = "WBWW";
		vWebent.cd_cible                  = cd_cible;
		vWebent.cd_application            = Constantes.CD_APPLICATION;
		vWebent.transaction               = Constantes.CD_TRANS_RACINE;
		jxweblog.setNxweblogAxwebent(vWebent);

		// Weblog
		Jxweblog.NxweblogAxweblog vWeblog = jxweblog.new NxweblogAxweblog();
		vWeblog.password                  = Constantes.USER_LOGGIA_PASSWORD;
		jxweblog.setNxweblogAxweblog(vWeblog);

		// CALL SERVER
		jxweblog.nxweblog();

		// Verification du code retour
		System.out.println("Code retour connexion = "
				+ vWebent.code_retour.intValue());
		if (vWebent.code_retour.intValue() != 0) {
			for (int i = 0; i < vWebent.nb_ano.intValue(); i++) {
				System.out.println(vWebent.t_ano[i].code_ano + "["
						+ vWebent.t_ano[i].type_ano + "] :  "
						+ vWebent.t_ano[i].lib_ano.trim() + " ");
			}
		}
		return jxweblog;
	} // End getConnexion
	 
	/* 
	 * <p>Titre : getConnexion </p>
	 * <p>Description : Surcharge n°1 </p>
	*/
	
	public static Jxweblog getConnexion(
		String cd_cible,
		String dbid,
		String rpc_name) throws BrokerException {
		Broker broker = logonBroker();
		Jxweblog jxweblog = getConnexion(cd_cible, dbid, rpc_name, broker);
		closeBroker(broker);
		return jxweblog;
	}

	/* Fin méthode getConnexion ************************************/

	/*************************************************************
	 * <p>Titre : closeConnexion</p>
	 * <p>Description : Deconnexion a Loggia sans disposer de jxweblog</p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static void closeConnexion(
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String password,
			String cd_cible_exploitant,
			String cd_stegrp,
			String cd_cible,
			String dbid,
			String rpc_name,
			Broker broker) throws BrokerException {

		// Reconstruction de jxweblog
		Jxweblog jxweblog                 = new Jxweblog(broker, Utils.formatSrvAddr(rpc_name));
		Jxweblog.NxweblogAxwebent vWebent = jxweblog.new NxweblogAxwebent();

		// Initialiser la vWebent
		vWebent.dbid                      = new BigDecimal(dbid);
		vWebent.cd_application            = Constantes.CD_APPLICATION;
		vWebent.groupe_exploitant         = groupe_exploitant;
		vWebent.numero_session            = new BigDecimal(numero_session);
		vWebent.utilisateur               = utilisateur;
		vWebent.cible_exploitant          = cd_cible_exploitant;
		vWebent.cd_stegrp                 = cd_stegrp;
		vWebent.fonction                  = "CLOSE";
		jxweblog.setNxweblogAxwebent(vWebent);

		// Weblog
		Jxweblog.NxweblogAxweblog vWeblog = jxweblog.new NxweblogAxweblog();
		vWeblog.password                  = password;
		jxweblog.setNxweblogAxweblog(vWeblog);

		// CALL SERVER
		jxweblog.nxweblog();

	} // End closeConnexion

	/**
	 * <p>Titre : closeConnexion</p>
	 * <p>Description : Variante n°1 sans broker</p>
	 */
	public static void closeConnexion(
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String password,
			String cd_cible_exploitant,
			String cd_stegrp,
			String cd_cible,
			String dbid,
			String rpc_name) throws BrokerException {
	// Connexion Broker
	Broker broker = logonBroker();
	closeConnexion(
		groupe_exploitant,
		numero_session,
		utilisateur,
		password,
		cd_cible_exploitant,
		cd_stegrp,
		cd_cible,
		dbid,
		rpc_name,
		broker);
	closeBroker(broker);
	}
			
	/**
	 * <p>Titre : closeConnexion</p>
	 * <p>Description : Variante n°2 via la jxweblog </p>
	 */
	public static void closeConnexion(
		String rpc_name,
		Jxweblog jxweblog,
		Broker broker)
		throws BrokerException {
		Jxweblog.NxweblogAxwebent vWebent = jxweblog.getNxweblogAxwebent();
		vWebent.fonction                  = "CLOSE";
		Jxweblog jxwebloglocal            = new Jxweblog(broker, Utils.formatSrvAddr (rpc_name) );
		jxwebloglocal.setNxweblogAxwebent(vWebent);

		// CALL SERVER
		jxwebloglocal.nxweblog();
	}

	/**
	 * <p>Titre : closeConnexion</p>
	 * <p>Description : Variante n°3 via sans le broker </p>
	 */
	public static void closeConnexion(
		String rpc_name,
		Jxweblog jxweblog) throws BrokerException {
		Broker broker = logonBroker();
		closeConnexion(Utils.formatSrvAddr (rpc_name), jxweblog, broker);
		closeBroker(broker);
	}
	/* Fin méthode closeConnexion ************************************/
	
	/**
	 * <p>Titre : main</p>
	 * <p>Description : Programme principal de tests </p>
	 * <p>Copyright : Copyright (c) 2013</p>
	 * <p>Societe : ENSOR</p>
	 * 
	 * @author AR & DR <i>11/10/2013</i>
	 * @version 1.0
	 */
	public static void main(String[] args) throws BrokerException {
		// Chargement Constantes
		Constantes.loadProperties("BW");
		setUtilisateur("MADAA01");
		setPassword("MADAAA1");

		System.out.println("Connexion Loggia de : " + getUtilisateur());

		// Connexion Broker
		Broker broker = JxweblogBw.logonBroker();
		
		// Connexion Loggia
		//Jxweblog jxweblog = getConnexion("50", "14", "SRV1", broker);
		Jxweblog jxweblog = getConnexion("00", "17", "ABOEX17", broker);
		//Jxweblog jxweblog = getConnexion("50", "14", "SRV1", broker);
		
		System.out.println("Connexion Loggia OK");
		System.out.println("groupe_exploitant de l utilisateur : "
				+ jxweblog.getNxweblogAxwebent().groupe_exploitant);
		System.out.println("Liste des transactions possibles :");
		for (int i = 0; i < jxweblog.getNxweblogAxweblog().code_transaction.length; i++) {
			System.out.println("code="
					+ jxweblog.getNxweblogAxweblog().code_transaction[i]
					+ "; type="
					+ jxweblog.getNxweblogAxweblog().type_transaction[i]
					+ "; niv="
					+ jxweblog.getNxweblogAxweblog().niv_transaction[i]
					+ "; lib="
					+ jxweblog.getNxweblogAxweblog().lib_transaction[i]
					+ "; master="
					+ jxweblog.getNxweblogAxweblog().master_transaction[i]);
		}
		System.out.println("Transaction defaut :");
		System.out.println("transaction_defaut="
				+ jxweblog.getNxweblogAxweblog().transaction_defaut);

		System.out.println("Deconnexion Loggia...");
		// Deconnexion
		Jxweblog.NxweblogAxwebent vWebent = jxweblog.getNxweblogAxwebent();

		System.out.println("vWebent.groupe_exploitant="	+ vWebent.groupe_exploitant
				+ "numero_session="  		            + Integer.toString(vWebent.numero_session.intValue())
				+ "vWebent.utilisateur="                + vWebent.utilisateur
				+ "vWebent.cible_exploitant ="          + vWebent.cible_exploitant
				+ "vWebent.dbid="                       + vWebent.dbid
				+ "vWebent.cd_stegrp="                  + vWebent.cd_stegrp
				+ "vWebent.cd_cible="                   + vWebent.cd_cible);

		// Fermeture Loggia
		String password = jxweblog.getNxweblogAxweblog().password;
		closeConnexion(
			vWebent.groupe_exploitant,
			Integer.toString(vWebent.numero_session.intValue()),
			vWebent.utilisateur,
			password,
			vWebent.cible_exploitant,
			vWebent.cd_stegrp,
			vWebent.cd_cible,
			Integer.toString(vWebent.dbid.intValue()),
			"ABOEX17");
		System.out.println("Deconnexion Loggia OK");
		
		// Fermeture Broker
		System.out.println("Deconnexion Broker");
		closeBroker (broker);
	}
} // END