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