/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.ellectra.gui.managebeans;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.faces.component.UIData;
import javax.faces.context.FacesContext;
import javax.xml.ws.WebServiceRef;
import org.ellectra.gui.utils.JSFUtils;
import org.ellectra.wsmu.ManageUserService;
import org.ellectra.wsmu.UserAccountType;

/**
 *
 * @author tdi
 */
public class usermanager {

    @WebServiceRef(wsdlLocation = "http://localhost:18181/ManageUserService/ManageUserPort?wsdl")
    private ManageUserService service;
    
    private UIData userdata = null;
    private ArrayList<userlist> auser = new ArrayList();
    
    List<org.ellectra.wsmu.UserAccountType> dGlobalList;
    private String szsearchname;
    private String searchcc = "0";
    private String sort = "0";
    private int iuserselected;
    UserAccountType dCurrentUser;


    public ArrayList<userlist> getAuser() {
        PopulateList();
        return auser;
    }

    public void setAuser(ArrayList<userlist> auser) {
        this.auser = auser;
    }

    public void dosearch() {
        PopulateList();
    }

    private void PopulateList() { 
        auser.clear();
        try {
            org.ellectra.wsmu.ManageUserPortType port = service.getManageUserPort();

            org.ellectra.wsmu.ManageUserRequest part1 = new org.ellectra.wsmu.ManageUserRequest();
            part1.setAction("FR2_UserAcc_ViewAll");
            part1.setActorSignature("123456");
            org.ellectra.wsmu.ManageUserResponse result = port.manageUserOperation(part1);
            dGlobalList = result.getUserAccount();

            for (int i = 0; i < dGlobalList.size(); i++) {
                org.ellectra.wsmu.UserAccountType dItem = dGlobalList.get(i);
                
                userlist pAdd = new userlist();
                if (searchcc.equalsIgnoreCase("0")) {
                    pAdd.setId(dItem.getUserId());
                    pAdd.setName(dItem.getFirstName());
                    pAdd.setSurname(dItem.getLastName());
                    pAdd.setUsername(dItem.getUserName());
                    String MyRoles="";
                  List<String> Roles = dItem.getUserRole().getRole();
                  for (int iroles = 0; iroles < Roles.size(); iroles++) {
                  String dget = Roles.get(iroles);
                  if (dget.equalsIgnoreCase("ADMIN")) {
                      MyRoles=MyRoles+" Admin";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_ADMINISTRATIVE")) {
                      MyRoles=MyRoles+" PO Admin";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_OPENING")) {
                     MyRoles=MyRoles+" PO Opening";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_EVALUATING")) {
                      MyRoles=MyRoles+" PO Evaluating";
                  }
                  if (dget.equalsIgnoreCase("ECONOMIC_OPERATOR")) {
                      MyRoles=MyRoles+" EO";
                  }
                  }
                                                      pAdd.setSzuserrole(MyRoles);
                    if (dItem.isApproved()) {
                        pAdd.setStatus("Approved");
                    } else {
                        pAdd.setStatus("New");
                    }
                    pAdd.setOrganization(GetOrganizationname(dItem.getOrgId()));
                    pAdd.setOrgName(dItem.getOrgName());
                    auser.add(pAdd);
                } else {
                    if (dItem.getUserName().equalsIgnoreCase(szsearchname)) {
                        pAdd.setId(dItem.getUserId());
                        pAdd.setName(dItem.getFirstName());
                        pAdd.setSurname(dItem.getLastName());
                        //if(dItem.get)
                        pAdd.setUsername(dItem.getUserName());
                        String MyRoles="";
                  List<String> Roles = dItem.getUserRole().getRole();
                  for (int iroles = 0; iroles < Roles.size(); iroles++) {
                  String dget = Roles.get(iroles);
                  if (dget.equalsIgnoreCase("ADMIN")) {
                      MyRoles=MyRoles+" Admin";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_ADMINISTRATIVE")) {
                      MyRoles=MyRoles+" PO Admin";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_OPENING")) {
                     MyRoles=MyRoles+" PO Opening";
                  }
                  if (dget.equalsIgnoreCase("PROCUREMENT_OFFICER_EVALUATING")) {
                      MyRoles=MyRoles+" PO Evaluating";
                  }
                  if (dget.equalsIgnoreCase("ECONOMIC_OPERATOR")) {
                      MyRoles=MyRoles+" EO";
                  }
                  }
                                                      pAdd.setSzuserrole(MyRoles);
                        if (dItem.isApproved()) {
                            pAdd.setStatus("Approved");
                        } else {
                            pAdd.setStatus("New");
                        }
                        pAdd.setOrganization(GetOrganizationname(dItem.getOrgId()));
                        pAdd.setOrgName(dItem.getOrgName());
                        auser.add(pAdd);
                    }
                }
            }
            try {

                if (getSort().equalsIgnoreCase("0")) {
                    Collections.sort(auser, CompareStatus);
                }
                if (getSort().equalsIgnoreCase("1")) {
                    Collections.sort(auser, CompareUsername);
                }
                if (getSort().equalsIgnoreCase("2")) {
                    Collections.sort(auser, CompareOrganization);
                }
                if (getSort().equalsIgnoreCase("3")) {
                    Collections.sort(auser, CompareDate);
                }
            } catch (Exception ex) {
            }
        } catch (Exception ex) {

            userlist pAdd = new userlist();
            pAdd.setId(1);
            pAdd.setName("error");
            pAdd.setSurname("error");
            //if(dItem.get)
            pAdd.setStatus("Error");
            pAdd.setOrganization("error" + ex.getLocalizedMessage());
            auser.add(pAdd);
        }

    }

