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.7 $ $Date: 2002/06/28 20:35:38 $
24 * @author $Author: mh $
26 * $Log: Entity.java,v $
27 * Revision 1.7 2002/06/28 20:35:38 mh
28 * use some cvs macros/id's
33 public class Entity implements TemplateHashModel, TemplateModelRoot
35 private boolean changed;
36 protected HashMap theValuesHash; // tablekey / value
37 protected StorageObject theStorageObject;
38 protected static Logfile theLog;
39 protected ArrayList streamedInput=null;
40 private static int instances = 0;
42 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
49 Integer i = new Integer(instances);
50 //System.err.println("New abstract entity instance: "+i.toString());
55 * @param StorageObject The StorageObject of the Entity.
57 public Entity (StorageObject StorageObject) {
59 setStorage(StorageObject);
63 * Sets the StorageObject of the Entity.
65 public void setStorage (StorageObject storage) {
66 this.theStorageObject = storage;
70 * Sets the values of the Entity.
71 * @param theStringValues HashMap containing the new values of the Entity
74 public void setValues(HashMap theStringValues)
76 /** @todo should be synchronized */
77 if (theStringValues!=null) {
78 theValuesHash = new HashMap();
80 Set set = theStringValues.keySet();
81 Iterator it = set.iterator();
82 int size = set.size();
83 for (int i = 0; i < size; i++) {
84 aKey = (String)it.next();
85 theValuesHash.put(aKey, (String)theStringValues.get(aKey));
88 else theLog.printWarning("Entity.setValues called with null HashMap");
92 * Returns whether the content of the Entity has changed.
93 * @return true wenn ja, sonst false
95 public boolean changed () {
100 * Returns the primary key of the Entity.
103 public String getId () {
104 return (String)getValue(theStorageObject.getIdName());
108 * Defines the primary key of the Entity
111 public void setId (String id) {
112 theValuesHash.put(theStorageObject.getIdName(), id);
116 * Returns the value of a field by field name.
117 * @param field The name of the field
118 * @return value of the field
120 public String getValue (String field) {
121 String returnValue = null;
124 if (field.equals("webdb_create_formatted"))
126 if (hasValueForField("webdb_create"))
127 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
129 else if (field.equals("webdb_lastchange_formatted"))
131 if (hasValueForField("webdblast_change"))
132 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
134 else if (field.equals("webdb_create_dc"))
136 if (hasValueForField("webdb_create"))
137 returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
140 returnValue = (String)theValuesHash.get(field);
145 public boolean hasValueForField(String field)
147 if (theValuesHash!=null)
148 return theValuesHash.containsKey(field);
153 * Insers Entity into the database via StorageObject
154 * @return Primary Key of the Entity
155 * @exception StorageObjectException
157 public String insert () throws StorageObjectException {
158 theLog.printDebugInfo("Entity: trying to insert ...");
159 if (theStorageObject != null) {
160 return theStorageObject.insert((Entity)this);
163 throw new StorageObjectException("Kein StorageObject gesetzt!");
167 * Saves changes of this Entity to the database
168 * @exception StorageObjectException
170 public void update () throws StorageObjectException {
171 theStorageObject.update((Entity)this);
175 * Sets the value for a field. Issues a log message if the field name
176 * supplied was not found in the Entity.
177 * @param theProp The field name whose value has to be set
178 * @param theValue The new value of the field
179 * @exception StorageObjectException
181 public void setValueForProperty (String theProp, String theValue)
182 throws StorageObjectException {
184 if (isField(theProp))
185 theValuesHash.put(theProp, theValue);
187 theLog.printWarning("Property not found: " + theProp+theValue);
193 * Returns the field names of the Entity as ArrayListe.
194 * @return ArrayList with field names
195 * @exception StorageObjectException is throuwn if database access was impossible
197 public ArrayList getFields () throws StorageObjectException {
198 return theStorageObject.getFields();
202 * Returns an int[] with the types of the fields
203 * @return int[] that contains the types of the fields
204 * @exception StorageObjectException
206 public int[] getTypes () throws StorageObjectException {
207 return theStorageObject.getTypes();
211 * Returns an ArrayList with field names
212 * @return List with field names
213 * @exception StorageObjectException
215 public ArrayList getLabels () throws StorageObjectException {
216 return theStorageObject.getLabels();
220 * Returns a Hashmap with all values of the Entity.
221 * @return HashMap with field name as key and the corresponding values
223 * @deprecated This method is deprecated and will be deleted in the next release.
224 * Entity interfaces freemarker.template.TemplateHashModel now and can
225 * be used in the same way as SimpleHash.
228 public HashMap getValues() {
229 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
230 return theValuesHash;
234 * Returns an ArrayList with all database fields that can
235 * be evaluated as streamedInput.
236 * Could be automated by the types (blob, etc.)
237 * Until now to be created manually in the inheriting class
239 * Liefert einen ArrayList mit allen Datenbankfeldern, die
240 * als streamedInput ausgelesen werden muessen.
241 * Waere automatisierbar ueber die types (blob, etc.)
242 * Bisher manuell anzulegen in der erbenden Klasse
245 public ArrayList streamedInput() {
246 return streamedInput;
249 /** Returns whether fieldName is a valid field name of this Entity.
251 * @return true in case fieldName is a field name, else false.
252 * @exception StorageObjectException
254 public boolean isField (String fieldName) throws StorageObjectException {
255 return theStorageObject.getFields().contains(fieldName);
258 /** Returns the number of instances of this Entity
259 * @return int The number of instances
261 public int getInstances() {
265 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
266 theLog.printError( e.toString() + " Funktion: "+ wo);
267 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
273 /*public void finalize () {
275 Integer i = new Integer(instances);
276 System.err.println("Removing abstract entity instance: "+i.toString());
279 } catch (Throwable t) {
280 System.err.println(t.toString());
285 // Now implements freemarkers TemplateHashModel
286 // two methods have to be overridden:
287 // 1. public boolean isEmpty() throws TemplateModelException
288 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
290 public boolean isEmpty() throws TemplateModelException
292 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
295 public TemplateModel get(java.lang.String key) throws TemplateModelException
297 return new SimpleScalar(getValue(key));
300 public void put(java.lang.String key, TemplateModel model)
302 // putting should only take place via setValue and is limited to the
303 // database fields associated with the entity. no additional freemarker
304 // stuff will be available via Entity.
305 theLog.printWarning("### put is called on entity! - the values will be lost!");
308 public void remove(java.lang.String key)
314 //////////////////////////////////////////////////////////////////////////////////