1.1 restoration
[mir.git] / source / mir / entity / AbstractEntity.java
index aed7e85..a4e6585 100755 (executable)
-/**
- * <b>abstrakte Basisklasse der Entity-Klassen</b><p>
+/*
+ * 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  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.
  */
-
-
 package  mir.entity;
 
-import java.lang.*;
-import java.io.*;
-import java.util.*;
-import java.sql.*;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
 
-import mir.storage.*;
-import mir.misc.*;
+import mir.config.MirPropertiesConfiguration;
+import mir.config.MirPropertiesConfiguration.PropertiesConfigExc;
+import mir.log.LoggerWrapper;
+import mir.storage.StorageObject;
+import mir.storage.StorageObjectExc;
+import mir.storage.StorageObjectFailure;
 
 /**
- * abstrakte Basisklasse der Entity-Klassen
- *
- * @author RK
- * @version 29.6.1999
+ * Base class the entities are derived from. Provides base functionality of
+ * an entity.
  *
+ * @version $Id: AbstractEntity.java,v 1.8.2.3 2004/11/21 22:07:13 zapata Exp $
  */
 
-public class AbstractEntity implements Entity
-{
-  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"));
-    }
+public class AbstractEntity implements Entity {
+  protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
 
-    public AbstractEntity() {
-      this.changed = false;
-      instances++;
-    }
+  protected Map values;
+  protected StorageObject storageObject;
+  protected LoggerWrapper logger;
+
+  public AbstractEntity() {
+    logger = new LoggerWrapper("Entity");
+
+    values = new HashMap();
+  }
 
   /**
-   * Konstruktor
+   * Constructor
+   * @param StorageObject The StorageObject of the Entity.
    */
-  public AbstractEntity (StorageObject StorageObject) {
+  public AbstractEntity(StorageObject StorageObject) {
     this();
+
     setStorage(StorageObject);
   }
 
-  /*
-   * Setzt das StorageObject der Entity.
-   */
-  public void setStorage (StorageObject storage) {
-    this.theStorageObject = storage;
+  public void setStorage(StorageObject storage) {
+    this.storageObject = storage;
   }
 
-  /**
-   * Setzt die Werte der Entity
-   * @param theStringValues
-   */
+  /** {@inheritDoc} */
+  public void setFieldValues(Map aMap) {
+    if (aMap!=null) {
+      Iterator i = aMap.entrySet().iterator();
+      synchronized(this) {
+        while (i.hasNext()) {
+          Map.Entry entry = (Map.Entry) i.next();
 
-  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));
+          setFieldValue( (String) entry.getKey(), (String) entry.getValue());
+        }
+      }
     }
- }
 }
 
