Mir goes GPL
[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.*;
35 import  java.sql.*;
36 import  freemarker.template.*;
37 import  mir.storage.*;
38 import  mir.misc.*;
39 import  mir.entity.*;
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 ModuleException {
75                 try {
76                         if (theStorage == null)
77                                 throw  new ModuleException("No StorageObject set!");
78                         Entity entity = (Entity)theStorage.selectById(id);
79                         if (entity == null)
80                                  throw new ModuleException("No object for id = " + id);
81                         else return entity;
82                         }
83                 catch (StorageObjectException e){
84                         throw new ModuleException(e.toString());
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 ModuleException {
95                 try {
96                         if (theStorage == null)
97                                 throw  new ModuleException("Kein StorageObject gesetzt");
98                         return theStorage.selectByWhereClause(whereClause, offset);
99                 }
100                 catch (StorageObjectException e){
101                         throw new ModuleException(e.toString());
102                 }
103         }
104
105         /**
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
111          */
112         public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
113                 try {
114                         if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
115                         return theStorage.selectByWhereClause(where, order, offset);
116                 }
117                 catch (StorageObjectException e){
118                         throw new ModuleException(e.toString());
119                 }
120         }
121         /**
122          *   Executes a where clause on the StorageObject with order criteria
123          *   fetching from offset the number of limit objects
124          *
125          *   @param String where
126          *   @param String order
127          *   @param int offset
128          *   @param int limit
129          *   @return EntityList
130          */
131
132         public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
133         {
134                 try {
135                         if (theStorage==null) throw new ModuleException("StorageObject not set!");
136                         return theStorage.selectByWhereClause(where, order, offset, limit);
137                 }
138                 catch (StorageObjectException e){
139                         throw new ModuleException(e.toString());
140                 }
141         }
142
143         /**
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
149          */
150         public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
151                 String whereClause;
152                 whereClause = aField + " like '%" + aValue + "%'";
153                 return getByWhereClause(whereClause, offset);
154         }
155
156         /**
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
161          */
162         public String add (HashMap theValues) throws ModuleException {
163                 try {
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());
170                 }
171         }
172
173         /**
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
178          */
179         public String set (HashMap theValues) throws ModuleException {
180                 try {
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);
185                  theEntity.update();
186                  return theEntity.getId();
187                 }
188                 catch (StorageObjectException e){
189                         e.printStackTrace(System.err);
190                         throw new ModuleException(e.toString());
191                 }
192  }
193
194         /**
195          * Löscht einen Datensatz anhand seiner Id
196          * @param idParam
197          * @exception ModuleException
198          */
199         public void deleteById (String idParam) throws ModuleException {
200     try {
201             theStorage.delete(idParam);
202     } catch (StorageObjectException e){
203       throw new ModuleException(e.toString());
204     }
205   }
206
207         /**
208          * Liefert den Lookuptable aller Objekte des StorageObjects
209          * @return freemarker.template.SimpleHash
210          */
211         public SimpleHash getHashData() {
212                 return theStorage.getHashData();
213         }
214
215   /**
216    * returns the number of rows
217    */
218   public int getSize(String where)
219     throws SQLException,StorageObjectException {
220     return theStorage.getSize(where);
221   }
222
223 }