3 Replies Latest reply on Jan 19, 2016 10:16 AM by Michal Petrov

    ExtendedDataModel getRowKey() always returns null

    James Land Newbie

      Currently working on an upgrade from richfaces 3.3 to 4.5. I am trying to migrate a class that was previously a SerializableDataModel and working to an ExtendedDataModel. The it seems to display correctly but when I do an ajax call on the row click event the rowKey is being set to null.

       

      xhtml code:

       

      <rich:dataTable id="trans"  value="#{modelMonitorAction}"
        width="100%" var="md" rowClasses="odd-row,even-row" styleClass="gfits-table"
        rendered="#{modelMonitorAction.rowDetailsCount > 0}" >
        <a4j:ajax event="rowclick" execute="@this"
        listener="#{mdDetailsAction.modelDetails(1, '',modelMonitorAction.selectedModel)}"
        render="mdDetails" >
      

       

      ModelMonitorAction extends PageanatingDataModel.java:

       

      
      public class PaginatingDataModel<Model, Integer> extends
        ExtendedDataModel<Model>{
        /** */
        private static final long serialVersionUID = 2954923950179861809L;
        /** */
        protected Integer currentPk;
        /** */
        protected boolean descending = true;
        /** */
        protected String sortField = getDefaultSortField();
        /** */
        protected boolean detached = false;
        /** */
        protected List<Integer> wrappedKeys = new ArrayList<Integer>();
        /** */
        private int rowCount = 0;
        /** */
        private int rowIndex;
        /** */
        protected Map<Integer, Model> wrappedData = new HashMap<Integer, Model>();
        /**
        * @see org.ajax4jsf.model.ExtendedDataModel#getRowKey()
        */
        @Override
        public Object getRowKey() {
        return currentPk;
        }
        /**
        * @see org.ajax4jsf.model.ExtendedDataModel#setRowKey(java.lang.Object)
        */
        @SuppressWarnings("unchecked")
        @Override
        public void setRowKey(Object key) {
        this.currentPk = (Integer) key;
        }
        /**
        * @see org.ajax4jsf.model.SerializableDataModel#update()
        */
        //Looks like this is doing sorting of some sort will fix once application deploys
      // @Override
      // public void update() {
      // if (getSortFieldObject() != null) {
      // final String newSortField = getSortFieldObject().toString();
      // if (newSortField.equals(sortField)) {
      // descending = !descending;
      // }
      // sortField = newSortField;
      // }
      // detached = false;
      // }
      
        /**
        * @return Object
        */
        protected Object getSortFieldObject() {
        final FacesContext context = FacesContext.getCurrentInstance();
        final Object sortFieldObject = context.getExternalContext()
        .getRequestParameterMap().get("sortField");
        return sortFieldObject;
        }
      
        /**
        * @param sortField
        */
        public void setSortField(final String sortField) {
      
        if (this.sortField.equals(sortField)) {
        descending = !descending;
        } else {
        this.sortField = sortField;
        }
        }
        /**
        * @return String
        */
        public String getSortField() {
        return sortField;
        }
      
      
        public void updateSortField(){
        if (getSortFieldObject() != null) {
        final String newSortField = getSortFieldObject().toString();
        if (newSortField.equals(sortField)) {
        descending = !descending;
        }
        sortField = newSortField;
        }
        detached = false;
        }
        /**
        * @see javax.faces.model.DataModel#setRowIndex(int)
        */
        @Override
        public void setRowIndex(int arg0) {
        // TODO Auto-generated method stub
        //throw new UnsupportedOperationException();
        rowIndex = arg0;
        return;
        }
        /**
        * @see javax.faces.model.DataModel#setWrappedData(java.lang.Object)
        */
        @Override
        public void setWrappedData(final Object data) {
        throw new UnsupportedOperationException();
        }
        /**
        * @see javax.faces.model.DataModel#getRowIndex()
        */
        @Override
        public int getRowIndex() {
        // TODO Auto-generated method stub
        //throw new UnsupportedOperationException();
        return rowIndex;
        }
        /**
        * @see javax.faces.model.DataModel#getWrappedData()
        */
        @Override
        public Object getWrappedData() {
        throw new UnsupportedOperationException();
        }
      
        public void wrap(FacesContext context, DataVisitor visitor, Range range, Object argument, List<Model> list) throws IOException
        {
        wrappedKeys = new ArrayList<Integer>();
        wrappedData = new HashMap<Integer, Model>();
        for (Model row : list)
        {
        Integer id = getId(row);
        setRowKey(id);
        wrappedKeys.add(id);
        wrappedData.put(id, row);
        visitor.process(context, id, argument);
        }
        }
      
        @Override
        public void walk(FacesContext context, DataVisitor visitor, Range range, Object argument)
        {
        if(validate()) {
        setRowCount(0);
        return;
        }
      
        int firstRow = ((SequenceRange) range).getFirstRow();
        int maxResults = ((SequenceRange) range).getRows();
        if (detached && getSortFieldObject() != null) {
        for (final Integer key : wrappedKeys) {
        setRowKey(key);
        visitor.process(context, key, argument);
        }
        } else {
        String sortField = getSortField();
      
        try {
        wrap(context, visitor, range, argument, findObjects(firstRow, maxResults, sortField, descending));
        } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        }
        }
        }
        /**
        * @see javax.faces.model.DataModel#isRowAvailable()
        */
        @Override
        public boolean isRowAvailable() {
        if (currentPk == null) {
        return false;
        }
        if (wrappedKeys.contains(currentPk)) {
        return true;
        }
        if (wrappedData.entrySet().contains(currentPk)) {
        return true;
        }
        try {
        if (getObjectById(currentPk) != null) {
        return true;
        }
        } catch (final Exception e) {
        }
        return false;
        }
        /**
        * @see javax.faces.model.DataModel#getRowData()
        */
        @Override
        public Model getRowData() {
        if (currentPk == null) {
        return null;
        } else {
        Model ret = wrappedData.get(currentPk);
        if (ret == null) {
        ret = this.getObjectById(currentPk);
        wrappedData.put(currentPk, ret);
        return ret;
        } else {
        return ret;
        }
        }
        }
        /**
        * @see javax.faces.model.DataModel#getRowCount()
        */
        @Override
        public int getRowCount() {
        if (rowCount == 0) {
        rowCount = getNumRecords();
        }
        return rowCount;
        }
        /**
        * @param rowCount
        */
        public void setRowCount(int rowCount) {
        this.rowCount = rowCount;
        }
        /**
        *
        * @param object
        * @return U
        */
        public Integer getId(Model object) {
        return null;
        }
        public boolean validate(){
        return false;
        }
        /**
        * @param firstRow
        * @param numberOfRows
        * @param sortField
        * @param descending
        * @return List<T>
        */
        public List<Model> findObjects(int firstRow,
        int numberOfRows, String sortField, boolean descending){
        return null;
        }
        /**
        * @param id
        * @return T
        */
        public Model getObjectById(Integer id){
        return null;
        }
        /**
        * @return String
        */
        public  String getDefaultSortField(){
        return null;
        }
        /**
        * @return int
        */
        public int getNumRecords(){
        return 0;
        }
      
      }
      
      
      

       

       

      Can anyone see anything obvious I am doing wrong or forgetting? This is my first experience using the ExtendedDataModel. Also if anybody could just point me to a good tutorial on how to use it that would be much appreciated.