-  /**
-   * Liefert boolschen Wert, ob sich der Inhalt der Entity geändert hat.
-   * @return true wenn ja, sonst false
-   */
-  public boolean changed () {
-    return  changed;
+  /** {@inheritDoc} */
+  public String getId() {
+    return getFieldValue(storageObject.getIdName());
   }
 
-  /**
-   * Liefert den Primärschluessel der Entity zurueck
-   * @return String Id
-   */
-  public String getId () {
-    return  (String)getValue(theStorageObject.getIdName());
+  /** {@inheritDoc} */
+  public void setId(String id) {
+    setFieldValue(storageObject.getIdName(), id);
   }
 
-  /**
-   * Setzt den Primaerschluessel der Entity
-   * @param id
-   */
-  public void setId (String id) {
-    theValuesHash.put(theStorageObject.getIdName(), id);
-      }
+  /** {@inheritDoc} */
+  public String insert() throws StorageObjectExc {
+    logger.debug("Entity: trying to insert ...");
 
-  /**
-   * Liefert den Wert für einen Feldnamen zurueck
-   * @param theFieldString
-   * @return Wert für Feld
-   */
-  public String getValue (String theFieldString) {
-    return  (String)theValuesHash.get(theFieldString);
-    }
-
-  /**
-   * Fügt Entity via StorageObject in Datenbank ein.
-   * @return Primary Key der Entity
-   * @exception StorageObjectException
-   */
-  public String insert () throws StorageObjectException {
-    theLog.printDebugInfo("Entity: trying to insert ...");
-    if (theStorageObject != null) {
-      return theStorageObject.insert((Entity)this);
+    if (storageObject != null) {
+      return storageObject.insert(this);
     }
     else
-      throw  new StorageObjectException("Kein StorageObject gesetzt!");
+      throw new StorageObjectExc("storageObject == null!");
   }
 
-  /**
-   * Aktualisiert Aenderungen an der Entity in der Datenbank
-   * @exception StorageObjectException
-   */
-  public void update () throws StorageObjectException {
-    theStorageObject.update((Entity)this);
+  /** {@inheritDoc} */
+  public void update() throws StorageObjectFailure {
+    storageObject.update(this);
   }
 
-  /**
-   * Setzt den Wert fuer ein Feld
-   * @param theProp
-   * @param theValue
-   * @exception StorageObjectException
-   */
-  public void setValueForProperty (String theProp, String theValue) throws StorageObjectException {
-    this.changed = true;
-    if (isField(theProp))
-      theValuesHash.put(theProp, theValue);
-    else
-      theLog.printWarning("Property not found: " + theProp+theValue);
+  /** {@inheritDoc} */
+  public String getFieldValue(String aFieldName) {
+    String returnValue = null;
 
-  }
-
-  /**
-   * Gibt die Feldnamen der Entity als ArrayList zurueck
-   * @return ArrayList mit Feldnamen
-   * @exception StorageObjectException wird geworfen, wenn kein Zugriff auf die Datenbank
-   *    möglich.
-   */
-  public ArrayList getFields () throws StorageObjectException {
-    return  theStorageObject.getFields();
+    if (aFieldName != null) {
+      returnValue = (String) values.get(aFieldName);
     }
+    return returnValue;
+  }
 
-  /**
-   * Liefert ein int[] mit den Typen der Felder zurueck
-   * @return int[] mit den Feldtypen
-   * @exception StorageObjectException
-   */
-  public int[] getTypes () throws StorageObjectException {
-    return  theStorageObject.getTypes();
-    }
+  /** {@inheritDoc} */
+  public boolean hasFieldValue(String aFieldName) {
+    return values.containsKey(aFieldName);
+  }
 
   /**
-   * Liefert ArrayListe mit Feldnamen zurueck.
-   * @return Liste mit Feldnamen
-   * @exception StorageObjectException
-   */
-  public ArrayList getLabels () throws StorageObjectException {
-    return  theStorageObject.getLabels();
+   * 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 StorageObjectFailure
+   */
+  public void setFieldValue(String theProp, String theValue) throws StorageObjectFailure {
+    if (hasField(theProp))
+      values.put(theProp, theValue);
+    else {
+      logger.warn("Entity.setFieldValue: Property not found: " + theProp + " (" + theValue + ")");
     }
+  }
 
   /**
-   * Liefert eine Hashmap mit allen Werten der Entity zurueck
-   * @return HashMap mit Feldname/Wert
+   * Returns the field names of the Entity
    */
-    public HashMap getValues() {
-      return theValuesHash;
-    }
-
-    /**
-     *  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;
+  public List getFieldNames() throws StorageObjectFailure {
+    return storageObject.getFieldNames();
   }
 
-   /* 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
-   * @exception StorageObjectException
+   * @return true in case fieldName is a field name, else false.
+   * @exception StorageObjectFailure
    */
-  public boolean isField (String fieldName) throws StorageObjectException {
-    return  theStorageObject.getFields().contains(fieldName);
-  }
-
-   /** Liefert Anzahl der Instanzen zurück
-   * @return int
-   */
-  public int getInstances() {
-     return instances;
-  }
-  /**
-   * Gibt eine Instanz frei
-   */
-  public void finalize () {
-    instances--;
-    try {
-      super.finalize();
-    } catch (Throwable t) {
-      System.err.println(t.toString());
-    }
+  public boolean hasField(String fieldName) throws StorageObjectFailure {
+    return getFieldNames().contains(fieldName);
   }
 }