Class GenericEntity

java.lang.Object
java.util.Observable
org.ofbiz.core.entity.GenericEntity
All Implemented Interfaces:
Serializable, Cloneable, Comparable<GenericEntity>, Map<String,Object>
Direct Known Subclasses:
GenericPK, GenericValue

public class GenericEntity extends Observable implements Map<String,Object>, Serializable, Comparable<GenericEntity>, Cloneable
Generic Entity Value Object - Handles persistence for any defined entity.

Note that this class extends Observable to achieve change notification for Observers. Whenever a field changes the name of the field will be passed to the notifyObservers() method, and through that to the update() method of each Observer. Created Wed Aug 08 2001

Version:
1.0
Author:
David E. Jones, Andy Zeneski
See Also:
  • Field Details

    • delegatorName

      public String delegatorName
      Name of the GenericDelegator, used to re-get the GenericDelegator when deserialized
    • internalDelegator

      public transient GenericDelegator internalDelegator
      Reference to an instance of GenericDelegator used to do some basic operations on this entity value. If null various methods in this class will fail. This is automatically set by the GenericDelegator for all GenericValue objects instantiated through it. You may set this manually for objects you instantiate manually, but it is optional.
    • fields

      protected Map<String,Object> fields
      Contains the fields for this entity.
    • entityName

      public String entityName
      Contains the entityName of this entity, necessary for efficiency when creating EJBs
    • modelEntity

      public transient ModelEntity modelEntity
      Contains the ModelEntity instance that represents the definition of this entity, not to be serialized
    • modified

      public boolean modified
      Denotes whether or not this entity has been modified, or is known to be out of sync with the persistent record
  • Constructor Details

  • Method Details

    • isModified

      public boolean isModified()
    • getEntityName

      public String getEntityName()
    • getModelEntity

      public ModelEntity getModelEntity()
    • getDelegator

      public GenericDelegator getDelegator()
      Get the GenericDelegator instance that created this value object and that is responsible for it.
      Returns:
      GenericDelegator object
    • setDelegator

      public void setDelegator(GenericDelegator internalDelegator)
      Set the GenericDelegator instance that created this value object and that is responsible for it.
    • get

      public Object get(String name)
    • isPrimaryKey

      public boolean isPrimaryKey()
      Returns true if the entity contains all of the primary key fields, but NO others.
    • containsPrimaryKey

      public boolean containsPrimaryKey()
      Returns true if the entity contains all of the primary key fields.
    • set

      public void set(String name, Object value)
      Sets the named field to the passed value, even if the value is null
      Parameters:
      name - The field name to set
      value - The value to set
    • set

      public Object set(String field, Object value, boolean setIfNull)
      Sets the named field to the passed value. If value is null, it is only set if the setIfNull parameter is true. This is useful because an update will only set values that are included in the HashMap and will store null values in the HashMap to the datastore. If a value is not in the HashMap, it will be left unmodified in the datastore.
      Parameters:
      field - the name of the field to set; must be a valid field of this entity
      value - the value to set; if null, will only be stored if setIfNull is true
      setIfNull - specifies whether or not to set the value if it is null
      Returns:
      the previous value of the given field (whether updated or not)
    • dangerousSetNoCheckButFast

      public void dangerousSetNoCheckButFast(ModelField modelField, Object value)
    • dangerousGetNoCheckButFast

      public Object dangerousGetNoCheckButFast(ModelField modelField)
    • setString

      public void setString(String name, String value)
      Sets the named field to the passed value, converting the value from a String to the correct type using Type.valueOf() or similar.

      WARNING: calling this for an OBJECT field is ambiguous, because you could mean either that the String is the Base64-encoded representation of the object and should be be deserialized or that the String is the actual object to be stored. Since this method is intended for use in restoring the entity from an XML export, it assumes that the value is the Base64-encoding of an arbitrary object and attempts to deserialize it. If this is not what is intended, then it is up to the caller to use set(String, Object) for those fields, instead.

      Parameters:
      name - The field name to set
      value - The String value to convert and set
    • setBytes

      public void setBytes(String name, byte[] bytes)
      Sets a field with an array of bytes, wrapping them automatically for easy use.
      Parameters:
      name - The field name to set
      bytes - The byte array to be wrapped and set
    • getBoolean

      public Boolean getBoolean(String name)
    • getString

      public String getString(String name)
    • getTimestamp

      public Timestamp getTimestamp(String name)
    • getTime

      public Time getTime(String name)
    • getDate

      public Date getDate(String name)
    • getInteger

      public Integer getInteger(String name)
    • getLong

      public Long getLong(String name)
    • getFloat

      public Float getFloat(String name)
    • getDouble

      public Double getDouble(String name)
    • getBytes

      public byte[] getBytes(String name)
    • getPrimaryKey

      public GenericPK getPrimaryKey()
    • setPKFields

      public void setPKFields(Map<String,Object> fields)
      go through the pks and for each one see if there is an entry in fields to set
    • setPKFields

      public void setPKFields(Map<String,Object> fields, boolean setIfEmpty)
      go through the pks and for each one see if there is an entry in fields to set
    • setNonPKFields

      public void setNonPKFields(Map<String,Object> fields)
      go through the non-pks and for each one see if there is an entry in fields to set
    • setNonPKFields

      public void setNonPKFields(Map<String,Object> fields, boolean setIfEmpty)
      go through the non-pks and for each one see if there is an entry in fields to set
    • getAllKeys

      public Collection<String> getAllKeys()
      Returns keys of entity fields
      Returns:
      java.util.Collection
    • getAllFields

      public Map<String,Object> getAllFields()
      Returns key/value pairs of entity fields
      Returns:
      java.util.Map
    • getFields

      public Map<String,Object> getFields(Collection<String> keysofFields)
      Used by clients to specify exactly the fields they are interested in
      Parameters:
      keysofFields - the name of the fields the client is interested in
      Returns:
      java.util.Map
    • setFields

      public void setFields(Map<? extends String,?> newFieldValues)
      Updates the given fields of this entity with the values in the given map, including nulls.
      Parameters:
      newFieldValues - map of valid field names to new values; if null, this method does nothing
    • matchesFields

      public boolean matchesFields(Map<String,?> keyValuePairs)
    • lockEnabled

      public boolean lockEnabled()
      Used to indicate if locking is enabled for this entity
      Returns:
      True if locking is enabled
    • makeXmlDocument

      public static Document makeXmlDocument(Collection<GenericValue> values)
    • addToXmlDocument

      public static int addToXmlDocument(Collection<GenericValue> values, Document document)
    • makeXmlElement

      public Element makeXmlElement(Document document)
      Makes an XML Element object with an attribute for each field of the entity
      Parameters:
      document - The XML Document that the new Element will be part of
      Returns:
      org.w3c.dom.Element object representing this generic entity
    • makeXmlElement

      public Element makeXmlElement(Document document, String prefix)
      Makes an XML Element object with an attribute for each field of the entity
      Parameters:
      document - The XML Document that the new Element will be part of
      prefix - A prefix to put in front of the entity name in the tag name
      Returns:
      org.w3c.dom.Element object representing this generic entity
    • writeXmlText

      public void writeXmlText(PrintWriter writer, String prefix)
      Writes XML text with an attribute or CDATA element for each field of the entity
      Parameters:
      writer - A PrintWriter to write to
      prefix - A prefix to put in front of the entity name in the tag name
    • equals

      public boolean equals(Object obj)
      Determines the equality of two GenericEntity objects, overrides the default equals
      Specified by:
      equals in interface Map<String,Object>
      Overrides:
      equals in class Object
      Parameters:
      obj - The object (GenericEntity) to compare this two
      Returns:
      boolean stating if the two objects are equal
    • hashCode

      public int hashCode()
      Creates a hashCode for the entity, using the default String hashCode and Map hashCode, overrides the default hashCode
      Specified by:
      hashCode in interface Map<String,Object>
      Overrides:
      hashCode in class Object
      Returns:
      Hashcode corresponding to this entity
    • toString

      public String toString()
      Creates a String for the entity, overrides the default toString
      Overrides:
      toString in class Object
      Returns:
      String corresponding to this entity
    • compareTo

      public int compareTo(GenericEntity obj)
      Compares this GenericEntity to the passed object
      Specified by:
      compareTo in interface Comparable<GenericEntity>
      Parameters:
      obj - Object to compare this to
      Returns:
      int representing the result of the comparison (-1,0, or 1)
    • clone

      public Object clone()
      Clones this GenericEntity, this is a shallow clone & uses the default shallow HashMap clone
      Overrides:
      clone in class Object
      Returns:
      Object that is a clone of this GenericEntity
    • remove

      public Object remove(Object key)
      Specified by:
      remove in interface Map<String,Object>
    • containsKey

      public boolean containsKey(Object key)
      Specified by:
      containsKey in interface Map<String,Object>
    • entrySet

      public Set<Map.Entry<String,Object>> entrySet()
      Specified by:
      entrySet in interface Map<String,Object>
    • put

      public Object put(String key, Object value)
      Specified by:
      put in interface Map<String,Object>
    • putAll

      public void putAll(Map<? extends String,?> map)
      Specified by:
      putAll in interface Map<String,Object>
    • clear

      public void clear()
      Specified by:
      clear in interface Map<String,Object>
    • get

      public Object get(Object key)
      Specified by:
      get in interface Map<String,Object>
    • keySet

      public Set<String> keySet()
      Specified by:
      keySet in interface Map<String,Object>
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Map<String,Object>
    • values

      public Collection<Object> values()
      Specified by:
      values in interface Map<String,Object>
    • containsValue

      public boolean containsValue(Object value)
      Specified by:
      containsValue in interface Map<String,Object>
    • size

      public int size()
      Specified by:
      size in interface Map<String,Object>