fixed / clean ups
[mir.git] / source / mir / entity / Entity.java
index e2e19c4..adfe2d0 100755 (executable)
@@ -40,61 +40,17 @@ import mir.log.LoggerWrapper;
 import mir.storage.StorageObject;
 import mir.storage.StorageObjectExc;
 import mir.storage.StorageObjectFailure;
-import mir.util.*;
 
 /**
- * Base class the entities are derived from. Provides base functionality of
- * an entity. Entities are used to represent rows of a database table.<p>
- *
- * @version $Id: Entity.java,v 1.21.2.6 2003/12/21 13:32:03 zapata Exp $
- * @author rk
+ * An <code>Entity</code> represents a persistent data object, typically
+ *   stored in a database.<p>
  *
+ * @version $Id: Entity.java,v 1.21.2.7 2004/01/18 17:30:56 zapata Exp $
  */
 
-public class Entity
+public interface Entity
 {
-  protected static MirPropertiesConfiguration configuration;
-
-//  protected Map theValuesHash; // tablekey / value
-  protected Map values;
-  protected StorageObject theStorageObject;
-  protected List streamedInput = null;
-  protected LoggerWrapper logger;
-
-  static {
-    try {
-      configuration = MirPropertiesConfiguration.instance();
-    }
-    catch (PropertiesConfigExc e) {
-      throw new RuntimeException(e.getMessage());
-    }
-  }
-
-  public Entity() {
-    logger = new LoggerWrapper("Entity");
-
-    values = new HashMap();
-  }
-
-  /**
-   * Constructor
-   * @param StorageObject The StorageObject of the Entity.
-   */
-
-  public Entity(StorageObject StorageObject) {
-    this();
-
-    setStorage(StorageObject);
-  }
-
-  /**
-   *
-   * @param storage
-   */
-
-  public void setStorage(StorageObject storage) {
-    this.theStorageObject = storage;
-  }
+  public void setStorage(StorageObject storage);
 
   /**
    * Sets the values of the Entity. (Only to be called by the Storage Object)
@@ -102,175 +58,58 @@ public class Entity
    * @param aMap Map containing the new values of the Entity
    */
 
-  public void setValues(Map aMap) {
-    if (aMap!=null) {
-      Iterator i = aMap.entrySet().iterator();
-      synchronized(this) {
-        while (i.hasNext()) {
-          Map.Entry entry = (Map.Entry) i.next();
-
-          setValueForProperty( (String) entry.getKey(), (String) entry.getValue());
-        }
-      }
-    }
-  }
+  public void setValues(Map aMap);
 
   /**
    * Returns the primary key of the Entity.
    * @return String Id
    */
-  public String getId() {
-    return (String) getValue(theStorageObject.getIdName());
-  }
+  public String getId();
 
   /**
    * Defines the primary key of the Entity (only to be set by the StorageObject)
    * @param id
    */
-  public void setId(String id) {
-    setValueForProperty(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) {
-      returnValue = (String) values.get(field);
-    }
-    return returnValue;
-  }
-
-  public boolean hasValueForField(String field) {
-    return values.containsKey(field);
-  }
+  public void setId(String id);
 
   /**
    * Insers Entity into the database via StorageObject
    * @return Primary Key of the Entity
-   * @exception StorageObjectException
+   * @exception StorageObjectExc
    */
-  public String insert() throws StorageObjectExc {
-    logger.debug("Entity: trying to insert ...");
-
-    if (theStorageObject != null) {
-      return theStorageObject.insert(this);
-    }
-    else
-      throw new StorageObjectExc("theStorageObject == null!");
-  }
+  public String insert() throws StorageObjectExc;
 
   /**
    * Saves changes of this Entity to the database
-   * @exception StorageObjectException
+   * @exception StorageObjectFailure
    */
-  public void update() throws StorageObjectFailure {
-    theStorageObject.update(this);
-  }
+  public void update() throws StorageObjectFailure;
 
   /**
    * 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
+   * @exception StorageObjectFailure
    */
-  public void setValueForProperty(String theProp, String theValue) throws StorageObjectFailure {
-    try {
-      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(LoggerWrapper.DEBUG_MESSAGE));
-
-      throw new StorageObjectFailure(t);
-    }
-  }
+  public void setFieldValue(String theProp, String theValue);
 
   /**
-   * Returns the field names of the Entity as ArrayListe.
-   * @return ArrayList with field names
-       * @exception StorageObjectException is throuwn if database access was impossible
+   * Returns the field names of the Entity
    */
-  public List 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 List getLabels() throws StorageObjectFailure {
-    return theStorageObject.getLabels();
-  }
-
+  public List getFieldNames() throws StorageObjectFailure;
 
   /**
-   * 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
+   * Returns the value of a field by field name.
+   * @param field The name of the field
+   * @return value of the field
    */
+  public String getFieldValue(String field);
 
-  public List streamedInput() {
-    return streamedInput;
-  }
+  public boolean hasFieldValue(String field);
 
   /** 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(Throwable e, String wo) throws StorageObjectFailure {
-    logger.error(e.toString() + " function: " + wo);
-    e.printStackTrace(logger.asPrintWriter(LoggerWrapper.DEBUG_MESSAGE));
-
-    throw new StorageObjectFailure("Storage Object Exception in entity", e);
-  }
-
-  /**
-   * Helper method to append a line to a field
    */
-  public void appendLineToField(String aFieldName, String aLine) {
-    StringBuffer fieldContent = new StringBuffer();
-    try {
-      fieldContent.append(StringRoutines.interpretAsString(getValue(aFieldName)));
-    }
-    catch (Throwable t) {
-    }
-    if (fieldContent.length() > 0 && fieldContent.charAt(fieldContent.length() - 1) != '\n') {
-      fieldContent.append('\n');
-    }
-
-    fieldContent.append(aLine);
-    setValueForProperty(aFieldName, fieldContent.toString());
-  }
+  public boolean hasField(String aFieldName) throws StorageObjectFailure;
 }