Du bist hier: Snippet-Verzeichnis » Java (241)
Sprache:

Agilesoft Query Iterator

Sprache: English
Programmiersprache: Java
Veröffentlicht von: ashleym [nicht registriert]
Letzte Änderung: 15.05.2006
Aufrufe: 1091


Beschreibung

Iterator class for an Agilesoft API Query.

Code

1 package com.agilesoft.api.iterator; 2 3 import com.agilesoft.api.APIException; 4 import com.agilesoft.api.Collection; 5 import com.agilesoft.api.Query; 6 7 import org.apache.log4j.Logger; 8 9 import java.util.NoSuchElementException; 10 11 /** 12 * This class simplifies com.agilesoft.api.Query processing. To use:<br> 13 * <br><pre> 14 * Query query = new Query(); 15 * query.execute(); 16 * QueryIterator qi = new QueryIterator(query); 17 * while (qi.hasNext()) { 18 * Row row = (Row) qi.next(); 19 * } 20 * </pre> 21 * 22 * @author Ashley Martens 23 * @version 1.0 24 */ 25 public class QueryIterator 26 implements java.util.Iterator 27 { 28 /* Create a default logger instance */ 29 protected static Logger logger = Logger.getLogger(QueryIterator.class); 30 31 protected Collection m_oRows = null; 32 protected Query m_oQuery = null; 33 protected int m_iQueryCounter = 0, 34 m_iRangeCounter = 0, 35 m_iRowRange =200, 36 m_iRowCount = 0; 37 38 /** 39 * Create the iterator. This should really be in the Query class as a method 40 * iterator() that return a java.util.Iterator instance. 41 */ 42 public QueryIterator(Query query) 43 throws com.agilesoft.api.APIException 44 { 45 m_iRowCount = query.getResultCount(); 46 m_oQuery = query; 47 if (m_iRowCount < m_iRowRange) { 48 /* 49 * You need to process the whole query in one shot or 0 rows will 50 * be returned by getResults() for the second range 51 */ 52 m_iRowRange = m_iRowCount + 1; 53 } 54 /* 55 * 200 is the maximum that setResultRange will accept without 56 * throwing an APIException("Invalid parameter") 57 */ 58 /*if (m_iRowCount % m_iRowRange == 0) { 59 m_iRowRange--; 60 }*/ 61 try { 62 m_oQuery.setResultRange(m_iRowRange); 63 } catch (com.agilesoft.api.APIException e) { 64 logger.error(e.getMessage() + "\tRowCount = " + m_iRowCount + 65 "\tRowRange = " + m_iRowRange, e); 66 throw e; 67 } 68 m_oQuery.setResultStart(0); 69 } 70 71 /** 72 * Will next() return a valid object 73 */ 74 public boolean hasNext() 75 { 76 if (m_iQueryCounter >= m_iRowCount && m_iRowCount > 0) { 77 /* This query iteration is finished, cleanup */ 78 if (m_oRows != null) { 79 m_oRows.compact(); 80 } 81 if (m_oQuery != null) { 82 m_oQuery.compact(); 83 } 84 /* Check for query repeat bug. */ 85 if (m_iRangeCounter < m_iRowRange) { 86 logger.warn("Query repeat error." + 87 ". RowStart = " + m_iQueryCounter + 88 ". RowCount = " + m_iRowCount + 89 ". RangeCounter = " + m_iRangeCounter + 90 ". RangeCount = " + m_iRowRange); 91 } 92 } 93 return m_iQueryCounter < m_iRowCount; 94 } 95 96 private void loadInfo() 97 throws APIException 98 { 99 /* Cleanup the rows collection */ 100 if (m_oRows != null) { 101 m_oRows.compact(); 102 m_oRows = null; 103 } 104 105 m_oQuery.setResultStart(m_iQueryCounter); 106 m_oRows = m_oQuery.getResults(); 107 m_iRowRange = m_oRows.getCount(); 108 109 /* Make sure that the query object did not return too many rows */ 110 if (m_iRowRange > m_oQuery.getResultRange()) { 111 logger.warn("Query.getRows() bug. Asked for " + 112 m_oQuery.getResultRange() + " rows. Received " + 113 m_iRowRange + " rows" + 114 ". RowStart = " + m_iQueryCounter + 115 ". RowCount = " + m_iRowCount + 116 ". RangeCounter = " + m_iRangeCounter + 117 ". RangeCount = " + m_iRowRange); 118 m_iRowRange = m_oQuery.getResultRange(); 119 } 120 121 m_iRangeCounter = 0; 122 } 123 124 /** 125 * Return the next object in the query 126 */ 127 public Object next() 128 throws java.util.NoSuchElementException 129 { 130 Object oRet = null; 131 132 if (!hasNext()) { 133 throw new NoSuchElementException(); 134 } 135 136 try { 137 /* Load the new result range */ 138 if (m_oRows == null || m_iRangeCounter >= m_iRowRange) { 139 loadInfo(); 140 } 141 142 /* Get the return data */ 143 if (m_iRangeCounter < m_iRowRange) { 144 oRet = m_oRows.get(m_iRangeCounter); 145 m_iRangeCounter++; 146 m_iQueryCounter++; 147 } else { 148 logger.warn("Query.getRows() error." + 149 ". RowStart = " + m_iQueryCounter + 150 ". RowCount = " + m_iRowCount + 151 ". RangeCounter = " + m_iRangeCounter + 152 ". RangeCount = " + m_iRowRange); 153 m_iQueryCounter = m_iRowCount; 154 } 155 } catch (APIException a) { 156 NoSuchElementException e = 157 new NoSuchElementException(a.getMessage()); 158 e.initCause(a); 159 throw e; 160 } 161 162 return oRet; 163 } 164 165 /** 166 * This is an unsupported feature 167 */ 168 public void remove() 169 throws UnsupportedOperationException, IllegalStateException 170 { 171 throw new UnsupportedOperationException(); 172 } 173 }

Noch kein Kommentar vorhanden

Dieses Snippet kommentieren

Name *  

E-Mail (wird nicht angezeigt) *    

Website  

Kommentar *  

Sicherheitscode Sicherheitscode *    

RSS