various fixes/cleanup: old producers are now completely gone, old logfile class too
[mir.git] / source / mir / entity / EntityList.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package  mir.entity;
33
34 import java.util.ArrayList;
35 import java.util.Set;
36
37 import freemarker.template.TemplateListModel;
38 import freemarker.template.TemplateModel;
39
40 import mir.log.LoggerWrapper;
41 import mir.config.MirPropertiesConfiguration;
42 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
43 import mir.storage.StorageObject;
44 import mir.storage.store.StorableObject;
45 import mir.storage.store.StoreContainerType;
46 import mir.storage.store.StoreIdentifier;
47 import mir.storage.store.StoreUtil;
48
49 /**
50  *
51  * Container class for lists of Entities.
52  * Now implements freemarker.template.TemplateListModel
53  * and @see mir.storage.store.StorableObject.
54  *
55  * @author <RK>
56  * first version        27.6.1999
57  *
58  *  @version 1.0 (freemarker compliant & and storable in ObjectStore)
59  */
60 public class EntityList implements TemplateListModel, StorableObject {
61   protected static MirPropertiesConfiguration configuration;
62   protected LoggerWrapper logger;
63   private ArrayList           theEntityArrayList = new ArrayList();
64   private String              whereClause, orderClause;
65   private StorageObject       theStorage;
66   private int                 count, offset, limit;
67   private int                 offsetnext = -1, offsetprev = -1;
68   private int                 freemarkerListPointer=-1;
69
70
71   static {
72     try {
73       configuration = MirPropertiesConfiguration.instance();
74     }
75     catch (PropertiesConfigExc e) {
76       throw new RuntimeException("Unable to get configuration: " + e.getMessage());
77     }
78   }
79
80   /**
81    * Constructor.
82    */
83   public EntityList(){
84     logger = new LoggerWrapper("Entity.List");
85   }
86
87 /* get/set EntityClass of Objects stored in EntityList */
88   public void setStorage(StorageObject storage) { this.theStorage=storage; }
89   public StorageObject getStorage() { return theStorage; }
90
91   public void setLimit(int limit) { this.limit = limit; }
92
93   /**
94    * Sets the WHERE clause that fetched the Entities of this EntityList from the database.
95    * @param wc The string that contains the WHERE clause
96    */
97   public void setWhere(String wc) {
98     this.whereClause = wc;
99   }
100
101   /**
102    * Returns the WHERE clause that returned this EntityList from the database
103    * @return whereClause The WHERE clause
104    */
105   public String getWhere() {
106     return whereClause;
107   }
108
109
110   /**
111    * Sets the sorting criterium of this EntityList
112    * @param oc
113    */
114   public void setOrder(String oc) {
115     this.orderClause = oc;
116   }
117
118   /**
119    * Returns the sorting criterium.
120    * @return orderClause The sort order
121    */
122   public String getOrder() {
123     return orderClause;
124   }
125
126   /**
127    * Sets the number of rows that match the WHERE clause
128    * @param i The number of rows that match the WHERE clause
129    */
130   public void setCount(int i) {
131     this.count = i;
132   }
133
134   /**
135    * Returns the number of rows that match the WHERE clause
136    * @return The number of rows ...
137    */
138   public int getCount() {
139     return count;
140   }
141
142   /**
143    * Sets the offset
144    * @param i The offset
145    */
146   public void setOffset(int i) {
147     offset = i;
148   }
149
150   /**
151    * Returns the offset
152    * @return offset
153    */
154   public int getOffset() {
155     return offset;
156   }
157
158   /**
159    * Sets the offset of the next batch of Entities.
160    * @param i The next offset
161    */
162   public void setNextBatch(int i) {
163     offsetnext = i;
164   }
165
166   /**
167    * Returns the offset of the next batch of Entities.
168    * @return offset of the next batch
169    */
170   public int getNextBatch() {
171     return offsetnext;
172   }
173
174   /**
175    * Returns whether there is a next batch within the WHERE clause
176    * @return true if yes, false if no.
177    */
178   public boolean hasNextBatch() {
179     return (offsetnext >= 0);
180   }
181
182   /**
183    * Sets the offset of the previous batch.
184    * @param i the previous offset
185    */
186   public void setPrevBatch(int i) {
187     offsetprev = i;
188   }
189
190   /**
191    * Returns the offset of the previous batch.
192    * @return offset of the previous batch
193    */
194   public int getPrevBatch() {
195     return offsetprev;
196   }
197
198   /**
199    * Returns whether there is a previous batch.
200    * @return true if yes, false if no
201    */
202   public boolean hasPrevBatch() {
203     return (offsetprev >= 0);
204   }
205
206   /**
207    * Returns the start index of the batch.
208    * @return
209    */
210   public int getFrom() {
211     return offset+1;
212   }
213
214   /**
215    * Returns the end index of the batch.
216    * @return
217    */
218   public int getTo() {
219     if (hasNextBatch())
220       return offsetnext;
221     else
222       return count;
223   }
224
225   /**
226    * Inserts an Entity into the EntityList.
227    * @param anEntity The entity to be inserted.
228    */
229
230   public void add (Entity anEntity) {
231     if (anEntity!=null)
232       theEntityArrayList.add(anEntity);
233     else
234       logger.warn("EntityList: add called with empty Entity");
235   }
236
237
238   /**
239    * @return The number of Entities in the EntityList.
240    */
241
242   public int size() {
243     return theEntityArrayList.size();
244   }
245
246
247   /**
248    * Returns the element at position i in the EntityList as Entity
249    * @param i the position of the element in question
250    * @return The element at position i.
251    */
252
253   public Entity elementAt(int i) {
254     /** @todo check if i is in list.size() */
255     return (Entity)theEntityArrayList.get(i);
256   }
257
258
259 // The following methods have to be implemented
260 // for this class to be an implementation of the
261 // TemplateListModel of the Freemarker packages
262
263   public TemplateModel get(int i) { return elementAt(i); }
264   public boolean isRewound() { return (freemarkerListPointer==-1) ? true : false; }
265   public void rewind() { freemarkerListPointer=-1; }
266
267   public TemplateModel next() {
268     if (hasNext()) {
269     freemarkerListPointer++;return get(freemarkerListPointer); }
270     else return null;
271   }
272
273
274   /**
275    * Returns whether there is a next element
276    * @return true if there is a next element, else false
277    */
278
279   public boolean hasNext() {
280     return theEntityArrayList.size()>0 && freemarkerListPointer+2<=theEntityArrayList.size();
281   }
282
283
284   /**
285    * Returns whether EntityList is empty or not
286    * @return true in case of empty list, false otherwise
287    */
288
289   public boolean isEmpty() {
290     if (theEntityArrayList!=null)
291       return theEntityArrayList.size()<1 ;
292     else return false;
293   }
294
295
296 // Methods to implement StorableObject
297
298   public Set getNotifyOnReleaseSet() { return null; }
299
300   public StoreIdentifier getStoreIdentifier() {
301     if ( theStorage!=null ) {
302       return
303       new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,
304       StoreUtil.getEntityListUniqueIdentifierFor( theStorage.getTableName(),
305       whereClause, orderClause, offset, limit ));
306     }
307     logger.warn("EntityList could not return StoreIdentifier");
308     return null;
309   }
310
311 }