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 $Revision: 1.8.2.1 $ $Date: 2002/09/01 21:31:40 $
54 * @author $Author: mh $
56 * $Log: Entity.java,v $
57 * Revision 1.8.2.1 2002/09/01 21:31:40 mh
60 * Revision 1.8 2002/06/28 22:56:37 mh
61 * fix so that webdb_lastchange appears in contentlist.template as it should
63 * Revision 1.7 2002/06/28 20:35:38 mh
64 * use some cvs macros/id's
69 public class Entity implements TemplateHashModel, TemplateModelRoot
71 private boolean changed;
72 protected HashMap theValuesHash; // tablekey / value
73 protected StorageObject theStorageObject;
74 protected static Logfile theLog;
75 protected ArrayList streamedInput=null;
76 private static int instances = 0;
78 theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
85 Integer i = new Integer(instances);
86 //System.err.println("New abstract entity instance: "+i.toString());
91 * @param StorageObject The StorageObject of the Entity.
93 public Entity (StorageObject StorageObject) {
95 setStorage(StorageObject);
99 * Sets the StorageObject of the Entity.
101 public void setStorage (StorageObject storage) {
102 this.theStorageObject = storage;
106 * Sets the values of the Entity.
107 * @param theStringValues HashMap containing the new values of the Entity
110 public void setValues(HashMap theStringValues)
112 /** @todo should be synchronized */
113 if (theStringValues!=null) {
114 theValuesHash = new HashMap();
116 Set set = theStringValues.keySet();
117 Iterator it = set.iterator();
118 int size = set.size();
119 for (int i = 0; i < size; i++) {
120 aKey = (String)it.next();
121 theValuesHash.put(aKey, (String)theStringValues.get(aKey));
124 else theLog.printWarning("Entity.setValues called with null HashMap");
128 * Returns whether the content of the Entity has changed.
129 * @return true wenn ja, sonst false
131 public boolean changed () {
136 * Returns the primary key of the Entity.
139 public String getId () {
140 return (String)getValue(theStorageObject.getIdName());
144 * Defines the primary key of the Entity
147 public void setId (String id) {
148 theValuesHash.put(theStorageObject.getIdName(), id);
152 * Returns the value of a field by field name.
153 * @param field The name of the field
154 * @return value of the field
156 public String getValue (String field) {
157 String returnValue = null;
160 if (field.equals("webdb_create_formatted"))
162 if (hasValueForField("webdb_create"))
163 returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
165 else if (field.equals("webdb_lastchange_formatted"))
167 if (hasValueForField("webdb_lastchange"))
168 returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
170 else if (field.equals("webdb_create_dc"))
172 if (hasValueForField("webdb_create"))
173 returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
176 returnValue = (String)theValuesHash.get(field);
181 public boolean hasValueForField(String field)
183 if (theValuesHash!=null)
184 return theValuesHash.containsKey(field);
189 * Insers Entity into the database via StorageObject
190 * @return Primary Key of the Entity
191 * @exception StorageObjectException
193 public String insert () throws StorageObjectException {
194 theLog.printDebugInfo("Entity: trying to insert ...");
195 if (theStorageObject != null) {
196 return theStorageObject.insert((Entity)this);
199 throw new StorageObjectException("Kein StorageObject gesetzt!");
203 * Saves changes of this Entity to the database
204 * @exception StorageObjectException
206 public void update () throws StorageObjectException {
207 theStorageObject.update((Entity)this);
211 * Sets the value for a field. Issues a log message if the field name
212 * supplied was not found in the Entity.
213 * @param theProp The field name whose value has to be set
214 * @param theValue The new value of the field
215 * @exception StorageObjectException
217 public void setValueForProperty (String theProp, String theValue)
218 throws StorageObjectException {
220 if (isField(theProp))
221 theValuesHash.put(theProp, theValue);
223 theLog.printWarning("Property not found: " + theProp+theValue);
229 * Returns the field names of the Entity as ArrayListe.
230 * @return ArrayList with field names
231 * @exception StorageObjectException is throuwn if database access was impossible
233 public ArrayList getFields () throws StorageObjectException {
234 return theStorageObject.getFields();
238 * Returns an int[] with the types of the fields
239 * @return int[] that contains the types of the fields
240 * @exception StorageObjectException
242 public int[] getTypes () throws StorageObjectException {
243 return theStorageObject.getTypes();
247 * Returns an ArrayList with field names
248 * @return List with field names
249 * @exception StorageObjectException
251 public ArrayList getLabels () throws StorageObjectException {
252 return theStorageObject.getLabels();
256 * Returns a Hashmap with all values of the Entity.
257 * @return HashMap with field name as key and the corresponding values
259 * @deprecated This method is deprecated and will be deleted in the next release.
260 * Entity interfaces freemarker.template.TemplateHashModel now and can
261 * be used in the same way as SimpleHash.
264 public HashMap getValues() {
265 theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
266 return theValuesHash;
270 * Returns an ArrayList with all database fields that can
271 * be evaluated as streamedInput.
272 * Could be automated by the types (blob, etc.)
273 * Until now to be created manually in the inheriting class
275 * Liefert einen ArrayList mit allen Datenbankfeldern, die
276 * als streamedInput ausgelesen werden muessen.
277 * Waere automatisierbar ueber die types (blob, etc.)
278 * Bisher manuell anzulegen in der erbenden Klasse
281 public ArrayList streamedInput() {
282 return streamedInput;
285 /** Returns whether fieldName is a valid field name of this Entity.
287 * @return true in case fieldName is a field name, else false.
288 * @exception StorageObjectException
290 public boolean isField (String fieldName) throws StorageObjectException {
291 return theStorageObject.getFields().contains(fieldName);
294 /** Returns the number of instances of this Entity
295 * @return int The number of instances
297 public int getInstances() {
301 protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
302 theLog.printError( e.toString() + " Funktion: "+ wo);
303 throw new StorageObjectException("Storage Object Exception in entity" +e.toString());
309 /*public void finalize () {
311 Integer i = new Integer(instances);
312 System.err.println("Removing abstract entity instance: "+i.toString());
315 } catch (Throwable t) {
316 System.err.println(t.toString());
321 // Now implements freemarkers TemplateHashModel
322 // two methods have to be overridden:
323 // 1. public boolean isEmpty() throws TemplateModelException
324 // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
326 public boolean isEmpty() throws TemplateModelException
328 return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
331 public TemplateModel get(java.lang.String key) throws TemplateModelException
333 return new SimpleScalar(getValue(key));
336 public void put(java.lang.String key, TemplateModel model)
338 // putting should only take place via setValue and is limited to the
339 // database fields associated with the entity. no additional freemarker
340 // stuff will be available via Entity.
341 theLog.printWarning("### put is called on entity! - the values will be lost!");
344 public void remove(java.lang.String key)
350 //////////////////////////////////////////////////////////////////////////////////