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