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 any library licensed under the Apache Software License,
22 * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library
23 * (or with modified versions of the above that use the same license as the above),
24 * and distribute linked combinations including the two. You must obey the
25 * GNU General Public License in all respects for all of the code used other than
26 * the above mentioned libraries. If you modify this file, you may extend this
27 * exception to your version of the file, but you are not obligated to do so.
28 * If you do not wish to do so, delete this exception statement from your version.
32 import java.util.ArrayList;
35 import mir.config.MirPropertiesConfiguration;
36 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
37 import mir.log.LoggerWrapper;
38 import mir.storage.StorageObject;
39 import mir.storage.store.StorableObject;
40 import mir.storage.store.StoreContainerType;
41 import mir.storage.store.StoreIdentifier;
42 import mir.storage.store.StoreUtil;
43 import freemarker.template.TemplateListModel;
44 import freemarker.template.TemplateModel;
48 * Container class for lists of Entities.
49 * Now implements freemarker.template.TemplateListModel
50 * and @see mir.storage.store.StorableObject.
53 * first version 27.6.1999
55 * @version 1.0 (freemarker compliant & and storable in ObjectStore)
57 public class EntityList implements TemplateListModel, StorableObject {
58 protected static MirPropertiesConfiguration configuration;
59 protected LoggerWrapper logger;
60 private ArrayList theEntityArrayList = new ArrayList();
61 private String whereClause, orderClause;
62 private StorageObject theStorage;
63 private int count, offset, limit;
64 private int offsetnext = -1, offsetprev = -1;
65 private int freemarkerListPointer=-1;
70 configuration = MirPropertiesConfiguration.instance();
72 catch (PropertiesConfigExc e) {
73 throw new RuntimeException("Unable to get configuration: " + e.getMessage());
81 logger = new LoggerWrapper("Entity.List");
84 /* get/set EntityClass of Objects stored in EntityList */
85 public void setStorage(StorageObject storage) { this.theStorage=storage; }
86 public StorageObject getStorage() { return theStorage; }
88 public void setLimit(int limit) { this.limit = limit; }
91 * Sets the WHERE clause that fetched the Entities of this EntityList from the database.
92 * @param wc The string that contains the WHERE clause
94 public void setWhere(String wc) {
95 this.whereClause = wc;
99 * Returns the WHERE clause that returned this EntityList from the database
100 * @return whereClause The WHERE clause
102 public String getWhere() {
108 * Sets the sorting criterium of this EntityList
111 public void setOrder(String oc) {
112 this.orderClause = oc;
116 * Returns the sorting criterium.
117 * @return orderClause The sort order
119 public String getOrder() {
124 * Sets the number of rows that match the WHERE clause
125 * @param i The number of rows that match the WHERE clause
127 public void setCount(int i) {
132 * Returns the number of rows that match the WHERE clause
133 * @return The number of rows ...
135 public int getCount() {
141 * @param i The offset
143 public void setOffset(int i) {
151 public int getOffset() {
156 * Sets the offset of the next batch of Entities.
157 * @param i The next offset
159 public void setNextBatch(int i) {
164 * Returns the offset of the next batch of Entities.
165 * @return offset of the next batch
167 public int getNextBatch() {
172 * Returns whether there is a next batch within the WHERE clause
173 * @return true if yes, false if no.
175 public boolean hasNextBatch() {
176 return (offsetnext >= 0);
180 * Sets the offset of the previous batch.
181 * @param i the previous offset
183 public void setPrevBatch(int i) {
188 * Returns the offset of the previous batch.
189 * @return offset of the previous batch
191 public int getPrevBatch() {
196 * Returns whether there is a previous batch.
197 * @return true if yes, false if no
199 public boolean hasPrevBatch() {
200 return (offsetprev >= 0);
204 * Returns the start index of the batch.
207 public int getFrom() {
212 * Returns the end index of the batch.
223 * Inserts an Entity into the EntityList.
224 * @param anEntity The entity to be inserted.
227 public void add (Entity anEntity) {
229 theEntityArrayList.add(anEntity);
231 logger.warn("EntityList: add called with empty Entity");
236 * @return The number of Entities in the EntityList.
240 return theEntityArrayList.size();
245 * Returns the element at position i in the EntityList as Entity
246 * @param i the position of the element in question
247 * @return The element at position i.
250 public Entity elementAt(int i) {
251 /** @todo check if i is in list.size() */
252 return (Entity)theEntityArrayList.get(i);
256 // The following methods have to be implemented
257 // for this class to be an implementation of the
258 // TemplateListModel of the Freemarker packages
260 public TemplateModel get(int i) { return elementAt(i); }
261 public boolean isRewound() { return (freemarkerListPointer==-1) ? true : false; }
262 public void rewind() { freemarkerListPointer=-1; }
264 public TemplateModel next() {
266 freemarkerListPointer++;return get(freemarkerListPointer); }
272 * Returns whether there is a next element
273 * @return true if there is a next element, else false
276 public boolean hasNext() {
277 return theEntityArrayList.size()>0 && freemarkerListPointer+2<=theEntityArrayList.size();
282 * Returns whether EntityList is empty or not
283 * @return true in case of empty list, false otherwise
286 public boolean isEmpty() {
287 if (theEntityArrayList!=null)
288 return theEntityArrayList.size()<1 ;
293 // Methods to implement StorableObject
295 public Set getNotifyOnReleaseSet() { return null; }
297 public StoreIdentifier getStoreIdentifier() {
298 if ( theStorage!=null ) {
300 new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,
301 StoreUtil.getEntityListUniqueIdentifierFor( theStorage.getTableName(),
302 whereClause, orderClause, offset, limit ));
304 logger.warn("EntityList could not return StoreIdentifier");