testing TemplateHashModel
[mir.git] / source / mir / module / AbstractModule.java
1 /*
2  * put your module comment here
3  */
4
5
6 package  mir.module;
7
8 import  java.util.*;
9 import  java.sql.*;
10 import  freemarker.template.*;
11 import  mir.storage.*;
12 import  mir.misc.*;
13 import  mir.entity.*;
14
15
16 /**
17  * This class provides the base functionality for the derived Module-Classes.
18  * These classes should provide methods to make more or less complex actions
19  * on Database and Entity classes. The modules are used by ServletModules.
20  * Future possibility could be access via Applications.
21  *
22  * Abstrakte Klasse, von denen die Modules die Basisfunktionalität erben.
23  * Die Moduleschicht dient dazu, Funktionalitaeten zur Verfügung zu stellen,
24  * die von mehreren ServletModulen verwendet werden.
25  *
26  */
27
28 public class AbstractModule {
29         protected StorageObject theStorage;
30
31   public void setStorage(StorageObject storage) {
32          this.theStorage = storage;
33         }
34
35         /**
36          * Liefert das Standard-StorageObject zurück, mit dem das Module assoziiert ist.
37          * @return Standard-StorageObject
38          */
39         public StorageObject getStorageObject () {
40          return theStorage;
41         }
42
43         /**
44          *   Holt eine Entity anhand der Id via StorageObject
45          *   @param String der Entity
46          *   @return Entity
47          */
48         public Entity getById (String id) throws ModuleException {
49                 try {
50                         if (theStorage == null)
51                                 throw  new ModuleException("No StorageObject set!");
52                         Entity entity = (Entity)theStorage.selectById(id);
53                         if (entity == null)
54                                  throw new ModuleException("No object for id = " + id);
55                         else return entity;
56                         }
57                 catch (StorageObjectException e){
58                         throw new ModuleException(e.toString());
59                 }
60         }
61
62   /**
63          *   Holt eine EntityListe anhand des WhereClause via StorageObject
64          *   @param String whereclause
65          *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
66          *   @return EntityList Liste der gematchten Datensätze
67          */
68         public EntityList getByWhereClause (String whereClause, int offset) throws ModuleException {
69                 try {
70                         if (theStorage == null)
71                                 throw  new ModuleException("Kein StorageObject gesetzt");
72                         return theStorage.selectByWhereClause(whereClause, offset);
73                 }
74                 catch (StorageObjectException e){
75                         throw new ModuleException(e.toString());
76                 }
77         }
78
79         /**
80          *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject
81          *   @param String where WhereClause
82          *   @param String order Sortierreihenfolge
83          *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
84          *   @return EntityList Liste der gematchten Datensätze
85          */
86         public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
87                 try {
88                         if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
89                         return theStorage.selectByWhereClause(where, order, offset);
90                 }
91                 catch (StorageObjectException e){
92                         throw new ModuleException(e.toString());
93                 }
94         }
95         /**
96          *   Executes a where clause on the StorageObject with order criteria
97          *   fetching from offset the number of limit objects
98          *
99          *   @param String where
100          *   @param String order
101          *   @param int offset
102          *   @param int limit
103          *   @return EntityList
104          */
105
106         public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
107         {
108                 try {
109                         if (theStorage==null) throw new ModuleException("StorageObject not set!");
110                         return theStorage.selectByWhereClause(where, order, offset, limit);
111                 }
112                 catch (StorageObjectException e){
113                         throw new ModuleException(e.toString());
114                 }
115         }
116
117         /**
118          *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
119          *   @param String aField - Feldname im StorageObject
120          *   @param String aValue - Wert in Feld im StorageObject
121          *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
122          *   @return EntityList Liste der gematchten Datensätze
123          */
124         public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
125                 String whereClause;
126                 whereClause = aField + " like '%" + aValue + "%'";
127                 return getByWhereClause(whereClause, offset);
128         }
129
130         /**
131          * Standardfunktion, um einen Datensatz via StorageObject einzufügen
132          * @param theValues Hash mit Spalte/Wert-Paaren
133          * @return Id des eingefügten Objekts
134          * @exception ModuleException
135          */
136         public String add (HashMap theValues) throws ModuleException {
137                 try {
138                         Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
139                         theEntity.setStorage(theStorage);
140                         theEntity.setValues(theValues);
141                         return theEntity.insert();
142                 } catch (Exception e) {
143                         throw new ModuleException(e.toString());
144                 }
145         }
146
147         /**
148          * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
149          * @param theValues Hash mit Spalte/Wert-Paaren
150          * @return Id des eingefügten Objekts
151          * @exception ModuleException
152          */
153         public String set (HashMap theValues) throws ModuleException {
154                 try {
155                         Entity theEntity = theStorage.selectById((String)theValues.get("id"));
156                         if (theEntity == null)
157                                  throw new ModuleException("Kein Objekt mit id in Datenbank id: " + theValues.get("id"));
158                  theEntity.setValues(theValues);
159                  theEntity.update();
160                  return theEntity.getId();
161                 }
162                 catch (StorageObjectException e){
163                         e.printStackTrace(System.err);
164                         throw new ModuleException(e.toString());
165                 }
166  }
167
168         /**
169          * Löscht einen Datensatz anhand seiner Id
170          * @param idParam
171          * @exception ModuleException
172          */
173         public void deleteById (String idParam) throws ModuleException {
174     try {
175             theStorage.delete(idParam);
176     } catch (StorageObjectException e){
177       throw new ModuleException(e.toString());
178     }
179   }
180
181         /**
182          * Liefert den Lookuptable aller Objekte des StorageObjects
183          * @return freemarker.template.SimpleHash
184          */
185         public SimpleHash getHashData() {
186                 return theStorage.getHashData();
187         }
188
189   /**
190    * returns the number of rows
191    */
192   public int getSize(String where)
193     throws SQLException,StorageObjectException {
194     return theStorage.getSize(where);
195   }
196
197 }