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
23 * @version $Revision: 1.8 $ $Date: 2002/06/28 22:56:37 $
24 * @author $Author: mh $
26 * $Log: Entity.java,v $
27 * Revision 1.8 2002/06/28 22:56:37 mh
28 * fix so that webdb_lastchange appears in contentlist.template as it should
30 * Revision 1.7 2002/06/28 20:35:38 mh
31 * use some cvs macros/id's
36 public class Entity implements TemplateHashModel, TemplateModelRoot
38 private boolean changed;
39 protected HashMap theValuesHash; // tablekey / value
40 protected StorageObject theStorageObject;
41 protected static Logfile theLog;
42 protected ArrayList streamedInput=null;
43 private static int instances = 0;
45 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
52 Integer i = new Integer(instances);
53 //System.err.println("New abstract entity instance: "+i.toString());
58 * @param StorageObject The StorageObject of the Entity.
60 public Entity (StorageObject StorageObject) {
62 setStorage(StorageObject);
66 * Sets the StorageObject of the Entity.
68 public void setStorage (StorageObject storage) {
69 this.theStorageObject = storage;
73 * Sets the values of the Entity.
74 * @param theStringValues HashMap containing the new values of the Entity
77 public void setValues(HashMap theStringValues)
79 /** @todo should be synchronized */
80 if (theStringValues!=null) {
81 theValuesHash = new HashMap();
83 Set set = theStringValues.keySet();
84 Iterator it = set.iterator();
85 int size = set.size();
86 for (int i = 0; i < size; i++) {
87 aKey = (String)it.next();
88 theValuesHash.put(aKey, (String)theStringValues.get(aKey));
91 else theLog.printWarning("Entity.setValues called with null HashMap");
95 * Returns whether the content of the Entity has changed.
96 * @return true wenn ja, sonst false
98 public boolean changed () {
103 * Returns the primary key of the Entity.
106 public String getId () {
107 return (String)getValue(theStorageObject.getIdName());
111 * Defines the primary key of the Entity
114 public void setId (String id) {
115 theValuesHash.put(theStorageObject.getIdName(), id);
119 * Returns the value of a field by field name.
120 * @param field The name of the field
121 * @return value of the field
123 public String getValue (String field) {
124 String returnValue = null;
127 if (field.equals("webdb_create_formatted"))
129 if (hasValueForField("webdb_create"))
130 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
132 else if (field.equals("webdb_lastchange_formatted"))
134 if (hasValueForField("webdb_lastchange"))
135 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
137 else if (field.equals("webdb_create_dc"))
139 if (hasValueForField("webdb_create"))
140 returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
143 returnValue = (String)theValuesHash.get(field);
148 public boolean hasValueForField(String field)
150 if (theValuesHash!=null)
151 return theValuesHash.containsKey(field);
156 * Insers Entity into the database via StorageObject
157 * @return Primary Key of the Entity
158 * @exception StorageObjectException
160 public String insert () throws StorageObjectException {
161 theLog.printDebugInfo("Entity: trying to insert ...");
162 if (theStorageObject != null) {
163 return theStorageObject.insert((Entity)this);
166 throw new StorageObjectException("Kein StorageObject gesetzt!");
170 * Saves changes of this Entity to the database
171 * @exception StorageObjectException
173 public void update () throws StorageObjectException {
174 theStorageObject.update((Entity)this);
178 * Sets the value for a field. Issues a log message if the field name
179 * supplied was not found in the Entity.
180 * @param theProp The field name whose value has to be set
181 * @param theValue The new value of the field
182 * @exception StorageObjectException
184 public void setValueForProperty (String theProp, String theValue)
185 throws StorageObjectException {
187 if (isField(theProp))
188 theValuesHash.put(theProp, theValue);
190 theLog.printWarning("Property not found: " + theProp+theValue);
196 * Returns the field names of the Entity as ArrayListe.
197 * @return ArrayList with field names
198 * @exception StorageObjectException is throuwn if database access was impossible
200 public ArrayList getFields () throws StorageObjectException {
201 return theStorageObject.getFields();
205 * Returns an int[] with the types of the fields
206 * @return int[] that contains the types of the fields
207 * @exception StorageObjectException
209 public int[] getTypes () throws StorageObjectException {
210 return theStorageObject.getTypes();
214 * Returns an ArrayList with field names
215 * @return List with field names
216 * @exception StorageObjectException
218 public ArrayList getLabels () throws StorageObjectException {
219 return theStorageObject.getLabels();
223 * Returns a Hashmap with all values of the Entity.
224 * @return HashMap with field name as key and the corresponding values
226 * @deprecated This method is deprecated and will be deleted in the next release.
227 * Entity interfaces freemarker.template.TemplateHashModel now and can
228 * be used in the same way as SimpleHash.
231 public HashMap getValues() {
232 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
233 return theValuesHash;
237 * Returns an ArrayList with all database fields that can
238 * be evaluated as streamedInput.
239 * Could be automated by the types (blob, etc.)
240 * Until now to be created manually in the inheriting class
242 * Liefert einen ArrayList mit allen Datenbankfeldern, die
243 * als streamedInput ausgelesen werden muessen.
244 * Waere automatisierbar ueber die types (blob, etc.)
245 * Bisher manuell anzulegen in der erbenden Klasse
248 public ArrayList streamedInput() {
249 return streamedInput;
252 /** Returns whether fieldName is a valid field name of this Entity.
254 * @return true in case fieldName is a field name, else false.
255 * @exception StorageObjectException
257 public boolean isField (String fieldName) throws StorageObjectException {
258 return theStorageObject.getFields().contains(fieldName);
261 /** Returns the number of instances of this Entity
262 * @return int The number of instances
264 public int getInstances() {
268 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
269 theLog.printError( e.toString() + " Funktion: "+ wo);
270 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
276 /*public void finalize () {
278 Integer i = new Integer(instances);
279 System.err.println("Removing abstract entity instance: "+i.toString());
282 } catch (Throwable t) {
283 System.err.println(t.toString());
288 // Now implements freemarkers TemplateHashModel
289 // two methods have to be overridden:
290 // 1. public boolean isEmpty() throws TemplateModelException
291 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
293 public boolean isEmpty() throws TemplateModelException
295 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
298 public TemplateModel get(java.lang.String key) throws TemplateModelException
300 return new SimpleScalar(getValue(key));
303 public void put(java.lang.String key, TemplateModel model)
305 // putting should only take place via setValue and is limited to the
306 // database fields associated with the entity. no additional freemarker
307 // stuff will be available via Entity.
308 theLog.printWarning("### put is called on entity! - the values will be lost!");
311 public void remove(java.lang.String key)
317 //////////////////////////////////////////////////////////////////////////////////