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 supplied was not found in the Entity.
167 * @param theProp The field name whose value has to be set
168 * @param theValue The new value of the field
169 * @exception StorageObjectException
171 public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
173 if (isField(theProp))
174 theValuesHash.put(theProp, theValue);
176 theLog.printWarning("Property not found: " + theProp+theValue);
182 * Returns the field names of the Entity as ArrayListe.
183 * @return ArrayList with field names
184 * @exception StorageObjectException is throuwn if database access was impossible
186 public ArrayList getFields () throws StorageObjectException {
187 return theStorageObject.getFields();
191 * Returns an int[] with the types of the fields
192 * @return int[] that contains the types of the fields
193 * @exception StorageObjectException
195 public int[] getTypes () throws StorageObjectException {
196 return theStorageObject.getTypes();
200 * Returns an ArrayList with field names
201 * @return List with field names
202 * @exception StorageObjectException
204 public ArrayList getLabels () throws StorageObjectException {
205 return theStorageObject.getLabels();
209 * Returns a Hashmap with all values of the Entity.
210 * @return HashMap with field name as key and the corresponding values
212 * @deprecated This method is deprecated and will be deleted in the next release.
213 * Entity interfaces freemarker.template.TemplateHashModel now and can
214 * be used in the same way as SimpleHash.
217 public HashMap getValues() {
218 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
219 return theValuesHash;
223 * Returns an ArrayList with all database fields that can
224 * be evaluated as streamedInput.
225 * Could be automated by the types (blob, etc.)
226 * Until now to be created manually in the inheriting class
228 * Liefert einen ArrayList mit allen Datenbankfeldern, die
229 * als streamedInput ausgelesen werden muessen.
230 * Waere automatisierbar ueber die types (blob, etc.)
231 * Bisher manuell anzulegen in der erbenden Klasse
234 public ArrayList streamedInput() {
235 return streamedInput;
238 /** Returns whether fieldName is a valid field name of this Entity.
240 * @return true in case fieldName is a field name, else false.
241 * @exception StorageObjectException
243 public boolean isField (String fieldName) throws StorageObjectException {
244 return theStorageObject.getFields().contains(fieldName);
247 /** Returns the number of instances of this Entity
248 * @return int The number of instances
250 public int getInstances() {
254 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
255 theLog.printError( e.toString() + " Funktion: "+ wo);
256 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
262 /*public void finalize () {
264 Integer i = new Integer(instances);
265 System.err.println("Removing abstract entity instance: "+i.toString());
268 } catch (Throwable t) {
269 System.err.println(t.toString());
274 // Now implements freemarkers TemplateHashModel
275 // two methods have to be overridden:
276 // 1. public boolean isEmpty() throws TemplateModelException
277 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
279 public boolean isEmpty() throws TemplateModelException
281 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
284 public TemplateModel get(java.lang.String key) throws TemplateModelException
286 return new SimpleScalar(getValue(key));
289 public void put(java.lang.String key, TemplateModel model)
291 // putting should only take place via setValue and is limited to the
292 // database fields associated with the entity. no additional freemarker
293 // stuff will be available via Entity.
294 theLog.printWarning("### put is called on entity! - the values will be lost!");
297 public void remove(java.lang.String key)
303 //////////////////////////////////////////////////////////////////////////////////