X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;ds=sidebyside;f=source%2Fmir%2Fentity%2FEntity.java;h=10dcad38386eee3a26fb67d122ca39ae046adc7c;hb=b7ea95152eaddbf069564a5f2f117774165d36e6;hp=e4d892727989fbaadb551f486b8a02b4285985ef;hpb=aa0e25363f8099782b07ad71a627da64ef65ba09;p=mir.git diff --git a/source/mir/entity/Entity.java b/source/mir/entity/Entity.java index e4d89272..10dcad38 100755 --- a/source/mir/entity/Entity.java +++ b/source/mir/entity/Entity.java @@ -29,253 +29,78 @@ */ package mir.entity; -import java.util.HashMap; -import java.util.List; import java.util.Map; +import java.util.List; -import mir.config.MirPropertiesConfiguration; -import mir.config.MirPropertiesConfiguration.PropertiesConfigExc; -import mir.log.LoggerWrapper; -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; +import mir.storage.DatabaseExc; +import mir.storage.DatabaseFailure; /** - * Base class the entities are derived from. Provides base functionality of - * an entity. Entities are used to represent rows of a database table.

- * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant - * - * @version $Id: Entity.java,v 1.21 2003/05/03 00:21:22 zapata Exp $ - * @author rk + * An Entity represents a persistent data object, typically + * stored in a database.

* + * @version $Id: Entity.java,v 1.21.2.14 2005/08/21 17:09:18 zapata Exp $ */ -public class Entity implements TemplateHashModel, TemplateModelRoot -{ - protected static MirPropertiesConfiguration configuration; - - protected Map theValuesHash; // tablekey / value - 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"); - } - - /** - * 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; - } - +public interface Entity { /** * 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 */ - if (theStringValues != null) { - theValuesHash = new HashMap(); - theValuesHash.putAll(theStringValues); - } - else - logger.warn("Entity.setValues called with null Map"); - } + public void setFieldValues(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) + * Defines the primary key of the Entity (only to be set by the Database) * @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; - } + public void setId(String id); /** - * Insers Entity into the database via StorageObject + * Inserts Entity into the database via Database * @return Primary Key of the Entity - * @exception StorageObjectException + * @exception DatabaseExc */ - 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 DatabaseExc; /** * Saves changes of this Entity to the database - * @exception StorageObjectException + * @exception DatabaseFailure */ - public void update() throws StorageObjectFailure { - theStorageObject.update(this); - } + public void update() throws DatabaseFailure; /** * 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 + * @param aFieldName The field name whose value has to be set + * @param aValue The new value of the field + * @exception DatabaseFailure */ - 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 + ")"); - } - } + public void setFieldValue(String aFieldName, String aValue); /** - * 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(); - } + public List getFieldNames() throws DatabaseFailure; /** - * 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(); - } - - - /** - * 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 aFieldName The name of the field + * @return value of the field */ + public String getFieldValue(String aFieldName); - public List streamedInput() { - return streamedInput; - } + public boolean hasFieldValue(String aFieldName); /** 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); - } - - // 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. - logger.warn("put is called on entity! - the values will be lost!"); - } - - public void remove(java.lang.String key) { - // do we need this? - } - - ////////////////////////////////////////////////////////////////////////////////// + public boolean hasField(String aFieldName) throws DatabaseFailure; }