rebuilding head
[mir.git] / source / mir / entity / AbstractEntity.java
diff --git a/source/mir/entity/AbstractEntity.java b/source/mir/entity/AbstractEntity.java
new file mode 100755 (executable)
index 0000000..ec6d9b0
--- /dev/null
@@ -0,0 +1,164 @@
+/*
+ * 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.util.HashMap;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+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;
+
+/**
+ * Base class the entities are derived from. Provides base functionality of
+ * an entity.
+ *
+ * @version $Id: AbstractEntity.java,v 1.9 2004/11/06 19:18:12 idfx Exp $
+ */
+
+public class AbstractEntity implements Entity {
+  protected static MirPropertiesConfiguration configuration = MirPropertiesConfiguration.instance();
+
+  protected Map values;
+  protected StorageObject storageObject;
+  protected LoggerWrapper logger;
+
+  public AbstractEntity() {
+    logger = new LoggerWrapper("Entity");
+
+    values = new HashMap();
+  }
+
+  /**
+   * Constructor
+   * @param StorageObject The StorageObject of the Entity.
+   */
+  public AbstractEntity(StorageObject StorageObject) {
+    this();
+
+    setStorage(StorageObject);
+  }
+
+  public void setStorage(StorageObject storage) {
+    this.storageObject = storage;
+  }
+
+  /** {@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();
+
+          setFieldValue( (String) entry.getKey(), (String) entry.getValue());
+        }
+      }
+    }
+  }
+
+  /** {@inheritDoc} */
+  public String getId() {
+    return getFieldValue(storageObject.getIdName());
+  }
+
+  /** {@inheritDoc} */
+  public void setId(String id) {
+    setFieldValue(storageObject.getIdName(), id);
+  }
+
+  /** {@inheritDoc} */
+  public String insert() throws StorageObjectExc {
+    logger.debug("Entity: trying to insert ...");
+
+    if (storageObject != null) {
+      return storageObject.insert(this);
+    }
+    else
+      throw new StorageObjectExc("storageObject == null!");
+  }
+
+  /** {@inheritDoc} */
+  public void update() throws StorageObjectFailure {
+    storageObject.update(this);
+  }
+
+  /** {@inheritDoc} */
+  public String getFieldValue(String aFieldName) {
+    String returnValue = null;
+
+    if (aFieldName != null) {
+      returnValue = (String) values.get(aFieldName);
+    }
+    return returnValue;
+  }
+
+  /** {@inheritDoc} */
+  public boolean hasFieldValue(String aFieldName) {
+    return values.containsKey(aFieldName);
+  }
+
+  /**
+   * 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 + ")");
+    }
+  }
+
+  /**
+   * Returns the field names of the Entity
+   */
+  public List getFieldNames() throws StorageObjectFailure {
+    return storageObject.getFields();
+  }
+
+  /** 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 StorageObjectFailure
+   */
+  public boolean hasField(String fieldName) throws StorageObjectFailure {
+    return getFieldNames().contains(fieldName);
+  }
+}
+