1 /* 2 * $Source$ 3 * $Revision$ 4 * 5 * Copyright (C) 2000 William Chesters 6 * 7 * Part of Melati (http://melati.org), a framework for the rapid 8 * development of clean, maintainable web applications. 9 * 10 * Melati is free software; Permission is granted to copy, distribute 11 * and/or modify this software under the terms either: 12 * 13 * a) the GNU General Public License as published by the Free Software 14 * Foundation; either version 2 of the License, or (at your option) 15 * any later version, 16 * 17 * or 18 * 19 * b) any version of the Melati Software License, as published 20 * at http://melati.org 21 * 22 * You should have received a copy of the GNU General Public License and 23 * the Melati Software License along with this program; 24 * if not, write to the Free Software Foundation, Inc., 25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA to obtain the 26 * GNU General Public License and visit http://melati.org to obtain the 27 * Melati Software License. 28 * 29 * Feel free to contact the Developers of Melati (http://melati.org), 30 * if you would like to work out a different arrangement than the options 31 * outlined here. It is our intention to allow Melati to be used by as 32 * wide an audience as possible. 33 * 34 * This program is distributed in the hope that it will be useful, 35 * but WITHOUT ANY WARRANTY; without even the implied warranty of 36 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 37 * GNU General Public License for more details. 38 * 39 * Contact details for copyright holder: 40 * 41 * William Chesters <williamc At paneris.org> 42 * http://paneris.org/~williamc 43 * Obrechtstraat 114, 2517VX Den Haag, The Netherlands 44 */ 45 46 package org.melati.poem; 47 48 import java.util.Collections; 49 import java.util.Enumeration; 50 import java.util.List; 51 52 import org.melati.poem.util.MappedEnumeration; 53 54 /** 55 * A cached instance of an SQL <code>select</code> query. 56 */ 57 public class CachedSelection<P extends Persistent> extends CachedQuery<P> { 58 59 /** 60 * Constructor. 61 * @param table the primary Table we are selecting rows from 62 * @param whereClause SQL snippet 63 * @param orderByClause SQL snippet 64 * @param otherTables an Array of Tables which are also referenced in query 65 */ 66 public CachedSelection(final Table<?> table, 67 final String whereClause, 68 final String orderByClause, 69 final Table<?> otherTables[]) { 70 super(table, null, otherTables); 71 72 73 String fromClause = table.quotedName(); 74 if (otherTables != null) { 75 for (int i = 0; i<otherTables.length; i++) { 76 fromClause += ", " + otherTables[i].quotedName(); 77 } 78 } 79 setQuery(table.selectionSQL(fromClause, whereClause, orderByClause, false, true)); 80 81 } 82 83 /** 84 * Constructor for a single Table query. 85 * @param table the primary Table we are selecting rows from 86 * @param whereClause SQL snippet 87 * @param orderByClause SQL snippet 88 */ 89 public CachedSelection(final Table<P> table, 90 final String whereClause, 91 final String orderByClause) { 92 this(table,whereClause,orderByClause,null); 93 } 94 95 /** 96 * @return an Enumeration of Table Row Object Ids 97 */ 98 public Enumeration<Integer> troids() { 99 compute(); 100 return rows.elements(); 101 } 102 103 /** 104 * @return an Enumeration of Table Row ReferencePoemType objects 105 */ 106 public Enumeration<P> objects() { 107 return 108 new MappedEnumeration<P,Integer>(troids()) { 109 @SuppressWarnings("unchecked") 110 public P mapped(Integer troid) { 111 return (P)table.getObject((Integer)troid); 112 } 113 }; 114 } 115 /** 116 * Convenience method. 117 * 118 * @return a List of Table Row ReferencePoemType objects 119 */ 120 121 public List<P> objectList() { 122 return Collections.list(objects()); 123 } 124 125 /** 126 * @return the first, often only, result 127 */ 128 public Persistent firstObject() { 129 return nth(0); 130 } 131 132 /** 133 * @param n index into ResultSet 134 * @return the row corresponding to the index 135 */ 136 public Persistent nth(int n) { 137 compute(); 138 return rows.size() <= n ? 139 null : 140 table.getObject((Integer)rows.elementAt(n)); 141 } 142 143 /** 144 * @return the number of results 145 */ 146 public int count() { 147 compute(); 148 return this.rows.size(); 149 } 150 }