organizing imports
[mir.git] / source / mir / module / AbstractModule.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 package  mir.module;
33
34 import java.util.Map;
35
36 import mir.entity.Entity;
37 import mir.entity.EntityList;
38 import mir.storage.StorageObject;
39 import freemarker.template.SimpleHash;
40
41
42 /**
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.
47  *
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.
51  *
52  */
53
54 public class AbstractModule {
55   protected StorageObject theStorage;
56
57   public void setStorage(StorageObject storage) {
58     this.theStorage = storage;
59   }
60
61   /**
62    * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
63    * @return Standard-StorageObject
64    */
65   public StorageObject getStorageObject () {
66     return theStorage;
67   }
68
69   /**
70    *   Holt eine Entity anhand der Id via StorageObject
71    *   @param String der Entity
72    *   @return Entity
73    */
74   public Entity getById (String id) throws ModuleExc, ModuleFailure {
75     try {
76       if (theStorage == null)
77         throw  new ModuleExc("AbstractModule.getById: No StorageObject set!");
78       Entity entity = (Entity)theStorage.selectById(id);
79
80       if (entity == null)
81         throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
82       else
83         return entity;
84     }
85     catch (Throwable e) {
86       throw new ModuleFailure(e);
87     }
88   }
89
90   /**
91    *   Holt eine EntityListe anhand des WhereClause via StorageObject
92    *   @param String whereclause
93    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
94    *   @return EntityList Liste der gematchten Datens?tze
95    */
96   public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
97     try {
98       if (theStorage == null)
99         throw  new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
100
101       return theStorage.selectByWhereClause(whereClause, offset);
102     }
103     catch (Throwable e) {
104       throw new ModuleFailure(e);
105     }
106   }
107
108   /**
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
114    */
115   public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
116     try {
117       if (theStorage==null)
118         throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
119
120       return theStorage.selectByWhereClause(where, order, offset);
121     }
122     catch (Throwable e) {
123       throw new ModuleFailure(e);
124     }
125   }
126   /**
127    *   Executes a where clause on the StorageObject with order criteria
128    *   fetching from offset the number of limit objects
129    *
130    *   @param String where
131    *   @param String order
132    *   @param int offset
133    *   @param int limit
134    *   @return EntityList
135    */
136
137   public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure   {
138     try {
139       if (theStorage==null)
140         throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
141
142       return theStorage.selectByWhereClause(where, order, offset, limit);
143     }
144     catch (Throwable e) {
145       throw new ModuleFailure(e);
146     }
147   }
148
149   /**
150    *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
151    *   @param String aField - Feldname im StorageObject
152    *   @param String aValue - Wert in Feld im StorageObject
153    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
154    *   @return EntityList Liste der gematchten Datens?tze
155    */
156   public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {
157     String whereClause;
158     whereClause = aField + " like '%" + aValue + "%'";
159     return getByWhereClause(whereClause, offset);
160   }
161
162   /**
163    *    * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen
164    * @param theValues Hash mit Spalte/Wert-Paaren
165    * @return Id des eingef?gten Objekts
166    * @exception ModuleExc
167    * @exception ModuleFailure
168    */
169   public String add (Map theValues) throws ModuleExc, ModuleFailure {
170     try {
171       Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
172       theEntity.setStorage(theStorage);
173       theEntity.setValues(theValues);
174       return theEntity.insert();
175     }
176     catch (Throwable e) {
177       throw new ModuleFailure(e);
178     }
179   }
180
181   /**
182    * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
183    * @param theValues Hash mit Spalte/Wert-Paaren
184    * @return Id des eingef?gten Objekts
185    * @exception ModuleExc
186    * @exception ModuleFailure
187    */
188   public String set (Map theValues) throws ModuleExc, ModuleFailure {
189     try {
190       Entity theEntity = theStorage.selectById((String)theValues.get("id"));
191       if (theEntity == null)
192         throw new ModuleExc("No object found with id " + theValues.get("id"));
193       theEntity.setValues(theValues);
194       theEntity.update();
195       return theEntity.getId();
196     }
197     catch (Throwable e) {
198       throw new ModuleFailure(e);
199     }
200   }
201
202   /**
203    * Deletes a record using an id
204    * @param idParam
205    * @exception ModuleExc
206    * @exception ModuleFailure
207    */
208   public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
209     try {
210       theStorage.delete(idParam);
211     }
212     catch (Throwable e) {
213       throw new ModuleFailure(e);
214     }
215   }
216
217   /**
218    * Liefert den Lookuptable aller Objekte des StorageObjects
219    * @return freemarker.template.SimpleHash
220    */
221   public SimpleHash getHashData() {
222     return theStorage.getHashData();
223   }
224
225   /**
226    * returns the number of rows
227    */
228   public int getSize(String where) throws ModuleExc, ModuleFailure {
229     try {
230       return theStorage.getSize(where);
231     }
232     catch (Throwable e) {
233       throw new ModuleFailure(e);
234     }
235   }
236
237 }