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