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.
32 import java.util.Map;
\r
34 import mir.entity.Entity;
\r
35 import mir.entity.EntityList;
\r
36 import mir.storage.StorageObject;
40 * This class provides the base functionality for the derived Module-Classes.
41 * These classes should provide methods to make more or less complex actions
42 * on Database and Entity classes. The modules are used by ServletModules.
43 * Future possibility could be access via Applications.
45 * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
46 * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
47 * die von mehreren ServletModulen verwendet werden.
51 public class AbstractModule {
52 protected StorageObject theStorage;
54 public void setStorage(StorageObject storage) {
55 this.theStorage = storage;
59 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
60 * @return Standard-StorageObject
62 public StorageObject getStorageObject () {
67 * Holt eine Entity anhand der Id via StorageObject
68 * @param String der Entity
71 public Entity getById (String id) throws ModuleExc, ModuleFailure {
73 if (theStorage == null)
74 throw new ModuleExc("AbstractModule.getById: No StorageObject set!");
75 Entity entity = (Entity)theStorage.selectById(id);
78 throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
83 throw new ModuleFailure(e);
88 * Holt eine EntityListe anhand des WhereClause via StorageObject
89 * @param String whereclause
90 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
91 * @return EntityList Liste der gematchten Datens?tze
93 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
95 if (theStorage == null)
96 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
98 return theStorage.selectByWhereClause(whereClause, offset);
100 catch (Throwable e) {
101 throw new ModuleFailure(e);
106 * Holt eine EntityListe anhand des WhereClause aus dem StorageObject
107 * @param String where WhereClause
108 * @param String order Sortierreihenfolge
109 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
110 * @return EntityList Liste der gematchten Datens?tze
112 public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
114 if (theStorage==null)
115 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
117 return theStorage.selectByWhereClause(where, order, offset);
119 catch (Throwable e) {
120 throw new ModuleFailure(e);
124 * Executes a where clause on the StorageObject with order criteria
125 * fetching from offset the number of limit objects
127 * @param String where
128 * @param String order
134 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure {
136 if (theStorage==null)
137 throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
139 return theStorage.selectByWhereClause(where, order, offset, limit);
141 catch (Throwable e) {
142 throw new ModuleFailure(e);
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
153 public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {
155 whereClause = aField + " like '%" + aValue + "%'";
156 return getByWhereClause(whereClause, offset);
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 ModuleExc
164 * @exception ModuleFailure
166 public String add (Map theValues) throws ModuleExc, ModuleFailure {
168 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
169 theEntity.setStorage(theStorage);
170 theEntity.setValues(theValues);
171 return theEntity.insert();
173 catch (Throwable e) {
174 throw new ModuleFailure(e);
179 * This function creates an Entity without yet storing it in the database
184 * @throws ModuleFailure
186 public Entity createNew() throws ModuleExc, ModuleFailure {
188 Entity result = (Entity)theStorage.getEntityClass().newInstance();
189 result.setStorage(theStorage);
193 catch (Throwable e) {
194 throw new ModuleFailure(e);
199 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
200 * @param theValues Hash mit Spalte/Wert-Paaren
201 * @return Id des eingef?gten Objekts
202 * @exception ModuleExc
203 * @exception ModuleFailure
205 public String set (Map theValues) throws ModuleExc, ModuleFailure {
207 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
208 if (theEntity == null)
209 throw new ModuleExc("No object found with id " + theValues.get("id"));
210 theEntity.setValues(theValues);
212 return theEntity.getId();
214 catch (Throwable e) {
215 throw new ModuleFailure(e);
220 * Deletes a record using an id
222 * @exception ModuleExc
223 * @exception ModuleFailure
225 public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
227 theStorage.delete(idParam);
229 catch (Throwable e) {
230 throw new ModuleFailure(e);
235 * returns the number of rows
237 public int getSize(String where) throws ModuleExc, ModuleFailure {
239 return theStorage.getSize(where);
241 catch (Throwable e) {
242 throw new ModuleFailure(e);