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