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