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.HashMap;
\r
40 import java.util.List;
\r
41 import java.util.Map;
\r
43 import mir.config.MirPropertiesConfiguration;
\r
44 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
\r
45 import mir.log.LoggerWrapper;
\r
46 import mir.misc.StringUtil;
\r
47 import mir.storage.StorageObject;
\r
48 import mir.storage.StorageObjectExc;
\r
49 import mir.storage.StorageObjectFailure;
\r
50 import freemarker.template.SimpleScalar;
\r
51 import freemarker.template.TemplateHashModel;
\r
52 import freemarker.template.TemplateModel;
\r
53 import freemarker.template.TemplateModelException;
\r
54 import freemarker.template.TemplateModelRoot;
\r
57 * Base Class of Entities
\r
58 * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
\r
60 * @version $Id: Entity.java,v 1.18 2003/03/05 19:23:14 idfx Exp $
\r
65 public class Entity implements TemplateHashModel, TemplateModelRoot
\r
67 protected static MirPropertiesConfiguration configuration;
\r
69 private boolean changed;
\r
70 protected Map theValuesHash; // tablekey / value
\r
71 protected StorageObject theStorageObject;
\r
72 protected List streamedInput = null;
\r
73 protected LoggerWrapper logger;
\r
77 configuration = MirPropertiesConfiguration.instance();
\r
79 catch (PropertiesConfigExc e) {
\r
80 throw new RuntimeException(e.getMessage());
\r
85 logger = new LoggerWrapper("Entity");
\r
87 this.changed = false;
\r
92 * @param StorageObject The StorageObject of the Entity.
\r
94 public Entity(StorageObject StorageObject) {
\r
96 setStorage(StorageObject);
\r
100 * Sets the StorageObject of the Entity.
\r
102 public void setStorage(StorageObject storage) {
\r
103 this.theStorageObject = storage;
\r
107 * Sets the values of the Entity.
\r
108 * @param theStringValues Map containing the new values of the Entity
\r
111 public void setValues(Map theStringValues) {
\r
112 /** @todo should be synchronized */
\r
113 if (theStringValues != null) {
\r
114 theValuesHash = new HashMap();
\r
115 theValuesHash.putAll(theStringValues);
\r
118 logger.warn("Entity.setValues called with null Map");
\r
122 * Returns whether the content of the Entity has changed.
\r
123 * @return true wenn ja, sonst false
\r
125 public boolean changed() {
\r
130 * Returns the primary key of the Entity.
\r
131 * @return String Id
\r
133 public String getId() {
\r
134 return (String) getValue(theStorageObject.getIdName());
\r
138 * Defines the primary key of the Entity
\r
141 public void setId(String id) {
\r
142 theValuesHash.put(theStorageObject.getIdName(), id);
\r
146 * Returns the value of a field by field name.
\r
147 * @param field The name of the field
\r
148 * @return value of the field
\r
150 public String getValue(String field) {
\r
151 String returnValue = null;
\r
152 if (field != null) {
\r
153 if (field.equals("webdb_create_formatted")) {
\r
154 if (hasValueForField("webdb_create"))
\r
155 returnValue = StringUtil.dateToReadableDate(getValue("webdb_create"));
\r
157 else if (field.equals("webdb_lastchange_formatted")) {
\r
158 if (hasValueForField("webdb_lastchange"))
\r
159 returnValue = StringUtil.dateToReadableDate(getValue(
\r
160 "webdb_lastchange"));
\r
163 returnValue = (String) theValuesHash.get(field);
\r
165 return returnValue;
\r
168 public boolean hasValueForField(String field) {
\r
169 if (theValuesHash != null)
\r
170 return theValuesHash.containsKey(field);
\r
175 * Insers Entity into the database via StorageObject
\r
176 * @return Primary Key of the Entity
\r
177 * @exception StorageObjectException
\r
179 public String insert() throws StorageObjectExc {
\r
180 logger.debug("Entity: trying to insert ...");
\r
181 if (theStorageObject != null) {
\r
182 return theStorageObject.insert( (Entity)this);
\r
185 throw new StorageObjectExc("theStorageObject == null!");
\r
189 * Saves changes of this Entity to the database
\r
190 * @exception StorageObjectException
\r
192 public void update() throws StorageObjectFailure {
\r
193 theStorageObject.update( (Entity)this);
\r
197 * Sets the value for a field. Issues a log message if the field name
\r
198 * supplied was not found in the Entity.
\r
199 * @param theProp The field name whose value has to be set
\r
200 * @param theValue The new value of the field
\r
201 * @exception StorageObjectException
\r
203 public void setValueForProperty(String theProp, String theValue) throws
\r
204 StorageObjectFailure {
\r
205 this.changed = true;
\r
206 if (isField(theProp))
\r
207 theValuesHash.put(theProp, theValue);
\r
209 logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
\r
215 * Returns the field names of the Entity as ArrayListe.
\r
216 * @return ArrayList with field names
\r
217 * @exception StorageObjectException is throuwn if database access was impossible
\r
219 public List getFields() throws StorageObjectFailure {
\r
220 return theStorageObject.getFields();
\r
224 * Returns an int[] with the types of the fields
\r
225 * @return int[] that contains the types of the fields
\r
226 * @exception StorageObjectException
\r
228 public int[] getTypes() throws StorageObjectFailure {
\r
229 return theStorageObject.getTypes();
\r
233 * Returns an ArrayList with field names
\r
234 * @return List with field names
\r
235 * @exception StorageObjectException
\r
237 public List getLabels() throws StorageObjectFailure {
\r
238 return theStorageObject.getLabels();
\r
242 * Returns a Map with all values of the Entity.
\r
243 * @return Map with field name as key and the corresponding values
\r
245 * @deprecated This method is deprecated and will be deleted in the next release.
\r
246 * Entity interfaces freemarker.template.TemplateHashModel now and can
\r
247 * be used in the same way as SimpleHash.
\r
249 public Map getValues() {
\r
250 logger.warn("using deprecated Entity.getValues() - a waste of resources");
\r
251 return theValuesHash;
\r
255 * Returns an ArrayList with all database fields that can
\r
256 * be evaluated as streamedInput.
\r
257 * Could be automated by the types (blob, etc.)
\r
258 * Until now to be created manually in the inheriting class
\r
260 * Liefert einen ArrayList mit allen Datenbankfeldern, die
\r
261 * als streamedInput ausgelesen werden muessen.
\r
262 * Waere automatisierbar ueber die types (blob, etc.)
\r
263 * Bisher manuell anzulegen in der erbenden Klasse
\r
266 public List streamedInput() {
\r
267 return streamedInput;
\r
270 /** Returns whether fieldName is a valid field name of this Entity.
\r
272 * @return true in case fieldName is a field name, else false.
\r
273 * @exception StorageObjectException
\r
275 public boolean isField(String fieldName) throws StorageObjectFailure {
\r
276 return theStorageObject.getFields().contains(fieldName);
\r
279 protected void throwStorageObjectFailure(Throwable e, String wo) throws
\r
280 StorageObjectFailure {
\r
281 logger.error(e.toString() + " function: " + wo);
\r
282 e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
\r
284 throw new StorageObjectFailure("Storage Object Exception in entity", e);
\r
287 // Now implements freemarkers TemplateHashModel
\r
288 // two methods have to be overridden:
\r
289 // 1. public boolean isEmpty() throws TemplateModelException
\r
290 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
\r
292 public boolean isEmpty() throws TemplateModelException {
\r
293 return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;
\r
296 public TemplateModel get(java.lang.String key) throws TemplateModelException {
\r
297 return new SimpleScalar(getValue(key));
\r
300 public void put(java.lang.String key, TemplateModel model) {
\r
301 // putting should only take place via setValue and is limited to the
\r
302 // database fields associated with the entity. no additional freemarker
\r
303 // stuff will be available via Entity.
\r
304 logger.warn("put is called on entity! - the values will be lost!");
\r
307 public void remove(java.lang.String key) {
\r
308 // do we need this?
\r
311 //////////////////////////////////////////////////////////////////////////////////
\r