2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
34 import java.sql.SQLException;
37 import freemarker.template.SimpleHash;
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;
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.
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.
58 public class AbstractModule {
59 protected StorageObject theStorage;
61 public void setStorage(StorageObject storage) {
62 this.theStorage = storage;
66 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
67 * @return Standard-StorageObject
69 public StorageObject getStorageObject () {
74 * Holt eine Entity anhand der Id via StorageObject
75 * @param String der Entity
78 public Entity getById (String id) throws ModuleException {
80 if (theStorage == null)
81 throw new ModuleException("No StorageObject set!");
82 Entity entity = (Entity)theStorage.selectById(id);
84 throw new ModuleException("No object for id = " + id);
87 catch (StorageObjectExc e){
88 throw new ModuleException(e.toString());
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
98 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleException {
100 if (theStorage == null)
101 throw new ModuleException("Kein StorageObject gesetzt");
102 return theStorage.selectByWhereClause(whereClause, offset);
104 catch (StorageObjectFailure e){
105 throw new ModuleException(e.toString());
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
116 public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
118 if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
119 return theStorage.selectByWhereClause(where, order, offset);
121 catch (StorageObjectFailure e){
122 throw new ModuleException(e.toString());
126 * Executes a where clause on the StorageObject with order criteria
127 * fetching from offset the number of limit objects
129 * @param String where
130 * @param String order
136 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
139 if (theStorage==null) throw new ModuleException("StorageObject not set!");
140 return theStorage.selectByWhereClause(where, order, offset, limit);
142 catch (StorageObjectFailure e){
143 throw new ModuleException(e.toString());
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
154 public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
156 whereClause = aField + " like '%" + aValue + "%'";
157 return getByWhereClause(whereClause, offset);
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
166 public String add (Map theValues) throws ModuleException {
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());
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
183 public String set (Map theValues) throws ModuleException {
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);
190 return theEntity.getId();
192 catch (StorageObjectExc e){
193 throw new ModuleException(e.toString());
198 * Deletes a record using an id
200 * @exception ModuleException
202 public void deleteById (String idParam) throws ModuleException {
204 theStorage.delete(idParam);
205 } catch (StorageObjectFailure e){
206 throw new ModuleException(e.toString());
211 * Liefert den Lookuptable aller Objekte des StorageObjects
212 * @return freemarker.template.SimpleHash
214 public SimpleHash getHashData() {
215 return theStorage.getHashData();
219 * returns the number of rows
221 public int getSize(String where)
222 throws SQLException,StorageObjectFailure {
223 return theStorage.getSize(where);