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