a678d63c62c3259503f0f88dc83deba1e36b8473
[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
167    * supplied was not found in the Entity.
168    * @param theProp The field name whose value has to be set
169    * @param theValue The new value of the field
170    * @exception StorageObjectException
171    */
172   public void setValueForProperty (String theProp, String theValue)
173     throws StorageObjectException {
174     this.changed = true;
175     if (isField(theProp))
176       theValuesHash.put(theProp, theValue);
177     else {
178       theLog.printWarning("Property not found: " + theProp+theValue);
179     }
180
181   }
182
183   /**
184    * Returns the field names of the Entity as ArrayListe.
185    * @return ArrayList with field names
186    * @exception StorageObjectException is throuwn if database access was impossible
187    */
188   public ArrayList getFields () throws StorageObjectException {
189     return  theStorageObject.getFields();
190     }
191
192   /**
193    * Returns an int[] with the types of the fields
194    * @return int[] that contains the types of the fields
195    * @exception StorageObjectException
196    */
197   public int[] getTypes () throws StorageObjectException {
198     return  theStorageObject.getTypes();
199     }
200
201   /**
202    * Returns an ArrayList with field names
203    * @return List with field names
204    * @exception StorageObjectException
205    */
206   public ArrayList getLabels () throws StorageObjectException {
207     return  theStorageObject.getLabels();
208     }
209
210   /**
211    * Returns a Hashmap with all values of the Entity.
212    * @return HashMap with field name as key and the corresponding values
213    *
214    * @deprecated This method is deprecated and will be deleted in the next release.
215    *  Entity interfaces freemarker.template.TemplateHashModel now and can
216    *  be used in the same way as SimpleHash.
217
218    */
219     public HashMap getValues() {
220       theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
221       return theValuesHash;
222     }
223
224     /**
225      * Returns an ArrayList with all database fields that can
226      * be evaluated as streamedInput.
227      * Could be automated by the types (blob, etc.)
228      * Until now to be created manually in the inheriting class
229      *
230      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
231      *  als streamedInput ausgelesen werden muessen.
232      *  Waere automatisierbar ueber die types (blob, etc.)
233      *  Bisher manuell anzulegen in der erbenden Klasse
234      */
235
236   public ArrayList streamedInput() {
237     return streamedInput;
238   }
239
240    /** Returns whether fieldName is a valid field name of this Entity.
241    * @param fieldName
242    * @return true in case fieldName is a field name, else false.
243    * @exception StorageObjectException
244    */
245   public boolean isField (String fieldName) throws StorageObjectException {
246     return  theStorageObject.getFields().contains(fieldName);
247   }
248
249    /** Returns the number of instances of this Entity
250    * @return int The number of instances
251    */
252   public int getInstances() {
253      return instances;
254   }
255
256   protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
257     theLog.printError( e.toString() + " Funktion: "+ wo);
258     throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
259   }
260
261   /**
262    * Frees an instance
263    */
264   /*public void finalize () {
265     instances--;
266     Integer i = new Integer(instances);
267     System.err.println("Removing abstract entity instance: "+i.toString());
268     try {
269       super.finalize();
270     } catch (Throwable t) {
271       System.err.println(t.toString());
272     }
273   }*/
274
275
276   // Now implements freemarkers TemplateHashModel
277   // two methods have to be overridden:
278   // 1. public boolean isEmpty() throws TemplateModelException
279   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
280
281   public boolean isEmpty() throws TemplateModelException
282   {
283     return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
284   }
285
286   public TemplateModel get(java.lang.String key) throws TemplateModelException
287   {
288                 return new SimpleScalar(getValue(key));
289   }
290         
291         public void put(java.lang.String key, TemplateModel model)
292   {
293     // putting should only take place via setValue and is limited to the
294     // database fields associated with the entity. no additional freemarker
295     // stuff will be available via Entity.
296     theLog.printWarning("### put is called on entity! - the values will be lost!");
297   }
298
299   public void remove(java.lang.String key)
300   {
301     // do we need this?
302   }
303
304
305   //////////////////////////////////////////////////////////////////////////////////
306
307
308 }
309