/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ellectra.gui.controllers;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;

import javax.faces.component.UIInput;
import javax.faces.component.html.HtmlSelectOneListbox;
import javax.faces.context.FacesContext;
import javax.faces.event.AbortProcessingException;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;
import javax.faces.model.SelectItem;
import javax.xml.ws.WebServiceRef;
import org.ellectra.wsws.ManageOrgService;
/**
 *
 * @author Tom
 */
public class organizationcontroller implements java.io.Serializable{
    @WebServiceRef(wsdlLocation = "http://localhost:18181/ManageOrgService/ManageOrgPort?wsdl")
    private ManageOrgService service;

    private List organizations = Collections.EMPTY_LIST;
    private UIInput organizationslist;
    List <org.ellectra.wsws.OrgType> dInitalOrgList;
    private String orgvalue;
    
    public organizationcontroller()
    {
        super();
        LoadOrganization();
    }
    
    private void LoadOrganization()
    {
        
        try { // Call Web Service Operation
            org.ellectra.wsws.ManageOrgPortType port = service.getManageOrgPort();
            
            //  initialize WS operation arguments here
            org.ellectra.wsws.ManageOrgRequest part1 = new org.ellectra.wsws.ManageOrgRequest();
            part1.setAction("FR2_Org_ViewAll");
            part1.setActorSignature("123456");
            
            //call operation
            org.ellectra.wsws.ManageOrgResponse result = port.manageOrgOperation(part1);
            dInitalOrgList=result.getOrganisation();
        } catch (Exception ex) {
            // TODO handle custom exceptions here
        }
  
    }
    public List getOrganizations() {
        List orgs = new ArrayList();
        LoadOrganization();
        int i;
            //SelectItem item = new SelectItem("2","ff");
            //cats.add(item);
            //SelectItem item2 = new SelectItem("1","dd2");
            
            //cats.add(item2);
        for(i=0;i<dInitalOrgList.size();i++)
        {
            org.ellectra.wsws.OrgType dele=dInitalOrgList.get(i);
            String orgid = Integer.toString(dele.getId());
            SelectItem item = new SelectItem(orgid,dele.getOrganisationName());
           //SelectItem item = new SelectItem("1","dd2");
            orgs.add(item);
        }    
        return orgs;
    }

    public void setOrganizations(List organizations) {
        this.organizations = organizations;
    }

    public UIInput getOrganizationslist() {
        return organizationslist;
    }

    public void setOrganizationslist(UIInput organizationslist) {
        this.organizationslist = organizationslist;
    }

    public String getOrgvalue() {
        return orgvalue;
    }

    public void setOrgvalue(String orgvalue) {
        this.orgvalue = orgvalue;
    }
    
            	public void orgSelected(ValueChangeEvent event) 
		throws AbortProcessingException {
		
		//Get the selected region from the event
		String org = (String)event.getNewValue();
		setOrgvalue(org);
		
		
		//Force to render response phase so that any error messages DO NOT get
		//displayed. i.e. Skip update model phase. Use immediate="true" on the
		//component that calls this valueChangeListener so that it does not 
		//run any validation.
		FacesContext.getCurrentInstance().renderResponse();
		
	}

}
