templateModelRoot ist now a simplehash with two entries: data and config, bugfix...
[mir.git] / source / mir / entity / Entity.java
1 /**
2  * Base class the entities are derived from. Provides base functionality of
3  * an entity. Entities are used to represent rows of a database table.<p>
4  */
5
6
7 package  mir.entity;
8
9 import java.lang.*;
10 import java.io.*;
11 import java.util.*;
12 import java.sql.*;
13
14 import freemarker.template.*;
15
16 import mir.storage.*;
17 import mir.misc.*;
18
19 /**
20  * Base Class of Entities
21  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
22  *
23  * @author rk
24  * @version 29.6.1999
25  *
26  */
27
28 public class Entity implements TemplateHashModel, TemplateModelRoot
29 {
30   private boolean             changed;
31   protected HashMap           theValuesHash;   // tablekey / value
32   protected StorageObject     theStorageObject;
33   protected static Logfile    theLog;
34   protected ArrayList         streamedInput=null;
35   private static int instances = 0;
36     static {
37       theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
38     }
39
40     public Entity() {
41
42       this.changed = false;
43       instances++;
44       Integer i = new Integer(instances);
45       System.err.println("New abstract entity instance: "+i.toString());
46     }
47
48   /**
49    * Constructor
50    * @param StorageObject The StorageObject of the Entity.
51    */
52   public Entity (StorageObject StorageObject) {
53     this();
54     setStorage(StorageObject);
55   }
56
57   /*
58    * Sets the StorageObject of the Entity.
59    */
60   public void setStorage (StorageObject storage) {
61     this.theStorageObject = storage;
62   }
63
64   /**
65    * Sets the values of the Entity.
66    * @param theStringValues HashMap containing the new values of the Entity
67    */
68
69   public void setValues(HashMap theStringValues)
70   {
71     /** @todo should be synchronized */
72     if (theStringValues!=null) {
73       theValuesHash = new HashMap();
74       String aKey;
75       Set set = theStringValues.keySet();
76       Iterator it = set.iterator();
77       int size = set.size();
78       for (int i = 0; i < size; i++) {
79         aKey = (String)it.next();
80         theValuesHash.put(aKey, (String)theStringValues.get(aKey));
81       }
82     }
83     else theLog.printWarning("Entity.setValues called with null HashMap");
84  }
85
86   /**
87    * Returns whether the content of the Entity has changed.
88    * @return true wenn ja, sonst false
89    */
90   public boolean changed () {
91     return  changed;
92   }
93
94   /**
95    * Returns the primary key of the Entity.
96    * @return String Id
97    */
98   public String getId () {
99     return  (String)getValue(theStorageObject.getIdName());
100   }
101
102   /**
103    * Defines the primary key of the Entity
104    * @param id
105    */
106   public void setId (String id) {
107     theValuesHash.put(theStorageObject.getIdName(), id);
108       }
109
110   /**
111    * Returns the value of a field by field name.
112    * @param field The name of the field
113    * @return value of the field
114    */
115   public String getValue (String field) {
116     String returnValue = null;
117     if (field != null)
118     {
119       if (field.equals("webdb_create_formatted"))
120       {
121         if (hasValueForField("webdb_create"))
122           returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
123       }
124       else if (field.equals("webdb_lastchange_formatted"))
125       {
126         if (hasValueForField("webdblast_change"))
127           returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
128       }
129       else
130         returnValue = (String)theValuesHash.get(field);
131     }
132     return returnValue;
133   }
134
135
136   public boolean hasValueForField(String field)
137   {
138     if (theValuesHash!=null)
139       return theValuesHash.containsKey(field);
140     return false;
141   }
142
143   /**
144    * Insers Entity into the database via StorageObject
145    * @return Primary Key of the Entity
146    * @exception StorageObjectException
147    */
148   public String insert () throws StorageObjectException {
149     theLog.printDebugInfo("Entity: trying to insert ...");
150     if (theStorageObject != null) {
151       return theStorageObject.insert((Entity)this);
152     }
153     else
154       throw  new StorageObjectException("Kein StorageObject gesetzt!");
155   }
156
157   /**
158    * Saves changes of this Entity to the database
159    * @exception StorageObjectException
160    */
161   public void update () throws StorageObjectException {
162     theStorageObject.update((Entity)this);
163   }
164
165   /**
166    * Sets the value for a field. Issues a log message if the field name supplied was not found in the Entity.
167    * @param theProp The field name whose value has to be set
168    * @param theValue The new value of the field
169    * @exception StorageObjectException
170    */
171   public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
172     this.changed = true;
173     if (isField(theProp))
174       theValuesHash.put(theProp, theValue);
175     else {
176       theLog.printWarning("Property not found: " + theProp+theValue);
177     }
178
179   }
180
181   /**
182    * Returns the field names of the Entity as ArrayListe.
183    * @return ArrayList with field names
184    * @exception StorageObjectException is throuwn if database access was impossible
185    */
186   public ArrayList getFields () throws StorageObjectException {
187     return  theStorageObject.getFields();
188     }
189
190   /**
191    * Returns an int[] with the types of the fields
192    * @return int[] that contains the types of the fields
193    * @exception StorageObjectException
194    */
195   public int[] getTypes () throws StorageObjectException {
196     return  theStorageObject.getTypes();
197     }
198
199   /**
200    * Returns an ArrayList with field names
201    * @return List with field names
202    * @exception StorageObjectException
203    */
204   public ArrayList getLabels () throws StorageObjectException {
205     return  theStorageObject.getLabels();
206     }
207
208   /**
209    * Returns a Hashmap with all values of the Entity.
210    * @return HashMap with field name as key and the corresponding values
211    *
212    * @deprecated This method is deprecated and will be deleted in the next release.
213    *  Entity interfaces freemarker.template.TemplateHashModel now and can
214    *  be used in the same way as SimpleHash.
215
216    */
217     public HashMap getValues() {
218       theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
219       return theValuesHash;
220     }
221
222     /**
223      * Returns an ArrayList with all database fields that can
224      * be evaluated as streamedInput.
225      * Could be automated by the types (blob, etc.)
226      * Until now to be created manually in the inheriting class
227      *
228      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
229      *  als streamedInput ausgelesen werden muessen.
230      *  Waere automatisierbar ueber die types (blob, etc.)
231      *  Bisher manuell anzulegen in der erbenden Klasse
232      */
233
234   public ArrayList streamedInput() {
235     return streamedInput;
236   }
237
238    /** Returns whether fieldName is a valid field name of this Entity.
239    * @param fieldName
240    * @return true in case fieldName is a field name, else false.
241    * @exception StorageObjectException
242    */
243   public boolean isField (String fieldName) throws StorageObjectException {
244     return  theStorageObject.getFields().contains(fieldName);
245   }
246
247    /** Returns the number of instances of this Entity
248    * @return int The number of instances
249    */
250   public int getInstances() {
251      return instances;
252   }
253
254   protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
255     theLog.printError( e.toString() + " Funktion: "+ wo);
256     throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
257   }
258
259   /**
260    * Frees an instance
261    */
262   /*public void finalize () {
263     instances--;
264     Integer i = new Integer(instances);
265     System.err.println("Removing abstract entity instance: "+i.toString());
266     try {
267       super.finalize();
268     } catch (Throwable t) {
269       System.err.println(t.toString());
270     }
271   }*/
272
273
274   // Now implements freemarkers TemplateHashModel
275   // two methods have to be overridden:
276   // 1. public boolean isEmpty() throws TemplateModelException
277   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
278
279   public boolean isEmpty() throws TemplateModelException
280   {
281     return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
282   }
283
284   public TemplateModel get(java.lang.String key) throws TemplateModelException
285   {
286                 return new SimpleScalar(getValue(key));
287   }
288         
289         public void put(java.lang.String key, TemplateModel model)
290   {
291     // putting should only take place via setValue and is limited to the
292     // database fields associated with the entity. no additional freemarker
293     // stuff will be available via Entity.
294     theLog.printWarning("### put is called on entity! - the values will be lost!");
295   }
296
297   public void remove(java.lang.String key)
298   {
299     // do we need this?
300   }
301
302
303   //////////////////////////////////////////////////////////////////////////////////
304
305
306 }
307