Package org.apache.ofbiz.entity
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
Observer
s. 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:
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
static class
static class
-
Field Summary
-
Constructor Summary
ModifierConstructorDescriptionprotected
Creates new GenericEntity - Should never be used, prefer the other options. -
Method Summary
Modifier and TypeMethodDescriptionvoid
addObserver
(Observer observer) Add observer.static int
addToXmlDocument
(Collection<GenericValue> values, Document document) static int
addToXmlElement
(Collection<GenericValue> values, Document document, Element element) protected void
Assert is mutable.boolean
checkFks
(boolean insertDummy) Checks to see if all foreign key records exist in the database.void
clear()
void
Clear changed.clone()
Clones this GenericEntity, this is a shallow clone and uses the default shallow HashMap cloneint
compareTo
(GenericEntity that) Compares this GenericEntity to the passed objectprotected int
compareToFields
(GenericEntity that, String name) Compare to fields int.boolean
containsKey
(Object key) boolean
Returns true if the entity contains all of the primary key fields.boolean
containsPrimaryKey
(boolean requireValue) Contains primary key boolean.boolean
containsValue
(Object value) static GenericEntity
createGenericEntity
(Delegator delegator, ModelEntity modelEntity, Map<String, ? extends Object> fields) Creates new GenericEntity from existing Mapstatic GenericEntity
createGenericEntity
(GenericEntity value) Copy Factory Method: Creates new GenericEntity from existing GenericEntitystatic GenericEntity
createGenericEntity
(ModelEntity modelEntity) Creates new GenericEntitydangerousGetNoCheckButFast
(ModelField modelField) Dangerous get no check but fast object.void
dangerousSetNoCheckButFast
(ModelField modelField, Object value) Dangerous set no check but fast.void
deleteObserver
(Observer observer) Delete observer.void
Delete observers.entrySet()
boolean
Determines the equality of two GenericEntity objects, overrides the default equalsGet object.Same as the getResource method that does not take resource name, but instead allows manually specifying the resource name.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.Returns key/value pairs of entity fieldsReturns keys of entity fieldsgetBigDecimal
(String name) Gets big decimal.getBoolean
(String name) Gets boolean.byte[]
Get bytes byte [ ].Gets date.Get the GenericDelegator instance that created this value object and that is responsible for it.Gets double.getDuration
(String name) Returns the specified field as aTimeDuration
instance.Gets entity name.getFields
(Collection<String> keysofFields) Used by clients to specify exactly the fields they are interested inGets float.getInteger
(String name) Gets integer.boolean
Gets is from entity sync.Gets long.Gets model entity.getOriginalDbValue
(String name) Gets original db value.Gets pk short value string.Gets primary key.Gets string.Gets time.getTimestamp
(String name) Gets timestamp.boolean
Has changed boolean.int
hashCode()
Creates a hashCode for the entity, using the default String hashCode and Map hashCode, overrides the default hashCodeprotected void
init
(Delegator delegator, ModelEntity modelEntity, Object singlePkValue) Creates new GenericEntity from existing Mapprotected void
Creates new GenericEntity from existing Mapprotected void
init
(GenericEntity value) Copy Constructor: Creates new GenericEntity from existing GenericEntityprotected void
init
(ModelEntity modelEntity) Creates new GenericEntityboolean
isEmpty()
boolean
Deprecated.Use hasChanged()boolean
Is mutable boolean.boolean
Returns true if the entity contains all of the primary key fields, but NO others.boolean
isPrimaryKey
(boolean requireValue) Is primary key boolean.keySet()
boolean
Used to indicate if locking is enabled for this entitystatic Document
makeXmlDocument
(Collection<GenericValue> values) makeXmlElement
(Document document) Makes an XML Element object with an attribute for each field of the entitymakeXmlElement
(Document document, String prefix) Makes an XML Element object with an attribute for each field of the entityboolean
matches
(EntityCondition condition) Matches boolean.boolean
matchesFields
(Map<String, ? extends Object> keyValuePairs) Matches fields boolean.void
Notify observers.void
notifyObservers
(Object arg) Notify observers.boolean
Original db values available boolean.void
void
refreshFromValue
(GenericEntity newValue) Refresh from value.void
Flags this object as being removed from the data source.void
reset()
Reset.void
Sets the named field to the passed value, even if the value is nullSets the named field to the passed value.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 invoid
Sets a field with an array of bytes, wrapping them automatically for easy use.void
Sets changed.void
setDelegator
(Delegator internalDelegator) Set the GenericDelegator instance that created this value object and that is responsible for it.void
Used by clients to update particular fields in the entityvoid
Sets immutable.void
setIsFromEntitySync
(boolean isFromEntitySync) Sets is from entity sync.void
Sets next seq id.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 fieldsvoid
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 fieldsvoid
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 fieldsvoid
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 fieldsvoid
Sets the named field to the passed value, converting the value from a String to the corrent type usingType.valueOf()
int
size()
void
Flags this object as being synchronized with the data source.toString()
Creates a String for the entity, overrides the default toString This method is secure, it will not display encrypted fieldsCreates a String for the entity, overrides the default toString This method is NOT secure, it WILL display encrypted fieldsvalues()
void
writeXmlText
(PrintWriter writer, String prefix) Writes XML text with an attribute or CDATA element for each field of the entityMethods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, putIfAbsent, remove, replace, replace, replaceAll
-
Field Details
-
NULL_ENTITY
-
NULL_FIELD
-
-
Constructor Details
-
GenericEntity
protected GenericEntity()Creates new GenericEntity - Should never be used, prefer the other options.
-
-
Method Details
-
createGenericEntity
Creates new GenericEntity -
createGenericEntity
public static GenericEntity createGenericEntity(Delegator delegator, ModelEntity modelEntity, Map<String, ? extends Object> fields) Creates new GenericEntity from existing Map -
createGenericEntity
Copy Factory Method: Creates new GenericEntity from existing GenericEntity -
assertIsMutable
protected void assertIsMutable()Assert is mutable. -
init
Creates new GenericEntity -
init
protected void init(Delegator delegator, ModelEntity modelEntity, Map<String, ? extends Object> fields) Creates new GenericEntity from existing Map -
init
Creates new GenericEntity from existing Map -
init
Copy Constructor: Creates new GenericEntity from existing GenericEntity -
reset
public void reset()Reset. -
refreshFromValue
Refresh from value.- Parameters:
newValue
- the new value- Throws:
GenericEntityException
- the generic entity exception
-
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
Gets entity name.- Returns:
- the entity name
-
getModelEntity
Gets model entity.- Returns:
- the model entity
-
getDelegator
Get the GenericDelegator instance that created this value object and that is responsible for it.- Returns:
- GenericDelegator object
-
setDelegator
Set the GenericDelegator instance that created this value object and that is responsible for it. @param internalDelegator the internal delegator -
get
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
Gets pk short value string.- Returns:
- the pk short value string
-
set
Sets the named field to the passed value, even if the value is null- Parameters:
name
- The field name to setvalue
- The value to set
-
set
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 setvalue
- The value to setsetIfNull
- Specifies whether or not to set the value if it is null
-
dangerousSetNoCheckButFast
Dangerous set no check but fast.- Parameters:
modelField
- the model fieldvalue
- the value
-
dangerousGetNoCheckButFast
Dangerous get no check but fast object.- Parameters:
modelField
- the model field- Returns:
- the object
-
setString
Sets the named field to the passed value, converting the value from a String to the corrent type usingType.valueOf()
- Parameters:
name
- The field name to setvalue
- The String value to convert and set
-
setBytes
Sets a field with an array of bytes, wrapping them automatically for easy use.- Parameters:
name
- The field name to setbytes
- The byte array to be wrapped and set
-
setNextSeqId
public void setNextSeqId()Sets next seq id. -
getBoolean
Gets boolean.- Parameters:
name
- the name- Returns:
- the boolean
-
getDuration
Returns the specified field as aTimeDuration
instance. The field's Java data type can be eitherString
orNumber
. Invalid Java data types will throwIllegalArgumentException
.- Parameters:
name
- The name of the desired field- Returns:
- A
TimeDuration
instance ornull
-
getString
Gets string.- Parameters:
name
- the name- Returns:
- the string
-
getTimestamp
Gets timestamp.- Parameters:
name
- the name- Returns:
- the timestamp
-
getTime
Gets time.- Parameters:
name
- the name- Returns:
- the time
-
getDate
Gets date.- Parameters:
name
- the name- Returns:
- the date
-
getInteger
Gets integer.- Parameters:
name
- the name- Returns:
- the integer
-
getLong
Gets long.- Parameters:
name
- the name- Returns:
- the long
-
getFloat
Gets float.- Parameters:
name
- the name- Returns:
- the float
-
getDouble
Gets double.- Parameters:
name
- the name- Returns:
- the double
-
getBigDecimal
Gets big decimal.- Parameters:
name
- the name- Returns:
- the big decimal
-
getBytes
Get bytes byte [ ].- Parameters:
name
- the name- Returns:
- the byte [ ]
-
get
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 interfaceLocalizedMap<Object>
- Parameters:
name
- The name of the field on the entitylocale
- 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
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 entityresource
- The name of the resource to get the value from; if null defaults to the default-resource-name on the entity definition, if specified therelocale
- 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
Gets primary key.- Returns:
- the primary key
-
setPKFields
go through the pks and for each one see if there is an entry in fields to set @param fields the fields -
setPKFields
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
go through the non-pks and for each one see if there is an entry in fields to set @param fields the fields -
setNonPKFields
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 fromsetIfEmpty
- Used to specify whether empty/null values in the field Map should over-write non-empty values in this entitynamePrefix
- 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-namepks
- If null, get all values, if TRUE just get PKs, if FALSE just get non-PKs
-
getAllKeys
Returns keys of entity fields- Returns:
- java.util.Collection all keys
-
getAllFields
Returns key/value pairs of entity fields- Returns:
- java.util.Map all fields
-
getFields
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
Used by clients to update particular fields in the entity- Parameters:
keyValuePairs
- java.util.Map
-
matchesFields
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
-
addToXmlDocument
-
addToXmlElement
public static int addToXmlElement(Collection<GenericValue> values, Document document, Element element) -
makeXmlElement
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
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 ofprefix
- 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
Writes XML text with an attribute or CDATA element for each field of the entity- Parameters:
writer
- A PrintWriter to write toprefix
- A prefix to put in front of the entity name in the tag name
-
equals
Determines the equality of two GenericEntity objects, overrides the default equals -
hashCode
public int hashCode()Creates a hashCode for the entity, using the default String hashCode and Map hashCode, overrides the default hashCode -
toString
Creates a String for the entity, overrides the default toString This method is secure, it will not display encrypted fields -
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
Compare to fields int.- Parameters:
that
- the thatname
- the name- Returns:
- the int
-
compareTo
Compares this GenericEntity to the passed object- Specified by:
compareTo
in interfaceComparable<GenericEntity>
- Parameters:
that
- Object to compare this to- Returns:
- int representing the result of the comparison (-1,0, or 1)
-
clone
Clones this GenericEntity, this is a shallow clone and uses the default shallow HashMap clone -
remove
-
containsKey
- Specified by:
containsKey
in interfaceMap<String,
Object>
-
entrySet
-
put
-
putAll
-
clear
public void clear() -
get
-
keySet
-
isEmpty
public boolean isEmpty() -
values
-
containsValue
- Specified by:
containsValue
in interfaceMap<String,
Object>
-
size
public int size() -
matches
Matches boolean.- Parameters:
condition
- the condition- Returns:
- the boolean
-
addObserver
Add observer.- Parameters:
observer
- the observer
-
clearChanged
public void clearChanged()Clear changed. -
deleteObserver
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
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
Gets original db value.- Parameters:
name
- the name- Returns:
- the original db value
-
checkFks
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
-