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