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>
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;
37 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
44 Integer i = new Integer(instances);
45 //System.err.println("New abstract entity instance: "+i.toString());
50 * @param StorageObject The StorageObject of the Entity.
52 public Entity (StorageObject StorageObject) {
54 setStorage(StorageObject);
58 * Sets the StorageObject of the Entity.
60 public void setStorage (StorageObject storage) {
61 this.theStorageObject = storage;
65 * Sets the values of the Entity.
66 * @param theStringValues HashMap containing the new values of the Entity
69 public void setValues(HashMap theStringValues)
71 /** @todo should be synchronized */
72 if (theStringValues!=null) {
73 theValuesHash = new HashMap();
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));
83 else theLog.printWarning("Entity.setValues called with null HashMap");
87 * Returns whether the content of the Entity has changed.
88 * @return true wenn ja, sonst false
90 public boolean changed () {
95 * Returns the primary key of the Entity.
98 public String getId () {
99 return (String)getValue(theStorageObject.getIdName());
103 * Defines the primary key of the Entity
106 public void setId (String id) {
107 theValuesHash.put(theStorageObject.getIdName(), id);
111 * Returns the value of a field by field name.
112 * @param field The name of the field
113 * @return value of the field
115 public String getValue (String field) {
116 String returnValue = null;
119 if (field.equals("webdb_create_formatted"))
121 if (hasValueForField("webdb_create"))
122 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
124 else if (field.equals("webdb_lastchange_formatted"))
126 if (hasValueForField("webdblast_change"))
127 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
130 returnValue = (String)theValuesHash.get(field);
136 public boolean hasValueForField(String field)
138 if (theValuesHash!=null)
139 return theValuesHash.containsKey(field);
144 * Insers Entity into the database via StorageObject
145 * @return Primary Key of the Entity
146 * @exception StorageObjectException
148 public String insert () throws StorageObjectException {
149 theLog.printDebugInfo("Entity: trying to insert ...");
150 if (theStorageObject != null) {
151 return theStorageObject.insert((Entity)this);
154 throw new StorageObjectException("Kein StorageObject gesetzt!");
158 * Saves changes of this Entity to the database
159 * @exception StorageObjectException
161 public void update () throws StorageObjectException {
162 theStorageObject.update((Entity)this);
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
172 public void setValueForProperty (String theProp, String theValue)
173 throws StorageObjectException {
175 if (isField(theProp))
176 theValuesHash.put(theProp, theValue);
178 theLog.printWarning("Property not found: " + theProp+theValue);
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
188 public ArrayList getFields () throws StorageObjectException {
189 return theStorageObject.getFields();
193 * Returns an int[] with the types of the fields
194 * @return int[] that contains the types of the fields
195 * @exception StorageObjectException
197 public int[] getTypes () throws StorageObjectException {
198 return theStorageObject.getTypes();
202 * Returns an ArrayList with field names
203 * @return List with field names
204 * @exception StorageObjectException
206 public ArrayList getLabels () throws StorageObjectException {
207 return theStorageObject.getLabels();
211 * Returns a Hashmap with all values of the Entity.
212 * @return HashMap with field name as key and the corresponding values
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.
219 public HashMap getValues() {
220 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
221 return theValuesHash;
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
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
236 public ArrayList streamedInput() {
237 return streamedInput;
240 /** Returns whether fieldName is a valid field name of this Entity.
242 * @return true in case fieldName is a field name, else false.
243 * @exception StorageObjectException
245 public boolean isField (String fieldName) throws StorageObjectException {
246 return theStorageObject.getFields().contains(fieldName);
249 /** Returns the number of instances of this Entity
250 * @return int The number of instances
252 public int getInstances() {
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());
264 /*public void finalize () {
266 Integer i = new Integer(instances);
267 System.err.println("Removing abstract entity instance: "+i.toString());
270 } catch (Throwable t) {
271 System.err.println(t.toString());
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
281 public boolean isEmpty() throws TemplateModelException
283 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
286 public TemplateModel get(java.lang.String key) throws TemplateModelException
288 return new SimpleScalar(getValue(key));
291 public void put(java.lang.String key, TemplateModel model)
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!");
299 public void remove(java.lang.String key)
305 //////////////////////////////////////////////////////////////////////////////////