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  public class ResultDataModel extends DataModel<SortedMap<String, Object>>
30  {
31      // FIELDS
32      private int _rowIndex = -1;
33      private Result _data;
34  
35      // CONSTRUCTORS
36      public ResultDataModel()
37      {
38          super();
39      }
40  
41      public ResultDataModel(Result result)
42      {
43          if (result == null)
44          {
45              throw new NullPointerException("result");
46          }
47          setWrappedData(result);
48      }
49  
50      // METHODS
51      @Override
52      public int getRowCount()
53      {
54          if (getRows() == null)
55          {
56              return -1;
57          }
58          return getRows().length;
59      }
60  
61      @Override
62      public SortedMap<String, Object> getRowData()
63      {
64          if (getRows() == null)
65          {
66              return null;
67          }
68          if (!isRowAvailable())
69          {
70              throw new IllegalArgumentException("row is unavailable");
71          }
72          return getRows()[_rowIndex];
73      }
74  
75      @Override
76      public int getRowIndex()
77      {
78          return _rowIndex;
79      }
80  
81      @Override
82      public Object getWrappedData()
83      {
84          return _data;
85      }
86  
87      @Override
88      public boolean isRowAvailable()
89      {
90          return getRows() != null && _rowIndex >= 0 && _rowIndex < getRows().length;
91      }
92  
93      @Override
94      public void setRowIndex(int rowIndex)
95      {
96          if (rowIndex < -1)
97          {
98              throw new IllegalArgumentException("illegal rowIndex " + rowIndex);
99          }
100         int oldRowIndex = _rowIndex;
101         _rowIndex = rowIndex;
102         if (getRows() != null && oldRowIndex != _rowIndex)
103         {
104             SortedMap<String, Object> data = isRowAvailable() ? getRowData() : null;
105             DataModelEvent event = new DataModelEvent(this, _rowIndex, data);
106             DataModelListener[] listeners = getDataModelListeners();
107             for (int i = 0; i < listeners.length; i++)
108             {
109                 listeners[i].rowSelected(event);
110             }
111         }
112     }
113 
114     private SortedMap<String, Object>[] getRows()
115     {
116         if (_data == null)
117         {
118             return null;
119         }
120 
121         return _data.getRows();
122     }
123 
124     @Override
125     public void setWrappedData(Object data)
126     {
127         if (data == null)
128         {
129             setRowIndex(-1);
130             _data = null;
131         }
132         else
133         {
134             _data = ((Result)data);
135             _rowIndex = -1;
136             setRowIndex(0);
137         }
138     }
139 
140 }