Class GenericEntity

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

public class GenericEntity extends Object implements Map<String,Object>, LocalizedMap<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.

This class is not thread-safe. If an instance of this class is shared between threads, then it should be made immutable by calling the setImmutable() method.

See Also:
  • Field Details

  • Constructor Details

    • GenericEntity

      protected GenericEntity()
      Creates new GenericEntity - Should never be used, prefer the other options.
  • Method Details

    • createGenericEntity

      public static GenericEntity createGenericEntity(ModelEntity modelEntity)
      Creates new GenericEntity
    • createGenericEntity

      public static GenericEntity createGenericEntity(Delegator delegator, ModelEntity modelEntity, Map<String,? extends Object> fields)
      Creates new GenericEntity from existing Map
    • createGenericEntity

      public static GenericEntity createGenericEntity(GenericEntity value)
      Copy Factory Method: Creates new GenericEntity from existing GenericEntity
    • assertIsMutable

      protected void assertIsMutable()
      Assert is mutable.
    • init

      protected void init(ModelEntity modelEntity)
      Creates new GenericEntity
    • init

      protected void init(Delegator delegator, ModelEntity modelEntity, Map<String,? extends Object> fields)
      Creates new GenericEntity from existing Map
    • init

      protected void init(Delegator delegator, ModelEntity modelEntity, Object singlePkValue)
      Creates new GenericEntity from existing Map
    • init

      protected void init(GenericEntity value)
      Copy Constructor: Creates new GenericEntity from existing GenericEntity
    • reset

      public void reset()
      Reset.
    • refreshFromValue

      public void refreshFromValue(GenericEntity newValue) throws GenericEntityException
      Refresh from value.
      Parameters:
      newValue - the new value
      Throws:
      GenericEntityException - the generic entity exception
    • isModified

      @Deprecated public boolean isModified()
      Deprecated.
      Use hasChanged()
    • synchronizedWithDatasource

      public void synchronizedWithDatasource()
      Flags this object as being synchronized with the data source. The entity engine will call this method immediately after populating this object with data from the data source.
    • removedFromDatasource

      public void removedFromDatasource()
      Flags this object as being removed from the data source. The entity engine will call this method immediately after removing this value from the data source. Once this method is called, the object is immutable.
    • isMutable

      public boolean isMutable()
      Is mutable boolean.
      Returns:
      the boolean
    • setImmutable

      public void setImmutable()
      Sets immutable.
    • getIsFromEntitySync

      public boolean getIsFromEntitySync()
      Gets is from entity sync.
      Returns:
      Returns the isFromEntitySync.
    • setIsFromEntitySync

      public void setIsFromEntitySync(boolean isFromEntitySync)
      Sets is from entity sync.
      Parameters:
      isFromEntitySync - The isFromEntitySync to set.
    • getEntityName

      public String getEntityName()
      Gets entity name.
      Returns:
      the entity name
    • getModelEntity

      public ModelEntity getModelEntity()
      Gets model entity.
      Returns:
      the model entity
    • getDelegator

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

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

      public Object get(String name)
      Get object.
      Parameters:
      name - the name
      Returns:
      the object
    • isPrimaryKey

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

      public boolean isPrimaryKey(boolean requireValue)
      Is primary key boolean.
      Parameters:
      requireValue - the require value
      Returns:
      the boolean
    • containsPrimaryKey

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

      public boolean containsPrimaryKey(boolean requireValue)
      Contains primary key boolean.
      Parameters:
      requireValue - the require value
      Returns:
      the boolean
    • getPkShortValueString

      public String getPkShortValueString()
      Gets pk short value string.
      Returns:
      the pk short value string
    • 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 name, 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:
      name - The field name to set
      value - The value to set
      setIfNull - Specifies whether or not to set the value if it is null
    • dangerousSetNoCheckButFast

      public void dangerousSetNoCheckButFast(ModelField modelField, Object value)
      Dangerous set no check but fast.
      Parameters:
      modelField - the model field
      value - the value
    • dangerousGetNoCheckButFast

      public Object dangerousGetNoCheckButFast(ModelField modelField)
      Dangerous get no check but fast object.
      Parameters:
      modelField - the model field
      Returns:
      the object
    • setString

      public void setString(String name, String value)
      Sets the named field to the passed value, converting the value from a String to the corrent type using Type.valueOf()
      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
    • setNextSeqId

      public void setNextSeqId()
      Sets next seq id.
    • getBoolean

      public Boolean getBoolean(String name)
      Gets boolean.
      Parameters:
      name - the name
      Returns:
      the boolean
    • getDuration

      public TimeDuration getDuration(String name)
      Returns the specified field as a TimeDuration instance. The field's Java data type can be either String or Number. Invalid Java data types will throw IllegalArgumentException.
      Parameters:
      name - The name of the desired field
      Returns:
      A TimeDuration instance or null
    • getString

      public String getString(String name)
      Gets string.
      Parameters:
      name - the name
      Returns:
      the string
    • getTimestamp

      public Timestamp getTimestamp(String name)
      Gets timestamp.
      Parameters:
      name - the name
      Returns:
      the timestamp
    • getTime

      public Time getTime(String name)
      Gets time.
      Parameters:
      name - the name
      Returns:
      the time
    • getDate

      public Date getDate(String name)
      Gets date.
      Parameters:
      name - the name
      Returns:
      the date
    • getInteger

      public Integer getInteger(String name)
      Gets integer.
      Parameters:
      name - the name
      Returns:
      the integer
    • getLong

      public Long getLong(String name)
      Gets long.
      Parameters:
      name - the name
      Returns:
      the long
    • getFloat

      public Float getFloat(String name)
      Gets float.
      Parameters:
      name - the name
      Returns:
      the float
    • getDouble

      public Double getDouble(String name)
      Gets double.
      Parameters:
      name - the name
      Returns:
      the double
    • getBigDecimal

      public BigDecimal getBigDecimal(String name)
      Gets big decimal.
      Parameters:
      name - the name
      Returns:
      the big decimal
    • getBytes

      public byte[] getBytes(String name)
      Get bytes byte [ ].
      Parameters:
      name - the name
      Returns:
      the byte [ ]
    • get

      public Object get(String name, Locale locale)
      Checks a resource bundle for a value for this field using the entity name, the field name and a composite of the Primary Key field values as a key. If no value is found in the resource then the field value is returned. Uses the default-resource-name from the entity definition as the resource name. To specify a resource name manually, use the other getResource method. So, the key in the resource bundle (properties file) should be as follows: <entity-name>.<field-name>.<pk-field-value-1>.<pk-field-value-2>...<pk-field-value-n> For example: ProductType.description.FINISHED_GOOD
      Specified by:
      get in interface LocalizedMap<Object>
      Parameters:
      name - The name of the field on the entity
      locale - The locale to use when finding the ResourceBundle, if null uses the default locale for the current instance of Java
      Returns:
      If the corresponding resource is found and contains a key as described above, then that property value is returned; otherwise returns the field value
    • get

      public Object get(String name, String resource, Locale locale)
      Same as the getResource method that does not take resource name, but instead allows manually specifying the resource name. In general you should use the other method for more consistent naming and use of the corresponding properties files.
      Parameters:
      name - The name of the field on the entity
      resource - The name of the resource to get the value from; if null defaults to the default-resource-name on the entity definition, if specified there
      locale - The locale to use when finding the ResourceBundle, if null uses the default locale for the current instance of Java
      Returns:
      If the specified resource is found and contains a key as described above, then that property value is returned; otherwise returns the field value
    • getPrimaryKey

      public GenericPK getPrimaryKey()
      Gets primary key.
      Returns:
      the primary key
    • setPKFields

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

      public void setPKFields(Map<? extends Object,? extends Object> fields, boolean setIfEmpty)
      go through the pks and for each one see if there is an entry in fields to set @param fields the fields
      Parameters:
      setIfEmpty - the set if empty
    • setNonPKFields

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

      public void setNonPKFields(Map<? extends Object,? extends Object> fields, boolean setIfEmpty)
      go through the non-pks and for each one see if there is an entry in fields to set @param fields the fields
      Parameters:
      setIfEmpty - the set if empty
    • setAllFields

      public void setAllFields(Map<? extends Object,? extends Object> fields, boolean setIfEmpty, String namePrefix, Boolean pks)
      Intelligently sets fields on this entity from the Map of fields passed in
      Parameters:
      fields - The fields Map to get the values from
      setIfEmpty - Used to specify whether empty/null values in the field Map should over-write non-empty values in this entity
      namePrefix - If not null or empty will be pre-pended to each field name (upper-casing the first letter of the field name first), and that will be used as the fields Map lookup name instead of the field-name
      pks - If null, get all values, if TRUE just get PKs, if FALSE just get non-PKs
    • getAllKeys

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

      public Map<String,Object> getAllFields()
      Returns key/value pairs of entity fields
      Returns:
      java.util.Map all fields
    • 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 fields
    • setFields

      public void setFields(Map<? extends String,? extends Object> keyValuePairs)
      Used by clients to update particular fields in the entity
      Parameters:
      keyValuePairs - java.util.Map
    • matchesFields

      public boolean matchesFields(Map<String,? extends Object> keyValuePairs)
      Matches fields boolean.
      Parameters:
      keyValuePairs - the key value pairs
      Returns:
      the boolean
    • 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)
    • addToXmlElement

      public static int addToXmlElement(Collection<GenericValue> values, Document document, Element element)
    • 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 This method is secure, it will not display encrypted fields
      Overrides:
      toString in class Object
      Returns:
      String corresponding to this entity
    • toStringInsecure

      public String toStringInsecure()
      Creates a String for the entity, overrides the default toString This method is NOT secure, it WILL display encrypted fields
      Returns:
      String corresponding to this entity
    • compareToFields

      protected int compareToFields(GenericEntity that, String name)
      Compare to fields int.
      Parameters:
      that - the that
      name - the name
      Returns:
      the int
    • compareTo

      public int compareTo(GenericEntity that)
      Compares this GenericEntity to the passed object
      Specified by:
      compareTo in interface Comparable<GenericEntity>
      Parameters:
      that - 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 and 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,? extends Object> 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>
    • matches

      public boolean matches(EntityCondition condition)
      Matches boolean.
      Parameters:
      condition - the condition
      Returns:
      the boolean
    • addObserver

      public void addObserver(Observer observer)
      Add observer.
      Parameters:
      observer - the observer
    • clearChanged

      public void clearChanged()
      Clear changed.
    • deleteObserver

      public void deleteObserver(Observer observer)
      Delete observer.
      Parameters:
      observer - the observer
    • deleteObservers

      public void deleteObservers()
      Delete observers.
    • hasChanged

      public boolean hasChanged()
      Has changed boolean.
      Returns:
      the boolean
    • notifyObservers

      public void notifyObservers()
      Notify observers.
    • notifyObservers

      public void notifyObservers(Object arg)
      Notify observers.
      Parameters:
      arg - the arg
    • setChanged

      public void setChanged()
      Sets changed.
    • originalDbValuesAvailable

      public boolean originalDbValuesAvailable()
      Original db values available boolean.
      Returns:
      the boolean
    • getOriginalDbValue

      public Object getOriginalDbValue(String name)
      Gets original db value.
      Parameters:
      name - the name
      Returns:
      the original db value
    • checkFks

      public boolean checkFks(boolean insertDummy) throws GenericEntityException
      Checks to see if all foreign key records exist in the database. Will create a dummy value for those missing when specified.
      Parameters:
      insertDummy - Create a dummy record using the provided fields
      Returns:
      true if all FKs exist (or when all missing are created)
      Throws:
      GenericEntityException