    private String GetUserSignatureFromSession() {
        String dtemp = (String) JSFUtils.retrieveOnSession(FacesContext.getCurrentInstance(), "LOGINSIGNATURE");
        return dtemp;
    }

    /**
     * to view data for a user
     */
    public void LoadUser() {
        try { 
            org.ellectra.wsmu.ManageUserPortType port = service.getManageUserPort();

            org.ellectra.wsmu.ManageUserRequest part1 = new org.ellectra.wsmu.ManageUserRequest();
            part1.setAction("FR2_UserAcc_Read");
            part1.setActorSignature(GetUserSignatureFromSession());
            part1.setUserId(iuserselected);

            org.ellectra.wsmu.ManageUserResponse result = port.manageUserOperation(part1);
            List<UserAccountType> dAcc = result.getUserAccount();
            dCurrentUser = dAcc.get(0);
        } catch (Exception ex) {
            // TODO handle custom exceptions here
            System.err.print("Error on load");
        }

    }

    public void AcceptSelectedUser() {
        try { 
            org.ellectra.wsmu.ManageUserPortType port = service.getManageUserPort();

            org.ellectra.wsmu.ManageUserRequest part1 = new org.ellectra.wsmu.ManageUserRequest();
            part1.setAction("FR2_UserAcc_Update");
            part1.setActorSignature(GetUserSignatureFromSession());
            part1.setUserId(iuserselected);
            part1.setAddressLine1(dCurrentUser.getAddressLine1());
            part1.setAddressLine2("  ");
            part1.setApproved(true);
            part1.setBuyerURL(dCurrentUser.getBuyerURL());
            part1.setCityName(dCurrentUser.getCityName());
            part1.setCountry(dCurrentUser.getCountry());
            part1.setCreated(dCurrentUser.getCreated());
            part1.setDepartmentName(dCurrentUser.getDepartmentName());
            part1.setDirectLine(dCurrentUser.getDirectLine());
            part1.setEmail(dCurrentUser.getEmail());
            part1.setFax(dCurrentUser.getFax());
            part1.setFirstName(dCurrentUser.getFirstName());
            part1.setGender(dCurrentUser.getGender());
            part1.setJobTitle(dCurrentUser.getJobTitle());
            part1.setLanguagePreference(dCurrentUser.getLanguagePreference());
            part1.setLastLogin(dCurrentUser.getLastLogin());
            part1.setLastName(dCurrentUser.getLastName());
            part1.setMobilePhone(dCurrentUser.getMobilePhone());
            part1.setOJSeSenderUserName(dCurrentUser.getOJSeSenderUserName());
            part1.setOrgId(dCurrentUser.getOrgId());
            part1.setPassword(dCurrentUser.getPassword());
            part1.setPostBox(dCurrentUser.getPostBox());
            part1.setPostCode(dCurrentUser.getPostCode());
            //part1.setSignature(dCurrentUser.);
            part1.setTitle(dCurrentUser.getTitle());
            part1.setUserName(dCurrentUser.getUserName());
            part1.setOrgId(iuserselected);
            //part1(dCurrentUser);
            

            org.ellectra.wsmu.ManageUserResponse result = port.manageUserOperation(part1);

        } catch (Exception ex) {
            // TODO handle custom exceptions here
            System.err.print("Error on accept");
        }
    }

