first cut of merge of STABLE-pre1_0 into HEAD. I won't even guarantee that it
[mir.git] / source / mir / entity / Entity.java
index c6790d7..6dd39ac 100755 (executable)
@@ -1,6 +1,6 @@
 /**
  * Base class the entities are derived from. Provides base functionality of
- * an entity<p>
+ * an entity. Entities are used to represent rows of a database table.<p>
  */
 
 
@@ -33,9 +33,12 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   protected static Logfile    theLog;
   protected ArrayList         streamedInput=null;
   private static int instances = 0;
+    static {
+      theLog = Logfile.getInstance(MirConfig.getProp("Home") + MirConfig.getProp("Entity.Logfile"));
+    }
 
     public Entity() {
-      theLog = Logfile.getInstance(this.getClass().getName());
+
       this.changed = false;
       instances++;
       Integer i = new Integer(instances);
@@ -43,7 +46,8 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     }
 
   /**
-   * Konstruktor
+   * Constructor
+   * @param StorageObject The StorageObject of the Entity.
    */
   public Entity (StorageObject StorageObject) {
     this();
@@ -51,33 +55,36 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /*
-   * Setzt das StorageObject der Entity.
+   * Sets the StorageObject of the Entity.
    */
   public void setStorage (StorageObject storage) {
     this.theStorageObject = storage;
   }
 
   /**
-   * Setzt die Werte der Entity
-   * @param theStringValues
+   * Sets the values of the Entity.
+   * @param theStringValues HashMap containing the new values of the Entity
    */
 
   public void setValues(HashMap theStringValues)
   {
     /** @todo should be synchronized */
-    theValuesHash = new HashMap();
-    String aKey;
-    Set set = theStringValues.keySet();
-    Iterator it = set.iterator();
-    int size = set.size();
-    for (int i = 0; i < size; i++) {
-      aKey = (String)it.next();
-      theValuesHash.put(aKey, (String)theStringValues.get(aKey));
+    if (theStringValues!=null) {
+      theValuesHash = new HashMap();
+      String aKey;
+      Set set = theStringValues.keySet();
+      Iterator it = set.iterator();
+      int size = set.size();
+      for (int i = 0; i < size; i++) {
+        aKey = (String)it.next();
+        theValuesHash.put(aKey, (String)theStringValues.get(aKey));
+      }
     }
+    else theLog.printWarning("Entity.setValues called with null HashMap");
  }
 
   /**
-   * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
+   * Returns whether the content of the Entity has changed.
    * @return true wenn ja, sonst false
    */
   public boolean changed () {
@@ -85,7 +92,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /**
-   * Liefert den Primärschluessel der Entity zurueck
+   * Returns the primary key of the Entity.
    * @return String Id
    */
   public String getId () {
@@ -93,7 +100,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /**
-   * Setzt den Primaerschluessel der Entity
+   * Defines the primary key of the Entity
    * @param id
    */
   public void setId (String id) {
@@ -101,9 +108,9 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
       }
 
   /**
-   * Liefert den Wert für einen Feldnamen zurueck
-   * @param theFieldString
-   * @return Wert für Feld
+   * Returns the value of a field by field name.
+   * @param field The name of the field
+   * @return value of the field
    */
   public String getValue (String field) {
     String returnValue = null;
@@ -134,8 +141,8 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /**
-   * Fügt Entity via StorageObject in Datenbank ein.
-   * @return Primary Key der Entity
+   * Insers Entity into the database via StorageObject
+   * @return Primary Key of the Entity
    * @exception StorageObjectException
    */
   public String insert () throws StorageObjectException {
@@ -148,7 +155,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /**
-   * Aktualisiert Aenderungen an der Entity in der Datenbank
+   * Saves changes of this Entity to the database
    * @exception StorageObjectException
    */
   public void update () throws StorageObjectException {
@@ -156,33 +163,33 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   /**
-   * Setzt den Wert fuer ein Feld
-   * @param theProp
-   * @param theValue
+   * 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 {
     this.changed = true;
     if (isField(theProp))
       theValuesHash.put(theProp, theValue);
-    else
+    else {
       theLog.printWarning("Property not found: " + theProp+theValue);
+    }
 
   }
 
   /**
-   * Gibt die Feldnamen der Entity als ArrayList zurueck
-   * @return ArrayList mit Feldnamen
-   * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
-   *    möglich.
+   * Returns the field names of the Entity as ArrayListe.
+   * @return ArrayList with field names
+   * @exception StorageObjectException is throuwn if database access was impossible
    */
   public ArrayList getFields () throws StorageObjectException {
     return  theStorageObject.getFields();
     }
 
   /**
-   * Liefert ein int[] mit den Typen der Felder zurueck
-   * @return int[] mit den Feldtypen
+   * Returns an int[] with the types of the fields
+   * @return int[] that contains the types of the fields
    * @exception StorageObjectException
    */
   public int[] getTypes () throws StorageObjectException {
@@ -190,8 +197,8 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     }
 
   /**
-   * Liefert ArrayListe mit Feldnamen zurueck.
-   * @return Liste mit Feldnamen
+   * Returns an ArrayList with field names
+   * @return List with field names
    * @exception StorageObjectException
    */
   public ArrayList getLabels () throws StorageObjectException {
@@ -199,19 +206,25 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     }
 
   /**
-   * Liefert eine Hashmap mit allen Werten der Entity zurueck
-   * @return HashMap mit Feldname/Wert
+   * Returns a Hashmap with all values of the Entity.
+   * @return HashMap with field name as key and the corresponding values
    *
    * @deprecated This method is deprecated and will be deleted in the next release.
-   *  AbstractEntity interfaces freemarker.template.TemplateHashModel now and can
+   *  Entity interfaces freemarker.template.TemplateHashModel now and can
    *  be used in the same way as SimpleHash.
 
    */
     public HashMap getValues() {
+      theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources");
       return theValuesHash;
     }
 
     /**
+     * Returns an ArrayList with all database fields that can
+     * be evaluated as streamedInput.
+     * Could be automated by the types (blob, etc.)
+     * Until now to be created manually in the inheriting class
+     *
      *  Liefert einen ArrayList mit allen Datenbankfeldern, die
      *  als streamedInput ausgelesen werden muessen.
      *  Waere automatisierbar ueber die types (blob, etc.)
@@ -222,32 +235,40 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
     return streamedInput;
   }
 
-   /* Fragt ab, ob fieldName einem Feld entspricht
+   /** Returns whether fieldName is a valid field name of this Entity.
    * @param fieldName
-   * @return true, wennn ja, sonst false
+   * @return true in case fieldName is a field name, else false.
    * @exception StorageObjectException
    */
   public boolean isField (String fieldName) throws StorageObjectException {
     return  theStorageObject.getFields().contains(fieldName);
   }
 
-   /** Liefert Anzahl der Instanzen zurück
-   * @return int
+   /** 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 {
+    theLog.printError( e.toString() + " Funktion: "+ wo);
+    throw  new StorageObjectException("Storage Object Exception in entity" +e.toString());
+  }
+
   /**
-   * Gibt eine Instanz frei
+   * Frees an instance
    */
-  public void finalize () {
+  /*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
@@ -257,25 +278,25 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
 
   public boolean isEmpty() throws TemplateModelException
   {
-    if (theValuesHash==null || theValuesHash.isEmpty())
-      return true;
-    return false;
+    return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
   }
 
   public TemplateModel get(java.lang.String key) throws TemplateModelException
   {
-    theLog.printDebugInfo("trying to get: " + key);
     return new SimpleScalar(getValue(key));
   }
 
   public void put(java.lang.String key, TemplateModel model)
   {
-    // empty for testing
+    // putting should only take place via setValue and is limited to the
+    // database fields associated with the entity. no additional freemarker
+    // stuff will be available via Entity.
+    theLog.printWarning("### put is called on entity! - the values will be lost!");
   }
 
   public void remove(java.lang.String key)
   {
-    // empty for testing
+    // do we need this?
   }