2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
34 import java.sql.SQLException;
\r
35 import java.util.Map;
\r
37 import mir.entity.Entity;
\r
38 import mir.entity.EntityList;
\r
39 import mir.storage.StorageObject;
\r
40 import mir.storage.StorageObjectExc;
\r
41 import mir.storage.StorageObjectFailure;
\r
42 import freemarker.template.SimpleHash;
\r
46 * This class provides the base functionality for the derived Module-Classes.
\r
47 * These classes should provide methods to make more or less complex actions
\r
48 * on Database and Entity classes. The modules are used by ServletModules.
\r
49 * Future possibility could be access via Applications.
\r
51 * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
\r
52 * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
\r
53 * die von mehreren ServletModulen verwendet werden.
\r
57 public class AbstractModule {
\r
58 protected StorageObject theStorage;
\r
60 public void setStorage(StorageObject storage) {
\r
61 this.theStorage = storage;
\r
65 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
\r
66 * @return Standard-StorageObject
\r
68 public StorageObject getStorageObject () {
\r
73 * Holt eine Entity anhand der Id via StorageObject
\r
74 * @param String der Entity
\r
77 public Entity getById (String id) throws ModuleExc, ModuleFailure {
\r
79 if (theStorage == null)
\r
80 throw new ModuleExc("AbstractModule.getById: No StorageObject set!");
\r
81 Entity entity = (Entity)theStorage.selectById(id);
\r
84 throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
\r
88 catch (Throwable e) {
\r
89 throw new ModuleFailure(e);
\r
94 * Holt eine EntityListe anhand des WhereClause via StorageObject
\r
95 * @param String whereclause
\r
96 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
\r
97 * @return EntityList Liste der gematchten Datens?tze
\r
99 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
\r
101 if (theStorage == null)
\r
102 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
\r
104 return theStorage.selectByWhereClause(whereClause, offset);
\r
106 catch (Throwable e) {
\r
107 throw new ModuleFailure(e);
\r
112 * Holt eine EntityListe anhand des WhereClause aus dem StorageObject
\r
113 * @param String where WhereClause
\r
114 * @param String order Sortierreihenfolge
\r
115 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
\r
116 * @return EntityList Liste der gematchten Datens?tze
\r
118 public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
\r
120 if (theStorage==null)
\r
121 throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
\r
123 return theStorage.selectByWhereClause(where, order, offset);
\r
125 catch (Throwable e) {
\r
126 throw new ModuleFailure(e);
\r
130 * Executes a where clause on the StorageObject with order criteria
\r
131 * fetching from offset the number of limit objects
\r
133 * @param String where
\r
134 * @param String order
\r
135 * @param int offset
\r
137 * @return EntityList
\r
140 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure {
\r
142 if (theStorage==null)
\r
143 throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
\r
145 return theStorage.selectByWhereClause(where, order, offset, limit);
\r
147 catch (Throwable e) {
\r
148 throw new ModuleFailure(e);
\r
153 * Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
\r
154 * @param String aField - Feldname im StorageObject
\r
155 * @param String aValue - Wert in Feld im StorageObject
\r
156 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
\r
157 * @return EntityList Liste der gematchten Datens?tze
\r
159 public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {
\r
160 String whereClause;
\r
161 whereClause = aField + " like '%" + aValue + "%'";
\r
162 return getByWhereClause(whereClause, offset);
\r
166 * * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen
\r
167 * @param theValues Hash mit Spalte/Wert-Paaren
\r
168 * @return Id des eingef?gten Objekts
\r
169 * @exception ModuleExc
\r
170 * @exception ModuleFailure
\r
172 public String add (Map theValues) throws ModuleExc, ModuleFailure {
\r
174 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
\r
175 theEntity.setStorage(theStorage);
\r
176 theEntity.setValues(theValues);
\r
177 return theEntity.insert();
\r
179 catch (Throwable e) {
\r
180 throw new ModuleFailure(e);
\r
185 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
\r
186 * @param theValues Hash mit Spalte/Wert-Paaren
\r
187 * @return Id des eingef?gten Objekts
\r
188 * @exception ModuleExc
\r
189 * @exception ModuleFailure
\r
191 public String set (Map theValues) throws ModuleExc, ModuleFailure {
\r
193 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
\r
194 if (theEntity == null)
\r
195 throw new ModuleExc("No object found with id " + theValues.get("id"));
\r
196 theEntity.setValues(theValues);
\r
197 theEntity.update();
\r
198 return theEntity.getId();
\r
200 catch (Throwable e) {
\r
201 throw new ModuleFailure(e);
\r
206 * Deletes a record using an id
\r
208 * @exception ModuleExc
\r
209 * @exception ModuleFailure
\r
211 public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
\r
213 theStorage.delete(idParam);
\r
215 catch (Throwable e) {
\r
216 throw new ModuleFailure(e);
\r
221 * Liefert den Lookuptable aller Objekte des StorageObjects
\r
222 * @return freemarker.template.SimpleHash
\r
224 public SimpleHash getHashData() {
\r
225 return theStorage.getHashData();
\r
229 * returns the number of rows
\r
231 public int getSize(String where) throws ModuleExc, ModuleFailure {
\r
233 return theStorage.getSize(where);
\r
235 catch (Throwable e) {
\r
236 throw new ModuleFailure(e);
\r