View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.trinidad.model;
20  
21  /**
22   * Creates a CollectionModel whose row keys are defined by a unique data property in the model.
23   */
24  public class RowKeyPropertyModel extends SortableModel
25  {
26    /**
27     * Creates a RowKeyPropertyModel.
28     *
29     * @param model The underlying model. If necessary, this will be converted into a {@link DataModel}
30     * @param rowKeyProperty The property by which the row key can be accessed. Row key value must be unique
31     */
32    public RowKeyPropertyModel(Object model, String rowKeyProperty)
33    {
34      super(model);
35      _rowKeyProperty = rowKeyProperty;
36    }
37    
38    /**
39     * No arg constructor for use as a managed-bean.
40     * Must call {@link #setWrappedData} and {@link #setRowKeyProperty} before using this instance.
41     */
42    public RowKeyPropertyModel()
43    {
44      super();
45    }
46  
47    /**
48     * Gets the row key for the current row
49     * @return row key or null if model is not on any row
50     */
51    public Object getRowKey()
52    {
53      if (isRowAvailable())
54      {
55        Object rowKey = _getRowKey();
56        return rowKey;
57      }
58      else
59      {
60        return null;
61      }
62    }
63  
64    /**
65     * Moves the model to the row identified by the key.
66     * @param key target row key
67     */
68    public void setRowKey(Object key)
69    {
70      if (key == null)
71      {
72        setRowIndex(-1);
73        return;
74      }
75  
76      if (getRowKey() != null && getRowKey().equals(key))
77        return;
78      
79      for (int i = 0; i < getRowCount(); i++)
80      {
81        setRowIndex(i);
82        Object prop = getRowKey();
83        if (key.equals(prop))
84        {
85          return;
86        }
87      }
88  
89      // if we didn't find an element with a matching key,
90      // then don't make any rows current
91      setRowIndex(-1);
92    }
93  
94    /**
95     * Gets the row key property name for this model
96     * @return row key property name
97     */
98    public String getRowKeyProperty()
99    {
100     return _rowKeyProperty;
101   }
102 
103   /**
104    * Sets the row key property for this model
105    * @param rowKeyProperty row key property to set
106    */
107   public void setRowKeyProperty(String rowKeyProperty)
108   {
109     _rowKeyProperty = rowKeyProperty;
110   }
111 
112   /**
113    * gets the row key for the given row by resolving the _rowKeyProperty
114    * @param row row to retrieve the row key for
115    * @return row key value
116    */
117   protected Object getRowKey(Object row)
118   {
119     assert (_rowKeyProperty != null);
120     return __resolveProperty(row, _rowKeyProperty);
121   }
122 
123   /**
124    * gets the row key for current row by resolving the _rowKeyProperty
125    * @return
126    */
127   private Object _getRowKey()
128   {
129     Object data = getRowData();
130     
131     assert (_rowKeyProperty != null);
132     return __resolveProperty(data, _rowKeyProperty);
133   }
134   
135   private String _rowKeyProperty;
136 
137 }