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