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