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