support for translations + lots of misc. fixes
[mir.git] / source / mir / entity / Entity.java
index 5a69e3e..e37ea7a 100755 (executable)
-/*
- * 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.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
- *
- */
-
-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 ArrayList         streamedInput=null;
-  
-
-    static {      
-      try {
-        configuration = MirPropertiesConfiguration.instance();
-      } catch (PropertiesConfigExc e) {
-        e.printStackTrace();
-      }
-      theLog = Logfile.getInstance(configuration.getStringWithHome("Entity.Logfile"));
-    }
-
-    public Entity() {
-      this.changed = false;
-    }
-
-  /**
-   * Constructor
-   * @param StorageObject The StorageObject of the Entity.
-   */
-  public Entity (StorageObject StorageObject) {
-    this();
-    setStorage(StorageObject);
-  }
-
-  /*
-   * Sets the StorageObject of the Entity.
-   */
-  public void setStorage (StorageObject storage) {
-    this.theStorageObject = storage;
-  }
-
-  /**
-   * 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 */
-    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");
- }
-
-  /**
-   * Returns whether the content of the Entity has changed.
-   * @return true wenn ja, sonst false
-   */
-  public boolean changed () {
-    return  changed;
-  }
-
-  /**
-   * Returns the primary key of the Entity.
-   * @return String Id
-   */
-  public String getId () {
-    return  (String) getValue(theStorageObject.getIdName());
-  }
-
-  /**
-   * Defines the primary key of the Entity
-   * @param id
-   */
-  public void setId (String id) {
-    theValuesHash.put(theStorageObject.getIdName(), id);
-      }
-
-  /**
-   * 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;
-    if (field != null)
-    {
-      if (field.equals("webdb_create_formatted"))
-      {
-        if (hasValueForField("webdb_create"))
-          returnValue=StringUtil.dateToReadableDate(getValue("webdb_create"));
-      }
-      else if (field.equals("webdb_lastchange_formatted"))
-      {
-        if (hasValueForField("webdb_lastchange"))
-          returnValue=StringUtil.dateToReadableDate(getValue("webdb_lastchange"));
-      }
-      else
-        returnValue = (String)theValuesHash.get(field);
-    }
-    return returnValue;
-  }
-
-  public boolean hasValueForField(String field)
-  {
-    if (theValuesHash!=null)
-      return theValuesHash.containsKey(field);
-    return false;
-  }
-
-  /**
-   * Insers Entity into the database via StorageObject
-   * @return Primary Key of the Entity
-   * @exception StorageObjectException
-   */
-  public String insert () throws StorageObjectExc {
-    theLog.printDebugInfo("Entity: trying to insert ...");
-    if (theStorageObject != null) {
-      return theStorageObject.insert((Entity)this);
-    }
-    else
-      throw  new StorageObjectExc("Kein StorageObject gesetzt!");
-  }
-
-  /**
-   * Saves changes of this Entity to the database
-   * @exception 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.
-   * @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 StorageObjectFailure {
-    this.changed = true;
-    if (isField(theProp))
-      theValuesHash.put(theProp, theValue);
-    else {
-      theLog.printWarning("Property not found: " + theProp+theValue);
-    }
-
-  }
-
-  /**
-   * 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 StorageObjectFailure {
-    return  theStorageObject.getFields();
-    }
-
-  /**
-   * Returns an int[] with the types of the fields
-   * @return int[] that contains the types of the fields
-   * @exception StorageObjectException
-   */
-  public int[] getTypes () throws StorageObjectFailure {
-    return  theStorageObject.getTypes();
-    }
-
-  /**
-   * Returns an ArrayList with field names
-   * @return List with field names
-   * @exception StorageObjectException
-   */
-  public ArrayList getLabels () throws StorageObjectFailure {
-    return  theStorageObject.getLabels();
-    }
-
-  /**
-   * 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.
-   *  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.)
-     *  Bisher manuell anzulegen in der erbenden Klasse
-     */
-
-  public ArrayList streamedInput() {
-    return streamedInput;
-  }
-
-   /** Returns whether fieldName is a valid field name of this Entity.
-   * @param fieldName
-   * @return true in case fieldName is a field name, else false.
-   * @exception StorageObjectException
-   */
-  public boolean isField (String fieldName) throws StorageObjectFailure {
-    return  theStorageObject.getFields().contains(fieldName);
-  }
-
-
-
-  protected void throwStorageObjectFailure (Exception e, String wo) 
-       throws StorageObjectFailure {
-    theLog.printError( e.toString() + " Funktion: "+ wo);
-    e.printStackTrace(System.out);
-    throw  new StorageObjectFailure("Storage Object Exception in entity", e);
-  }
-
-  // Now implements freemarkers TemplateHashModel
-  // two methods have to be overridden:
-  // 1. public boolean isEmpty() throws TemplateModelException
-  // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
-
-  public boolean isEmpty() throws TemplateModelException
-  {
-    return (theValuesHash==null || theValuesHash.isEmpty()) ? true : false;
-  }
-
-  public TemplateModel get(java.lang.String key) throws TemplateModelException
-  {
-                return new SimpleScalar(getValue(key));
-  }
-
-        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
-    // 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)
-  {
-    // do we need this?
-  }
-
-
-  //////////////////////////////////////////////////////////////////////////////////
-
-
-}
-
+/*\r
+ * Copyright (C) 2001, 2002  The Mir-coders group\r
+ *\r
+ * This file is part of Mir.\r
+ *\r
+ * Mir is free software; you can redistribute it and/or modify\r
+ * it under the terms of the GNU General Public License as published by\r
+ * the Free Software Foundation; either version 2 of the License, or\r
+ * (at your option) any later version.\r
+ *\r
+ * Mir is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
+ * GNU General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU General Public License\r
+ * along with Mir; if not, write to the Free Software\r
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA\r
+ *\r
+ * In addition, as a special exception, The Mir-coders gives permission to link\r
+ * the code of this program with the com.oreilly.servlet library, any library\r
+ * licensed under the Apache Software License, The Sun (tm) Java Advanced\r
+ * Imaging library (JAI), The Sun JIMI library (or with modified versions of\r
+ * the above that use the same license as the above), and distribute linked\r
+ * combinations including the two.  You must obey the GNU General Public\r
+ * License in all respects for all of the code used other than the above\r
+ * mentioned libraries.  If you modify this file, you may extend this exception\r
+ * to your version of the file, but you are not obligated to do so.  If you do\r
+ * not wish to do so, delete this exception statement from your version.\r
+ */\r
+\r
+/**\r
+ * Base class the entities are derived from. Provides base functionality of\r
+ * an entity. Entities are used to represent rows of a database table.<p>\r
+ */\r
+\r
+package  mir.entity;\r
+\r
+import java.util.List;\r
+import java.util.HashMap;\r
+import java.util.Iterator;\r
+import java.util.Set;\r
+\r
+import mir.config.MirPropertiesConfiguration;\r
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;\r
+import mir.misc.Logfile;\r
+import mir.misc.StringUtil;\r
+import mir.storage.StorageObject;\r
+import mir.storage.StorageObjectExc;\r
+import mir.storage.StorageObjectFailure;\r
+import freemarker.template.SimpleScalar;\r
+import freemarker.template.TemplateHashModel;\r
+import freemarker.template.TemplateModel;\r
+import freemarker.template.TemplateModelException;\r
+import freemarker.template.TemplateModelRoot;\r
+\r
+/**\r
+ * Base Class of Entities\r
+ * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant\r
+ *\r
+ * @version $Id: Entity.java,v 1.14 2003/02/20 16:05:32 zapata Exp $\r
+ * @author rk\r
+ *\r
+ */\r
+\r
+public class Entity implements TemplateHashModel, TemplateModelRoot\r
+{\r
+  protected static MirPropertiesConfiguration configuration;\r
+  protected static Logfile theLog;\r
+\r
+  private boolean changed;\r
+  protected HashMap theValuesHash; // tablekey / value\r
+  protected StorageObject theStorageObject;\r
+  protected List streamedInput = null;\r
+\r
+  static {\r
+    try {\r
+      configuration = MirPropertiesConfiguration.instance();\r
+    }\r
+    catch (PropertiesConfigExc e) {\r
+      e.printStackTrace();\r
+    }\r
+    theLog = Logfile.getInstance(configuration.getStringWithHome(\r
+        "Entity.Logfile"));\r
+  }\r
+\r
+  public Entity() {\r
+    this.changed = false;\r
+  }\r
+\r
+  /**\r
+   * Constructor\r
+   * @param StorageObject The StorageObject of the Entity.\r
+   */\r
+  public Entity(StorageObject StorageObject) {\r
+    this();\r
+    setStorage(StorageObject);\r
+  }\r
+\r
+  /*\r
+   * Sets the StorageObject of the Entity.\r
+   */\r
+  public void setStorage(StorageObject storage) {\r
+    this.theStorageObject = storage;\r
+  }\r
+\r
+  /**\r
+   * Sets the values of the Entity.\r
+   * @param theStringValues HashMap containing the new values of the Entity\r
+   */\r
+\r
+  public void setValues(HashMap theStringValues) {\r
+    /** @todo should be synchronized */\r
+    if (theStringValues != null) {\r
+      theValuesHash = new HashMap();\r
+      String aKey;\r
+      Set set = theStringValues.keySet();\r
+      Iterator it = set.iterator();\r
+      int size = set.size();\r
+      for (int i = 0; i < size; i++) {\r
+        aKey = (String) it.next();\r
+        theValuesHash.put(aKey, (String) theStringValues.get(aKey));\r
+      }\r
+    }\r
+    else\r
+      theLog.printWarning("Entity.setValues called with null HashMap");\r
+  }\r
+\r
+  /**\r
+   * Returns whether the content of the Entity has changed.\r
+   * @return true wenn ja, sonst false\r
+   */\r
+  public boolean changed() {\r
+    return changed;\r
+  }\r
+\r
+  /**\r
+   * Returns the primary key of the Entity.\r
+   * @return String Id\r
+   */\r
+  public String getId() {\r
+    return (String) getValue(theStorageObject.getIdName());\r
+  }\r
+\r
+  /**\r
+   * Defines the primary key of the Entity\r
+   * @param id\r
+   */\r
+  public void setId(String id) {\r
+    theValuesHash.put(theStorageObject.getIdName(), id);\r
+  }\r
+\r
+  /**\r
+   * Returns the value of a field by field name.\r
+   * @param field The name of the field\r
+   * @return value of the field\r
+   */\r
+  public String getValue(String field) {\r
+    String returnValue = null;\r
+    if (field != null) {\r
+      if (field.equals("webdb_create_formatted")) {\r
+        if (hasValueForField("webdb_create"))\r
+          returnValue = StringUtil.dateToReadableDate(getValue("webdb_create"));\r
+      }\r
+      else if (field.equals("webdb_lastchange_formatted")) {\r
+        if (hasValueForField("webdb_lastchange"))\r
+          returnValue = StringUtil.dateToReadableDate(getValue(\r
+              "webdb_lastchange"));\r
+      }\r
+      else\r
+        returnValue = (String) theValuesHash.get(field);\r
+    }\r
+    return returnValue;\r
+  }\r
+\r
+  public boolean hasValueForField(String field) {\r
+    if (theValuesHash != null)\r
+      return theValuesHash.containsKey(field);\r
+    return false;\r
+  }\r
+\r
+  /**\r
+   * Insers Entity into the database via StorageObject\r
+   * @return Primary Key of the Entity\r
+   * @exception StorageObjectException\r
+   */\r
+  public String insert() throws StorageObjectExc {\r
+    theLog.printDebugInfo("Entity: trying to insert ...");\r
+    if (theStorageObject != null) {\r
+      return theStorageObject.insert( (Entity)this);\r
+    }\r
+    else\r
+      throw new StorageObjectExc("Kein StorageObject gesetzt!");\r
+  }\r
+\r
+  /**\r
+   * Saves changes of this Entity to the database\r
+   * @exception StorageObjectException\r
+   */\r
+  public void update() throws StorageObjectFailure {\r
+    theStorageObject.update( (Entity)this);\r
+  }\r
+\r
+  /**\r
+   * Sets the value for a field. Issues a log message if the field name\r
+   * supplied was not found in the Entity.\r
+   * @param theProp The field name whose value has to be set\r
+   * @param theValue The new value of the field\r
+   * @exception StorageObjectException\r
+   */\r
+  public void setValueForProperty(String theProp, String theValue) throws\r
+      StorageObjectFailure {\r
+    this.changed = true;\r
+    if (isField(theProp))\r
+      theValuesHash.put(theProp, theValue);\r
+    else {\r
+      theLog.printWarning("Property not found: " + theProp + theValue);\r
+    }\r
+\r
+  }\r
+\r
+  /**\r
+   * Returns the field names of the Entity as ArrayListe.\r
+   * @return ArrayList with field names\r
+       * @exception StorageObjectException is throuwn if database access was impossible\r
+   */\r
+  public List getFields() throws StorageObjectFailure {\r
+    return theStorageObject.getFields();\r
+  }\r
+\r
+  /**\r
+   * Returns an int[] with the types of the fields\r
+   * @return int[] that contains the types of the fields\r
+   * @exception StorageObjectException\r
+   */\r
+  public int[] getTypes() throws StorageObjectFailure {\r
+    return theStorageObject.getTypes();\r
+  }\r
+\r
+  /**\r
+   * Returns an ArrayList with field names\r
+   * @return List with field names\r
+   * @exception StorageObjectException\r
+   */\r
+  public List getLabels() throws StorageObjectFailure {\r
+    return theStorageObject.getLabels();\r
+  }\r
+\r
+  /**\r
+   * Returns a Hashmap with all values of the Entity.\r
+   * @return HashMap with field name as key and the corresponding values\r
+   *\r
+       * @deprecated This method is deprecated and will be deleted in the next release.\r
+   *  Entity interfaces freemarker.template.TemplateHashModel now and can\r
+   *  be used in the same way as SimpleHash.\r
+   */\r
+  public HashMap getValues() {\r
+    theLog.printWarning(\r
+        "## using deprecated Entity.getValues() - a waste of resources");\r
+    return theValuesHash;\r
+  }\r
+\r
+  /**\r
+   * Returns an ArrayList with all database fields that can\r
+   * be evaluated as streamedInput.\r
+   * Could be automated by the types (blob, etc.)\r
+   * Until now to be created manually in the inheriting class\r
+   *\r
+   *  Liefert einen ArrayList mit allen Datenbankfeldern, die\r
+   *  als streamedInput ausgelesen werden muessen.\r
+   *  Waere automatisierbar ueber die types (blob, etc.)\r
+   *  Bisher manuell anzulegen in der erbenden Klasse\r
+   */\r
+\r
+  public List streamedInput() {\r
+    return streamedInput;\r
+  }\r
+\r
+  /** Returns whether fieldName is a valid field name of this Entity.\r
+   * @param fieldName\r
+   * @return true in case fieldName is a field name, else false.\r
+   * @exception StorageObjectException\r
+   */\r
+  public boolean isField(String fieldName) throws StorageObjectFailure {\r
+    return theStorageObject.getFields().contains(fieldName);\r
+  }\r
+\r
+  protected void throwStorageObjectFailure(Exception e, String wo) throws\r
+      StorageObjectFailure {\r
+    theLog.printError(e.toString() + " Funktion: " + wo);\r
+    e.printStackTrace(System.out);\r
+    throw new StorageObjectFailure("Storage Object Exception in entity", e);\r
+  }\r
+\r
+  // Now implements freemarkers TemplateHashModel\r
+  // two methods have to be overridden:\r
+  // 1. public boolean isEmpty() throws TemplateModelException\r
+  // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException\r
+\r
+  public boolean isEmpty() throws TemplateModelException {\r
+    return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;\r
+  }\r
+\r
+  public TemplateModel get(java.lang.String key) throws TemplateModelException {\r
+    return new SimpleScalar(getValue(key));\r
+  }\r
+\r
+  public void put(java.lang.String key, TemplateModel model) {\r
+    // putting should only take place via setValue and is limited to the\r
+    // database fields associated with the entity. no additional freemarker\r
+    // stuff will be available via Entity.\r
+    theLog.printWarning(\r
+        "### put is called on entity! - the values will be lost!");\r
+  }\r
+\r
+  public void remove(java.lang.String key) {\r
+    // do we need this?\r
+  }\r
+\r
+  //////////////////////////////////////////////////////////////////////////////////\r
+}\r
+\r