JBoss Community

RichFaces ExtendedDataTable not setting the Selection attribute

created by david hickman in JBoss Web Services - View the full discussion

Hi

I am using Richfaces  richfaces-ui-3.3.3.Final with jojarra-2.0.3 Core JSF and Jave 1.6 running in tomcat 6.0.29.

 

I am populating the extendedDataTable using the data model shown below but when I select an entry, the backing bean method is being called but the selection variable is being set but with no keys, any help would be appreciated:

 

 

// snippet from jsp

             <r:simpleTogglePanel>   
               
                    <f:facet name="header">
                        <h:outputText value="User Search Results"/>
                    </f:facet>
                   
                    <a:outputPanel id="userSearchResultsPanel" >

 

                        <r:extendedDataTable id="userTable"
                                     height="200px"
                                       value="#{userBean.dataModel}"
                                       var="user"
                                       selectionMode="single"
                                       selection="#{userBean.searchSelection}">
                                  <r:column sortOrder="ASCENDING"  >
                                      <f:facet name="header">
                                          <h:outputText value="User Name" />
                                      </f:facet>
                                      <h:outputText value="#{user.userName}" />
                                  </r:column>
                                  <r:column sortOrder="ASCENDING"   >
                                      <f:facet name="header">
                                          <h:outputText value="First Name" />
                                      </f:facet>
                                      <h:outputText value="#{user.firstName}" />
                                  </r:column>
                                  <r:column sortOrder="ASCENDING"  >
                                      <f:facet name="header">
                                          <h:outputText value="Surname" />
                                      </f:facet>
                                      <h:outputText value="#{user.surName}" />
                                  </r:column>
                                  <r:column sortOrder="ASCENDING">
                                      <f:facet name="header">
                                          <h:outputText value="Cost Center" />
                                      </f:facet>
                                      <h:outputText value="#{user.costCenter}" />
                                  </r:column>
                                 
                                  <a:support event="onselectionchange"
                                             reRender="userDetailPanel"
                                             action="#{userBean.identifySelectedUser}" />
                          </r:extendedDataTable>

 

                          <r:spacer height="10px" />                     
                         
                    </a:outputPanel>
                </r:simpleTogglePanel>

 

// Data Provider

package util;

 

import java.util.*;
import vo.User;

 

import org.apache.log4j.Logger;
import org.richfaces.model.DataProvider;

 

public class UserTableDataProvider implements DataProvider{
   
    private Logger log = Logger.getLogger( "UserTableDataProvider: " );
   
    private Map<UUID, UIUser>        dataTable;
    private List<UIUser>            dataList;
   
    public UserTableDataProvider( List<User> users ) {
       
        dataTable = new HashMap<UUID,UIUser>();
        dataList  = new ArrayList<UIUser>( users.size());
        for ( User user : users ) {
            UIUser uiUser = new UIUser( user );
            dataTable.put( uiUser.getId(), uiUser);
            dataList.add( uiUser );
        }

 

    }
    /**
     *
     */
    public Object getItemByKey( Object key ) {
        return dataTable.get(( String)key );
    }
    /**
     *
     */
    public List getItemsByRange( int start, int end ) {
        return dataList.subList( start, end );
    }
    /**
     *
     */
    public Object getKey( Object user ) {
        return ((UIUser)user).getId();
    }
    /**
     *
     */
    public int getRowCount() {
        return dataList.size();
    }
}

 

// Backing Bean

package beans;

 

import org.apache.log4j.*;
import org.richfaces.model.selection.SimpleSelection;
import org.richfaces.model.ExtendedTableDataModel;
import util.UserTableDataProvider;

 

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

 


import exception.UserException;
import exception.BaseRuntimeException;
import vo.User;
import service.UserService;

 

public class UserBean extends BaseBean {

 

    private Logger log = Logger.getLogger( "UserBean: " );
   
    //////////////////////////////////////////////////////////
    // Injected properties
    //////////////////////////////////////////////////////////
    private UserService userService;
   
    //////////////////////////////////////////////////////////
    // Private static data
    //////////////////////////////////////////////////////////
    private final static String COST_CENTER     = "CC";
    private final static String SURNAME         = "SN";
    private final static String USERNAME        = "UN";
   
    //////////////////////////////////////////////////////////
    // Private member variables
    //////////////////////////////////////////////////////////
    // basic attributes
    private String                  userName;
    private String                  password;
    private String                  userType;   
    private String                    email;
    private String                    firstName;
    private String                    surname;
    private String                  costCenter;
    private HashMap                 userTypes;
    private String                    active;
    private List<User>               users;

 

    // search attributes
    private String                    searchType;
    private String                    searchCriteria;

 

   
    private SimpleSelection        searchSelection;
    private ExtendedTableDataModel dataModel;
   
   
    // default bean constructor
    public UserBean() {
        users = new ArrayList<User>();
    }
   
   
    //////////////////////////////////////////////
    // Bean property accessor methods
    //////////////////////////////////////////////
    public String getUserName() {
        return userName;
    }
   
    public void setUserName(String userName) {
        this.userName = userName;
    }
   
    public String getPassword() {
        return password;
    }
   
    public void setPassword(String password) {
        this.password = password;
    }
    public String getUserType() {
        return userType;
    }
    public void setUserType(String userType) {
        this.userType = userType;
    }

 

    public String getEmail() {
        return email;
    }

 

    public void setEmail(String email) {
        this.email = email;
    }

 


    public String getFirstName() {
        return firstName;
    }

 

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

 

    public String getSurname() {
        return surname;
    }

 

