189eaa5d192c604ec910058b56aadf76677e01f3
[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 $Revision: 1.8.2.1 $ $Date: 2002/09/01 21:31:40 $
54  * @author $Author: mh $
55  *
56  * $Log: Entity.java,v $
57  * Revision 1.8.2.1  2002/09/01 21:31:40  mh
58  * Mir goes GPL
59  *
60  * Revision 1.8  2002/06/28 22:56:37  mh
61  * fix so that webdb_lastchange appears in contentlist.template as it should
62  *
63  * Revision 1.7  2002/06/28 20:35:38  mh
64  * use some cvs macros/id's
65  *
66  *
67  */
68
69 public class Entity implements TemplateHashModel, TemplateModelRoot
70 {
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;
77     static {
78       theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
79     }
80
81     public Entity() {
82
83       this.changed = false;
84       instances++;
85       Integer i = new Integer(instances);
86       //System.err.println("New abstract entity instance: "+i.toString());
87     }
88
89   /**
90    * Constructor
91    * @param StorageObject The StorageObject of the Entity.
92    */
93   public Entity (StorageObject StorageObject) {
94     this();
95     setStorage(StorageObject);
96   }
97
98   /*
99    * Sets the StorageObject of the Entity.
100    */
101   public void setStorage (StorageObject storage) {
102     this.theStorageObject = storage;
103   }
104
105   /**
106    * Sets the values of the Entity.
107    * @param theStringValues HashMap containing the new values of the Entity
108    */
109
110   public void setValues(HashMap theStringValues)
111   {
112     /** @todo should be synchronized */
113     if (theStringValues!=null) {
114       theValuesHash = new HashMap();
115       String aKey;
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));
122       }
123     }
124     else theLog.printWarning("Entity.setValues called with null HashMap");
125  }
126
127   /**
128    * Returns whether the content of the Entity has changed.
129    * @return true wenn ja, sonst false
130    */
131   public boolean changed () {
132     return  changed;
133   }
134
135   /**
136    * Returns the primary key of the Entity.
137    * @return String Id
138    */
139   public String getId () {
140     return  (String)getValue(theStorageObject.getIdName());
141   }
142
143   /**
144    * Defines the primary key of the Entity
145    * @param id
146    */
147   public void setId (String id) {
148     theValuesHash.put(theStorageObject.getIdName(), id);
149       }
150
151   /**
152    * Returns the value of a field by field name.
153    * @param field The name of the field
154    * @return value of the field
155    */
156   public String getValue (String field) {
157     String returnValue = null;
158     if (field != null)
159     {
160       if (field.equals("webdb_create_formatted"))
161       {
162         if (hasValueForField("webdb_create"))
163           returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
164       }
165       else if (field.equals("webdb_lastchange_formatted"))
166       {
167         if (hasValueForField("webdb_lastchange"))
168           returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
169       }
170       else if (field.equals("webdb_create_dc"))
171       {
172         if (hasValueForField("webdb_create"))
173           returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
174       }
175       else
176         returnValue = (String)theValuesHash.get(field);
177     }
178     return returnValue;
179   }
180
181   public boolean hasValueForField(String field)
182   {
183     if (theValuesHash!=null)
184       return theValuesHash.containsKey(field);
185     return false;
186   }
187
188   /**
189    * Insers Entity into the database via StorageObject
190    * @return Primary Key of the Entity
191    * @exception StorageObjectException
192    */
193   public String insert () throws StorageObjectException {
194     theLog.printDebugInfo("Entity: trying to insert ...");
195     if (theStorageObject != null) {
196       return theStorageObject.insert((Entity)this);
197     }
198     else
199       throw  new StorageObjectException("Kein StorageObject gesetzt!");
200   }
201
202   /**
203    * Saves changes of this Entity to the database
204    * @exception StorageObjectException
205    */
206   public void update () throws StorageObjectException {
207     theStorageObject.update((Entity)this);
208   }
209
210   /**
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
216    */
217   public void setValueForProperty (String theProp, String theValue)
218     throws StorageObjectException {
219     this.changed = true;
220     if (isField(theProp))
221       theValuesHash.put(theProp, theValue);
222     else {
223       theLog.printWarning("Property not found: " + theProp+theValue);
224     }
225
226   }
227
228   /**
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
232    */
233   public ArrayList getFields () throws StorageObjectException {
234     return  theStorageObject.getFields();
235     }
236
237   /**
238    * Returns an int[] with the types of the fields
239    * @return int[] that contains the types of the fields
240    * @exception StorageObjectException
241    */
242   public int[] getTypes () throws StorageObjectException {
243     return  theStorageObject.getTypes();
244     }
245
246   /**
247    * Returns an ArrayList with field names
248    * @return List with field names
249    * @exception StorageObjectException
250    */
251   public ArrayList getLabels () throws StorageObjectException {
252     return  theStorageObject.getLabels();
253     }
254
255   /**
256    * Returns a Hashmap with all values of the Entity.
257    * @return HashMap with field name as key and the corresponding values
258    *
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.
262
263    */
264     public HashMap getValues() {
265       theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
266       return theValuesHash;
267     }
268
269     /**
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
274      *
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
279      */
280
281   public ArrayList streamedInput() {
282     return streamedInput;
283   }
284
285    /** Returns whether fieldName is a valid field name of this Entity.
286    * @param fieldName
287    * @return true in case fieldName is a field name, else false.
288    * @exception StorageObjectException
289    */
290   public boolean isField (String fieldName) throws StorageObjectException {
291     return  theStorageObject.getFields().contains(fieldName);
292   }
293
294    /** Returns the number of instances of this Entity
295    * @return int The number of instances
296    */
297   public int getInstances() {
298      return instances;
299   }
300
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());
304   }
305
306   /**
307    * Frees an instance
308    */
309   /*public void finalize () {
310     instances--;
311     Integer i = new Integer(instances);
312     System.err.println("Removing abstract entity instance: "+i.toString());
313     try {
314       super.finalize();
315     } catch (Throwable t) {
316       System.err.println(t.toString());
317     }
318   }*/
319
320
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
325
326   public boolean isEmpty() throws TemplateModelException
327   {
328     return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
329   }
330
331   public TemplateModel get(java.lang.String key) throws TemplateModelException
332   {
333                 return new SimpleScalar(getValue(key));
334   }
335         
336         public void put(java.lang.String key, TemplateModel model)
337   {
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!");
342   }
343
344   public void remove(java.lang.String key)
345   {
346     // do we need this?
347   }
348
349
350   //////////////////////////////////////////////////////////////////////////////////
351
352
353 }
354