2 * Copyright (C) 2001, 2002 The Mir-coders group
\r
4 * This file is part of Mir.
\r
6 * Mir is free software; you can redistribute it and/or modify
\r
7 * it under the terms of the GNU General Public License as published by
\r
8 * the Free Software Foundation; either version 2 of the License, or
\r
9 * (at your option) any later version.
\r
11 * Mir is distributed in the hope that it will be useful,
\r
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
14 * GNU General Public License for more details.
\r
16 * You should have received a copy of the GNU General Public License
\r
17 * along with Mir; if not, write to the Free Software
\r
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
\r
20 * In addition, as a special exception, The Mir-coders gives permission to link
\r
21 * the code of this program with the com.oreilly.servlet library, any library
\r
22 * licensed under the Apache Software License, The Sun (tm) Java Advanced
\r
23 * Imaging library (JAI), The Sun JIMI library (or with modified versions of
\r
24 * the above that use the same license as the above), and distribute linked
\r
25 * combinations including the two. You must obey the GNU General Public
\r
26 * License in all respects for all of the code used other than the above
\r
27 * mentioned libraries. If you modify this file, you may extend this exception
\r
28 * to your version of the file, but you are not obligated to do so. If you do
\r
29 * not wish to do so, delete this exception statement from your version.
\r
33 * Base class the entities are derived from. Provides base functionality of
\r
34 * an entity. Entities are used to represent rows of a database table.<p>
\r
39 import java.util.List;
\r
40 import java.util.HashMap;
\r
41 import java.util.Iterator;
\r
42 import java.util.Set;
\r
44 import mir.config.MirPropertiesConfiguration;
\r
45 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
46 import mir.misc.Logfile;
\r
47 import mir.misc.StringUtil;
\r
48 import mir.storage.StorageObject;
\r
49 import mir.storage.StorageObjectExc;
\r
50 import mir.storage.StorageObjectFailure;
\r
51 import freemarker.template.SimpleScalar;
\r
52 import freemarker.template.TemplateHashModel;
\r
53 import freemarker.template.TemplateModel;
\r
54 import freemarker.template.TemplateModelException;
\r
55 import freemarker.template.TemplateModelRoot;
\r
58 * Base Class of Entities
\r
59 * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
\r
61 * @version $Id: Entity.java,v 1.14 2003/02/20 16:05:32 zapata Exp $
\r
66 public class Entity implements TemplateHashModel, TemplateModelRoot
\r
68 protected static MirPropertiesConfiguration configuration;
\r
69 protected static Logfile theLog;
\r
71 private boolean changed;
\r
72 protected HashMap theValuesHash; // tablekey / value
\r
73 protected StorageObject theStorageObject;
\r
74 protected List streamedInput = null;
\r
78 configuration = MirPropertiesConfiguration.instance();
\r
80 catch (PropertiesConfigExc e) {
\r
81 e.printStackTrace();
\r
83 theLog = Logfile.getInstance(configuration.getStringWithHome(
\r
88 this.changed = false;
\r
93 * @param StorageObject The StorageObject of the Entity.
\r
95 public Entity(StorageObject StorageObject) {
\r
97 setStorage(StorageObject);
\r
101 * Sets the StorageObject of the Entity.
\r
103 public void setStorage(StorageObject storage) {
\r
104 this.theStorageObject = storage;
\r
108 * Sets the values of the Entity.
\r
109 * @param theStringValues HashMap containing the new values of the Entity
\r
112 public void setValues(HashMap theStringValues) {
\r
113 /** @todo should be synchronized */
\r
114 if (theStringValues != null) {
\r
115 theValuesHash = new HashMap();
\r
117 Set set = theStringValues.keySet();
\r
118 Iterator it = set.iterator();
\r
119 int size = set.size();
\r
120 for (int i = 0; i < size; i++) {
\r
121 aKey = (String) it.next();
\r
122 theValuesHash.put(aKey, (String) theStringValues.get(aKey));
\r
126 theLog.printWarning("Entity.setValues called with null HashMap");
\r
130 * Returns whether the content of the Entity has changed.
\r
131 * @return true wenn ja, sonst false
\r
133 public boolean changed() {
\r
138 * Returns the primary key of the Entity.
\r
139 * @return String Id
\r
141 public String getId() {
\r
142 return (String) getValue(theStorageObject.getIdName());
\r
146 * Defines the primary key of the Entity
\r
149 public void setId(String id) {
\r
150 theValuesHash.put(theStorageObject.getIdName(), id);
\r
154 * Returns the value of a field by field name.
\r
155 * @param field The name of the field
\r
156 * @return value of the field
\r
158 public String getValue(String field) {
\r
159 String returnValue = null;
\r
160 if (field != null) {
\r
161 if (field.equals("webdb_create_formatted")) {
\r
162 if (hasValueForField("webdb_create"))
\r
163 returnValue = StringUtil.dateToReadableDate(getValue("webdb_create"));
\r
165 else if (field.equals("webdb_lastchange_formatted")) {
\r
166 if (hasValueForField("webdb_lastchange"))
\r
167 returnValue = StringUtil.dateToReadableDate(getValue(
\r
168 "webdb_lastchange"));
\r
171 returnValue = (String) theValuesHash.get(field);
\r
173 return returnValue;
\r
176 public boolean hasValueForField(String field) {
\r
177 if (theValuesHash != null)
\r
178 return theValuesHash.containsKey(field);
\r
183 * Insers Entity into the database via StorageObject
\r
184 * @return Primary Key of the Entity
\r
185 * @exception StorageObjectException
\r
187 public String insert() throws StorageObjectExc {
\r
188 theLog.printDebugInfo("Entity: trying to insert ...");
\r
189 if (theStorageObject != null) {
\r
190 return theStorageObject.insert( (Entity)this);
\r
193 throw new StorageObjectExc("Kein StorageObject gesetzt!");
\r
197 * Saves changes of this Entity to the database
\r
198 * @exception StorageObjectException
\r
200 public void update() throws StorageObjectFailure {
\r
201 theStorageObject.update( (Entity)this);
\r
205 * Sets the value for a field. Issues a log message if the field name
\r
206 * supplied was not found in the Entity.
\r
207 * @param theProp The field name whose value has to be set
\r
208 * @param theValue The new value of the field
\r
209 * @exception StorageObjectException
\r
211 public void setValueForProperty(String theProp, String theValue) throws
\r
212 StorageObjectFailure {
\r
213 this.changed = true;
\r
214 if (isField(theProp))
\r
215 theValuesHash.put(theProp, theValue);
\r
217 theLog.printWarning("Property not found: " + theProp + theValue);
\r
223 * Returns the field names of the Entity as ArrayListe.
\r
224 * @return ArrayList with field names
\r
225 * @exception StorageObjectException is throuwn if database access was impossible
\r
227 public List getFields() throws StorageObjectFailure {
\r
228 return theStorageObject.getFields();
\r
232 * Returns an int[] with the types of the fields
\r
233 * @return int[] that contains the types of the fields
\r
234 * @exception StorageObjectException
\r
236 public int[] getTypes() throws StorageObjectFailure {
\r
237 return theStorageObject.getTypes();
\r
241 * Returns an ArrayList with field names
\r
242 * @return List with field names
\r
243 * @exception StorageObjectException
\r
245 public List getLabels() throws StorageObjectFailure {
\r
246 return theStorageObject.getLabels();
\r
250 * Returns a Hashmap with all values of the Entity.
\r
251 * @return HashMap with field name as key and the corresponding values
\r
253 * @deprecated This method is deprecated and will be deleted in the next release.
\r
254 * Entity interfaces freemarker.template.TemplateHashModel now and can
\r
255 * be used in the same way as SimpleHash.
\r
257 public HashMap getValues() {
\r
258 theLog.printWarning(
\r
259 "## using deprecated Entity.getValues() - a waste of resources");
\r
260 return theValuesHash;
\r
264 * Returns an ArrayList with all database fields that can
\r
265 * be evaluated as streamedInput.
\r
266 * Could be automated by the types (blob, etc.)
\r
267 * Until now to be created manually in the inheriting class
\r
269 * Liefert einen ArrayList mit allen Datenbankfeldern, die
\r
270 * als streamedInput ausgelesen werden muessen.
\r
271 * Waere automatisierbar ueber die types (blob, etc.)
\r
272 * Bisher manuell anzulegen in der erbenden Klasse
\r
275 public List streamedInput() {
\r
276 return streamedInput;
\r
279 /** Returns whether fieldName is a valid field name of this Entity.
\r
281 * @return true in case fieldName is a field name, else false.
\r
282 * @exception StorageObjectException
\r
284 public boolean isField(String fieldName) throws StorageObjectFailure {
\r
285 return theStorageObject.getFields().contains(fieldName);
\r
288 protected void throwStorageObjectFailure(Exception e, String wo) throws
\r
289 StorageObjectFailure {
\r
290 theLog.printError(e.toString() + " Funktion: " + wo);
\r
291 e.printStackTrace(System.out);
\r
292 throw new StorageObjectFailure("Storage Object Exception in entity", e);
\r
295 // Now implements freemarkers TemplateHashModel
\r
296 // two methods have to be overridden:
\r
297 // 1. public boolean isEmpty() throws TemplateModelException
\r
298 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
\r
300 public boolean isEmpty() throws TemplateModelException {
\r
301 return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;
\r
304 public TemplateModel get(java.lang.String key) throws TemplateModelException {
\r
305 return new SimpleScalar(getValue(key));
\r
308 public void put(java.lang.String key, TemplateModel model) {
\r
309 // putting should only take place via setValue and is limited to the
\r
310 // database fields associated with the entity. no additional freemarker
\r
311 // stuff will be available via Entity.
\r
312 theLog.printWarning(
\r
313 "### put is called on entity! - the values will be lost!");
\r
316 public void remove(java.lang.String key) {
\r
317 // do we need this?
\r
320 //////////////////////////////////////////////////////////////////////////////////
\r