support for translations + lots of misc. fixes
[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.List;\r
40 import java.util.HashMap;\r
41 import java.util.Iterator;\r
42 import java.util.Set;\r
43 \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
56 \r
57 /**\r
58  * Base Class of Entities\r
59  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant\r
60  *\r
61  * @version $Id: Entity.java,v 1.14 2003/02/20 16:05:32 zapata Exp $\r
62  * @author rk\r
63  *\r
64  */\r
65 \r
66 public class Entity implements TemplateHashModel, TemplateModelRoot\r
67 {\r
68   protected static MirPropertiesConfiguration configuration;\r
69   protected static Logfile theLog;\r
70 \r
71   private boolean changed;\r
72   protected HashMap theValuesHash; // tablekey / value\r
73   protected StorageObject theStorageObject;\r
74   protected List streamedInput = null;\r
75 \r
76   static {\r
77     try {\r
78       configuration = MirPropertiesConfiguration.instance();\r
79     }\r
80     catch (PropertiesConfigExc e) {\r
81       e.printStackTrace();\r
82     }\r
83     theLog = Logfile.getInstance(configuration.getStringWithHome(\r
84         "Entity.Logfile"));\r
85   }\r
86 \r
87   public Entity() {\r
88     this.changed = false;\r
89   }\r
90 \r
91   /**\r
92    * Constructor\r
93    * @param StorageObject The StorageObject of the Entity.\r
94    */\r
95   public Entity(StorageObject StorageObject) {\r
96     this();\r
97     setStorage(StorageObject);\r
98   }\r
99 \r
100   /*\r
101    * Sets the StorageObject of the Entity.\r
102    */\r
103   public void setStorage(StorageObject storage) {\r
104     this.theStorageObject = storage;\r
105   }\r
106 \r
107   /**\r
108    * Sets the values of the Entity.\r
109    * @param theStringValues HashMap containing the new values of the Entity\r
110    */\r
111 \r
112   public void setValues(HashMap theStringValues) {\r
113     /** @todo should be synchronized */\r
114     if (theStringValues != null) {\r
115       theValuesHash = new HashMap();\r
116       String aKey;\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
123       }\r
124     }\r
125     else\r
126       theLog.printWarning("Entity.setValues called with null HashMap");\r
127   }\r
128 \r
129   /**\r
130    * Returns whether the content of the Entity has changed.\r
131    * @return true wenn ja, sonst false\r
132    */\r
133   public boolean changed() {\r
134     return changed;\r
135   }\r
136 \r
137   /**\r
138    * Returns the primary key of the Entity.\r
139    * @return String Id\r
140    */\r
141   public String getId() {\r
142     return (String) getValue(theStorageObject.getIdName());\r
143   }\r
144 \r
145   /**\r
146    * Defines the primary key of the Entity\r
147    * @param id\r
148    */\r
149   public void setId(String id) {\r
150     theValuesHash.put(theStorageObject.getIdName(), id);\r
151   }\r
152 \r
153   /**\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
157    */\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
164       }\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
169       }\r
170       else\r
171         returnValue = (String) theValuesHash.get(field);\r
172     }\r
173     return returnValue;\r
174   }\r
175 \r
176   public boolean hasValueForField(String field) {\r
177     if (theValuesHash != null)\r
178       return theValuesHash.containsKey(field);\r
179     return false;\r
180   }\r
181 \r
182   /**\r
183    * Insers Entity into the database via StorageObject\r
184    * @return Primary Key of the Entity\r
185    * @exception StorageObjectException\r
186    */\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
191     }\r
192     else\r
193       throw new StorageObjectExc("Kein StorageObject gesetzt!");\r
194   }\r
195 \r
196   /**\r
197    * Saves changes of this Entity to the database\r
198    * @exception StorageObjectException\r
199    */\r
200   public void update() throws StorageObjectFailure {\r
201     theStorageObject.update( (Entity)this);\r
202   }\r
203 \r
204   /**\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
210    */\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
216     else {\r
217       theLog.printWarning("Property not found: " + theProp + theValue);\r
218     }\r
219 \r
220   }\r
221 \r
222   /**\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
226    */\r
227   public List getFields() throws StorageObjectFailure {\r
228     return theStorageObject.getFields();\r
229   }\r
230 \r
231   /**\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
235    */\r
236   public int[] getTypes() throws StorageObjectFailure {\r
237     return theStorageObject.getTypes();\r
238   }\r
239 \r
240   /**\r
241    * Returns an ArrayList with field names\r
242    * @return List with field names\r
243    * @exception StorageObjectException\r
244    */\r
245   public List getLabels() throws StorageObjectFailure {\r
246     return theStorageObject.getLabels();\r
247   }\r
248 \r
249   /**\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
252    *\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
256    */\r
257   public HashMap getValues() {\r
258     theLog.printWarning(\r
259         "## using deprecated Entity.getValues() - a waste of resources");\r
260     return theValuesHash;\r
261   }\r
262 \r
263   /**\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
268    *\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
273    */\r
274 \r
275   public List streamedInput() {\r
276     return streamedInput;\r
277   }\r
278 \r
279   /** Returns whether fieldName is a valid field name of this Entity.\r
280    * @param fieldName\r
281    * @return true in case fieldName is a field name, else false.\r
282    * @exception StorageObjectException\r
283    */\r
284   public boolean isField(String fieldName) throws StorageObjectFailure {\r
285     return theStorageObject.getFields().contains(fieldName);\r
286   }\r
287 \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
293   }\r
294 \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
299 \r
300   public boolean isEmpty() throws TemplateModelException {\r
301     return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;\r
302   }\r
303 \r
304   public TemplateModel get(java.lang.String key) throws TemplateModelException {\r
305     return new SimpleScalar(getValue(key));\r
306   }\r
307 \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
314   }\r
315 \r
316   public void remove(java.lang.String key) {\r
317     // do we need this?\r
318   }\r
319 \r
320   //////////////////////////////////////////////////////////////////////////////////\r
321 }\r
322 \r