9aca5c78edf25f2c1c400763dd57b5f5a8d0b685
[mir.git] / source / mir / entity / EntityList.java
1 /*
2  * The former (German) documentation of this classe
3  * stated that this class is an abstract one. There is,
4  * however, not a single abstract method in this class.
5  */
6
7
8 package  mir.entity;
9
10 import  java.lang.*;
11 import  java.util.*;
12
13 import  freemarker.template.*;
14
15 import  mir.misc.*;
16 import  mir.storage.store.*;
17
18
19 /**
20  *
21  * Container class for lists of Entities.
22  * Now implements freemarker.template.TemplateListModel
23  * and @see mir.storage.store.StorableObject.
24  *
25  * @author <RK>
26  * first version        27.6.1999
27  *
28  *  @version 1.0 (freemarker compliant & and storable in ObjectStore)
29  */
30 public class EntityList implements TemplateListModel, StorableObject {
31
32   private static Logfile      theLog;
33   private ArrayList           theEntityArrayList = new ArrayList();
34   private String              whereClause, orderClause;
35   private Class               theEntityClass;
36   private int                 count, offset, limit;
37   private int                 offsetnext = -1, offsetprev = -1;
38   private int                 freemarkerListPointer=-1;
39
40
41   static {
42     theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
43   }
44
45         /**
46          * Constructor.
47          */
48   public EntityList(){          }
49
50   /* get/set EntityClass of Objects stored in EntityList */
51   public void setEntityClass(Class theEntityClass) { this.theEntityClass=theEntityClass; }
52   public Class getEntityClass() { return theEntityClass; }
53
54   public void setLimit(int limit) { this.limit = limit; }
55
56         /**
57          * Sets the WHERE clause that fetched the Entities of this EntityList from the database.
58          * @param wc The string that contains the WHERE clause
59          */
60                 public void setWhere(String wc) {
61                         this.whereClause = wc;
62                 }
63
64         /**
65          * Returns the WHERE clause that returned this EntityList from the database
66          * @return whereClause The WHERE clause
67          */
68                 public String getWhere() {
69                         return whereClause;
70                 }
71
72
73         /**
74          * Sets the sorting criterium of this EntityList
75          * @param oc
76          */
77                 public void setOrder(String oc) {
78                         this.orderClause = oc;
79                 }
80
81         /**
82          * Returns the sorting criterium.
83          * @return orderClause The sort order
84          */
85                 public String getOrder() {
86                         return orderClause;
87                 }
88
89         /**
90          * Sets the number of rows that match the WHERE clause
91          * @param i The number of rows that match the WHERE clause
92          */
93                 public void setCount(int i) {
94                         this.count = i;
95                 }
96
97         /**
98          * Returns the number of rows that match the WHERE clause
99          * @return The number of rows ...
100          */
101                 public int getCount() {
102                         return count;
103                 }
104
105         /**
106          * Sets the offset
107          * @param i The offset
108          */
109                 public void setOffset(int i) {
110                         offset = i;
111                 }
112
113         /**
114          * Returns the offset
115          * @return offset
116          */
117                 public int getOffset() {
118                         return offset;
119                 }
120
121         /**
122          * Sets the offset of the next batch of Entities.
123          * @param i The next offset
124          */
125                 public void setNextBatch(int i) {
126                         offsetnext = i;
127                 }
128
129         /**
130          * Returns the offset of the next batch of Entities.
131          * @return offset of the next batch
132          */
133                 public int getNextBatch() {
134                         return offsetnext;
135                 }
136
137         /**
138          * Returns whether there is a next batch within the WHERE clause
139          * @return true if yes, false if no.
140          */
141                 public boolean hasNextBatch() {
142                         return (offsetnext >= 0);
143                 }
144
145         /**
146          * Sets the offset of the previous batch.
147          * @param i the previous offset
148          */
149                 public void setPrevBatch(int i) {
150                         offsetprev = i;
151                 }
152
153         /**
154          * Returns the offset of the previous batch.
155          * @return offset of the previous batch
156          */
157                 public int getPrevBatch() {
158                         return offsetprev;
159                 }
160
161         /**
162          * Returns whether there is a previous batch.
163          * @return true if yes, false if no
164          */
165                 public boolean hasPrevBatch() {
166                         return (offsetprev >= 0);
167                 }
168
169         /**
170          * Returns the start index of the batch.
171          * @return
172          */
173                 public int getFrom() {
174                         return offset+1;
175                 }
176
177         /**
178          * Returns the end index of the batch.
179          * @return
180          */
181                 public int getTo() {
182                         if (hasNextBatch())
183                                 return offsetnext;
184                         else
185                                 return count;
186                 }
187
188   /**
189    * Inserts an Entity into the EntityList.
190    * @param anEntity The entity to be inserted.
191    */
192
193   public void add (Entity anEntity) {
194     if (anEntity!=null)
195         theEntityArrayList.add(anEntity);
196     else
197         theLog.printWarning("EntityList: add called with empty Entity");
198   }
199
200
201   /**
202    * @return The number of Entities in the EntityList.
203    */
204
205   public int size() {
206     return theEntityArrayList.size();
207   }
208
209
210   /**
211    * Returns the element at position i in the EntityList as Entity
212    * @param i the position of the element in question
213    * @return The element at position i.
214    */
215
216   public Entity elementAt(int i) {
217     /** @todo check if i is in list.size() */
218     return (Entity)theEntityArrayList.get(i);
219   }
220
221
222   // The following methods have to be implemented
223   // for this class to be an implementation of the
224   // TemplateListModel of the Freemarker packages
225
226   public TemplateModel get(int i) { return elementAt(i); }
227   public boolean isRewound() { return (freemarkerListPointer==-1) ? true : false; }
228   public void rewind() { freemarkerListPointer=-1; }
229
230   public TemplateModel next() {
231     if (hasNext()) {
232       freemarkerListPointer++;return get(freemarkerListPointer); }
233     else return null;
234   }
235
236
237   /**
238    * Returns whether there is a next element
239    * @return true if there is a next element, else false
240    */
241
242   public boolean hasNext() {
243     return theEntityArrayList.size()>0 && freemarkerListPointer+2<=theEntityArrayList.size();
244   }
245
246
247   /**
248    * Returns whether EntityList is empty or not
249    * @return true in case of empty list, false otherwise
250    */
251
252   public boolean isEmpty() {
253     if (theEntityArrayList!=null)
254       return theEntityArrayList.size()<1 ;
255     else return false;
256   }
257
258
259   // Methods to implement StorableObject
260
261   public Set getNotifyOnReleaseSet() { return null; }
262
263   public StoreIdentifier getStoreIdentifier() {
264     if ( theEntityClass!=null ) {
265       return
266         new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,
267             StoreUtil.getEntityListUniqueIdentifierFor( whereClause, orderClause, offset, limit ));
268     }
269     theLog.printWarning("EntityList could not return StoreIdentifier");
270     return null;
271   }
272
273 }