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 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.
34 import mir.entity.Entity;
35 import mir.entity.EntityList;
36 import mir.storage.StorageObject;
37 import freemarker.template.SimpleHash;
41 * This class provides the base functionality for the derived Module-Classes.
42 * These classes should provide methods to make more or less complex actions
43 * on Database and Entity classes. The modules are used by ServletModules.
44 * Future possibility could be access via Applications.
46 * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
47 * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
48 * die von mehreren ServletModulen verwendet werden.
52 public class AbstractModule {
53 protected StorageObject theStorage;
55 public void setStorage(StorageObject storage) {
56 this.theStorage = storage;
60 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
61 * @return Standard-StorageObject
63 public StorageObject getStorageObject () {
68 * Holt eine Entity anhand der Id via StorageObject
69 * @param String der Entity
72 public Entity getById (String id) throws ModuleExc, ModuleFailure {
74 if (theStorage == null)
75 throw new ModuleExc("AbstractModule.getById: No StorageObject set!");
76 Entity entity = (Entity)theStorage.selectById(id);
79 throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
84 throw new ModuleFailure(e);
89 * Holt eine EntityListe anhand des WhereClause via StorageObject
90 * @param String whereclause
91 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
92 * @return EntityList Liste der gematchten Datens?tze
94 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
96 if (theStorage == null)
97 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
99 return theStorage.selectByWhereClause(whereClause, offset);
101 catch (Throwable e) {
102 throw new ModuleFailure(e);
107 * Holt eine EntityListe anhand des WhereClause aus dem StorageObject
108 * @param String where WhereClause
109 * @param String order Sortierreihenfolge
110 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
111 * @return EntityList Liste der gematchten Datens?tze
113 public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
115 if (theStorage==null)
116 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
118 return theStorage.selectByWhereClause(where, order, offset);
120 catch (Throwable e) {
121 throw new ModuleFailure(e);
125 * Executes a where clause on the StorageObject with order criteria
126 * fetching from offset the number of limit objects
128 * @param String where
129 * @param String order
135 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure {
137 if (theStorage==null)
138 throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
140 return theStorage.selectByWhereClause(where, order, offset, limit);
142 catch (Throwable e) {
143 throw new ModuleFailure(e);
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 ModuleExc, ModuleFailure {
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 ModuleExc
165 * @exception ModuleFailure
167 public String add (Map theValues) throws ModuleExc, ModuleFailure {
169 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
170 theEntity.setStorage(theStorage);
171 theEntity.setValues(theValues);
172 return theEntity.insert();
174 catch (Throwable e) {
175 throw new ModuleFailure(e);
180 * This function creates an Entity without yet storing it in the database
185 * @throws ModuleFailure
187 public Entity createNew() throws ModuleExc, ModuleFailure {
189 Entity result = (Entity)theStorage.getEntityClass().newInstance();
190 result.setStorage(theStorage);
194 catch (Throwable e) {
195 throw new ModuleFailure(e);
200 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
201 * @param theValues Hash mit Spalte/Wert-Paaren
202 * @return Id des eingef?gten Objekts
203 * @exception ModuleExc
204 * @exception ModuleFailure
206 public String set (Map theValues) throws ModuleExc, ModuleFailure {
208 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
209 if (theEntity == null)
210 throw new ModuleExc("No object found with id " + theValues.get("id"));
211 theEntity.setValues(theValues);
213 return theEntity.getId();
215 catch (Throwable e) {
216 throw new ModuleFailure(e);
221 * Deletes a record using an id
223 * @exception ModuleExc
224 * @exception ModuleFailure
226 public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
228 theStorage.delete(idParam);
230 catch (Throwable e) {
231 throw new ModuleFailure(e);
236 * Liefert den Lookuptable aller Objekte des StorageObjects
237 * @return freemarker.template.SimpleHash
239 public SimpleHash getHashData() {
240 return theStorage.getHashData();
244 * returns the number of rows
246 public int getSize(String where) throws ModuleExc, ModuleFailure {
248 return theStorage.getSize(where);
250 catch (Throwable e) {
251 throw new ModuleFailure(e);