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 javax.faces.model;
20  
21  import java.util.SortedMap;
22  
23  import javax.servlet.jsp.jstl.sql.Result;
24  
25  //import javax.servlet.jsp.
26  /**
27   * see Javadoc of <a href="http://java.sun.com/javaee/javaserverfaces/1.2/docs/api/index.html">JSF Specification</a>
28   *
29   * @author Thomas Spiegl (latest modification by $Author: lu4242 $)
30   * @version $Revision: 1154078 $ $Date: 2011-08-04 22:03:06 -0500 (Thu, 04 Aug 2011) $
31   */
32  public class ResultDataModel extends DataModel
33  {
34      // FIELDS
35      private int _rowIndex = -1;
36      private Result _data;
37  
38      // CONSTRUCTORS
39      public ResultDataModel()
40      {
41          super();
42      }
43  
44      public ResultDataModel(Result result)
45      {
46          if (result == null) throw new NullPointerException("result");
47          setWrappedData(result);
48      }
49  
50      // METHODS
51      public int getRowCount()
52      {
53          if (getRows() == null)
54          {
55              return -1;
56          }
57          return getRows().length;
58      }
59  
60      public Object getRowData()
61      {
62          if (getRows() == null)
63          {
64              return null;
65          }
66          if (!isRowAvailable())
67          {
68              throw new IllegalArgumentException("row is unavailable");
69          }
70          return getRows()[_rowIndex];
71      }
72  
73      public int getRowIndex()
74      {
75          return _rowIndex;
76      }
77  
78      public Object getWrappedData()
79      {
80          return _data;
81      }
82  
83      public boolean isRowAvailable()
84      {
85          if (getRows() == null)
86          {
87              return false;
88          }
89          return _rowIndex >= 0 && _rowIndex < getRows().length;
90      }
91  
92      public void setRowIndex(int rowIndex)
93      {
94          if (rowIndex < -1)
95          {
96              throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
97          }
98          int oldRowIndex = _rowIndex;
99          _rowIndex = rowIndex;
100         if (getRows() != null && oldRowIndex != _rowIndex)
101         {
102             Object data = isRowAvailable() ? getRowData() : null;
103             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
104             DataModelListener[] listeners = getDataModelListeners();
105             for (int i = 0; i < listeners.length; i++)
106             {
107                 listeners[i].rowSelected(event);
108             }
109         }
110     }
111 
112     private SortedMap[] getRows()
113     {
114         if(_data==null)
115             return null;
116 
117         return _data.getRows();
118     }
119 
120     public void setWrappedData(Object data)
121     {
122         if (data == null)
123         {
124             setRowIndex(-1);
125             _data = null;
126         }
127         else
128         {
129             _data = ((Result)data);
130             _rowIndex = -1;
131             setRowIndex(0);
132         }
133     }
134 
135 }