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>
39 import java.util.ArrayList;
40 import java.util.HashMap;
41 import java.util.Iterator;
44 import mir.config.MirPropertiesConfiguration;
45 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
46 import mir.misc.Logfile;
47 import mir.misc.StringUtil;
48 import mir.storage.StorageObject;
49 import mir.storage.StorageObjectExc;
50 import mir.storage.StorageObjectFailure;
51 import freemarker.template.SimpleScalar;
52 import freemarker.template.TemplateHashModel;
53 import freemarker.template.TemplateModel;
54 import freemarker.template.TemplateModelException;
55 import freemarker.template.TemplateModelRoot;
58 * Base Class of Entities
59 * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
61 * @version $Id: Entity.java,v 1.13 2003/01/25 17:45:17 idfx Exp $
66 public class Entity implements TemplateHashModel, TemplateModelRoot
68 protected static MirPropertiesConfiguration configuration;
69 protected static Logfile theLog;
71 private boolean changed;
72 protected HashMap theValuesHash; // tablekey / value
73 protected StorageObject theStorageObject;
74 protected ArrayList streamedInput=null;
79 configuration = MirPropertiesConfiguration.instance();
80 } catch (PropertiesConfigExc e) {
83 theLog = Logfile.getInstance(configuration.getStringWithHome("Entity.Logfile"));
92 * @param StorageObject The StorageObject of the Entity.
94 public Entity (StorageObject StorageObject) {
96 setStorage(StorageObject);
100 * Sets the StorageObject of the Entity.
102 public void setStorage (StorageObject storage) {
103 this.theStorageObject = storage;
107 * Sets the values of the Entity.
108 * @param theStringValues HashMap containing the new values of the Entity
111 public void setValues(HashMap theStringValues)
113 /** @todo should be synchronized */
114 if (theStringValues!=null) {
115 theValuesHash = new HashMap();
117 Set set = theStringValues.keySet();
118 Iterator it = set.iterator();
119 int size = set.size();
120 for (int i = 0; i < size; i++) {
121 aKey = (String)it.next();
122 theValuesHash.put(aKey, (String)theStringValues.get(aKey));
125 else theLog.printWarning("Entity.setValues called with null HashMap");
129 * Returns whether the content of the Entity has changed.
130 * @return true wenn ja, sonst false
132 public boolean changed () {
137 * Returns the primary key of the Entity.
140 public String getId () {
141 return (String) getValue(theStorageObject.getIdName());
145 * Defines the primary key of the Entity
148 public void setId (String id) {
149 theValuesHash.put(theStorageObject.getIdName(), id);
153 * Returns the value of a field by field name.
154 * @param field The name of the field
155 * @return value of the field
157 public String getValue (String field) {
158 String returnValue = null;
161 if (field.equals("webdb_create_formatted"))
163 if (hasValueForField("webdb_create"))
164 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
166 else if (field.equals("webdb_lastchange_formatted"))
168 if (hasValueForField("webdb_lastchange"))
169 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
172 returnValue = (String)theValuesHash.get(field);
177 public boolean hasValueForField(String field)
179 if (theValuesHash!=null)
180 return theValuesHash.containsKey(field);
185 * Insers Entity into the database via StorageObject
186 * @return Primary Key of the Entity
187 * @exception StorageObjectException
189 public String insert () throws StorageObjectExc {
190 theLog.printDebugInfo("Entity: trying to insert ...");
191 if (theStorageObject != null) {
192 return theStorageObject.insert((Entity)this);
195 throw new StorageObjectExc("Kein StorageObject gesetzt!");
199 * Saves changes of this Entity to the database
200 * @exception StorageObjectException
202 public void update () throws StorageObjectFailure {
203 theStorageObject.update((Entity)this);
207 * Sets the value for a field. Issues a log message if the field name
208 * supplied was not found in the Entity.
209 * @param theProp The field name whose value has to be set
210 * @param theValue The new value of the field
211 * @exception StorageObjectException
213 public void setValueForProperty (String theProp, String theValue)
214 throws StorageObjectFailure {
216 if (isField(theProp))
217 theValuesHash.put(theProp, theValue);
219 theLog.printWarning("Property not found: " + theProp+theValue);
225 * Returns the field names of the Entity as ArrayListe.
226 * @return ArrayList with field names
227 * @exception StorageObjectException is throuwn if database access was impossible
229 public ArrayList getFields () throws StorageObjectFailure {
230 return theStorageObject.getFields();
234 * Returns an int[] with the types of the fields
235 * @return int[] that contains the types of the fields
236 * @exception StorageObjectException
238 public int[] getTypes () throws StorageObjectFailure {
239 return theStorageObject.getTypes();
243 * Returns an ArrayList with field names
244 * @return List with field names
245 * @exception StorageObjectException
247 public ArrayList getLabels () throws StorageObjectFailure {
248 return theStorageObject.getLabels();
252 * Returns a Hashmap with all values of the Entity.
253 * @return HashMap with field name as key and the corresponding values
255 * @deprecated This method is deprecated and will be deleted in the next release.
256 * Entity interfaces freemarker.template.TemplateHashModel now and can
257 * be used in the same way as SimpleHash.
260 public HashMap getValues() {
261 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
262 return theValuesHash;
266 * Returns an ArrayList with all database fields that can
267 * be evaluated as streamedInput.
268 * Could be automated by the types (blob, etc.)
269 * Until now to be created manually in the inheriting class
271 * Liefert einen ArrayList mit allen Datenbankfeldern, die
272 * als streamedInput ausgelesen werden muessen.
273 * Waere automatisierbar ueber die types (blob, etc.)
274 * Bisher manuell anzulegen in der erbenden Klasse
277 public ArrayList streamedInput() {
278 return streamedInput;
281 /** Returns whether fieldName is a valid field name of this Entity.
283 * @return true in case fieldName is a field name, else false.
284 * @exception StorageObjectException
286 public boolean isField (String fieldName) throws StorageObjectFailure {
287 return theStorageObject.getFields().contains(fieldName);
292 protected void throwStorageObjectFailure (Exception e, String wo)
293 throws StorageObjectFailure {
294 theLog.printError( e.toString() + " Funktion: "+ wo);
295 e.printStackTrace(System.out);
296 throw new StorageObjectFailure("Storage Object Exception in entity", e);
299 // Now implements freemarkers TemplateHashModel
300 // two methods have to be overridden:
301 // 1. public boolean isEmpty() throws TemplateModelException
302 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
304 public boolean isEmpty() throws TemplateModelException
306 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
309 public TemplateModel get(java.lang.String key) throws TemplateModelException
311 return new SimpleScalar(getValue(key));
314 public void put(java.lang.String key, TemplateModel model)
316 // putting should only take place via setValue and is limited to the
317 // database fields associated with the entity. no additional freemarker
318 // stuff will be available via Entity.
319 theLog.printWarning("### put is called on entity! - the values will be lost!");
322 public void remove(java.lang.String key)
328 //////////////////////////////////////////////////////////////////////////////////