some small changes delting unneeded imports. two new exceptions in mir.storage. usage...
[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.HashMap;
36
37 import mir.entity.Entity;
38 import mir.entity.EntityList;
39 import mir.storage.StorageObject;
40 import mir.storage.StorageObjectExc;
41 import mir.storage.StorageObjectFailure;
42 import freemarker.template.SimpleHash;
43
44
45 /**
46  * This class provides the base functionality for the derived Module-Classes.
47  * These classes should provide methods to make more or less complex actions
48  * on Database and Entity classes. The modules are used by ServletModules.
49  * Future possibility could be access via Applications.
50  *
51  * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
52  * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
53  * die von mehreren ServletModulen verwendet werden.
54  *
55  */
56
57 public class AbstractModule {
58   protected StorageObject theStorage;
59
60   public void setStorage(StorageObject storage) {
61     this.theStorage = storage;
62   }
63
64   /**
65    * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
66    * @return Standard-StorageObject
67    */
68   public StorageObject getStorageObject () {
69     return theStorage;
70   }
71
72   /**
73    *   Holt eine Entity anhand der Id via StorageObject
74    *   @param String der Entity
75    *   @return Entity
76    */
77   public Entity getById (String id) throws ModuleException {
78     try {
79       if (theStorage == null)
80         throw  new ModuleException("No StorageObject set!");
81       Entity entity = (Entity)theStorage.selectById(id);
82       if (entity == null)
83         throw new ModuleException("No object for id = " + id);
84       else return entity;
85     }
86     catch (StorageObjectExc e){
87       throw new ModuleException(e.toString());
88     }
89   }
90
91   /**
92    *   Holt eine EntityListe anhand des WhereClause via StorageObject
93    *   @param String whereclause
94    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
95    *   @return EntityList Liste der gematchten Datens?tze
96    */
97   public EntityList getByWhereClause (String whereClause, int offset) throws ModuleException {
98     try {
99       if (theStorage == null)
100         throw  new ModuleException("Kein StorageObject gesetzt");
101       return theStorage.selectByWhereClause(whereClause, offset);
102     }
103     catch (StorageObjectFailure e){
104       throw new ModuleException(e.toString());
105     }
106   }
107
108   /**
109    *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject
110    *   @param String where WhereClause
111    *   @param String order Sortierreihenfolge
112    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
113    *   @return EntityList Liste der gematchten Datens?tze
114    */
115   public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
116     try {
117       if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
118       return theStorage.selectByWhereClause(where, order, offset);
119     }
120     catch (StorageObjectFailure e){
121       throw new ModuleException(e.toString());
122     }
123   }
124   /**
125    *   Executes a where clause on the StorageObject with order criteria
126    *   fetching from offset the number of limit objects
127    *
128    *   @param String where
129    *   @param String order
130    *   @param int offset
131    *   @param int limit
132    *   @return EntityList
133    */
134
135   public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
136   {
137     try {
138       if (theStorage==null) throw new ModuleException("StorageObject not set!");
139       return theStorage.selectByWhereClause(where, order, offset, limit);
140     }
141     catch (StorageObjectFailure e){
142       throw new ModuleException(e.toString());
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 ModuleException {
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 ModuleException
164    */
165   public String add (HashMap theValues) throws ModuleException {
166     try {
167       Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
168       theEntity.setStorage(theStorage);
169       theEntity.setValues(theValues);
170       return theEntity.insert();
171     } catch (Exception e) {
172       throw new ModuleException(e.toString());
173     }
174   }
175
176   /**
177    * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
178    * @param theValues Hash mit Spalte/Wert-Paaren
179    * @return Id des eingef?gten Objekts
180    * @exception ModuleException
181    */
182   public String set (HashMap theValues) throws ModuleException {
183     try {
184       Entity theEntity = theStorage.selectById((String)theValues.get("id"));
185       if (theEntity == null)
186         throw new ModuleException("Kein Objekt mit id in Datenbank id: " + theValues.get("id"));
187       theEntity.setValues(theValues);
188       theEntity.update();
189       return theEntity.getId();
190     }
191     catch (StorageObjectExc e){
192       e.printStackTrace(System.err);
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 }