major cleanup:
[mir.git] / source / mir / entity / EntityList.java
1 /*\r
2  * Copyright (C) 2001, 2002 The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\r
6  * Mir is free software; you can redistribute it and/or modify\r
7  * it under the terms of the GNU General Public License as published by\r
8  * the Free Software Foundation; either version 2 of the License, or\r
9  * (at your option) any later version.\r
10  *\r
11  * Mir is distributed in the hope that it will be useful,\r
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14  * GNU General Public License for more details.\r
15  *\r
16  * You should have received a copy of the GNU General Public License\r
17  * along with Mir; if not, write to the Free Software\r
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
19  *\r
20  * In addition, as a special exception, The Mir-coders gives permission to link\r
21  * the code of this program with  any library licensed under the Apache Software License,\r
22  * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library\r
23  * (or with modified versions of the above that use the same license as the above),\r
24  * and distribute linked combinations including the two.  You must obey the\r
25  * GNU General Public License in all respects for all of the code used other than\r
26  * the above mentioned libraries.  If you modify this file, you may extend this\r
27  * exception to your version of the file, but you are not obligated to do so.\r
28  * If you do not wish to do so, delete this exception statement from your version.\r
29  */\r
30 package  mir.entity;\r
31 \r
32 import java.util.ArrayList;\r
33 import java.util.Set;\r
34 \r
35 import mir.config.MirPropertiesConfiguration;\r
36 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
37 import mir.log.LoggerWrapper;\r
38 import mir.storage.StorageObject;\r
39 import mir.storage.store.StorableObject;\r
40 import mir.storage.store.StoreContainerType;\r
41 import mir.storage.store.StoreIdentifier;\r
42 import mir.storage.store.StoreUtil;\r
43 \r
44 /**\r
45  *\r
46  * Container class for lists of Entities.\r
47  * Now implements @see mir.storage.store.StorableObject.\r
48  *\r
49  * @author <RK>\r
50  * first version        27.6.1999\r
51  *\r
52  * @version 1.1 (cleaned up)\r
53  */\r
54 public class EntityList implements StorableObject {\r
55   protected static MirPropertiesConfiguration configuration;\r
56   protected LoggerWrapper logger;\r
57   private ArrayList           theEntityArrayList = new ArrayList();\r
58   private String              whereClause, orderClause;\r
59   private StorageObject       theStorage;\r
60   private int                 count, offset, limit;\r
61   private int                 offsetnext = -1, offsetprev = -1;\r
62 \r
63   static {\r
64     try {\r
65       configuration = MirPropertiesConfiguration.instance();\r
66     }\r
67     catch (PropertiesConfigExc e) {\r
68       throw new RuntimeException("Unable to get configuration: " + e.getMessage());\r
69     }\r
70   }\r
71 \r
72   /**\r
73    * Constructor.\r
74    */\r
75   public EntityList(){\r
76     logger = new LoggerWrapper("Entity.List");\r
77   }\r
78 \r
79 /* get/set EntityClass of Objects stored in EntityList */\r
80   public void setStorage(StorageObject storage) { this.theStorage=storage; }\r
81   public StorageObject getStorage() { return theStorage; }\r
82 \r
83   public void setLimit(int limit) { this.limit = limit; }\r
84 \r
85   /**\r
86    * Sets the WHERE clause that fetched the Entities of this EntityList from the database.\r
87    * @param wc The string that contains the WHERE clause\r
88    */\r
89   public void setWhere(String wc) {\r
90     this.whereClause = wc;\r
91   }\r
92 \r
93   /**\r
94    * Returns the WHERE clause that returned this EntityList from the database\r
95    * @return whereClause The WHERE clause\r
96    */\r
97   public String getWhere() {\r
98     return whereClause;\r
99   }\r
100 \r
101 \r
102   /**\r
103    * Sets the sorting criterium of this EntityList\r
104    * @param oc\r
105    */\r
106   public void setOrder(String oc) {\r
107     this.orderClause = oc;\r
108   }\r
109 \r
110   /**\r
111    * Returns the sorting criterium.\r
112    * @return orderClause The sort order\r
113    */\r
114   public String getOrder() {\r
115     return orderClause;\r
116   }\r
117 \r
118   /**\r
119    * Sets the number of rows that match the WHERE clause\r
120    * @param i The number of rows that match the WHERE clause\r
121    */\r
122   public void setCount(int i) {\r
123     this.count = i;\r
124   }\r
125 \r
126   /**\r
127    * Returns the number of rows that match the WHERE clause\r
128    * @return The number of rows ...\r
129    */\r
130   public int getCount() {\r
131     return count;\r
132   }\r
133 \r
134   /**\r
135    * Sets the offset\r
136    * @param i The offset\r
137    */\r
138   public void setOffset(int i) {\r
139     offset = i;\r
140   }\r
141 \r
142   /**\r
143    * Returns the offset\r
144    * @return offset\r
145    */\r
146   public int getOffset() {\r
147     return offset;\r
148   }\r
149 \r
150   /**\r
151    * Sets the offset of the next batch of Entities.\r
152    * @param i The next offset\r
153    */\r
154   public void setNextBatch(int i) {\r
155     offsetnext = i;\r
156   }\r
157 \r
158   /**\r
159    * Returns the offset of the next batch of Entities.\r
160    * @return offset of the next batch\r
161    */\r
162   public int getNextBatch() {\r
163     return offsetnext;\r
164   }\r
165 \r
166   /**\r
167    * Returns whether there is a next batch within the WHERE clause\r
168    * @return true if yes, false if no.\r
169    */\r
170   public boolean hasNextBatch() {\r
171     return (offsetnext >= 0);\r
172   }\r
173 \r
174   /**\r
175    * Sets the offset of the previous batch.\r
176    * @param i the previous offset\r
177    */\r
178   public void setPrevBatch(int i) {\r
179     offsetprev = i;\r
180   }\r
181 \r
182   /**\r
183    * Returns the offset of the previous batch.\r
184    * @return offset of the previous batch\r
185    */\r
186   public int getPrevBatch() {\r
187     return offsetprev;\r
188   }\r
189 \r
190   /**\r
191    * Returns whether there is a previous batch.\r
192    * @return true if yes, false if no\r
193    */\r
194   public boolean hasPrevBatch() {\r
195     return (offsetprev >= 0);\r
196   }\r
197 \r
198   /**\r
199    * Returns the start index of the batch.\r
200    * @return\r
201    */\r
202   public int getFrom() {\r
203     return offset+1;\r
204   }\r
205 \r
206   /**\r
207    * Returns the end index of the batch.\r
208    * @return\r
209    */\r
210   public int getTo() {\r
211     if (hasNextBatch())\r
212       return offsetnext;\r
213     else\r
214       return count;\r
215   }\r
216 \r
217   /**\r
218    * Inserts an Entity into the EntityList.\r
219    * @param anEntity The entity to be inserted.\r
220    */\r
221 \r
222   public void add (Entity anEntity) {\r
223     if (anEntity!=null)\r
224       theEntityArrayList.add(anEntity);\r
225     else\r
226       logger.warn("EntityList: add called with empty Entity");\r
227   }\r
228 \r
229 \r
230   /**\r
231    * @return The number of Entities in the EntityList.\r
232    */\r
233 \r
234   public int size() {\r
235     return theEntityArrayList.size();\r
236   }\r
237 \r
238 \r
239   /**\r
240    * Returns the element at position i in the EntityList as Entity\r
241    * @param i the position of the element in question\r
242    * @return The element at position i.\r
243    */\r
244 \r
245   public Entity elementAt(int i) {\r
246     /** @todo check if i is in list.size() */\r
247     return (Entity)theEntityArrayList.get(i);\r
248   }\r
249 \r
250 \r
251 \r
252 // Methods to implement StorableObject\r
253 \r
254   public Set getNotifyOnReleaseSet() { return null; }\r
255 \r
256   public StoreIdentifier getStoreIdentifier() {\r
257     if ( theStorage!=null ) {\r
258       return\r
259       new StoreIdentifier( this, StoreContainerType.STOC_TYPE_ENTITYLIST,\r
260       StoreUtil.getEntityListUniqueIdentifierFor( theStorage.getTableName(),\r
261       whereClause, orderClause, offset, limit ));\r
262     }\r
263     logger.warn("EntityList could not return StoreIdentifier");\r
264     return null;\r
265   }\r
266 \r
267 }\r