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.
34 import java.sql.SQLException;
37 import mir.entity.Entity;
38 import mir.entity.EntityList;
39 import mir.storage.StorageObject;
40 import mir.storage.StorageObjectExc;
41 import mir.storage.StorageObjectFailure;
42 import freemarker.template.SimpleHash;
46 * This class provides the base functionality for the derived Module-Classes.
47 * These classes should provide methods to make more or less complex actions
48 * on Database and Entity classes. The modules are used by ServletModules.
49 * Future possibility could be access via Applications.
51 * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
52 * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
53 * die von mehreren ServletModulen verwendet werden.
57 public class AbstractModule {
58 protected StorageObject theStorage;
60 public void setStorage(StorageObject storage) {
61 this.theStorage = storage;
65 * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
66 * @return Standard-StorageObject
68 public StorageObject getStorageObject () {
73 * Holt eine Entity anhand der Id via StorageObject
74 * @param String der Entity
77 public Entity getById (String id) throws ModuleException {
79 if (theStorage == null)
80 throw new ModuleException("No StorageObject set!");
81 Entity entity = (Entity)theStorage.selectById(id);
83 throw new ModuleException("No object for id = " + id);
86 catch (StorageObjectExc e){
87 throw new ModuleException(e.toString());
92 * Holt eine EntityListe anhand des WhereClause via StorageObject
93 * @param String whereclause
94 * @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
95 * @return EntityList Liste der gematchten Datens?tze
97 public EntityList getByWhereClause (String whereClause, int offset) throws ModuleException {
99 if (theStorage == null)
100 throw new ModuleException("Kein StorageObject gesetzt");
101 return theStorage.selectByWhereClause(whereClause, offset);
103 catch (StorageObjectFailure e){
104 throw new ModuleException(e.toString());
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 ModuleException {
117 if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
118 return theStorage.selectByWhereClause(where, order, offset);
120 catch (StorageObjectFailure e){
121 throw new ModuleException(e.toString());
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 ModuleException
138 if (theStorage==null) throw new ModuleException("StorageObject not set!");
139 return theStorage.selectByWhereClause(where, order, offset, limit);
141 catch (StorageObjectFailure e){
142 throw new ModuleException(e.toString());
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 ModuleException {
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 ModuleException
165 public String add (Map theValues) throws ModuleException {
167 Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
168 theEntity.setStorage(theStorage);
169 theEntity.setValues(theValues);
170 return theEntity.insert();
172 catch (Exception e) {
173 throw new ModuleException(e.toString());
178 * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
179 * @param theValues Hash mit Spalte/Wert-Paaren
180 * @return Id des eingef?gten Objekts
181 * @exception ModuleException
183 public String set (Map theValues) throws ModuleException {
185 Entity theEntity = theStorage.selectById((String)theValues.get("id"));
186 if (theEntity == null)
187 throw new ModuleException("No object found with id " + theValues.get("id"));
188 theEntity.setValues(theValues);
190 return theEntity.getId();
192 catch (StorageObjectExc e){
193 throw new ModuleException(e.toString());
198 * Deletes a record using an id
200 * @exception ModuleException
202 public void deleteById (String idParam) throws ModuleException {
204 theStorage.delete(idParam);
205 } catch (StorageObjectFailure e){
206 throw new ModuleException(e.toString());
211 * Liefert den Lookuptable aller Objekte des StorageObjects
212 * @return freemarker.template.SimpleHash
214 public SimpleHash getHashData() {
215 return theStorage.getHashData();
219 * returns the number of rows
221 public int getSize(String where)
222 throws SQLException,StorageObjectFailure {
223 return theStorage.getSize(where);