84e87b5a913c4a7f984f828cae371ad153d2593c
[mir.git] / source / mir / entity / Entity.java
1 /*\r
2  * Copyright (C) 2001, 2002  The Mir-coders group\r
3  *\r
4  * This file is part of Mir.\r
5  *\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
10  *\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
15  *\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
19  *\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
30  */\r
31 \r
32 /**\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
35  */\r
36 \r
37 package  mir.entity;\r
38 \r
39 import java.util.HashMap;\r
40 import java.util.List;\r
41 import java.util.Map;\r
42 \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
55 \r
56 /**\r
57  * Base Class of Entities\r
58  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant\r
59  *\r
60  * @version $Id: Entity.java,v 1.18 2003/03/05 19:23:14 idfx Exp $\r
61  * @author rk\r
62  *\r
63  */\r
64 \r
65 public class Entity implements TemplateHashModel, TemplateModelRoot\r
66 {\r
67   protected static MirPropertiesConfiguration configuration;\r
68 \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
74 \r
75   static {\r
76     try {\r
77       configuration = MirPropertiesConfiguration.instance();\r
78     }\r
79     catch (PropertiesConfigExc e) {\r
80       throw new RuntimeException(e.getMessage());\r
81     }\r
82   }\r
83 \r
84   public Entity() {\r
85     logger = new LoggerWrapper("Entity");\r
86 \r
87     this.changed = false;\r
88   }\r
89 \r
90   /**\r
91    * Constructor\r
92    * @param StorageObject The StorageObject of the Entity.\r
93    */\r
94   public Entity(StorageObject StorageObject) {\r
95     this();\r
96     setStorage(StorageObject);\r
97   }\r
98 \r
99   /*\r
100    * Sets the StorageObject of the Entity.\r
101    */\r
102   public void setStorage(StorageObject storage) {\r
103     this.theStorageObject = storage;\r
104   }\r
105 \r
106   /**\r
107    * Sets the values of the Entity.\r
108    * @param theStringValues Map containing the new values of the Entity\r
109    */\r
110 \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
116     }\r
117     else\r
118       logger.warn("Entity.setValues called with null Map");\r
119   }\r
120 \r
121   /**\r
122    * Returns whether the content of the Entity has changed.\r
123    * @return true wenn ja, sonst false\r
124    */\r
125   public boolean changed() {\r
126     return changed;\r
127   }\r
128 \r
129   /**\r
130    * Returns the primary key of the Entity.\r
131    * @return String Id\r
132    */\r
133   public String getId() {\r
134     return (String) getValue(theStorageObject.getIdName());\r
135   }\r
136 \r
137   /**\r
138    * Defines the primary key of the Entity\r
139    * @param id\r
140    */\r
141   public void setId(String id) {\r
142     theValuesHash.put(theStorageObject.getIdName(), id);\r
143   }\r
144 \r
145   /**\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
149    */\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
156       }\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
161       }\r
162       else\r
163         returnValue = (String) theValuesHash.get(field);\r
164     }\r
165     return returnValue;\r
166   }\r
167 \r
168   public boolean hasValueForField(String field) {\r
169     if (theValuesHash != null)\r
170       return theValuesHash.containsKey(field);\r
171     return false;\r
172   }\r
173 \r
174   /**\r
175    * Insers Entity into the database via StorageObject\r
176    * @return Primary Key of the Entity\r
177    * @exception StorageObjectException\r
178    */\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
183     }\r
184     else\r
185       throw new StorageObjectExc("theStorageObject == null!");\r
186   }\r
187 \r
188   /**\r
189    * Saves changes of this Entity to the database\r
190    * @exception StorageObjectException\r
191    */\r
192   public void update() throws StorageObjectFailure {\r
193     theStorageObject.update( (Entity)this);\r
194   }\r
195 \r
196   /**\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
202    */\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
208     else {\r
209       logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");\r
210     }\r
211 \r
212   }\r
213 \r
214   /**\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
218    */\r
219   public List getFields() throws StorageObjectFailure {\r
220     return theStorageObject.getFields();\r
221   }\r
222 \r
223   /**\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
227    */\r
228   public int[] getTypes() throws StorageObjectFailure {\r
229     return theStorageObject.getTypes();\r
230   }\r
231 \r
232   /**\r
233    * Returns an ArrayList with field names\r
234    * @return List with field names\r
235    * @exception StorageObjectException\r
236    */\r
237   public List getLabels() throws StorageObjectFailure {\r
238     return theStorageObject.getLabels();\r
239   }\r
240 \r
241   /**\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
244    *\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
248    */\r
249   public Map getValues() {\r
250     logger.warn("using deprecated Entity.getValues() - a waste of resources");\r
251     return theValuesHash;\r
252   }\r
253 \r
254   /**\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
259    *\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
264    */\r
265 \r
266   public List streamedInput() {\r
267     return streamedInput;\r
268   }\r
269 \r
270   /** Returns whether fieldName is a valid field name of this Entity.\r
271    * @param fieldName\r
272    * @return true in case fieldName is a field name, else false.\r
273    * @exception StorageObjectException\r
274    */\r
275   public boolean isField(String fieldName) throws StorageObjectFailure {\r
276     return theStorageObject.getFields().contains(fieldName);\r
277   }\r
278 \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
283 \r
284     throw new StorageObjectFailure("Storage Object Exception in entity", e);\r
285   }\r
286 \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
291 \r
292   public boolean isEmpty() throws TemplateModelException {\r
293     return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;\r
294   }\r
295 \r
296   public TemplateModel get(java.lang.String key) throws TemplateModelException {\r
297     return new SimpleScalar(getValue(key));\r
298   }\r
299 \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
305   }\r
306 \r
307   public void remove(java.lang.String key) {\r
308     // do we need this?\r
309   }\r
310 \r
311   //////////////////////////////////////////////////////////////////////////////////\r
312 }\r
313 \r