login languages made configurable
[mir.git] / source / mir / entity / Entity.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
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.
10  *
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.
15  *
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
19  *
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.
30  */
31
32 /**
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>
35  */
36
37 package  mir.entity;
38
39 import java.lang.*;
40 import java.io.*;
41 import java.util.*;
42 import java.sql.*;
43
44 import freemarker.template.*;
45
46 import mir.storage.*;
47 import mir.misc.*;
48
49 /**
50  * Base Class of Entities
51  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
52  *
53  * @version $Id: Entity.java,v 1.12 2002/12/17 19:20:31 zapata Exp $
54  * @author rk
55  *
56  */
57
58 public class Entity implements TemplateHashModel, TemplateModelRoot
59 {
60   private boolean             changed;
61   protected HashMap           theValuesHash;   // tablekey / value
62   protected StorageObject     theStorageObject;
63   protected static Logfile    theLog;
64   protected ArrayList         streamedInput=null;
65   private static int instances = 0;
66     static {
67       theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
68     }
69
70     public Entity() {
71
72       this.changed = false;
73       instances++;
74       Integer i = new Integer(instances);
75       //System.err.println("New abstract entity instance: "+i.toString());
76     }
77
78   /**
79    * Constructor
80    * @param StorageObject The StorageObject of the Entity.
81    */
82   public Entity (StorageObject StorageObject) {
83     this();
84     setStorage(StorageObject);
85   }
86
87   /*
88    * Sets the StorageObject of the Entity.
89    */
90   public void setStorage (StorageObject storage) {
91     this.theStorageObject = storage;
92   }
93
94   /**
95    * Sets the values of the Entity.
96    * @param theStringValues HashMap containing the new values of the Entity
97    */
98
99   public void setValues(HashMap theStringValues)
100   {
101     /** @todo should be synchronized */
102     if (theStringValues!=null) {
103       theValuesHash = new HashMap();
104       String aKey;
105       Set set = theStringValues.keySet();
106       Iterator it = set.iterator();
107       int size = set.size();
108       for (int i = 0; i < size; i++) {
109         aKey = (String)it.next();
110         theValuesHash.put(aKey, (String)theStringValues.get(aKey));
111       }
112     }
113     else theLog.printWarning("Entity.setValues called with null HashMap");
114  }
115
116   /**
117    * Returns whether the content of the Entity has changed.
118    * @return true wenn ja, sonst false
119    */
120   public boolean changed () {
121     return  changed;
122   }
123
124   /**
125    * Returns the primary key of the Entity.
126    * @return String Id
127    */
128   public String getId () {
129     return  (String) getValue(theStorageObject.getIdName());
130   }
131
132   /**
133    * Defines the primary key of the Entity
134    * @param id
135    */
136   public void setId (String id) {
137     theValuesHash.put(theStorageObject.getIdName(), id);
138       }
139
140   /**
141    * Returns the value of a field by field name.
142    * @param field The name of the field
143    * @return value of the field
144    */
145   public String getValue (String field) {
146     String returnValue = null;
147     if (field != null)
148     {
149       if (field.equals("webdb_create_formatted"))
150       {
151         if (hasValueForField("webdb_create"))
152           returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
153       }
154       else if (field.equals("webdb_lastchange_formatted"))
155       {
156         if (hasValueForField("webdb_lastchange"))
157           returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
158       }
159       else
160         returnValue = (String)theValuesHash.get(field);
161     }
162     return returnValue;
163   }
164
165   public boolean hasValueForField(String field)
166   {
167     if (theValuesHash!=null)
168       return theValuesHash.containsKey(field);
169     return false;
170   }
171
172   /**
173    * Insers Entity into the database via StorageObject
174    * @return Primary Key of the Entity
175    * @exception StorageObjectException
176    */
177   public String insert () throws StorageObjectException {
178     theLog.printDebugInfo("Entity: trying to insert ...");
179     if (theStorageObject != null) {
180       return theStorageObject.insert((Entity)this);
181     }
182     else
183       throw  new StorageObjectException("Kein StorageObject gesetzt!");
184   }
185
186   /**
187    * Saves changes of this Entity to the database
188    * @exception StorageObjectException
189    */
190   public void update () throws StorageObjectException {
191     theStorageObject.update((Entity)this);
192   }
193
194   /**
195    * Sets the value for a field. Issues a log message if the field name
196    * supplied was not found in the Entity.
197    * @param theProp The field name whose value has to be set
198    * @param theValue The new value of the field
199    * @exception StorageObjectException
200    */
201   public void setValueForProperty (String theProp, String theValue)
202     throws StorageObjectException {
203     this.changed = true;
204     if (isField(theProp))
205       theValuesHash.put(theProp, theValue);
206     else {
207       theLog.printWarning("Property not found: " + theProp+theValue);
208     }
209
210   }
211
212   /**
213    * Returns the field names of the Entity as ArrayListe.
214    * @return ArrayList with field names
215    * @exception StorageObjectException is throuwn if database access was impossible
216    */
217   public ArrayList getFields () throws StorageObjectException {
218     return  theStorageObject.getFields();
219     }
220
221   /**
222    * Returns an int[] with the types of the fields
223    * @return int[] that contains the types of the fields
224    * @exception StorageObjectException
225    */
226   public int[] getTypes () throws StorageObjectException {
227     return  theStorageObject.getTypes();
228     }
229
230   /**
231    * Returns an ArrayList with field names
232    * @return List with field names
233    * @exception StorageObjectException
234    */
235   public ArrayList getLabels () throws StorageObjectException {
236     return  theStorageObject.getLabels();
237     }
238
239   /**
240    * Returns a Hashmap with all values of the Entity.
241    * @return HashMap with field name as key and the corresponding values
242    *
243    * @deprecated This method is deprecated and will be deleted in the next release.
244    *  Entity interfaces freemarker.template.TemplateHashModel now and can
245    *  be used in the same way as SimpleHash.
246
247    */
248     public HashMap getValues() {
249       theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
250       return theValuesHash;
251     }
252
253     /**
254      * Returns an ArrayList with all database fields that can
255      * be evaluated as streamedInput.
256      * Could be automated by the types (blob, etc.)
257      * Until now to be created manually in the inheriting class
258      *
259      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
260      *  als streamedInput ausgelesen werden muessen.
261      *  Waere automatisierbar ueber die types (blob, etc.)
262      *  Bisher manuell anzulegen in der erbenden Klasse
263      */
264
265   public ArrayList streamedInput() {
266     return streamedInput;
267   }
268
269    /** Returns whether fieldName is a valid field name of this Entity.
270    * @param fieldName
271    * @return true in case fieldName is a field name, else false.
272    * @exception StorageObjectException
273    */
274   public boolean isField (String fieldName) throws StorageObjectException {
275     return  theStorageObject.getFields().contains(fieldName);
276   }
277
278    /** Returns the number of instances of this Entity
279    * @return int The number of instances
280    */
281   public int getInstances() {
282      return instances;
283   }
284
285   protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
286     theLog.printError( e.toString() + " Funktion: "+ wo);
287     e.printStackTrace(System.out);
288     throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
289   }
290
291   // Now implements freemarkers TemplateHashModel
292   // two methods have to be overridden:
293   // 1. public boolean isEmpty() throws TemplateModelException
294   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
295
296   public boolean isEmpty() throws TemplateModelException
297   {
298     return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
299   }
300
301   public TemplateModel get(java.lang.String key) throws TemplateModelException
302   {
303                 return new SimpleScalar(getValue(key));
304   }
305
306         public void put(java.lang.String key, TemplateModel model)
307   {
308     // putting should only take place via setValue and is limited to the
309     // database fields associated with the entity. no additional freemarker
310     // stuff will be available via Entity.
311     theLog.printWarning("### put is called on entity! - the values will be lost!");
312   }
313
314   public void remove(java.lang.String key)
315   {
316     // do we need this?
317   }
318
319
320   //////////////////////////////////////////////////////////////////////////////////
321
322
323 }
324