Entity / Database fix
authorzapata <zapata>
Thu, 22 May 2003 19:45:06 +0000 (19:45 +0000)
committerzapata <zapata>
Thu, 22 May 2003 19:45:06 +0000 (19:45 +0000)
source/mir/entity/Entity.java
source/mir/storage/Database.java

index 465366b..14014e1 100755 (executable)
@@ -31,7 +31,7 @@ package  mir.entity;
 
 import java.util.HashMap;
 import java.util.List;
-import java.util.Map;
+import java.util.*;
 
 import mir.config.MirPropertiesConfiguration;
 import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
@@ -51,7 +51,7 @@ import freemarker.template.TemplateModelRoot;
  * an entity. Entities are used to represent rows of a database table.<p>
  * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant
  *
- * @version $Id: Entity.java,v 1.21.2.1 2003/05/22 03:15:07 zapata Exp $
+ * @version $Id: Entity.java,v 1.21.2.2 2003/05/22 19:45:06 zapata Exp $
  * @author rk
  *
  */
@@ -60,7 +60,8 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
 {
   protected static MirPropertiesConfiguration configuration;
 
-  protected Map theValuesHash; // tablekey / value
+//  protected Map theValuesHash; // tablekey / value
+  protected Map values;
   protected StorageObject theStorageObject;
   protected List streamedInput = null;
   protected LoggerWrapper logger;
@@ -76,37 +77,59 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
 
   public Entity() {
     logger = new LoggerWrapper("Entity");
+
+    values = new HashMap();
   }
 
   /**
    * Constructor
    * @param StorageObject The StorageObject of the Entity.
    */
+
   public Entity(StorageObject StorageObject) {
     this();
+
     setStorage(StorageObject);
   }
 
-  /*
-   * Sets the StorageObject of the Entity.
+  /**
+   *
+   * @param storage
    */
+
   public void setStorage(StorageObject storage) {
     this.theStorageObject = storage;
   }
 
   /**
    * Sets the values of the Entity. (Only to be called by the Storage Object)
-   * @param theStringValues Map containing the new values of the Entity
+   *
+   * @param aMap Map containing the new values of the Entity
    */
 
-  public void setValues(Map theStringValues) {
-    /** @todo should be synchronized */
+  public void setValues(Map aMap) {
+    if (aMap!=null) {
+      Iterator i = aMap.entrySet().iterator();
+      logger.info("aMap = " + aMap.toString());
+
+      synchronized(this) {
+        while (i.hasNext()) {
+          Map.Entry entry = (Map.Entry) i.next();
+
+          logger.info("setting " + entry.getKey());
+
+          setValueForProperty( (String) entry.getKey(), (String) entry.getValue());
+        }
+      }
+    }
+/*
     if (theStringValues != null) {
       theValuesHash = new HashMap();
       theValuesHash.putAll(theStringValues);
     }
     else
       logger.warn("Entity.setValues called with null Map");
+*/
   }
 
   /**
@@ -122,7 +145,11 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @param id
    */
   public void setId(String id) {
+
+    setValueForProperty(theStorageObject.getIdName(), id);
+/*
     theValuesHash.put(theStorageObject.getIdName(), id);
+ */
   }
 
   /**
@@ -132,7 +159,9 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    */
   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"));
@@ -142,16 +171,14 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
           returnValue = StringUtil.dateToReadableDate(getValue(
               "webdb_lastchange"));
       }
-      else
-        returnValue = (String) theValuesHash.get(field);
+      else */
+      returnValue = (String) values.get(field);
     }
     return returnValue;
   }
 
   public boolean hasValueForField(String field) {
-    if (theValuesHash != null)
-      return theValuesHash.containsKey(field);
-    return false;
+    return values.containsKey(field);
   }
 
   /**
@@ -161,6 +188,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    */
   public String insert() throws StorageObjectExc {
     logger.debug("Entity: trying to insert ...");
+
     if (theStorageObject != null) {
       return theStorageObject.insert(this);
     }
@@ -184,10 +212,19 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
    * @exception StorageObjectException
    */
   public void setValueForProperty(String theProp, String theValue) throws StorageObjectFailure {
-    if (isField(theProp))
-      theValuesHash.put(theProp, theValue);
-    else {
-      logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
+    try {
+      logger.info("setting " + theProp + " to (" + theValue + ")");
+      if (isField(theProp))
+        values.put(theProp, theValue);
+      else {
+        logger.warn("Entity.setValueForProperty: Property not found: " + theProp + " (" + theValue + ")");
+      }
+    }
+    catch (Throwable t) {
+      logger.error("Entity.setValueForProperty: " + t.toString());
+      t.printStackTrace(logger.asPrintWriter(logger.DEBUG_MESSAGE));
+
+      throw new StorageObjectFailure(t);
     }
   }
 
@@ -257,7 +294,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   // 2. public TemplateModel get(java.lang.String key) throws TemplateModelException
 
   public boolean isEmpty() throws TemplateModelException {
-    return (theValuesHash == null || theValuesHash.isEmpty()) ? true : false;
+    return false;
   }
 
   public TemplateModel get(java.lang.String key) throws TemplateModelException {
@@ -272,7 +309,7 @@ public class Entity implements TemplateHashModel, TemplateModelRoot
   }
 
   public void remove(java.lang.String key) {
-    // do we need this?
+    logger.warn("remove is called on entity!");
   }
 
   //////////////////////////////////////////////////////////////////////////////////
index d11554b..4c4ac94 100755 (executable)
@@ -76,7 +76,7 @@ import mir.util.JDBCStringRoutines;
  * Treiber, Host, User und Passwort, ueber den der Zugriff auf die
  * Datenbank erfolgt.
  *
- * @version $Id: Database.java,v 1.44 2003/05/06 18:08:05 zapata Exp $
+ * @version $Id: Database.java,v 1.44.2.1 2003/05/22 19:45:06 zapata Exp $
  * @author rk
  *
  */
@@ -725,8 +725,8 @@ public class Database implements StorageObject {
 
       if (theEntityClass != null) {
         returnEntity = (Entity) theEntityClass.newInstance();
-        returnEntity.setValues(theResultHash);
         returnEntity.setStorage(this);
+        returnEntity.setValues(theResultHash);
 
         if (returnEntity instanceof StorableObject) {
           logger.debug("CACHE: ( in) " + returnEntity.getId() + " :" + theTable);