c6790d7206159fba1c5deba3b90b3f2495329082
[mir.git] / source / mir / entity / Entity.java
1 /**
2  * Base class the entities are derived from. Provides base functionality of
3  * an entity<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
37     public Entity() {
38       theLog = Logfile.getInstance(this.getClass().getName());
39       this.changed = false;
40       instances++;
41       Integer i = new Integer(instances);
42       System.err.println("New abstract entity instance: "+i.toString());
43     }
44
45   /**
46    * Konstruktor
47    */
48   public Entity (StorageObject StorageObject) {
49     this();
50     setStorage(StorageObject);
51   }
52
53   /*
54    * Setzt das StorageObject der Entity.
55    */
56   public void setStorage (StorageObject storage) {
57     this.theStorageObject = storage;
58   }
59
60   /**
61    * Setzt die Werte der Entity
62    * @param theStringValues
63    */
64
65   public void setValues(HashMap theStringValues)
66   {
67     /** @todo should be synchronized */
68     theValuesHash = new HashMap();
69     String aKey;
70     Set set = theStringValues.keySet();
71     Iterator it = set.iterator();
72     int size = set.size();
73     for (int i = 0; i < size; i++) {
74       aKey = (String)it.next();
75       theValuesHash.put(aKey, (String)theStringValues.get(aKey));
76     }
77  }
78
79   /**
80    * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
81    * @return true wenn ja, sonst false
82    */
83   public boolean changed () {
84     return  changed;
85   }
86
87   /**
88    * Liefert den Primärschluessel der Entity zurueck
89    * @return String Id
90    */
91   public String getId () {
92     return  (String)getValue(theStorageObject.getIdName());
93   }
94
95   /**
96    * Setzt den Primaerschluessel der Entity
97    * @param id
98    */
99   public void setId (String id) {
100     theValuesHash.put(theStorageObject.getIdName(), id);
101       }
102
103   /**
104    * Liefert den Wert für einen Feldnamen zurueck
105    * @param theFieldString
106    * @return Wert für Feld
107    */
108   public String getValue (String field) {
109     String returnValue = null;
110     if (field != null)
111     {
112       if (field.equals("webdb_create_formatted"))
113       {
114                 if (hasValueForField("webdb_create"))
115                         returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
116       }
117       else if (field.equals("webdb_lastchange_formatted"))
118       {
119         if (hasValueForField("webdblast_change"))
120                             returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
121       }
122       else
123         returnValue = (String)theValuesHash.get(field);
124     }
125     return returnValue;
126   }
127
128
129   public boolean hasValueForField(String field)
130   {
131     if (theValuesHash!=null)
132       return theValuesHash.containsKey(field);
133     return false;
134   }
135
136   /**
137    * Fügt Entity via StorageObject in Datenbank ein.
138    * @return Primary Key der Entity
139    * @exception StorageObjectException
140    */
141   public String insert () throws StorageObjectException {
142     theLog.printDebugInfo("Entity: trying to insert ...");
143     if (theStorageObject != null) {
144       return theStorageObject.insert((Entity)this);
145     }
146     else
147       throw  new StorageObjectException("Kein StorageObject gesetzt!");
148   }
149
150   /**
151    * Aktualisiert Aenderungen an der Entity in der Datenbank
152    * @exception StorageObjectException
153    */
154   public void update () throws StorageObjectException {
155     theStorageObject.update((Entity)this);
156   }
157
158   /**
159    * Setzt den Wert fuer ein Feld
160    * @param theProp
161    * @param theValue
162    * @exception StorageObjectException
163    */
164   public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
165     this.changed = true;
166     if (isField(theProp))
167       theValuesHash.put(theProp, theValue);
168     else
169       theLog.printWarning("Property not found: " + theProp+theValue);
170
171   }
172
173   /**
174    * Gibt die Feldnamen der Entity als ArrayList zurueck
175    * @return ArrayList mit Feldnamen
176    * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
177    *    möglich.
178    */
179   public ArrayList getFields () throws StorageObjectException {
180     return  theStorageObject.getFields();
181     }
182
183   /**
184    * Liefert ein int[] mit den Typen der Felder zurueck
185    * @return int[] mit den Feldtypen
186    * @exception StorageObjectException
187    */
188   public int[] getTypes () throws StorageObjectException {
189     return  theStorageObject.getTypes();
190     }
191
192   /**
193    * Liefert ArrayListe mit Feldnamen zurueck.
194    * @return Liste mit Feldnamen
195    * @exception StorageObjectException
196    */
197   public ArrayList getLabels () throws StorageObjectException {
198     return  theStorageObject.getLabels();
199     }
200
201   /**
202    * Liefert eine Hashmap mit allen Werten der Entity zurueck
203    * @return HashMap mit Feldname/Wert
204    *
205    * @deprecated This method is deprecated and will be deleted in the next release.
206    *  AbstractEntity interfaces freemarker.template.TemplateHashModel now and can
207    *  be used in the same way as SimpleHash.
208
209    */
210     public HashMap getValues() {
211       return theValuesHash;
212     }
213
214     /**
215      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
216      *  als streamedInput ausgelesen werden muessen.
217      *  Waere automatisierbar ueber die types (blob, etc.)
218      *  Bisher manuell anzulegen in der erbenden Klasse
219      */
220
221   public ArrayList streamedInput() {
222     return streamedInput;
223   }
224
225    /* Fragt ab, ob fieldName einem Feld entspricht
226    * @param fieldName
227    * @return true, wennn ja, sonst false
228    * @exception StorageObjectException
229    */
230   public boolean isField (String fieldName) throws StorageObjectException {
231     return  theStorageObject.getFields().contains(fieldName);
232   }
233
234    /** Liefert Anzahl der Instanzen zurück
235    * @return int
236    */
237   public int getInstances() {
238      return instances;
239   }
240   /**
241    * Gibt eine Instanz frei
242    */
243   public void finalize () {
244     instances--;
245     try {
246       super.finalize();
247     } catch (Throwable t) {
248       System.err.println(t.toString());
249     }
250   }
251
252
253   // Now implements freemarkers TemplateHashModel
254   // two methods have to be overridden:
255   // 1. public boolean isEmpty() throws TemplateModelException
256   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
257
258   public boolean isEmpty() throws TemplateModelException
259   {
260     if (theValuesHash==null || theValuesHash.isEmpty())
261       return true;
262     return false;
263   }
264
265   public TemplateModel get(java.lang.String key) throws TemplateModelException
266   {
267     theLog.printDebugInfo("trying to get: " + key);
268     return new SimpleScalar(getValue(key));
269   }
270
271   public void put(java.lang.String key, TemplateModel model)
272   {
273     // empty for testing
274   }
275
276   public void remove(java.lang.String key)
277   {
278     // empty for testing
279   }
280
281
282   //////////////////////////////////////////////////////////////////////////////////
283
284
285 }
286