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