Organizing import, refresh the license (zapata deleted cos.jar)
[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  any library licensed under the Apache Software License, 
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library 
23  * (or with modified versions of the above that use the same license as the above), 
24  * and distribute linked combinations including the two.  You must obey the 
25  * GNU General Public License in all respects for all of the code used other than 
26  * the above mentioned libraries.  If you modify this file, you may extend this 
27  * exception to your version of the file, but you are not obligated to do so.  
28  * If you do not wish to do so, delete this exception statement from your version.
29  */
30 package  mir.entity;
31
32 import java.util.ArrayList;
33 import java.util.Set;
34
35 import mir.config.MirPropertiesConfiguration;
36 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
37 import mir.log.LoggerWrapper;
38 import mir.storage.StorageObject;
39 import mir.storage.store.StorableObject;
40 import mir.storage.store.StoreContainerType;
41 import mir.storage.store.StoreIdentifier;
42 import mir.storage.store.StoreUtil;
43 import freemarker.template.TemplateListModel;
44 import freemarker.template.TemplateModel;
45
46 /**
47  *
48  * Container class for lists of Entities.
49  * Now implements freemarker.template.TemplateListModel
50  * and @see mir.storage.store.StorableObject.
51  *
52  * @author <RK>
53  * first version        27.6.1999
54  *
55  *  @version 1.0 (freemarker compliant & and storable in ObjectStore)
56  */
57 public class EntityList implements TemplateListModel, StorableObject {
58   protected static MirPropertiesConfiguration configuration;
59   protected LoggerWrapper logger;
60   private ArrayList           theEntityArrayList = new ArrayList();
61   private String              whereClause, orderClause;
62   private StorageObject       theStorage;
63   private int                 count, offset, limit;
64   private int                 offsetnext = -1, offsetprev = -1;
65   private int                 freemarkerListPointer=-1;
66
67
68   static {
69     try {
70       configuration = MirPropertiesConfiguration.instance();
71     }
72     catch (PropertiesConfigExc e) {
73       throw new RuntimeException("Unable to get configuration: " + e.getMessage());
74     }
75   }
76
77   /**
78    * Constructor.
79    */
80   public EntityList(){
81     logger = new LoggerWrapper("Entity.List");
82   }
83
84 /* get/set EntityClass of Objects stored in EntityList */
85   public void setStorage(StorageObject storage) { this.theStorage=storage; }
86   public StorageObject getStorage() { return theStorage; }
87
88   public void setLimit(int limit) { this.limit = limit; }
89
90   /**
91    * Sets the WHERE clause that fetched the Entities of this EntityList from the database.
92    * @param wc The string that contains the WHERE clause
93    */
94   public void setWhere(String wc) {
95     this.whereClause = wc;
96   }
97
98   /**
99    * Returns the WHERE clause that returned this EntityList from the database
100    * @return whereClause The WHERE clause
101    */
102   public String getWhere() {
103     return whereClause;
104   }
105
106
107   /**
108    * Sets the sorting criterium of this EntityList
109    * @param oc
110    */
111   public void setOrder(String oc) {
112     this.orderClause = oc;
113   }
114
115   /**
116    * Returns the sorting criterium.
117    * @return orderClause The sort order
118    */
119   public String getOrder() {
120     return orderClause;
121   }
122
123   /**
124    * Sets the number of rows that match the WHERE clause
125    * @param i The number of rows that match the WHERE clause
126    */
127   public void setCount(int i) {
128     this.count = i;
129   }
130
131   /**
132    * Returns the number of rows that match the WHERE clause
133    * @return The number of rows ...
134    */
135   public int getCount() {
136     return count;
137   }
138
139   /**
140    * Sets the offset
141    * @param i The offset
142    */
143   public void setOffset(int i) {
144     offset = i;
145   }
146
147   /**
148    * Returns the offset
149    * @return offset
150    */
151   public int getOffset() {
152     return offset;
153   }
154
155   /**
156    * Sets the offset of the next batch of Entities.
157    * @param i The next offset
158    */
159   public void setNextBatch(int i) {
160     offsetnext = i;
161   }
162
163   /**
164    * Returns the offset of the next batch of Entities.
165    * @return offset of the next batch
166    */
167   public int getNextBatch() {
168     return offsetnext;
169   }
170
171   /**
172    * Returns whether there is a next batch within the WHERE clause
173    * @return true if yes, false if no.
174    */
175   public boolean hasNextBatch() {
176     return (offsetnext >= 0);
177   }
178
179   /**
180    * Sets the offset of the previous batch.
181    * @param i the previous offset
182    */
183   public void setPrevBatch(int i) {
184     offsetprev = i;
185   }
186
187   /**
188    * Returns the offset of the previous batch.
189    * @return offset of the previous batch
190    */
191   public int getPrevBatch() {
192     return offsetprev;
193   }
194
195   /**
196    * Returns whether there is a previous batch.
197    * @return true if yes, false if no
198    */
199   public boolean hasPrevBatch() {
200     return (offsetprev >= 0);
201   }
202
203   /**
204    * Returns the start index of the batch.
205    * @return
206    */
207   public int getFrom() {
208     return offset+1;
209   }
210
211   /**
212    * Returns the end index of the batch.
213    * @return
214    */
215   public int getTo() {
216     if (hasNextBatch())
217       return offsetnext;
218     else
219       return count;
220   }
221
222   /**
223    * Inserts an Entity into the EntityList.
224    * @param anEntity The entity to be inserted.
225    */
226
227   public void add (Entity anEntity) {
228     if (anEntity!=null)
229       theEntityArrayList.add(anEntity);
230     else
231       logger.warn("EntityList: add called with empty Entity");
232   }
233
234
235   /**
236    * @return The number of Entities in the EntityList.
237    */
238
239   public int size() {
240     return theEntityArrayList.size();
241   }
242
243
244   /**
245    * Returns the element at position i in the EntityList as Entity
246    * @param i the position of the element in question
247    * @return The element at position i.
248    */
249
250   public Entity elementAt(int i) {
251     /** @todo check if i is in list.size() */
252     return (Entity)theEntityArrayList.get(i);
253   }
254
255
256 // The following methods have to be implemented
257 // for this class to be an implementation of the
258 // TemplateListModel of the Freemarker packages
259
260   public TemplateModel get(int i) { return elementAt(i); }
261   public boolean isRewound() { return (freemarkerListPointer==-1) ? true : false; }
262   public void rewind() { freemarkerListPointer=-1; }
263
264   public TemplateModel next() {
265     if (hasNext()) {
266     freemarkerListPointer++;return get(freemarkerListPointer); }
267     else return null;
268   }
269
270
271   /**
272    * Returns whether there is a next element
273    * @return true if there is a next element, else false
274    */
275
276   public boolean hasNext() {
277     return theEntityArrayList.size()>0 && freemarkerListPointer+2<=theEntityArrayList.size();
278   }
279
280
281   /**
282    * Returns whether EntityList is empty or not
283    * @return true in case of empty list, false otherwise
284    */
285
286   public boolean isEmpty() {
287     if (theEntityArrayList!=null)
288       return theEntityArrayList.size()<1 ;
289     else return false;
290   }
291
292
293 // Methods to implement StorableObject
294
295   public Set getNotifyOnReleaseSet() { return null; }
296
297   public StoreIdentifier getStoreIdentifier() {
298     if ( theStorage!=null ) {
299       return
300       new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,
301       StoreUtil.getEntityListUniqueIdentifierFor( theStorage.getTableName(),
302       whereClause, orderClause, offset, limit ));
303     }
304     logger.warn("EntityList could not return StoreIdentifier");
305     return null;
306   }
307
308 }