5f16a227c127e1d77282a5452355e77edb5d916c
[mir.git] / source / mir / module / AbstractModule.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 the com.oreilly.servlet library, any library\r
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
24  * the above that use the same license as the above), and distribute linked\r
25  * combinations including the two.  You must obey the GNU General Public\r
26  * License in all respects for all of the code used other than the above\r
27  * mentioned libraries.  If you modify this file, you may extend this exception\r
28  * to your version of the file, but you are not obligated to do so.  If you do\r
29  * not wish to do so, delete this exception statement from your version.\r
30  */\r
31 \r
32 package  mir.module;\r
33 \r
34 import java.sql.SQLException;\r
35 import java.util.Map;\r
36 \r
37 import mir.entity.Entity;\r
38 import mir.entity.EntityList;\r
39 import mir.storage.StorageObject;\r
40 import mir.storage.StorageObjectExc;\r
41 import mir.storage.StorageObjectFailure;\r
42 import freemarker.template.SimpleHash;\r
43 \r
44 \r
45 /**\r
46  * This class provides the base functionality for the derived Module-Classes.\r
47  * These classes should provide methods to make more or less complex actions\r
48  * on Database and Entity classes. The modules are used by ServletModules.\r
49  * Future possibility could be access via Applications.\r
50  *\r
51  * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.\r
52  * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,\r
53  * die von mehreren ServletModulen verwendet werden.\r
54  *\r
55  */\r
56 \r
57 public class AbstractModule {\r
58   protected StorageObject theStorage;\r
59 \r
60   public void setStorage(StorageObject storage) {\r
61     this.theStorage = storage;\r
62   }\r
63 \r
64   /**\r
65    * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.\r
66    * @return Standard-StorageObject\r
67    */\r
68   public StorageObject getStorageObject () {\r
69     return theStorage;\r
70   }\r
71 \r
72   /**\r
73    *   Holt eine Entity anhand der Id via StorageObject\r
74    *   @param String der Entity\r
75    *   @return Entity\r
76    */\r
77   public Entity getById (String id) throws ModuleExc, ModuleFailure {\r
78     try {\r
79       if (theStorage == null)\r
80         throw  new ModuleExc("AbstractModule.getById: No StorageObject set!");\r
81       Entity entity = (Entity)theStorage.selectById(id);\r
82 \r
83       if (entity == null)\r
84         throw new ModuleExc("AbstractModule.getById: No object for id = " + id);\r
85       else\r
86         return entity;\r
87     }\r
88     catch (Throwable e) {\r
89       throw new ModuleFailure(e);\r
90     }\r
91   }\r
92 \r
93   /**\r
94    *   Holt eine EntityListe anhand des WhereClause via StorageObject\r
95    *   @param String whereclause\r
96    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
97    *   @return EntityList Liste der gematchten Datens?tze\r
98    */\r
99   public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {\r
100     try {\r
101       if (theStorage == null)\r
102         throw  new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");\r
103 \r
104       return theStorage.selectByWhereClause(whereClause, offset);\r
105     }\r
106     catch (Throwable e) {\r
107       throw new ModuleFailure(e);\r
108     }\r
109   }\r
110 \r
111   /**\r
112    *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject\r
113    *   @param String where WhereClause\r
114    *   @param String order Sortierreihenfolge\r
115    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
116    *   @return EntityList Liste der gematchten Datens?tze\r
117    */\r
118   public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {\r
119     try {\r
120       if (theStorage==null)\r
121         throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");\r
122 \r
123       return theStorage.selectByWhereClause(where, order, offset);\r
124     }\r
125     catch (Throwable e) {\r
126       throw new ModuleFailure(e);\r
127     }\r
128   }\r
129   /**\r
130    *   Executes a where clause on the StorageObject with order criteria\r
131    *   fetching from offset the number of limit objects\r
132    *\r
133    *   @param String where\r
134    *   @param String order\r
135    *   @param int offset\r
136    *   @param int limit\r
137    *   @return EntityList\r
138    */\r
139 \r
140   public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure   {\r
141     try {\r
142       if (theStorage==null)\r
143         throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");\r
144 \r
145       return theStorage.selectByWhereClause(where, order, offset, limit);\r
146     }\r
147     catch (Throwable e) {\r
148       throw new ModuleFailure(e);\r
149     }\r
150   }\r
151 \r
152   /**\r
153    *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject\r
154    *   @param String aField - Feldname im StorageObject\r
155    *   @param String aValue - Wert in Feld im StorageObject\r
156    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden\r
157    *   @return EntityList Liste der gematchten Datens?tze\r
158    */\r
159   public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {\r
160     String whereClause;\r
161     whereClause = aField + " like '%" + aValue + "%'";\r
162     return getByWhereClause(whereClause, offset);\r
163   }\r
164 \r
165   /**\r
166    *    * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen\r
167    * @param theValues Hash mit Spalte/Wert-Paaren\r
168    * @return Id des eingef?gten Objekts\r
169    * @exception ModuleExc\r
170    * @exception ModuleFailure\r
171    */\r
172   public String add (Map theValues) throws ModuleExc, ModuleFailure {\r
173     try {\r
174       Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();\r
175       theEntity.setStorage(theStorage);\r
176       theEntity.setValues(theValues);\r
177       return theEntity.insert();\r
178     }\r
179     catch (Throwable e) {\r
180       throw new ModuleFailure(e);\r
181     }\r
182   }\r
183 \r
184   /**\r
185    * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren\r
186    * @param theValues Hash mit Spalte/Wert-Paaren\r
187    * @return Id des eingef?gten Objekts\r
188    * @exception ModuleExc\r
189    * @exception ModuleFailure\r
190    */\r
191   public String set (Map theValues) throws ModuleExc, ModuleFailure {\r
192     try {\r
193       Entity theEntity = theStorage.selectById((String)theValues.get("id"));\r
194       if (theEntity == null)\r
195         throw new ModuleExc("No object found with id " + theValues.get("id"));\r
196       theEntity.setValues(theValues);\r
197       theEntity.update();\r
198       return theEntity.getId();\r
199     }\r
200     catch (Throwable e) {\r
201       throw new ModuleFailure(e);\r
202     }\r
203   }\r
204 \r
205   /**\r
206    * Deletes a record using an id\r
207    * @param idParam\r
208    * @exception ModuleExc\r
209    * @exception ModuleFailure\r
210    */\r
211   public void deleteById (String idParam) throws ModuleExc, ModuleFailure {\r
212     try {\r
213       theStorage.delete(idParam);\r
214     }\r
215     catch (Throwable e) {\r
216       throw new ModuleFailure(e);\r
217     }\r
218   }\r
219 \r
220   /**\r
221    * Liefert den Lookuptable aller Objekte des StorageObjects\r
222    * @return freemarker.template.SimpleHash\r
223    */\r
224   public SimpleHash getHashData() {\r
225     return theStorage.getHashData();\r
226   }\r
227 \r
228   /**\r
229    * returns the number of rows\r
230    */\r
231   public int getSize(String where) throws ModuleExc, ModuleFailure {\r
232     try {\r
233       return theStorage.getSize(where);\r
234     }\r
235     catch (Throwable e) {\r
236       throw new ModuleFailure(e);\r
237     }\r
238   }\r
239 \r
240 }\r