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 freemarker.template.*;
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 ModuleException {
76 if (theStorage == null)
77 throw new ModuleException("No StorageObject set!");
78 Entity entity = (Entity)theStorage.selectById(id);
80 throw new ModuleException("No object for id = " + id);
83 catch (StorageObjectException e){
84 throw new ModuleException(e.toString());
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 ModuleException {
96 if (theStorage == null)
97 throw new ModuleException("Kein StorageObject gesetzt");
98 return theStorage.selectByWhereClause(whereClause, offset);
100 catch (StorageObjectException e){
101 throw new ModuleException(e.toString());
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 ModuleException {
114 if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
115 return theStorage.selectByWhereClause(where, order, offset);
117 catch (StorageObjectException e){
118 throw new ModuleException(e.toString());
122 * Executes a where clause on the StorageObject with order criteria
123 * fetching from offset the number of limit objects
125 * @param String where
126 * @param String order
132 public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
135 if (theStorage==null) throw new ModuleException("StorageObject not set!");
136 return theStorage.selectByWhereClause(where, order, offset, limit);
138 catch (StorageObjectException e){
139 throw new ModuleException(e.toString());
144 * Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
145 * @param String aField - Feldname im StorageObject
146 * @param String aValue - Wert in Feld im StorageObject
147 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
148 * @return EntityList Liste der gematchten Datensätze
150 public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
152 whereClause = aField + " like '%" + aValue + "%'";
153 return getByWhereClause(whereClause, offset);
157 * Standardfunktion, um einen Datensatz via StorageObject einzufügen
158 * @param theValues Hash mit Spalte/Wert-Paaren
159 * @return Id des eingefügten Objekts
160 * @exception ModuleException
162 public String add (HashMap theValues) throws ModuleException {
164 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
165 theEntity.setStorage(theStorage);
166 theEntity.setValues(theValues);
167 return theEntity.insert();
168 } catch (Exception e) {
169 throw new ModuleException(e.toString());
174 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
175 * @param theValues Hash mit Spalte/Wert-Paaren
176 * @return Id des eingefügten Objekts
177 * @exception ModuleException
179 public String set (HashMap theValues) throws ModuleException {
181 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
182 if (theEntity == null)
183 throw new ModuleException("Kein Objekt mit id in Datenbank id: " + theValues.get("id"));
184 theEntity.setValues(theValues);
186 return theEntity.getId();
188 catch (StorageObjectException e){
189 e.printStackTrace(System.err);
190 throw new ModuleException(e.toString());
195 * Löscht einen Datensatz anhand seiner Id
197 * @exception ModuleException
199 public void deleteById (String idParam) throws ModuleException {
201 theStorage.delete(idParam);
202 } catch (StorageObjectException e){
203 throw new ModuleException(e.toString());
208 * Liefert den Lookuptable aller Objekte des StorageObjects
209 * @return freemarker.template.SimpleHash
211 public SimpleHash getHashData() {
212 return theStorage.getHashData();
216 * returns the number of rows
218 public int getSize(String where)
219 throws SQLException,StorageObjectException {
220 return theStorage.getSize(where);