some small changes delting unneeded imports. two new exceptions in mir.storage. usage...
[mir.git] / source / mir / entity / Entity.java
index 6dd39ac..5a69e3e 100755 (executable)
@@ -1,48 +1,90 @@
+/*
+ * Copyright (C) 2001, 2002  The Mir-coders group
+ *
+ * This file is part of Mir.
+ *
+ * Mir is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * Mir is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with Mir; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * In addition, as a special exception, The Mir-coders gives permission to link
+ * the code of this program with the com.oreilly.servlet library, any library
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of
+ * the above that use the same license as the above), and distribute linked
+ * combinations including the two.  You must obey the GNU General Public
+ * License in all respects for all of the code used other than the above
+ * mentioned libraries.  If you modify this file, you may extend this exception
+ * to your version of the file, but you are not obligated to do so.  If you do
+ * not wish to do so, delete this exception statement from your version.
+ */
+
 /**
  * Base class the entities are derived from. Provides base functionality of
  * an entity. Entities are used to represent rows of a database table.<p>
  */
 
-
 package  mir.entity;
 
-import java.lang.*;
-import java.io.*;
-import java.util.*;
-import java.sql.*;
-
-import freemarker.template.*;
-
-import mir.storage.*;
-import mir.misc.*;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Set;
+
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.misc.Logfile;
+import mir.misc.StringUtil;
+import mir.storage.StorageObject;
+import mir.storage.StorageObjectExc;
+import mir.storage.StorageObjectFailure;
+import freemarker.template.SimpleScalar;
+import freemarker.template.TemplateHashModel;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+import freemarker.template.TemplateModelRoot;
 
 /**
  * Base Class of Entities
  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
  *
+ * @version $Id: Entity.java,v 1.13 2003/01/25 17:45:17 idfx Exp $
  * @author rk
- * @version 29.6.1999
  *
  */
 
 public class Entity implements TemplateHashModel, TemplateModelRoot
 {
+  protected static MirPropertiesConfiguration configuration;
+  protected static Logfile    theLog;
+  
   private boolean             changed;
   protected HashMap           theValuesHash;   // tablekey / value
   protected StorageObject     theStorageObject;
-  protected static Logfile    theLog;
   protected ArrayList         streamedInput=null;
-  private static int instances = 0;
-    static {
-      theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
+  
+
+    static {      
+      try {
+        configuration = MirPropertiesConfiguration.instance();
+      } catch (PropertiesConfigExc e) {
+        e.printStackTrace();
+      }
+      theLog = Logfile.getInstance(configuration.getStringWithHome("Entity.Logfile"));
     }
 
     public Entity() {
-
       this.changed = false;
-      instances++;
-      Integer i = new Integer(instances);
-      System.err.println("New abstract entity instance: "+i.toString());
     }
 
   /**
@@ -96,7 +138,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return String Id
    */
   public String getId () {
-    return  (String)getValue(theStorageObject.getIdName());
+    return  (String) getValue(theStorageObject.getIdName());
   }
 
   /**
@@ -118,13 +160,13 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     {
       if (field.equals("webdb_create_formatted"))
       {
-               if (hasValueForField("webdb_create"))
-                       returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
+        if (hasValueForField("webdb_create"))
+          returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
       }
       else if (field.equals("webdb_lastchange_formatted"))
       {
-        if (hasValueForField("webdblast_change"))
-                           returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
+        if (hasValueForField("webdb_lastchange"))
+          returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
       }
       else
         returnValue = (String)theValuesHash.get(field);
@@ -132,7 +174,6 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     return returnValue;
   }
 
-
   public boolean hasValueForField(String field)
   {
     if (theValuesHash!=null)
@@ -145,30 +186,32 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return Primary Key of the Entity
    * @exception StorageObjectException
    */
-  public String insert () throws StorageObjectException {
+  public String insert () throws StorageObjectExc {
     theLog.printDebugInfo("Entity: trying to insert ...");
     if (theStorageObject != null) {
       return theStorageObject.insert((Entity)this);
     }
     else
-      throw  new StorageObjectException("Kein StorageObject gesetzt!");
+      throw  new StorageObjectExc("Kein StorageObject gesetzt!");
   }
 
   /**
    * Saves changes of this Entity to the database
    * @exception StorageObjectException
    */
-  public void update () throws StorageObjectException {
+  public void update () throws StorageObjectFailure {
     theStorageObject.update((Entity)this);
   }
 
   /**
-   * Sets the value for a field. Issues a log message if the field name supplied was not found in the Entity.
+   * Sets the value for a field. Issues a log message if the field name
+   * supplied was not found in the Entity.
    * @param theProp The field name whose value has to be set
    * @param theValue The new value of the field
    * @exception StorageObjectException
    */
-  public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
+  public void setValueForProperty (String theProp, String theValue)
+    throws StorageObjectFailure {
     this.changed = true;
     if (isField(theProp))
       theValuesHash.put(theProp, theValue);
@@ -183,7 +226,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return ArrayList with field names
    * @exception StorageObjectException is throuwn if database access was impossible
    */
-  public ArrayList getFields () throws StorageObjectException {
+  public ArrayList getFields () throws StorageObjectFailure {
     return  theStorageObject.getFields();
     }
 
@@ -192,7 +235,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return int[] that contains the types of the fields
    * @exception StorageObjectException
    */
-  public int[] getTypes () throws StorageObjectException {
+  public int[] getTypes () throws StorageObjectFailure {
     return  theStorageObject.getTypes();
     }
 
@@ -201,7 +244,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return List with field names
    * @exception StorageObjectException
    */
-  public ArrayList getLabels () throws StorageObjectException {
+  public ArrayList getLabels () throws StorageObjectFailure {
     return  theStorageObject.getLabels();
     }
 
@@ -240,37 +283,19 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @return true in case fieldName is a field name, else false.
    * @exception StorageObjectException
    */
-  public boolean isField (String fieldName) throws StorageObjectException {
+  public boolean isField (String fieldName) throws StorageObjectFailure {
     return  theStorageObject.getFields().contains(fieldName);
   }
 
-   /** Returns the number of instances of this Entity
-   * @return int The number of instances
-   */
-  public int getInstances() {
-     return instances;
-  }
 
-  protected void throwStorageObjectException (Exception e, String wo) throws StorageObjectException {
+
+  protected void throwStorageObjectFailure (Exception e, String wo) 
+       throws StorageObjectFailure {
     theLog.printError( e.toString() + " Funktion: "+ wo);
-    throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
+    e.printStackTrace(System.out);
+    throw  new StorageObjectFailure("Storage Object Exception in entity", e);
   }
 
-  /**
-   * Frees an instance
-   */
-  /*public void finalize () {
-    instances--;
-    Integer i = new Integer(instances);
-    System.err.println("Removing abstract entity instance: "+i.toString());
-    try {
-      super.finalize();
-    } catch (Throwable t) {
-      System.err.println(t.toString());
-    }
-  }*/
-
-
   // Now implements freemarkers TemplateHashModel
   // two methods have to be overridden:
   // 1. public boolean isEmpty() throws TemplateModelException
@@ -283,10 +308,10 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
 
   public TemplateModel get(java.lang.String key) throws TemplateModelException
   {
-    return new SimpleScalar(getValue(key));
+                return new SimpleScalar(getValue(key));
   }
 
-  public void put(java.lang.String key, TemplateModel model)
+        public void put(java.lang.String key, TemplateModel model)
   {
     // putting should only take place via setValue and is limited to the
     // database fields associated with the entity. no additional freemarker