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.11 2002/12/02 12:33:22 zapata 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"));
160 returnValue = (String)theValuesHash.get(field);
165 public boolean hasValueForField(String field)
167 if (theValuesHash!=null)
168 return theValuesHash.containsKey(field);
173 * Insers Entity into the database via StorageObject
174 * @return Primary Key of the Entity
175 * @exception StorageObjectException
177 public String insert () throws StorageObjectException {
178 theLog.printDebugInfo("Entity: trying to insert ...");
179 if (theStorageObject != null) {
180 return theStorageObject.insert((Entity)this);
183 throw new StorageObjectException("Kein StorageObject gesetzt!");
187 * Saves changes of this Entity to the database
188 * @exception StorageObjectException
190 public void update () throws StorageObjectException {
191 theStorageObject.update((Entity)this);
195 * Sets the value for a field. Issues a log message if the field name
196 * supplied was not found in the Entity.
197 * @param theProp The field name whose value has to be set
198 * @param theValue The new value of the field
199 * @exception StorageObjectException
201 public void setValueForProperty (String theProp, String theValue)
202 throws StorageObjectException {
204 if (isField(theProp))
205 theValuesHash.put(theProp, theValue);
207 theLog.printWarning("Property not found: " + theProp+theValue);
213 * Returns the field names of the Entity as ArrayListe.
214 * @return ArrayList with field names
215 * @exception StorageObjectException is throuwn if database access was impossible
217 public ArrayList getFields () throws StorageObjectException {
218 return theStorageObject.getFields();
222 * Returns an int[] with the types of the fields
223 * @return int[] that contains the types of the fields
224 * @exception StorageObjectException
226 public int[] getTypes () throws StorageObjectException {
227 return theStorageObject.getTypes();
231 * Returns an ArrayList with field names
232 * @return List with field names
233 * @exception StorageObjectException
235 public ArrayList getLabels () throws StorageObjectException {
236 return theStorageObject.getLabels();
240 * Returns a Hashmap with all values of the Entity.
241 * @return HashMap with field name as key and the corresponding values
243 * @deprecated This method is deprecated and will be deleted in the next release.
244 * Entity interfaces freemarker.template.TemplateHashModel now and can
245 * be used in the same way as SimpleHash.
248 public HashMap getValues() {
249 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
250 return theValuesHash;
254 * Returns an ArrayList with all database fields that can
255 * be evaluated as streamedInput.
256 * Could be automated by the types (blob, etc.)
257 * Until now to be created manually in the inheriting class
259 * Liefert einen ArrayList mit allen Datenbankfeldern, die
260 * als streamedInput ausgelesen werden muessen.
261 * Waere automatisierbar ueber die types (blob, etc.)
262 * Bisher manuell anzulegen in der erbenden Klasse
265 public ArrayList streamedInput() {
266 return streamedInput;
269 /** Returns whether fieldName is a valid field name of this Entity.
271 * @return true in case fieldName is a field name, else false.
272 * @exception StorageObjectException
274 public boolean isField (String fieldName) throws StorageObjectException {
275 return theStorageObject.getFields().contains(fieldName);
278 /** Returns the number of instances of this Entity
279 * @return int The number of instances
281 public int getInstances() {
285 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
286 theLog.printError( e.toString() + " Funktion: "+ wo);
287 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
290 // Now implements freemarkers TemplateHashModel
291 // two methods have to be overridden:
292 // 1. public boolean isEmpty() throws TemplateModelException
293 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
295 public boolean isEmpty() throws TemplateModelException
297 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
300 public TemplateModel get(java.lang.String key) throws TemplateModelException
302 return new SimpleScalar(getValue(key));
305 public void put(java.lang.String key, TemplateModel model)
307 // putting should only take place via setValue and is limited to the
308 // database fields associated with the entity. no additional freemarker
309 // stuff will be available via Entity.
310 theLog.printWarning("### put is called on entity! - the values will be lost!");
313 public void remove(java.lang.String key)
319 //////////////////////////////////////////////////////////////////////////////////