2 * Base class the entities are derived from. Provides base functionality of
14 import freemarker.template.*;
20 * Base Class of Entities
21 * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
28 public class Entity implements TemplateHashModel, TemplateModelRoot
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;
38 theLog = Logfile.getInstance(this.getClass().getName());
41 Integer i = new Integer(instances);
42 System.err.println("New abstract entity instance: "+i.toString());
48 public Entity (StorageObject StorageObject) {
50 setStorage(StorageObject);
54 * Setzt das StorageObject der Entity.
56 public void setStorage (StorageObject storage) {
57 this.theStorageObject = storage;
61 * Setzt die Werte der Entity
62 * @param theStringValues
65 public void setValues(HashMap theStringValues)
67 /** @todo should be synchronized */
68 theValuesHash = new HashMap();
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));
80 * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
81 * @return true wenn ja, sonst false
83 public boolean changed () {
88 * Liefert den Primärschluessel der Entity zurueck
91 public String getId () {
92 return (String)getValue(theStorageObject.getIdName());
96 * Setzt den Primaerschluessel der Entity
99 public void setId (String id) {
100 theValuesHash.put(theStorageObject.getIdName(), id);
104 * Liefert den Wert für einen Feldnamen zurueck
105 * @param theFieldString
106 * @return Wert für Feld
108 public String getValue (String field) {
109 String returnValue = null;
112 if (field.equals("webdb_create_formatted"))
114 if (hasValueForField("webdb_create"))
115 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
117 else if (field.equals("webdb_lastchange_formatted"))
119 if (hasValueForField("webdblast_change"))
120 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
123 returnValue = (String)theValuesHash.get(field);
129 public boolean hasValueForField(String field)
131 if (theValuesHash!=null)
132 return theValuesHash.containsKey(field);
137 * Fügt Entity via StorageObject in Datenbank ein.
138 * @return Primary Key der Entity
139 * @exception StorageObjectException
141 public String insert () throws StorageObjectException {
142 theLog.printDebugInfo("Entity: trying to insert ...");
143 if (theStorageObject != null) {
144 return theStorageObject.insert((Entity)this);
147 throw new StorageObjectException("Kein StorageObject gesetzt!");
151 * Aktualisiert Aenderungen an der Entity in der Datenbank
152 * @exception StorageObjectException
154 public void update () throws StorageObjectException {
155 theStorageObject.update((Entity)this);
159 * Setzt den Wert fuer ein Feld
162 * @exception StorageObjectException
164 public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
166 if (isField(theProp))
167 theValuesHash.put(theProp, theValue);
169 theLog.printWarning("Property not found: " + theProp+theValue);
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
179 public ArrayList getFields () throws StorageObjectException {
180 return theStorageObject.getFields();
184 * Liefert ein int[] mit den Typen der Felder zurueck
185 * @return int[] mit den Feldtypen
186 * @exception StorageObjectException
188 public int[] getTypes () throws StorageObjectException {
189 return theStorageObject.getTypes();
193 * Liefert ArrayListe mit Feldnamen zurueck.
194 * @return Liste mit Feldnamen
195 * @exception StorageObjectException
197 public ArrayList getLabels () throws StorageObjectException {
198 return theStorageObject.getLabels();
202 * Liefert eine Hashmap mit allen Werten der Entity zurueck
203 * @return HashMap mit Feldname/Wert
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.
210 public HashMap getValues() {
211 return theValuesHash;
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
221 public ArrayList streamedInput() {
222 return streamedInput;
225 /* Fragt ab, ob fieldName einem Feld entspricht
227 * @return true, wennn ja, sonst false
228 * @exception StorageObjectException
230 public boolean isField (String fieldName) throws StorageObjectException {
231 return theStorageObject.getFields().contains(fieldName);
234 /** Liefert Anzahl der Instanzen zurück
237 public int getInstances() {
241 * Gibt eine Instanz frei
243 public void finalize () {
247 } catch (Throwable t) {
248 System.err.println(t.toString());
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
258 public boolean isEmpty() throws TemplateModelException
260 if (theValuesHash==null || theValuesHash.isEmpty())
265 public TemplateModel get(java.lang.String key) throws TemplateModelException
267 theLog.printDebugInfo("trying to get: " + key);
268 return new SimpleScalar(getValue(key));
271 public void put(java.lang.String key, TemplateModel model)
276 public void remove(java.lang.String key)
282 //////////////////////////////////////////////////////////////////////////////////