    public void DeleteSelectedUser() {
        try { 
            org.ellectra.wsmu.ManageUserPortType port = service.getManageUserPort();

            org.ellectra.wsmu.ManageUserRequest part1 = new org.ellectra.wsmu.ManageUserRequest();
            part1.setAction("FR2_UserAcc_Delete");
            part1.setActorSignature(GetUserSignatureFromSession());
            part1.setUserId(iuserselected);

            org.ellectra.wsmu.ManageUserResponse result = port.manageUserOperation(part1);

        } catch (Exception ex) {
            // TODO handle custom exceptions here
            System.err.print("Error on delete");
        }
    }
    

    public String view() {
        userlist Selected = (userlist) this.getUserdata().getRowData();
        iuserselected = Selected.getId();
        //JSFUtils.storeOnSession(FacesContext.getCurrentInstance(), "SEARCHUVIEW", Integer.toString(iuserselected));      
        return "showview";
    }

    public String accept() {
        userlist Selected = (userlist) this.getUserdata().getRowData();
        iuserselected = Selected.getId();
        LoadUser();
        AcceptSelectedUser();
        return "useraccept";
    }

    public String delete() {
        userlist Selected = (userlist) this.getUserdata().getRowData();
        iuserselected = Selected.getId();
        DeleteSelectedUser();
        return "userdelete";
    }
    
    private static final Comparator CompareStatus = new Comparator() {
        public int compare(Object o1, Object o2) {
            String s1 = ((userlist) o1).getStatus();
            String s2 = ((userlist) o2).getStatus();
            return s1.compareTo(s2);
        }
    };
    
    private static final  Comparator CompareUsername = new Comparator() {
        public  int compare(Object o1, Object o2) {
            String s1 = ((userlist) o1).getUsername();
            String s2 = ((userlist) o2).getUsername();
            return s1.compareTo(s2);
        }
    };
    
    private static final  Comparator CompareOrganization = new Comparator() {
        public  int compare(Object o1, Object o2) {
            String s1 = ((userlist) o1).getOrganization();
            String s2 = ((userlist) o2).getOrganization();
            return s1.compareTo(s2);
        }
    };

    private static final  Comparator CompareDate = new Comparator() {
        public int compare(Object o1, Object o2) {
             Date s1=((userlist)o1).getLastcreated();
        Date s2=((userlist)o2).getLastcreated();
        return s1.compareTo(s2);
        }
    };

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }
    
    private String GetOrganizationname(int ID) {
        return Integer.toString(ID);
    }

    public int getIuserselected() {
        return iuserselected;
    }

    public void setIuserselected(int iuserselected) {
        this.iuserselected = iuserselected;
    }
    
    public String getSzsearchname() {
        return szsearchname;
    }

    public void setSzsearchname(String szsearchname) {
        this.szsearchname = szsearchname;
    }

    public String getSearchcc() {
        return searchcc;
    }

    public void setSearchcc(String searchcc) {
        this.searchcc = searchcc;
    }
    
    public UIData getUserdata() {
        return userdata;
    }

    public void setUserdata(UIData userdata) {
        this.userdata = userdata;
    }

}