    public void setSurname(String surname) {
        this.surname = surname;
    }

 

    public String getCostCenter() {
        return costCenter;
    }

 

    public void setCostCenter(String costCenter) {
        this.costCenter = costCenter;
    }

 

    public HashMap getUserTypes() {
        return userTypes;
    }

 


    public void setUserTypes(HashMap userTypes) {
        this.userTypes = userTypes;
    }

 


    public String getActive() {
        return active;
    }

 


    public void setActive(String active) {
        this.active = active;
    }

 


    public String getSearchType() {
        return searchType;
    }

 


    public void setSearchType(String searchType) {
        this.searchType = searchType;
    }

 


    public String getSearchCriteria() {
        return searchCriteria;
    }

 


    public void setSearchCriteria(String searchCriteria) {
        this.searchCriteria = searchCriteria;
    }

 

    public SimpleSelection getSearchSelection() {
        return searchSelection;
    }

 


    public void setSearchSelection(SimpleSelection searchSelection) {
        log.info("UserBean.setSearchSelection");
        this.searchSelection = searchSelection;
    }

 

    public ExtendedTableDataModel getDataModel() {

 

        dataModel = new ExtendedTableDataModel( new UserTableDataProvider( users ));
       
        return dataModel;
    }

 


    public void setDataModel(ExtendedTableDataModel dataModel) {
        this.dataModel = dataModel;
    }

 


    //////////////////////////////////////////////////////////
    // User Service methods
    //////////////////////////////////////////////////////////
    public void userSearch() {

 

       
        String status = validateSearchCriteria();
       
        users = new ArrayList();
        try {
            if ( searchType.equals(COST_CENTER )) {
               
                users = userService.getUsersByCostCenter( searchCriteria);
            }
            else if ( searchType.equals(SURNAME )) {
               
                users = userService.getUsersBySurname( searchCriteria );
            }
            else if ( searchType.equals(USERNAME)) {
               
                users.add( userService.getUser( searchCriteria ));
            }
        }
        catch ( UserException ue ) {
            if ( searchType.equals(COST_CENTER )) {
                addErrorMessage("userSearch", "Failed to retrieve users for Cost Center " + searchCriteria );
            }
            else if ( searchType.equals(SURNAME )) {
                addErrorMessage("userSearch", "Failed to retrieve users for Surname " + searchCriteria );
            }
            else if ( searchType.equals(USERNAME)) {
                addErrorMessage("userSearch", "Failed to retrieve users for Username " + searchCriteria );
            }           
           
        }
    }
   
    // identify selected user
    public void identifySelectedUser() {
       
        if ( searchSelection != null ) {
            log.info("Selection: " + searchSelection.size() );
            Iterator iterator = searchSelection.getKeys();
            while ( iterator.hasNext() ) {
                Object key = iterator.next();
            }
        }
    }
   
    // User Detail
    public void userDetail() {
       
    }
    //////////////////////////////////////////////////////////
    // IOC methods
    //////////////////////////////////////////////////////////
    public void setUserService( UserService userService ) {
        this.userService = userService;
    }
    //////////////////////////////////////////////////////////
    // Private methods
    //////////////////////////////////////////////////////////
    // searchCriteria
    private String validateSearchCriteria() {
        String status = "ok";
       
        if ( !validString(searchType) ) {
            addErrorMessage("userSearch","Search Type is required");
            status = "error";
        }
        if ( !validString(searchCriteria) ) {
            addErrorMessage("userSearch","Search Criteria is required");
            status = "error";
        }
       
        return status;
    }
    // Bean data
    private String validateData( ) {
       
        String status = "ok";

 

        if ( userName == null || userName.equals("")) {
            addErrorMessage("userSearch", "User Name is required");
            status = "error";
        }
        if ( !validString(password) ) {
            addErrorMessage("userSearch", "Password is required");
            status = "error";
        }
        if ( !validString(firstName) ) {
            addErrorMessage("userSearch", "First Name is required");
            status = "error";
        }       
        if ( !validString(surname) ) {
            addErrorMessage("userSearch", "Surname is required");
            status = "error";
        }       
        if ( !validString(email)) {
            addErrorMessage("userSearch", "Email is required");
            status = "error";
        }       
        if ( !validString(costCenter )) {
            addErrorMessage("userSearch", "Cost Center is required");
            status = "error";
        }       
        return status;   
    }

 

    // validString
    private boolean validString( String str ) {
        if ( str == null || str.equals("" ))
            return false;
        return true;
    }

 

}

 

// UI User class

package util;

 

import vo.User;
import java.util.UUID;

 

/**
*
* This class encapsulates a standard USER object but
* also includes a UUID for use by UI objects ed.ExtendedDataTable
*
* @author davidhickman
*
*/
public class UIUser extends User {

 

    /////////////////////////////////////////////////////
    // Private data
    /////////////////////////////////////////////////////
    private UUID    id;

 

   
    public  UIUser( User user ) {
       
        this.setUserName(user.getUserName());
        this.setPassword(user.getPassword());
        this.setUserType(user.getUserType());
        this.setEmail(user.getEmail());
        this.setFirstName(user.getFirstName());
        this.setSurName(user.getSurName());
        this.setCostCenter(user.getCostCenter());
        this.setActive(user.getActive());
        this.id = UUID.randomUUID();
       
    }

 

    public UUID getId() {
        return id;
    }

 

    public void setId(UUID id) {
        this.id = id;
    }

 

   
}

Reply to this message by going to Community

Start a new discussion in JBoss Web Services at Community