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.
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.storage.StorageObject;
39 import freemarker.template.SimpleHash;
43 * This class provides the base functionality for the derived Module-Classes.
44 * These classes should provide methods to make more or less complex actions
45 * on Database and Entity classes. The modules are used by ServletModules.
46 * Future possibility could be access via Applications.
48 * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
49 * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
50 * die von mehreren ServletModulen verwendet werden.
54 public class AbstractModule {
55 protected StorageObject theStorage;
57 public void setStorage(StorageObject storage) {
58 this.theStorage = storage;
62 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
63 * @return Standard-StorageObject
65 public StorageObject getStorageObject () {
70 * Holt eine Entity anhand der Id via StorageObject
71 * @param String der Entity
74 public Entity getById (String id) throws ModuleExc, ModuleFailure {
76 if (theStorage == null)
77 throw new ModuleExc("AbstractModule.getById: No StorageObject set!");
78 Entity entity = (Entity)theStorage.selectById(id);
81 throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
86 throw new ModuleFailure(e);
91 * Holt eine EntityListe anhand des WhereClause via StorageObject
92 * @param String whereclause
93 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
94 * @return EntityList Liste der gematchten Datens?tze
96 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
98 if (theStorage == null)
99 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
101 return theStorage.selectByWhereClause(whereClause, offset);
103 catch (Throwable e) {
104 throw new ModuleFailure(e);
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
115 public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
117 if (theStorage==null)
118 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
120 return theStorage.selectByWhereClause(where, order, offset);
122 catch (Throwable e) {
123 throw new ModuleFailure(e);
127 * Executes a where clause on the StorageObject with order criteria
128 * fetching from offset the number of limit objects
130 * @param String where
131 * @param String order
137 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure {
139 if (theStorage==null)
140 throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
142 return theStorage.selectByWhereClause(where, order, offset, limit);
144 catch (Throwable e) {
145 throw new ModuleFailure(e);
150 * Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
151 * @param String aField - Feldname im StorageObject
152 * @param String aValue - Wert in Feld im StorageObject
153 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
154 * @return EntityList Liste der gematchten Datens?tze
156 public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {
158 whereClause = aField + " like '%" + aValue + "%'";
159 return getByWhereClause(whereClause, offset);
163 * * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen
164 * @param theValues Hash mit Spalte/Wert-Paaren
165 * @return Id des eingef?gten Objekts
166 * @exception ModuleExc
167 * @exception ModuleFailure
169 public String add (Map theValues) throws ModuleExc, ModuleFailure {
171 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
172 theEntity.setStorage(theStorage);
173 theEntity.setValues(theValues);
174 return theEntity.insert();
176 catch (Throwable e) {
177 throw new ModuleFailure(e);
182 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
183 * @param theValues Hash mit Spalte/Wert-Paaren
184 * @return Id des eingef?gten Objekts
185 * @exception ModuleExc
186 * @exception ModuleFailure
188 public String set (Map theValues) throws ModuleExc, ModuleFailure {
190 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
191 if (theEntity == null)
192 throw new ModuleExc("No object found with id " + theValues.get("id"));
193 theEntity.setValues(theValues);
195 return theEntity.getId();
197 catch (Throwable e) {
198 throw new ModuleFailure(e);
203 * Deletes a record using an id
205 * @exception ModuleExc
206 * @exception ModuleFailure
208 public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
210 theStorage.delete(idParam);
212 catch (Throwable e) {
213 throw new ModuleFailure(e);
218 * Liefert den Lookuptable aller Objekte des StorageObjects
219 * @return freemarker.template.SimpleHash
221 public SimpleHash getHashData() {
222 return theStorage.getHashData();
226 * returns the number of rows
228 public int getSize(String where) throws ModuleExc, ModuleFailure {
230 return theStorage.getSize(where);
232 catch (Throwable e) {
233 throw new ModuleFailure(e);