X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fentity%2FEntity.java;h=d675ad61549e3ea5c7de3c8e5e5ecaf3e296f8a6;hb=6e0cd0d8650db7143b8c21f156df52a51902564d;hp=5a69e3e2915ec047dfb2f6489aa3e236dcdbdf62;hpb=e368bbb50c99177c97d0f3c7de877accc014a4b3;p=mir.git diff --git a/source/mir/entity/Entity.java b/source/mir/entity/Entity.java index 5a69e3e2..d675ad61 100755 --- a/source/mir/entity/Entity.java +++ b/source/mir/entity/Entity.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,167 +18,134 @@ * 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 the com.oreilly.servlet library, 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. + * 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. */ - -/** - * Base class the entities are derived from. Provides base functionality of - * an entity. Entities are used to represent rows of a database table.

- */ - package mir.entity; -import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; -import java.util.Set; +import java.util.List; +import java.util.Map; import mir.config.MirPropertiesConfiguration; import mir.config.MirPropertiesConfiguration.PropertiesConfigExc; -import mir.misc.Logfile; -import mir.misc.StringUtil; +import mir.log.LoggerWrapper; 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; /** - * Base Class of Entities - * Interfacing TemplateHashModel and TemplateModelRoot to be freemarker compliant + * Base class the entities are derived from. Provides base functionality of + * an entity. Entities are used to represent rows of a database table.

* - * @version $Id: Entity.java,v 1.13 2003/01/25 17:45:17 idfx Exp $ + * @version $Id: Entity.java,v 1.21.2.5 2003/10/23 14:55:28 rk Exp $ * @author rk * */ -public class Entity implements TemplateHashModel, TemplateModelRoot +public class Entity { protected static MirPropertiesConfiguration configuration; - protected static Logfile theLog; - - private boolean changed; - protected HashMap theValuesHash; // tablekey / value - protected StorageObject theStorageObject; - protected ArrayList streamedInput=null; - - - static { - try { - configuration = MirPropertiesConfiguration.instance(); - } catch (PropertiesConfigExc e) { - e.printStackTrace(); - } - theLog = Logfile.getInstance(configuration.getStringWithHome("Entity.Logfile")); - } - public Entity() { - this.changed = false; +// 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) { + + public Entity(StorageObject StorageObject) { this(); + setStorage(StorageObject); } - /* - * Sets the StorageObject of the Entity. + /** + * + * @param storage */ - public void setStorage (StorageObject storage) { + + public void setStorage(StorageObject storage) { this.theStorageObject = storage; } /** - * Sets the values of the Entity. - * @param theStringValues HashMap containing the new values of the Entity + * Sets the values of the Entity. (Only to be called by the Storage Object) + * + * @param aMap Map containing the new values of the Entity */ - public void setValues(HashMap theStringValues) - { - /** @todo should be synchronized */ - if (theStringValues!=null) { - 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)); + 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()); + } } } - else theLog.printWarning("Entity.setValues called with null HashMap"); - } - - /** - * Returns whether the content of the Entity has changed. - * @return true wenn ja, sonst false - */ - public boolean changed () { - return changed; } /** * Returns the primary key of the Entity. * @return String Id */ - public String getId () { - return (String) getValue(theStorageObject.getIdName()); + public String getId() { + return (String) getValue(theStorageObject.getIdName()); } /** - * Defines the primary key of the Entity + * Defines the primary key of the Entity (only to be set by the StorageObject) * @param id */ - public void setId (String id) { - theValuesHash.put(theStorageObject.getIdName(), 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) { + 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); + + if (field != null) { + returnValue = (String) values.get(field); } return returnValue; } - public boolean hasValueForField(String field) - { - if (theValuesHash!=null) - return theValuesHash.containsKey(field); - return false; + public boolean hasValueForField(String field) { + return values.containsKey(field); } /** @@ -186,21 +153,22 @@ public class Entity implements TemplateHashModel, TemplateModelRoot * @return Primary Key of the Entity * @exception StorageObjectException */ - public String insert () throws StorageObjectExc { - theLog.printDebugInfo("Entity: trying to insert ..."); + public String insert() throws StorageObjectExc { + logger.debug("Entity: trying to insert ..."); + if (theStorageObject != null) { - return theStorageObject.insert((Entity)this); + return theStorageObject.insert(this); } else - throw new StorageObjectExc("Kein StorageObject gesetzt!"); + throw new StorageObjectExc("theStorageObject == null!"); } /** * Saves changes of this Entity to the database * @exception StorageObjectException */ - public void update () throws StorageObjectFailure { - theStorageObject.update((Entity)this); + public void update() throws StorageObjectFailure { + theStorageObject.update(this); } /** @@ -210,123 +178,80 @@ public class Entity implements TemplateHashModel, TemplateModelRoot * @param theValue The new value of the field * @exception StorageObjectException */ - public void setValueForProperty (String theProp, String theValue) - throws StorageObjectFailure { - this.changed = true; - if (isField(theProp)) - theValuesHash.put(theProp, theValue); - else { - theLog.printWarning("Property not found: " + theProp+theValue); + 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); + } } /** * Returns the field names of the Entity as ArrayListe. * @return ArrayList with field names - * @exception StorageObjectException is throuwn if database access was impossible + * @exception StorageObjectException is throuwn if database access was impossible */ - public ArrayList getFields () throws StorageObjectFailure { - return theStorageObject.getFields(); - } + 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(); - } + public int[] getTypes() throws StorageObjectFailure { + return theStorageObject.getTypes(); + } /** * Returns an ArrayList with field names * @return List with field names * @exception StorageObjectException */ - public ArrayList getLabels () throws StorageObjectFailure { - return theStorageObject.getLabels(); - } + public List getLabels() throws StorageObjectFailure { + return theStorageObject.getLabels(); + } + /** - * Returns a Hashmap with all values of the Entity. - * @return HashMap with field name as key and the corresponding values + * 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 * - * @deprecated This method is deprecated and will be deleted in the next release. - * Entity interfaces freemarker.template.TemplateHashModel now and can - * be used in the same way as SimpleHash. - + * 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 HashMap getValues() { - theLog.printWarning("## using deprecated Entity.getValues() - a waste of resources"); - return theValuesHash; - } - /** - * 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 - */ - - public ArrayList streamedInput() { + public List streamedInput() { return streamedInput; } - /** Returns whether fieldName is a valid field name of this Entity. + /** 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); + 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)); - - protected void throwStorageObjectFailure (Exception e, String wo) - throws StorageObjectFailure { - theLog.printError( e.toString() + " Funktion: "+ wo); - e.printStackTrace(System.out); - 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. - theLog.printWarning("### put is called on entity! - the values will be lost!"); - } - - public void remove(java.lang.String key) - { - // do we need this? + throw new StorageObjectFailure("Storage Object Exception in entity", e); } - - - ////////////////////////////////////////////////////////////////////////////////// - - }