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