Organizing import, refresh the license (zapata deleted cos.jar)
[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  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.
29  */
30 package  mir.module;
31
32 import java.util.Map;
33
34 import mir.entity.Entity;
35 import mir.entity.EntityList;
36 import mir.storage.StorageObject;
37 import freemarker.template.SimpleHash;
38
39
40 /**
41  * This class provides the base functionality for the derived Module-Classes.
42  * These classes should provide methods to make more or less complex actions
43  * on Database and Entity classes. The modules are used by ServletModules.
44  * Future possibility could be access via Applications.
45  *
46  * Abstrakte Klasse, von denen die Modules die Basisfunktionalit?t erben.
47  * Die Moduleschicht dient dazu, Funktionalitaeten zur Verf?gung zu stellen,
48  * die von mehreren ServletModulen verwendet werden.
49  *
50  */
51
52 public class AbstractModule {
53   protected StorageObject theStorage;
54
55   public void setStorage(StorageObject storage) {
56     this.theStorage = storage;
57   }
58
59   /**
60    * Liefert das Standard-StorageObject zur?ck, mit dem das Module assoziiert ist.
61    * @return Standard-StorageObject
62    */
63   public StorageObject getStorageObject () {
64     return theStorage;
65   }
66
67   /**
68    *   Holt eine Entity anhand der Id via StorageObject
69    *   @param String der Entity
70    *   @return Entity
71    */
72   public Entity getById (String id) throws ModuleExc, ModuleFailure {
73     try {
74       if (theStorage == null)
75         throw  new ModuleExc("AbstractModule.getById: No StorageObject set!");
76       Entity entity = (Entity)theStorage.selectById(id);
77
78       if (entity == null)
79         throw new ModuleExc("AbstractModule.getById: No object for id = " + id);
80       else
81         return entity;
82     }
83     catch (Throwable e) {
84       throw new ModuleFailure(e);
85     }
86   }
87
88   /**
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
93    */
94   public EntityList getByWhereClause (String whereClause, int offset) throws ModuleExc, ModuleFailure {
95     try {
96       if (theStorage == null)
97         throw  new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
98
99       return theStorage.selectByWhereClause(whereClause, offset);
100     }
101     catch (Throwable e) {
102       throw new ModuleFailure(e);
103     }
104   }
105
106   /**
107    *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject
108    *   @param String where WhereClause
109    *   @param String order Sortierreihenfolge
110    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
111    *   @return EntityList Liste der gematchten Datens?tze
112    */
113   public EntityList getByWhereClause (String where, String order, int offset) throws ModuleExc, ModuleFailure {
114     try {
115       if (theStorage==null)
116         throw new ModuleExc("AbstractModule.getByWhereClause: No StorageObject set!");
117
118       return theStorage.selectByWhereClause(where, order, offset);
119     }
120     catch (Throwable e) {
121       throw new ModuleFailure(e);
122     }
123   }
124   /**
125    *   Executes a where clause on the StorageObject with order criteria
126    *   fetching from offset the number of limit objects
127    *
128    *   @param String where
129    *   @param String order
130    *   @param int offset
131    *   @param int limit
132    *   @return EntityList
133    */
134
135   public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleExc, ModuleFailure   {
136     try {
137       if (theStorage==null)
138         throw new ModuleExc("AbstractModule.getByWhereClause: StorageObject not set!");
139
140       return theStorage.selectByWhereClause(where, order, offset, limit);
141     }
142     catch (Throwable e) {
143       throw new ModuleFailure(e);
144     }
145   }
146
147   /**
148    *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
149    *   @param String aField - Feldname im StorageObject
150    *   @param String aValue - Wert in Feld im StorageObject
151    *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
152    *   @return EntityList Liste der gematchten Datens?tze
153    */
154   public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleExc, ModuleFailure {
155     String whereClause;
156     whereClause = aField + " like '%" + aValue + "%'";
157     return getByWhereClause(whereClause, offset);
158   }
159
160   /**
161    *    * Standardfunktion, um einen Datensatz via StorageObject einzuf?gen
162    * @param theValues Hash mit Spalte/Wert-Paaren
163    * @return Id des eingef?gten Objekts
164    * @exception ModuleExc
165    * @exception ModuleFailure
166    */
167   public String add (Map theValues) throws ModuleExc, ModuleFailure {
168     try {
169       Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
170       theEntity.setStorage(theStorage);
171       theEntity.setValues(theValues);
172       return theEntity.insert();
173     }
174     catch (Throwable e) {
175       throw new ModuleFailure(e);
176     }
177   }
178
179   /**
180    * This function creates an Entity without yet storing it in the database
181    *
182    * @param theValues
183    * @return
184    * @throws ModuleExc
185    * @throws ModuleFailure
186    */
187   public Entity createNew() throws ModuleExc, ModuleFailure {
188     try {
189       Entity result = (Entity)theStorage.getEntityClass().newInstance();
190       result.setStorage(theStorage);
191
192       return result;
193     }
194     catch (Throwable e) {
195       throw new ModuleFailure(e);
196     }
197   }
198
199   /**
200    * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
201    * @param theValues Hash mit Spalte/Wert-Paaren
202    * @return Id des eingef?gten Objekts
203    * @exception ModuleExc
204    * @exception ModuleFailure
205    */
206   public String set (Map theValues) throws ModuleExc, ModuleFailure {
207     try {
208       Entity theEntity = theStorage.selectById((String)theValues.get("id"));
209       if (theEntity == null)
210         throw new ModuleExc("No object found with id " + theValues.get("id"));
211       theEntity.setValues(theValues);
212       theEntity.update();
213       return theEntity.getId();
214     }
215     catch (Throwable e) {
216       throw new ModuleFailure(e);
217     }
218   }
219
220   /**
221    * Deletes a record using an id
222    * @param idParam
223    * @exception ModuleExc
224    * @exception ModuleFailure
225    */
226   public void deleteById (String idParam) throws ModuleExc, ModuleFailure {
227     try {
228       theStorage.delete(idParam);
229     }
230     catch (Throwable e) {
231       throw new ModuleFailure(e);
232     }
233   }
234
235   /**
236    * Liefert den Lookuptable aller Objekte des StorageObjects
237    * @return freemarker.template.SimpleHash
238    */
239   public SimpleHash getHashData() {
240     return theStorage.getHashData();
241   }
242
243   /**
244    * returns the number of rows
245    */
246   public int getSize(String where) throws ModuleExc, ModuleFailure {
247     try {
248       return theStorage.getSize(where);
249     }
250     catch (Throwable e) {
251       throw new ModuleFailure(e);
252     }
253   }
254
255 }