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