
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;
import java.util.List ;
import java.util.ArrayList ;

/**
 * <p>Titre : Jwwwlst1Bw</p>
 * <p>Description : Classe de recuperation des Listes Generalisees depuis NWWWLST1</p>
 * <p>Copyright : Copyright (c) 2013</p>
 * <p>Societe : ENSOR</p>
 * @author AR & DR <i>11/10/2013</i>
 * @version 1.1
 * Corrigée le 04/11/2013 : Suite PB clef de reprise sur TITREBW
 * La clef de reprise est désormais celle passée par l'objet Demande et non pas la dernière valeur de la liste
 */
public class Jwwwlst1Bw {	
   
		private static int NB_CLE_REPRISE = 250 ;

		/**
		 * <p>Titre : getListe</p>
		 * <p>Description : Recuperation des listes generiques NWWWLST1</p>
		 * <p>Copyright : Copyright (c) 2013</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR <i>11/10/2013</i>
		 * @version 1.1
		 */
		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible_exploitant,
			String cd_stegrp,
			String cd_cible,
			String dbid,
			String rpc_name,
			String cle_prefixe,
			String cle_reprise,
			String cd_action,
			String filtre,
			Broker broker) throws BrokerException {
			// Initialisation des variables en entrees
	     	Jwwwlst1 jwwwlst1                 = new Jwwwlst1(broker, Utils.formatSrvAddr(rpc_name));
			// Initialisation de la vWebent depuis les informations de connexion
			Jwwwlst1.Nwwwlst1Axwebent vWebent = jwwwlst1.new Nwwwlst1Axwebent();
			
			vWebent.utilisateur               = utilisateur;
			vWebent.cible_exploitant          = cd_cible_exploitant;
			vWebent.groupe_exploitant         = groupe_exploitant;
			vWebent.numero_session            = new BigDecimal(numero_session);
			vWebent.transaction               = transaction;
			vWebent.fonction                  = fonction;
			vWebent.cd_stegrp                 = cd_stegrp;
			vWebent.cd_application            = Constantes.CD_APPLICATION;
			
			vWebent.dbid                      = new BigDecimal (dbid);
			vWebent.cd_cible                  = cd_cible;
			
			jwwwlst1.setNwwwlst1Axwebent (vWebent);
			
			// Ajout 30/01/2013
			// Remarque : La compatibilite ascendante est geree dans Natural NWWWLST1
			if(cle_prefixe!=null || cle_reprise!=null || cd_action!=null || filtre!=null){
				Jwwwlst1.Nwwwlst1Awwwlst1 vAwwwl1 = jwwwlst1.new Nwwwlst1Awwwlst1();
				Jwwwlst1.Nwwwlst1Awwwlst1Demande vAwwwl1Demande = jwwwlst1.new Nwwwlst1Awwwlst1Demande();
				vAwwwl1Demande.cle_prefixe = cle_prefixe;
				vAwwwl1Demande.cle_reprise = cle_reprise;
				vAwwwl1Demande.cd_action   = cd_action;

					// Le filtre doit etre passe dans le 1er poste de la Liste des Reponses
				if (filtre!=null){
					Jwwwlst1.Nwwwlst1Awwwlst1Reponse vAwwwl1Reponse = jwwwlst1.new Nwwwlst1Awwwlst1Reponse();
					vAwwwl1Reponse.liste_cle        = new String[250];
					vAwwwl1Reponse.liste_libelle    = new String[250];
					vAwwwl1Reponse.liste_cle[0]     = filtre;
					vAwwwl1Reponse.liste_libelle[0] = filtre;
					vAwwwl1.reponse = vAwwwl1Reponse;
				}

				vAwwwl1.demande = vAwwwl1Demande ;
				jwwwlst1.setNwwwlst1Awwwlst1(vAwwwl1) ;
			}
			
			
		    // CALL SERVER
		    jwwwlst1.nwwwlst1();
			// FIN APPEL
			
			vWebent = jwwwlst1.getNwwwlst1Axwebent() ;
	        // Verification du code retour
			System.out.println("code_retour=="+vWebent.code_retour.intValue()) ;

			//Recuperation du resultat si code retour OK
			ReponseList reponses = new ReponseList() ;
			reponses.code_retour = vWebent.code_retour.intValue() ;
			ArrayList<Reponses> lstReponses = new ArrayList<Reponses>() ;
			ArrayList<Jwwwlst1.Nwwwlst1AxwebentT_ano> lstAno = new ArrayList<Jwwwlst1.Nwwwlst1AxwebentT_ano>() ;
						
			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() + " ");
				   lstAno.add(vWebent.t_ano[i]) ;
			   	}
				reponses.lstAnos = lstAno ;
			}
			else {
				
				//Affichage de la liste des resultats
				System.out.println ("AFFICHAGE DE LA LISTE DE RESULTATS");
				Jwwwlst1.Nwwwlst1Awwwlst1Reponse vReponse = jwwwlst1.getNwwwlst1Awwwlst1().reponse;
				int len = vReponse.nb_rep_rendues.intValue() ;
				String cleReprise ="";
				for(int i = 0; i < len; i++){
						System.out.println ("cle=" + vReponse.liste_cle[i] + " ; libelle=" + vReponse.liste_libelle[i] + " ; compl.=" + vReponse.liste_complement[i]);
						System.out.println("\n");
						Reponses rep = new Reponses() ;
						rep.cle = vReponse.liste_cle[i].trim();
						rep.libelle = vReponse.liste_libelle[i].trim();
						rep.complement = vReponse.liste_complement[i].trim();
						if(rep.cle!=null && !"".equals(rep.cle)){
							lstReponses.add(rep) ;
						}
						/* Début Correction du 04/11/2013 : Suite PB clef de reprise sur TITREBW
						*   La clef de reprise est désormais celle passée par l'objet Demande et non pas la dernière valeur de la liste */
						/* if(len>=NB_CLE_REPRISE){
							String[] tabCle = rep.cle.split("/") ;
							if(i==len-1){
								if(tabCle!=null && tabCle.length >1){
									for(int ic=0; ic< tabCle.length; ic++){
										cleReprise+= tabCle[ic].trim() ;
									}
								}
								else{
									cleReprise = rep.cle.trim();
								}
							}
						} */
				}
				if(len>=NB_CLE_REPRISE){
					Jwwwlst1.Nwwwlst1Awwwlst1 vAwwwl1 = jwwwlst1.getNwwwlst1Awwwlst1();
					cleReprise = vAwwwl1.demande.cle_reprise.trim();
				}
				// Fin correction du 04/11/2013
				reponses.lstReponses = lstReponses ;
				if(len>1 && !"".equals(cleReprise)){
					System.out.println("debut liste cleReprise="+cleReprise) ;
					ReponseList reponsesIter = getListe(transaction, fonction, groupe_exploitant, numero_session, utilisateur, cd_cible_exploitant, cd_stegrp, cd_cible, dbid, rpc_name, cle_prefixe, cleReprise, cd_action, filtre, broker) ;
					System.out.println("fin liste cleReprise="+cleReprise) ;
					List<Reponses> lstRepIter = reponsesIter.lstReponses ;
					boolean bOk = true;
					if(reponsesIter.code_retour!=0){
						return reponsesIter ;
					}
					else{
						if(lstRepIter.size()<=1){
							return reponses ;
						}
						for(Reponses repi : lstRepIter){
							if(!reponses.lstReponses.contains(repi)){
								reponses.lstReponses.add(repi) ;
							}
						}
					}
				}
			}
			return reponses ;
			
	   } // End getListe

		/*
		 * Variante n°1 après connexions Broker et Loggia OK
		 */
		public ReponseList getListe(
			String transaction,
			String fonction,
			String cle_prefixe,
			String cle_reprise,
			String cd_action,
			String filtre,
			String rpc_name,
			Jxweblog jxweblog,
			Broker broker) throws BrokerException {
			
		// Récupération Objet connexion
		Jxweblog.NxweblogAxwebent vWebent = jxweblog.getNxweblogAxwebent();
		// Appel getListe avec données Loggia et Broker
		ReponseList reponses = getListe(transaction,
			fonction,
			vWebent.groupe_exploitant,
			Integer.toString(vWebent.numero_session.intValue()),
			vWebent.utilisateur,
			vWebent.cible_exploitant,
			vWebent.cd_stegrp,
			vWebent.cd_cible,
			Integer.toString(vWebent.dbid.intValue()),
			rpc_name,
			cle_prefixe,
			cle_reprise,
			cd_action,
			filtre,
			broker);
		return reponses;
		}
		
		/*
		 * Variante n°2 sans le filtre
		 */
		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible_exploitant,
			String cd_stegrp,
			String cd_cible,
			String dbid,
			String rpc_name,
			String cle_prefixe,
			String cle_reprise,
			Broker broker) throws BrokerException {
		ReponseList reponses = getListe(transaction,
			fonction,
			groupe_exploitant,
			numero_session,
			utilisateur,
			cd_cible_exploitant,
			cd_stegrp,
			cd_cible,
			dbid,
			rpc_name,
			cle_prefixe, 
			cle_reprise,
			null,
			null,
			broker);
		// BROKER LOGOFF
		return reponses;
		}	

	   /**
		 * <p>Titre : Reponses</p>
		 * <p>Description : Classe d objet reponses</p>
		 * <p>Copyright : Copyright (c) 2013</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR <i>11/10/2013</i>
		 * @version 1.0
		 */
	  public class Reponses {
		public String cle;
		public String libelle;
		public String complement;
		public boolean equals(Object obj)
        {
            if (obj == null)
            {
                return false;
            }
			Reponses reponse = (Reponses)obj;
            if (reponse.cle!=null && !"".equals(reponse.cle))
            {
                return (reponse.cle.equals(this.cle));
            }
            return false;
        }
		
	  }
	  /**
		 * <p>Titre : ReponseList</p>
		 * <p>Description : Classe permettant de recuperer la liste</p>
		 * <p>Copyright : Copyright (c) 2013</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>11/10/2013</i>
		 * @version 1.0
		 */
		  public class ReponseList {
			  	public List<Reponses> lstReponses;
			  	public int code_retour ;
		  		public List<Jwwwlst1.Nwwwlst1AxwebentT_ano> lstAnos;
		  }
		
		/**
		 * <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 {
			// Récupération Constantes
			Constantes.loadProperties("BW") ;
			JxweblogBw.setUtilisateur ("MADAA01");
			JxweblogBw.setPassword ("MADAAA1");
			
			// Connexion Broker
			Broker broker = JxweblogBw.logonBroker();

			// Connexion Loggia
			String cd_cible = "00";
			String dbid     = "17";
			String rpc_name = "ABOEX17";
			
			//Jxweblog jxweblog = JxweblogBw.getConnexion(cd_cible, dbid, rpc_name, broker);
			Jxweblog jxweblog = JxweblogBw.getConnexion(cd_cible, dbid, rpc_name, broker);
			
			// Appel getListe
			Jxweblog.NxweblogAxwebent vWebent = jxweblog.getNxweblogAxwebent();

			System.out.println("vWebent.groupe_exploitant="	 + vWebent.groupe_exploitant
				+ "\n numero_session="  		             + Integer.toString(vWebent.numero_session.intValue())
				+ "\n vWebent.utilisateur="                  + vWebent.utilisateur
				+ "\n vWebent.cible_exploitant ="            + vWebent.cible_exploitant
				+ "\n vWebent.cd_cible="       + vWebent.cd_cible
				+ "\n vWebent.dbid="           + vWebent.dbid
				+ "\n vWebent.cd_stegrp="      + vWebent.cd_stegrp);

			Jwwwlst1Bw jwwwlst1Bw = new Jwwwlst1Bw();
			// Appel à la Liste
			ReponseList reponses = jwwwlst1Bw.getListe( "WBWLST",
														"TITREBW",
														vWebent.groupe_exploitant,
														Integer.toString(vWebent.numero_session.intValue()),
														vWebent.utilisateur,
														vWebent.cible_exploitant,
														vWebent.cd_stegrp,
														vWebent.cd_cible,
														Integer.toString(vWebent.dbid.intValue()),
														rpc_name,
														cd_cible,
														null,
														null,
														null,
														broker);
			
			System.out.println("AFFICHER REPONSES") ;
			if(reponses.code_retour!=0){
				List<Jwwwlst1.Nwwwlst1AxwebentT_ano> lstAnos = reponses.lstAnos ;
				for(Jwwwlst1.Nwwwlst1AxwebentT_ano ano : lstAnos){
					System.out.println("code="+ano.code_ano+" type="+ano.type_ano+" lib="+ano.lib_ano.trim()) ;
				}
			}
			else{
				List<Reponses> lstReponses = reponses.lstReponses ;
				for(Reponses rep : lstReponses){
					System.out.println("cle="+rep.cle+" lib="+rep.libelle) ;
				}
				System.out.println("lstReponses="+lstReponses.size()) ;
			}

			// Déconnexion Loggia
			String password = jxweblog.getNxweblogAxweblog().password;
			System.out.println("Deconnexion Loggia");
			JxweblogBw.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()),
				rpc_name,
				broker);

			// Fermeture Broker
			System.out.println("Deconnexion Broker");
			JxweblogBw.closeBroker (broker);
	   }
} // END Jwwwlst1Bw