2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
37 import freemarker.template.*;
41 import mir.storage.store.*;
45 * Container class for lists of Entities.
46 * Now implements freemarker.template.TemplateListModel
47 * and @see mir.storage.store.StorableObject.
50 * first version 27.6.1999
52 * @version 1.0 (freemarker compliant & and storable in ObjectStore)
54 public class EntityList implements TemplateListModel, StorableObject {
56 private static Logfile theLog;
57 private ArrayList theEntityArrayList = new ArrayList();
58 private String whereClause, orderClause;
59 private StorageObject theStorage;
60 private int count, offset, limit;
61 private int offsetnext = -1, offsetprev = -1;
62 private int freemarkerListPointer=-1;
66 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
72 public EntityList(){ }
74 /* get/set EntityClass of Objects stored in EntityList */
75 public void setStorage(StorageObject storage) { this.theStorage=storage; }
76 public StorageObject getStorage() { return theStorage; }
78 public void setLimit(int limit) { this.limit = limit; }
81 * Sets the WHERE clause that fetched the Entities of this EntityList from the database.
82 * @param wc The string that contains the WHERE clause
84 public void setWhere(String wc) {
85 this.whereClause = wc;
89 * Returns the WHERE clause that returned this EntityList from the database
90 * @return whereClause The WHERE clause
92 public String getWhere() {
98 * Sets the sorting criterium of this EntityList
101 public void setOrder(String oc) {
102 this.orderClause = oc;
106 * Returns the sorting criterium.
107 * @return orderClause The sort order
109 public String getOrder() {
114 * Sets the number of rows that match the WHERE clause
115 * @param i The number of rows that match the WHERE clause
117 public void setCount(int i) {
122 * Returns the number of rows that match the WHERE clause
123 * @return The number of rows ...
125 public int getCount() {
131 * @param i The offset
133 public void setOffset(int i) {
141 public int getOffset() {
146 * Sets the offset of the next batch of Entities.
147 * @param i The next offset
149 public void setNextBatch(int i) {
154 * Returns the offset of the next batch of Entities.
155 * @return offset of the next batch
157 public int getNextBatch() {
162 * Returns whether there is a next batch within the WHERE clause
163 * @return true if yes, false if no.
165 public boolean hasNextBatch() {
166 return (offsetnext >= 0);
170 * Sets the offset of the previous batch.
171 * @param i the previous offset
173 public void setPrevBatch(int i) {
178 * Returns the offset of the previous batch.
179 * @return offset of the previous batch
181 public int getPrevBatch() {
186 * Returns whether there is a previous batch.
187 * @return true if yes, false if no
189 public boolean hasPrevBatch() {
190 return (offsetprev >= 0);
194 * Returns the start index of the batch.
197 public int getFrom() {
202 * Returns the end index of the batch.
213 * Inserts an Entity into the EntityList.
214 * @param anEntity The entity to be inserted.
217 public void add (Entity anEntity) {
219 theEntityArrayList.add(anEntity);
221 theLog.printWarning("EntityList: add called with empty Entity");
226 * @return The number of Entities in the EntityList.
230 return theEntityArrayList.size();
235 * Returns the element at position i in the EntityList as Entity
236 * @param i the position of the element in question
237 * @return The element at position i.
240 public Entity elementAt(int i) {
241 /** @todo check if i is in list.size() */
242 return (Entity)theEntityArrayList.get(i);
246 // The following methods have to be implemented
247 // for this class to be an implementation of the
248 // TemplateListModel of the Freemarker packages
250 public TemplateModel get(int i) { return elementAt(i); }
251 public boolean isRewound() { return (freemarkerListPointer==-1) ? true : false; }
252 public void rewind() { freemarkerListPointer=-1; }
254 public TemplateModel next() {
256 freemarkerListPointer++;return get(freemarkerListPointer); }
262 * Returns whether there is a next element
263 * @return true if there is a next element, else false
266 public boolean hasNext() {
267 return theEntityArrayList.size()>0 && freemarkerListPointer+2<=theEntityArrayList.size();
272 * Returns whether EntityList is empty or not
273 * @return true in case of empty list, false otherwise
276 public boolean isEmpty() {
277 if (theEntityArrayList!=null)
278 return theEntityArrayList.size()<1 ;
283 // Methods to implement StorableObject
285 public Set getNotifyOnReleaseSet() { return null; }
287 public StoreIdentifier getStoreIdentifier() {
288 if ( theStorage!=null ) {
290 new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,
291 StoreUtil.getEntityListUniqueIdentifierFor( theStorage.getTableName(),
292 whereClause, orderClause, offset, limit ));
294 theLog.printWarning("EntityList could not return StoreIdentifier");