db40dd664a7b009c641a941c4487afeb83c01e24
[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             theLog = Logfile.getInstance(MirConfig.getProp("Home") + "log/media.log");
67             theLog.printError("aboot to run getByWhere...");
68                         return theStorage.selectByWhereClause(whereClause, offset);
69                 }
70                 catch (StorageObjectException e){
71                         throw new ModuleException(e.toString());
72                 }
73         }
74
75         /**
76          *   Holt eine EntityListe anhand des WhereClause aus dem StorageObject
77          *   @param String where WhereClause
78          *   @param String order Sortierreihenfolge
79          *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
80          *   @return EntityList Liste der gematchten Datensätze
81          */
82         public EntityList getByWhereClause (String where, String order, int offset) throws ModuleException {
83                 try {
84                         if (theStorage==null) throw new ModuleException("Kein StorageObject gesetzt");
85                         return theStorage.selectByWhereClause(where, order, offset);
86                 }
87                 catch (StorageObjectException e){
88                         throw new ModuleException(e.toString());
89                 }
90         }
91         /**
92          *   Executes a where clause on the StorageObject with order criteria
93          *   fetching from offset the number of limit objects
94          *
95          *   @param String where
96          *   @param String order
97          *   @param int offset
98          *   @param int limit
99          *   @return EntityList
100          */
101
102         public EntityList getByWhereClause(String where, String order, int offset, int limit) throws ModuleException
103         {
104                 try {
105                         if (theStorage==null) throw new ModuleException("StorageObject not set!");
106                         return theStorage.selectByWhereClause(where, order, offset, limit);
107                 }
108                 catch (StorageObjectException e){
109                         throw new ModuleException(e.toString());
110                 }
111         }
112
113         /**
114          *   Holt eine EntityListe anhand des Wertes aValue von Feld aField aus dem StorageObject
115          *   @param String aField - Feldname im StorageObject
116          *   @param String aValue - Wert in Feld im StorageObject
117          *   @param offset - ab welchem Datensatz die gematchten Entities zurueckgeliefert werden
118          *   @return EntityList Liste der gematchten Datensätze
119          */
120         public EntityList getByFieldValue (String aField, String aValue, int offset) throws ModuleException {
121                 String whereClause;
122                 whereClause = aField + " like '%" + aValue + "%'";
123                 return getByWhereClause(whereClause, offset);
124         }
125
126         /**
127          * Standardfunktion, um einen Datensatz via StorageObject einzufügen
128          * @param theValues Hash mit Spalte/Wert-Paaren
129          * @return Id des eingefügten Objekts
130          * @exception ModuleException
131          */
132         public String add (HashMap theValues) throws ModuleException {
133                 try {
134                         Entity theEntity = (Entity)theStorage.getEntityClass().newInstance();
135                         theEntity.setStorage(theStorage);
136                         theEntity.setValues(theValues);
137                         return theEntity.insert();
138                 } catch (Exception e) {
139                         throw new ModuleException(e.toString());
140                 }
141         }
142
143         /**
144          * Standardfunktion, um einen Datensatz via StorageObject zu aktualisieren
145          * @param theValues Hash mit Spalte/Wert-Paaren
146          * @return Id des eingefügten Objekts
147          * @exception ModuleException
148          */
149         public String set (HashMap theValues) throws ModuleException {
150                 try {
151                         Entity theEntity = theStorage.selectById((String)theValues.get("id"));
152                         if (theEntity == null)
153                                  throw new ModuleException("Kein Objekt mit id in Datenbank id: " + theValues.get("id"));
154                  theEntity.setValues(theValues);
155                  theEntity.update();
156                  return theEntity.getId();
157                 }
158                 catch (StorageObjectException e){
159                         e.printStackTrace(System.err);
160                         throw new ModuleException(e.toString());
161                 }
162  }
163
164         /**
165          * Löscht einen Datensatz anhand seiner Id
166          * @param idParam
167          * @exception ModuleException
168          */
169         public void deleteById (String idParam) throws ModuleException {
170     try {
171             theStorage.delete(idParam);
172     } catch (StorageObjectException e){
173       throw new ModuleException(e.toString());
174     }
175   }
176
177         /**
178          * Liefert den Lookuptable aller Objekte des StorageObjects
179          * @return freemarker.template.SimpleHash
180          */
181         public SimpleHash getHashData() {
182                 return theStorage.getHashData();
183         }
184
185   /**
186    * returns the number of rows
187    */
188   public int getSize(String where)
189     throws SQLException,StorageObjectException {
190     return theStorage.getSize(where);
191   }
192
193 }