1 /* 2 * $Id: ResultDataModel.java,v 1.21 2007/04/27 22:00:09 ofung Exp $ 3 */ 4 5 /* 6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. 7 * 8 * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved. 9 * 10 * The contents of this file are subject to the terms of either the GNU 11 * General Public License Version 2 only ("GPL") or the Common Development 12 * and Distribution License("CDDL") (collectively, the "License"). You 13 * may not use this file except in compliance with the License. You can obtain 14 * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html 15 * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific 16 * language governing permissions and limitations under the License. 17 * 18 * When distributing the software, include this License Header Notice in each 19 * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt. 20 * Sun designates this particular file as subject to the "Classpath" exception 21 * as provided by Sun in the GPL Version 2 section of the License file that 22 * accompanied this code. If applicable, add the following below the License 23 * Header, with the fields enclosed by brackets [] replaced by your own 24 * identifying information: "Portions Copyrighted [year] 25 * [name of copyright owner]" 26 * 27 * Contributor(s): 28 * 29 * If you wish your version of this file to be governed by only the CDDL or 30 * only the GPL Version 2, indicate your decision by adding "[Contributor] 31 * elects to include this software in this distribution under the [CDDL or GPL 32 * Version 2] license." If you don't indicate a single choice of license, a 33 * recipient has the option to distribute your version of this file under 34 * either the CDDL, the GPL Version 2 or to extend the choice of license to 35 * its licensees as provided above. However, if you add GPL Version 2 code 36 * and therefore, elected the GPL Version 2 license, then the option applies 37 * only if the new code is made subject to such option by the copyright 38 * holder. 39 */ 40 41 package javax.faces.model; 42 43 44 import java.util.SortedMap; 45 import javax.faces.FacesException; 46 import javax.servlet.jsp.jstl.sql.Result; 47 48 49 /** 50 * <p><strong>ResultDataModel</strong> is a convenience implementation of 51 * {@link DataModel} that wraps a JSTL <code>Result</code> object, typically 52 * representing the results of executing an SQL query via JSTL tags.</p> 53 */ 54 55 public class ResultDataModel extends DataModel<SortedMap<String,Object>> { 56 57 58 // ------------------------------------------------------------ Constructors 59 60 61 /** 62 * <p>Construct a new {@link ResultDataModel} with no specified 63 * wrapped data.</p> 64 */ 65 public ResultDataModel() { 66 67 this(null); 68 69 } 70 71 72 /** 73 * <p>Construct a new {@link ResultDataModel} wrapping the specified 74 * <code>Result</code>.</p> 75 * 76 * @param result <code>Result</code> to be wrapped (if any) 77 */ 78 public ResultDataModel(Result result) { 79 80 super(); 81 setWrappedData(result); 82 83 } 84 85 86 // ------------------------------------------------------ Instance Variables 87 88 89 // The current row index (zero relative) 90 private int index = -1; 91 92 93 // The Result we are wrapping 94 private Result result = null; 95 96 97 // The individual rows of this Result, each represented as a Map 98 // with column names as keys and associated data values as values 99 private SortedMap rows[] = null; 100 101 102 // -------------------------------------------------------------- Properties 103 104 105 /** 106 * <p>Return <code>true</code> if there is <code>wrappedData</code> 107 * available, and the current value of <code>rowIndex</code> is greater 108 * than or equal to zero, and less than the length of the array returned 109 * by calling <code>getRows()</code> on the underlying <code>Result</code>. 110 * Otherwise, return <code>false</code>.</p> 111 * 112 * @throws FacesException if an error occurs getting the row availability 113 */ 114 public boolean isRowAvailable() { 115 116 if (result == null) { 117 return (false); 118 } else if ((index >= 0) && (index < rows.length)) { 119 return (true); 120 } else { 121 return (false); 122 } 123 124 } 125 126 127 /** 128 * <p>If there is <code>wrappedData</code> available, return the 129 * length of the array returned by calling <code>getRows()</code> 130 * on the underlying <code>Result</code>. If no <code>wrappedData</code> 131 * is available, return -1.</p> 132 * 133 * @throws FacesException if an error occurs getting the row count 134 */ 135 public int getRowCount() { 136 137 if (result == null) { 138 return (-1); 139 } 140 return (rows.length); 141 142 } 143 144 145 /** 146 * <p>If row data is available, return the <code>SortedMap</code> array 147 * element at the index specified by <code>rowIndex</code> of the 148 * array returned by calling <code>getRows()</code> on the underlying 149 * <code>Result</code>. If no wrapped data is available, 150 * return <code>null</code>.</p> 151 * 152 * <p>Note that, if a non-<code>null</code> <code>Map</code> is returned 153 * by this method, it will contain the values of the columns for the 154 * current row, keyed by column name. Column name comparisons must be 155 * performed in a case-insensitive manner.</p> 156 * 157 * @throws FacesException if an error occurs getting the row data 158 * @throws IllegalArgumentException if now row data is available 159 * at the currently specified row index 160 */ 161 public SortedMap<String,Object> getRowData() { 162 163 if (result == null) { 164 return (null); 165 } else if (!isRowAvailable()) { 166 throw new NoRowAvailableException(); 167 } else { 168 //noinspection unchecked 169 return ((SortedMap<String,Object>)rows[index]); 170 } 171 172 } 173 174 175 /** 176 * @throws FacesException {@inheritDoc} 177 */ 178 public int getRowIndex() { 179 180 return (index); 181 182 } 183 184 185 /** 186 * @throws FacesException {@inheritDoc} 187 * @throws IllegalArgumentException {@inheritDoc} 188 */ 189 public void setRowIndex(int rowIndex) { 190 191 if (rowIndex < -1) { 192 throw new IllegalArgumentException(); 193 } 194 int old = index; 195 index = rowIndex; 196 if (result == null) { 197 return; 198 } 199 DataModelListener [] listeners = getDataModelListeners(); 200 if ((old != index) && (listeners != null)) { 201 SortedMap<String,Object> rowData = null; 202 if (isRowAvailable()) { 203 rowData = getRowData(); 204 } 205 DataModelEvent event = 206 new DataModelEvent(this, index, rowData); 207 int n = listeners.length; 208 for (int i = 0; i < n; i++) { 209 if (null != listeners[i]) { 210 listeners[i].rowSelected(event); 211 } 212 } 213 } 214 215 } 216 217 218 public Object getWrappedData() { 219 220 return (this.result); 221 222 } 223 224 225 /** 226 * @throws ClassCastException if <code>data</code> is 227 * non-<code>null</code> and is not a <code>Result</code> 228 */ 229 public void setWrappedData(Object data) { 230 231 if (data == null) { 232 result = null; 233 rows = null; 234 setRowIndex(-1); 235 } else { 236 result = (Result) data; 237 rows = result.getRows(); 238 index = -1; 239 setRowIndex(0); 240 } 241 242 } 243 244 245 }