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