
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 : Jwwwlst1Php</p>
 * <p>Description : Classe de récupération des Listes Généralisées depuis NWWWLST1</p>
 * <p>Copyright : Copyright (c) 2013</p>
 * <p>Societe : ENSOR</p>
 * @author AR & DR <i>25/10/2011</i>
 * <p>Modifiée le 30/01/2013 par AR : Ajout des paramètres CD-ACTION et FILTRE</p>
 * <p>Pour compatibilité ascendante : si CD-ACTION est vide, alors passer "+V". Si FILTRE est vide, alors passer "null" </p>
 * @version 1.1
 */
public class Jwwwlst1Web {	
   
		/**
		 * <p>Titre : getListe</p>
		 * <p>Description : Récupération des Listes Généralisées NWWWLST1</p>
		 * <p>Copyright : Copyright (c) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</i>
		 * @version 1.0
		 */
		private static int NB_CLE_REPRISE = 250 ;
		
		public ReponseList getListe(
			String transaction,
			String fonction,
			Jxweblog jxweblog) throws BrokerException {
			Broker broker = JxweblogWeb.logonBroker();
			// Appel à getListe avec Broker
			Jxweblog.NxweblogAxwebent connexion = jxweblog.getNxweblogAxwebent();
			ReponseList reponses = getListe(transaction, fonction, connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null, null, null, broker);
		    // BROKER LOGOFF
		    JxweblogWeb.closeBroker (broker);
			return reponses;
		}
		/**
		 * <p>Titre : getListe</p>
		 * <p>Description : Récupération des listes génériques (types et titres...)</p>
		 * <p>Copyright : Copyright (c) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</i>
		 * @version 1.0
		 */
		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible,
			String cd_stegrp,
			String cle_prefixe,
			String cle_reprise) throws BrokerException {
		Broker broker = JxweblogWeb.logonBroker();
		// Appel à getListe avec Broker
		ReponseList reponses = getListe(transaction, fonction, groupe_exploitant, numero_session, utilisateur, cd_cible, cd_stegrp, cle_prefixe, cle_reprise, null, null, broker);
		// BROKER LOGOFF
		JxweblogWeb.closeBroker (broker);
		return reponses;
		}

		//
		// Classes getListe pour compatibilité ascendate
		//
		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible,
			String cd_stegrp,
			String cle_prefixe,
			String cle_reprise,
			Broker broker) throws BrokerException {
		// Appel à getListe avec Broker
		ReponseList reponses = getListe(transaction, fonction, groupe_exploitant, numero_session, utilisateur, cd_cible, cd_stegrp, cle_prefixe, cle_reprise, null, null, broker);
		// BROKER LOGOFF
		return reponses;
		}	

		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible,
			String cd_stegrp,
			String cle_prefixe,
			String cle_reprise,
			String cd_action,
			String filtre) throws BrokerException {
		Broker broker = JxweblogWeb.logonBroker();
		// Appel à getListe avec Broker
		ReponseList reponses = getListe(transaction, fonction, groupe_exploitant, numero_session, utilisateur, cd_cible, cd_stegrp, cle_prefixe, cle_reprise, cd_action, filtre, broker);
		// BROKER LOGOFF
		JxweblogWeb.closeBroker (broker);
		return reponses;
		}
		
		/**
		 * <p>Titre : getListe</p>
		 * <p>Description : Récupération des listes génériques (types et titres...)</p>
		 * <p>Copyright : Copyright (c) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</i>
		 * <p>Modifiée le 30/01/2013 par AR : Ajout des paramètres CD-ACTION et FILTRE</p>
		 * @version 1.1
		 */
		public ReponseList getListe(
			String transaction,
			String fonction,
			String groupe_exploitant,
			String numero_session,
			String utilisateur,
			String cd_cible,
			String cd_stegrp,
			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, Constantes.SERVER_ADRESSE);
			// Initialisation de la vWebent depuis les informations de connexion
			Jwwwlst1.Nwwwlst1Axwebent vWebent = jwwwlst1.new Nwwwlst1Axwebent();
			vWebent.dbid = new BigDecimal (Constantes.DBID);
			vWebent.cible_exploitant = Constantes.CD_CIBLE;
			vWebent.groupe_exploitant = groupe_exploitant;
			vWebent.numero_session = new BigDecimal(numero_session);
			vWebent.utilisateur =  utilisateur;
			vWebent.transaction =  transaction;
			vWebent.fonction = fonction;
			vWebent.cd_cible =  cd_cible;
			vWebent.cd_stegrp = cd_stegrp;
			vWebent.cd_application = Constantes.CD_APPLICATION;
			
			jwwwlst1.setNwwwlst1Axwebent (vWebent);
			
			// Ajout 30/01/2013
			// Remarque : La compatibilité ascendante est gérée 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 être passé dans le 1er poste de la Liste des Réponses
				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()) ;

			//Récupération du résultat 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 résultats
				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) ;
						}
						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();
								}
							}
						}
				}
				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, cd_stegrp, cle_prefixe, cleReprise, 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

	   /**
		 * <p>Titre : Reponses</p>
		 * <p>Description : Classe d'objet reponses</p>
		 * <p>Copyright : Copyright (c) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</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 récupérer la liste</p>
		 * <p>Copyright : Copyright (c) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</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) 2011</p>
		 * <p>Societe : ENSOR</p>
		 * @author AR & DR <i>25/10/2011</i>
		 * @version 1.0
		 */
	   	 public static void main(String[] args) throws BrokerException {
			//FTPETAT
			/*Constantes.loadProperties("FTET") ;
			JxweblogWeb.setUtilisateur ("MADAA02");
			JxweblogWeb.setPassword ("MADAA02");
			Jxweblog jxweblog = JxweblogWeb.getConnexion();
			Jwwwlst1Web jwwwlst1Web = new Jwwwlst1Web() ;
			jwwwlst1Web.getListe("WFTLST", "PDFFORMS", jxweblog) ;
			jwwwlst1Web.getListe("WFTLST", "PDFRULES", jxweblog) ;*/
			
			//Logistique
			Constantes.loadProperties("LW") ;
			JxweblogWeb.setUtilisateur ("MADAA01");
			JxweblogWeb.setPassword ("MADAA01");
			Jxweblog jxweblog = JxweblogWeb.getConnexion();
			Jwwwlst1Web jwwwlst1Web = new Jwwwlst1Web() ;
			
			Broker broker = JxweblogWeb.logonBroker();
			// Appel à getListe avec Broker
			Jxweblog.NxweblogAxwebent connexion = jxweblog.getNxweblogAxwebent();

			ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "LGPRTNET", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "=", "GLIWB01");
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "CDPRESTA", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null, "-LF", "04");
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "CDPRESTA", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "TOURNEE", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null, "+VF", "ZO");
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "CDPRESTA", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "          90", null, "-V", null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "ZONEPORT", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "0002", null) ;
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "CDPRESTA", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "          90", null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "ZONEPORT", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "O0001", null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "ZONEPORT", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "O000190", null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "LOCAZP", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "INSEEZP", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, null, null);
			//ReponseList reponses = jwwwlst1Web.getListe("WLWLST", "TITRELW", connexion.groupe_exploitant, Integer.toString(connexion.numero_session.intValue()), connexion.utilisateur, connexion.cd_cible, connexion.cd_stegrp, "0002N", null);
			
			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()) ;
			}
			//jwwwlst1Web.getListe("WLWLST", "DECALIVR", jxweblog) ;
	   }
} // END Jwwwlst1Php
