Coverage Report - javax.faces.model.ResultSetDataModel
 
Classes in this File Line Coverage Branch Coverage Complexity
ResultSetDataModel
73%
44/60
71%
20/28
2.957
ResultSetDataModel$ResultSetMap
30%
13/44
12%
2/16
2.957
 
 1  
 /*
 2  
  * Copyright 2004-2011 the Seasar Foundation and the Others.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 13  
  * either express or implied. See the License for the specific language
 14  
  * governing permissions and limitations under the License.
 15  
  */
 16  
 package javax.faces.model;
 17  
 
 18  
 import java.sql.ResultSet;
 19  
 import java.sql.ResultSetMetaData;
 20  
 import java.sql.SQLException;
 21  
 import java.util.Collection;
 22  
 import java.util.Comparator;
 23  
 import java.util.Iterator;
 24  
 import java.util.Set;
 25  
 import java.util.TreeMap;
 26  
 
 27  
 import javax.faces.FacesException;
 28  
 import javax.faces.internal.ResultSetEntries;
 29  
 import javax.faces.internal.ResultSetKeys;
 30  
 import javax.faces.internal.ResultSetValues;
 31  
 
 32  
 /**
 33  
  * @author shot
 34  
  */
 35  9
 public class ResultSetDataModel extends DataModel {
 36  
 
 37  8
     private ResultSet resultSet = null;
 38  
 
 39  8
     private int index = -1;
 40  
 
 41  8
     private boolean isUpdated = false;
 42  
 
 43  3
     public ResultSetDataModel() {
 44  3
         setWrappedData(null);
 45  3
     }
 46  
 
 47  
     public ResultSetDataModel(ResultSet resultSet) {
 48  5
         super();
 49  5
         assertScrollable(resultSet);
 50  4
         setWrappedData(resultSet);
 51  3
     }
 52  
 
 53  
     public int getRowCount() {
 54  1
         return -1;
 55  
     }
 56  
 
 57  
     public Object getRowData() {
 58  5
         if (resultSet == null) {
 59  1
             return null;
 60  
         }
 61  
 
 62  4
         if (!isRowAvailable()) {
 63  1
             throw new IllegalArgumentException();
 64  
         }
 65  
 
 66  
         try {
 67  3
             return new ResultSetMap(String.CASE_INSENSITIVE_ORDER);
 68  0
         } catch (SQLException e) {
 69  0
             throw new FacesException();
 70  
         }
 71  
     }
 72  
 
 73  
     public int getRowIndex() {
 74  0
         return index;
 75  
     }
 76  
 
 77  
     public Object getWrappedData() {
 78  0
         return resultSet;
 79  
     }
 80  
 
 81  
     public boolean isRowAvailable() {
 82  12
         if (resultSet == null || index < 0) {
 83  5
             return false;
 84  
         }
 85  
         try {
 86  7
             return resultSet.absolute(index + 1);
 87  1
         } catch (SQLException e) {
 88  1
             throw new FacesException();
 89  
         }
 90  
     }
 91  
 
 92  
     public void setRowIndex(int rowIndex) {
 93  9
         if (rowIndex < -1) {
 94  0
             throw new IllegalArgumentException();
 95  
         }
 96  9
         if (isUpdated) {
 97  0
             updateResultSetIfNeeded();
 98  
         }
 99  9
         int oldIndex = index;
 100  9
         index = rowIndex;
 101  9
         DataModelListener[] listeners = getDataModelListeners();
 102  9
         if ((oldIndex != index) && (listeners != null)) {
 103  6
             Object rowData = null;
 104  6
             if (isRowAvailable()) {
 105  2
                 rowData = getRowData();
 106  
             }
 107  5
             DataModelEvent event = new DataModelEvent(this, index, rowData);
 108  5
             for (int i = 0; i < listeners.length; i++) {
 109  0
                 listeners[i].rowSelected(event);
 110  
             }
 111  
         }
 112  8
     }
 113  
 
 114  
     public void setWrappedData(Object data) {
 115  7
         if (data == null) {
 116  3
             setRowIndex(-1);
 117  
         } else {
 118  4
             resultSet = (ResultSet) data;
 119  4
             setRowIndex(0);
 120  
         }
 121  6
     }
 122  
 
 123  
     private void updateResultSetIfNeeded() {
 124  
         try {
 125  0
             if (resultSet != null && !resultSet.rowDeleted()) {
 126  0
                 resultSet.updateRow();
 127  0
                 isUpdated = false;
 128  
             }
 129  0
         } catch (SQLException e) {
 130  0
             throw new FacesException();
 131  0
         }
 132  0
     }
 133  
 
 134  
     private static void assertScrollable(ResultSet rs) {
 135  
         try {
 136  5
             int type = rs.getType();
 137  5
             if (type != ResultSet.TYPE_SCROLL_SENSITIVE) {
 138  1
                 throw new IllegalArgumentException();
 139  
             }
 140  
 
 141  0
         } catch (SQLException e) {
 142  0
             throw new IllegalArgumentException();
 143  4
         }
 144  
 
 145  4
     }
 146  
 
 147  
     public class ResultSetMap extends TreeMap {
 148  
 
 149  
         private static final long serialVersionUID = 1L;
 150  
 
 151  3
         private int mapIndex = 0;
 152  
 
 153  3
         private ResultSetMetaData metaData = null;
 154  
 
 155  3
         public ResultSetMap(Comparator comparator) throws SQLException {
 156  3
             super(comparator);
 157  3
             mapIndex = index;
 158  3
             resultSet.absolute(mapIndex + 1);
 159  3
             metaData = getMetaData();
 160  3
             for (int i = 1; i <= metaData.getColumnCount(); i++) {
 161  0
                 super.put(metaData.getColumnName(i), metaData.getColumnName(i));
 162  
             }
 163  3
         }
 164  
 
 165  
         public boolean containsValue(Object value) {
 166  0
             Object key = null;
 167  0
             for (Iterator itr = keySet().iterator(); itr.hasNext();) {
 168  0
                 key = itr.next();
 169  0
                 if ((key == null && value == null) || (key.equals(value))) {
 170  0
                     return true;
 171  
                 }
 172  
             }
 173  0
             return false;
 174  
         }
 175  
 
 176  
         public Set entrySet() {
 177  0
             return new ResultSetEntries(this);
 178  
         }
 179  
 
 180  
         public Object get(Object key) {
 181  0
             if (!containsKey(key)) {
 182  0
                 return null;
 183  
             }
 184  
             try {
 185  0
                 resultSet.absolute(index + 1);
 186  0
                 return resultSet.getObject((String) realKey(key));
 187  0
             } catch (SQLException e) {
 188  0
                 throw new FacesException();
 189  
             }
 190  
         }
 191  
 
 192  
         public Set keySet() {
 193  0
             return new ResultSetKeys(this);
 194  
         }
 195  
 
 196  
         public Object put(Object key, Object value) {
 197  0
             if ((!containsKey(key))) {
 198  0
                 throw new IllegalArgumentException();
 199  
             }
 200  
             try {
 201  0
                 resultSet.absolute(index + 1);
 202  0
                 String realKey = (String) realKey(key);
 203  0
                 resultSet.updateObject(realKey, value);
 204  0
                 isUpdated = true;
 205  0
                 return resultSet.getObject(realKey);
 206  0
             } catch (SQLException e) {
 207  0
                 throw new FacesException();
 208  
             }
 209  
         }
 210  
 
 211  
         public Collection values() {
 212  0
             return new ResultSetValues(this);
 213  
         }
 214  
 
 215  
         public void clear() {
 216  0
             throw new UnsupportedOperationException();
 217  
         }
 218  
 
 219  
         public Object remove(Object obj) {
 220  0
             throw new UnsupportedOperationException();
 221  
         }
 222  
 
 223  
         public Object realKey(Object key) {
 224  0
             return super.get(key);
 225  
         }
 226  
 
 227  
         public Iterator realKeys() {
 228  0
             return super.keySet().iterator();
 229  
         }
 230  
 
 231  
         private ResultSetMetaData getMetaData() {
 232  
             try {
 233  3
                 if (metaData == null) {
 234  3
                     metaData = resultSet.getMetaData();
 235  
                 }
 236  0
             } catch (SQLException e) {
 237  0
                 throw new FacesException();
 238  3
             }
 239  3
             return metaData;
 240  
         }
 241  
 
 242  
     }
 243  
 
 244  
 }