2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
6 * Mir is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * Mir is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with Mir; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * In addition, as a special exception, The Mir-coders gives permission to link
21 * the code of this program with the com.oreilly.servlet library, any library
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24 * the above that use the same license as the above), and distribute linked
25 * combinations including the two. You must obey the GNU General Public
26 * License in all respects for all of the code used other than the above
27 * mentioned libraries. If you modify this file, you may extend this exception
28 * to your version of the file, but you are not obligated to do so. If you do
29 * not wish to do so, delete this exception statement from your version.
33 * Base class the entities are derived from. Provides base functionality of
34 * an entity. Entities are used to represent rows of a database table.<p>
44 import freemarker.template.*;
50 * Base Class of Entities
51 * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
53 * @version $Id: Entity.java,v 1.10 2002/11/04 04:35:21 mh Exp $
58 public class Entity implements TemplateHashModel, TemplateModelRoot
60 private boolean changed;
61 protected HashMap theValuesHash; // tablekey / value
62 protected StorageObject theStorageObject;
63 protected static Logfile theLog;
64 protected ArrayList streamedInput=null;
65 private static int instances = 0;
67 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
74 Integer i = new Integer(instances);
75 //System.err.println("New abstract entity instance: "+i.toString());
80 * @param StorageObject The StorageObject of the Entity.
82 public Entity (StorageObject StorageObject) {
84 setStorage(StorageObject);
88 * Sets the StorageObject of the Entity.
90 public void setStorage (StorageObject storage) {
91 this.theStorageObject = storage;
95 * Sets the values of the Entity.
96 * @param theStringValues HashMap containing the new values of the Entity
99 public void setValues(HashMap theStringValues)
101 /** @todo should be synchronized */
102 if (theStringValues!=null) {
103 theValuesHash = new HashMap();
105 Set set = theStringValues.keySet();
106 Iterator it = set.iterator();
107 int size = set.size();
108 for (int i = 0; i < size; i++) {
109 aKey = (String)it.next();
110 theValuesHash.put(aKey, (String)theStringValues.get(aKey));
113 else theLog.printWarning("Entity.setValues called with null HashMap");
117 * Returns whether the content of the Entity has changed.
118 * @return true wenn ja, sonst false
120 public boolean changed () {
125 * Returns the primary key of the Entity.
128 public String getId () {
129 return (String)getValue(theStorageObject.getIdName());
133 * Defines the primary key of the Entity
136 public void setId (String id) {
137 theValuesHash.put(theStorageObject.getIdName(), id);
141 * Returns the value of a field by field name.
142 * @param field The name of the field
143 * @return value of the field
145 public String getValue (String field) {
146 String returnValue = null;
149 if (field.equals("webdb_create_formatted"))
151 if (hasValueForField("webdb_create"))
152 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
154 else if (field.equals("webdb_lastchange_formatted"))
156 if (hasValueForField("webdb_lastchange"))
157 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
159 else if (field.equals("webdb_create_dc"))
161 if (hasValueForField("webdb_create"))
162 returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
165 returnValue = (String)theValuesHash.get(field);
170 public boolean hasValueForField(String field)
172 if (theValuesHash!=null)
173 return theValuesHash.containsKey(field);
178 * Insers Entity into the database via StorageObject
179 * @return Primary Key of the Entity
180 * @exception StorageObjectException
182 public String insert () throws StorageObjectException {
183 theLog.printDebugInfo("Entity: trying to insert ...");
184 if (theStorageObject != null) {
185 return theStorageObject.insert((Entity)this);
188 throw new StorageObjectException("Kein StorageObject gesetzt!");
192 * Saves changes of this Entity to the database
193 * @exception StorageObjectException
195 public void update () throws StorageObjectException {
196 theStorageObject.update((Entity)this);
200 * Sets the value for a field. Issues a log message if the field name
201 * supplied was not found in the Entity.
202 * @param theProp The field name whose value has to be set
203 * @param theValue The new value of the field
204 * @exception StorageObjectException
206 public void setValueForProperty (String theProp, String theValue)
207 throws StorageObjectException {
209 if (isField(theProp))
210 theValuesHash.put(theProp, theValue);
212 theLog.printWarning("Property not found: " + theProp+theValue);
218 * Returns the field names of the Entity as ArrayListe.
219 * @return ArrayList with field names
220 * @exception StorageObjectException is throuwn if database access was impossible
222 public ArrayList getFields () throws StorageObjectException {
223 return theStorageObject.getFields();
227 * Returns an int[] with the types of the fields
228 * @return int[] that contains the types of the fields
229 * @exception StorageObjectException
231 public int[] getTypes () throws StorageObjectException {
232 return theStorageObject.getTypes();
236 * Returns an ArrayList with field names
237 * @return List with field names
238 * @exception StorageObjectException
240 public ArrayList getLabels () throws StorageObjectException {
241 return theStorageObject.getLabels();
245 * Returns a Hashmap with all values of the Entity.
246 * @return HashMap with field name as key and the corresponding values
248 * @deprecated This method is deprecated and will be deleted in the next release.
249 * Entity interfaces freemarker.template.TemplateHashModel now and can
250 * be used in the same way as SimpleHash.
253 public HashMap getValues() {
254 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
255 return theValuesHash;
259 * Returns an ArrayList with all database fields that can
260 * be evaluated as streamedInput.
261 * Could be automated by the types (blob, etc.)
262 * Until now to be created manually in the inheriting class
264 * Liefert einen ArrayList mit allen Datenbankfeldern, die
265 * als streamedInput ausgelesen werden muessen.
266 * Waere automatisierbar ueber die types (blob, etc.)
267 * Bisher manuell anzulegen in der erbenden Klasse
270 public ArrayList streamedInput() {
271 return streamedInput;
274 /** Returns whether fieldName is a valid field name of this Entity.
276 * @return true in case fieldName is a field name, else false.
277 * @exception StorageObjectException
279 public boolean isField (String fieldName) throws StorageObjectException {
280 return theStorageObject.getFields().contains(fieldName);
283 /** Returns the number of instances of this Entity
284 * @return int The number of instances
286 public int getInstances() {
290 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
291 theLog.printError( e.toString() + " Funktion: "+ wo);
292 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
298 /*public void finalize () {
300 Integer i = new Integer(instances);
301 System.err.println("Removing abstract entity instance: "+i.toString());
304 } catch (Throwable t) {
305 System.err.println(t.toString());
310 // Now implements freemarkers TemplateHashModel
311 // two methods have to be overridden:
312 // 1. public boolean isEmpty() throws TemplateModelException
313 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
315 public boolean isEmpty() throws TemplateModelException
317 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
320 public TemplateModel get(java.lang.String key) throws TemplateModelException
322 return new SimpleScalar(getValue(key));
325 public void put(java.lang.String key, TemplateModel model)
327 // putting should only take place via setValue and is limited to the
328 // database fields associated with the entity. no additional freemarker
329 // stuff will be available via Entity.
330 theLog.printWarning("### put is called on entity! - the values will be lost!");
333 public void remove(java.lang.String key)
339 //////////////////////////////////////////////////////////////////////////////////