Coverage Report - org.apache.tapestry.contrib.table.model.sql.ResultSetIterator
 
Classes in this File Line Coverage Branch Coverage Complexity
ResultSetIterator
0%
0/30
0%
0/10
2.667
 
 1  
 // Copyright 2004, 2005 The Apache Software Foundation
 2  
 //
 3  
 // Licensed under the Apache License, Version 2.0 (the "License");
 4  
 // you may not use this file except in compliance with the License.
 5  
 // You may obtain a copy of the License at
 6  
 //
 7  
 //     http://www.apache.org/licenses/LICENSE-2.0
 8  
 //
 9  
 // Unless required by applicable law or agreed to in writing, software
 10  
 // distributed under the License is distributed on an "AS IS" BASIS,
 11  
 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 12  
 // See the License for the specific language governing permissions and
 13  
 // limitations under the License.
 14  
 
 15  
 package org.apache.tapestry.contrib.table.model.sql;
 16  
 
 17  
 import java.sql.ResultSet;
 18  
 import java.sql.SQLException;
 19  
 import java.util.Iterator;
 20  
 
 21  
 import org.apache.commons.logging.Log;
 22  
 import org.apache.commons.logging.LogFactory;
 23  
 
 24  
 /**
 25  
  * @author mindbridge
 26  
  */
 27  
 public class ResultSetIterator implements Iterator
 28  
 {
 29  
 
 30  0
     private static final Log LOG = LogFactory.getLog(ResultSetIterator.class);
 31  
 
 32  
     private ResultSet m_objResultSet;
 33  
     private boolean m_bFetched;
 34  
     private boolean m_bAvailable;
 35  
 
 36  
     public ResultSetIterator(ResultSet objResultSet)
 37  0
     {
 38  0
         m_objResultSet = objResultSet;
 39  0
         m_bFetched = false;
 40  0
     }
 41  
 
 42  
     /**
 43  
      * @see java.util.Iterator#hasNext()
 44  
      */
 45  
     public synchronized boolean hasNext()
 46  
     {
 47  0
         if (getResultSet() == null) return false;
 48  
 
 49  0
         if (!m_bFetched)
 50  
         {
 51  0
             m_bFetched = true;
 52  
 
 53  
             try
 54  
             {
 55  0
                 m_bAvailable = !getResultSet().isLast();
 56  
             }
 57  0
             catch (SQLException e)
 58  
             {
 59  0
                 LOG.warn("SQLException while testing for end of the ResultSet",
 60  
                         e);
 61  0
                 m_bAvailable = false;
 62  0
             }
 63  
 
 64  0
             if (!m_bAvailable) notifyEnd();
 65  
         }
 66  
 
 67  0
         return m_bAvailable;
 68  
     }
 69  
 
 70  
     /**
 71  
      * @see java.util.Iterator#next()
 72  
      */
 73  
     public synchronized Object next()
 74  
     {
 75  0
         ResultSet objResultSet = getResultSet();
 76  
 
 77  
         try
 78  
         {
 79  0
             if (!objResultSet.next()) return null;
 80  
         }
 81  0
         catch (SQLException e)
 82  
         {
 83  0
             LOG.warn("SQLException while iterating over the ResultSet", e);
 84  0
             return null;
 85  0
         }
 86  
 
 87  0
         m_bFetched = false;
 88  0
         return objResultSet;
 89  
     }
 90  
 
 91  
     /**
 92  
      * @see java.util.Iterator#remove()
 93  
      */
 94  
     public void remove()
 95  
     {
 96  
         try
 97  
         {
 98  0
             getResultSet().deleteRow();
 99  
         }
 100  0
         catch (SQLException e)
 101  
         {
 102  0
             LOG.error("Cannot delete record", e);
 103  0
         }
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Returns the resultSet.
 108  
      * 
 109  
      * @return ResultSet
 110  
      */
 111  
     public ResultSet getResultSet()
 112  
     {
 113  0
         return m_objResultSet;
 114  
     }
 115  
 
 116  
     protected void notifyEnd()
 117  
     {
 118  0
     }
 119  
 
 120  
 }