merge media InputStream changes from MIR_1_0 branch
[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.10 2002/11/04 04:35:21 mh 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 if (field.equals("webdb_create_dc"))
160       {
161         if (hasValueForField("webdb_create"))
162           returnValue=StringUtil.webdbdateToDCDate(getValue("webdb_create"));
163       }
164       else
165         returnValue = (String)theValuesHash.get(field);
166     }
167     return returnValue;
168   }
169
170   public boolean hasValueForField(String field)
171   {
172     if (theValuesHash!=null)
173       return theValuesHash.containsKey(field);
174     return false;
175   }
176
177   /**
178    * Insers Entity into the database via StorageObject
179    * @return Primary Key of the Entity
180    * @exception StorageObjectException
181    */
182   public String insert () throws StorageObjectException {
183     theLog.printDebugInfo("Entity: trying to insert ...");
184     if (theStorageObject != null) {
185       return theStorageObject.insert((Entity)this);
186     }
187     else
188       throw  new StorageObjectException("Kein StorageObject gesetzt!");
189   }
190
191   /**
192    * Saves changes of this Entity to the database
193    * @exception StorageObjectException
194    */
195   public void update () throws StorageObjectException {
196     theStorageObject.update((Entity)this);
197   }
198
199   /**
200    * Sets the value for a field. Issues a log message if the field name
201    * supplied was not found in the Entity.
202    * @param theProp The field name whose value has to be set
203    * @param theValue The new value of the field
204    * @exception StorageObjectException
205    */
206   public void setValueForProperty (String theProp, String theValue)
207     throws StorageObjectException {
208     this.changed = true;
209     if (isField(theProp))
210       theValuesHash.put(theProp, theValue);
211     else {
212       theLog.printWarning("Property not found: " + theProp+theValue);
213     }
214
215   }
216
217   /**
218    * Returns the field names of the Entity as ArrayListe.
219    * @return ArrayList with field names
220    * @exception StorageObjectException is throuwn if database access was impossible
221    */
222   public ArrayList getFields () throws StorageObjectException {
223     return  theStorageObject.getFields();
224     }
225
226   /**
227    * Returns an int[] with the types of the fields
228    * @return int[] that contains the types of the fields
229    * @exception StorageObjectException
230    */
231   public int[] getTypes () throws StorageObjectException {
232     return  theStorageObject.getTypes();
233     }
234
235   /**
236    * Returns an ArrayList with field names
237    * @return List with field names
238    * @exception StorageObjectException
239    */
240   public ArrayList getLabels () throws StorageObjectException {
241     return  theStorageObject.getLabels();
242     }
243
244   /**
245    * Returns a Hashmap with all values of the Entity.
246    * @return HashMap with field name as key and the corresponding values
247    *
248    * @deprecated This method is deprecated and will be deleted in the next release.
249    *  Entity interfaces freemarker.template.TemplateHashModel now and can
250    *  be used in the same way as SimpleHash.
251
252    */
253     public HashMap getValues() {
254       theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
255       return theValuesHash;
256     }
257
258     /**
259      * Returns an ArrayList with all database fields that can
260      * be evaluated as streamedInput.
261      * Could be automated by the types (blob, etc.)
262      * Until now to be created manually in the inheriting class
263      *
264      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
265      *  als streamedInput ausgelesen werden muessen.
266      *  Waere automatisierbar ueber die types (blob, etc.)
267      *  Bisher manuell anzulegen in der erbenden Klasse
268      */
269
270   public ArrayList streamedInput() {
271     return streamedInput;
272   }
273
274    /** Returns whether fieldName is a valid field name of this Entity.
275    * @param fieldName
276    * @return true in case fieldName is a field name, else false.
277    * @exception StorageObjectException
278    */
279   public boolean isField (String fieldName) throws StorageObjectException {
280     return  theStorageObject.getFields().contains(fieldName);
281   }
282
283    /** Returns the number of instances of this Entity
284    * @return int The number of instances
285    */
286   public int getInstances() {
287      return instances;
288   }
289
290   protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
291     theLog.printError( e.toString() + " Funktion: "+ wo);
292     throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
293   }
294
295   /**
296    * Frees an instance
297    */
298   /*public void finalize () {
299     instances--;
300     Integer i = new Integer(instances);
301     System.err.println("Removing abstract entity instance: "+i.toString());
302     try {
303       super.finalize();
304     } catch (Throwable t) {
305       System.err.println(t.toString());
306     }
307   }*/
308
309
310   // Now implements freemarkers TemplateHashModel
311   // two methods have to be overridden:
312   // 1. public boolean isEmpty() throws TemplateModelException
313   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
314
315   public boolean isEmpty() throws TemplateModelException
316   {
317     return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
318   }
319
320   public TemplateModel get(java.lang.String key) throws TemplateModelException
321   {
322                 return new SimpleScalar(getValue(key));
323   }
324         
325         public void put(java.lang.String key, TemplateModel model)
326   {
327     // putting should only take place via setValue and is limited to the
328     // database fields associated with the entity. no additional freemarker
329     // stuff will be available via Entity.
330     theLog.printWarning("### put is called on entity! - the values will be lost!");
331   }
332
333   public void remove(java.lang.String key)
334   {
335     // do we need this?
336   }
337
338
339   //////////////////////////////////////////////////////////////////////////////////
340
341
342 }
343