public abstract class Property extends Composite
A property is responsible for mapping (converting) a value between a field (Field
and a database column.
It is also responsible for checking the consistency of this field.
Modifier and Type | Field and Description |
---|---|
protected AccessPath |
accessPath
Contains the access path used to obtain the target object containing the field
|
protected String |
alternativePropertyKey
Contains the alternative i18n key used to determine the label (official name) of the property.
|
protected String |
defaultValue
Contains a string representation of the default value for the column
|
protected EntityDescriptor |
descriptor
Contains the descriptor to which this property belongs
|
protected Field |
field
The field which generated the property (which stores the column value in the Java world)
|
protected String |
label
Contains a used defined name.
|
protected int |
length
Contains the length of this property
|
protected String |
name
Contains the effective property name.
|
protected Mapping |
nameAsMapping
Represents the name of the property a
Mapping |
protected boolean |
nullable
Determines the nullability of the property
|
protected String |
propertyKey
Contains the i18n key used to determine the label (official name) of the property.
|
protected String |
propertyName
Contains the effective property name.
|
Modifier | Constructor and Description |
---|---|
protected |
Property(EntityDescriptor descriptor,
AccessPath accessPath,
Field field)
Creates a new property for the given descriptor, access path and field.
|
Modifier and Type | Method and Description |
---|---|
protected void |
checkNullability(Object propertyValue)
Checks if the value is non-null or the property accepts null values.
|
protected void |
checkUniqueness(Object entity,
Object propertyValue)
Checks the uniqueness of the given value and entity if an
Unique annotation is present |
protected void |
determineDefaultValue()
Determines the default value of the column by checking for a
DefaultValue annotation on the field. |
protected void |
determineLengths()
Determines the column length by checking for a
Length annotation on the field. |
protected void |
determineNullability()
Determines the nullability of the column by checking for a
NullAllowed annotation on the field. |
boolean |
equals(Object obj) |
<A extends Annotation> |
getAnnotation(Class<A> type)
Returns the anotation of the given type.
|
String |
getDefaultValue()
Returns the default value to use.
|
protected String |
getDefinition()
Returns the class name and field name which "defined" this property.
|
EntityDescriptor |
getDescriptor()
Returns the entity descriptor to which this property belongs
|
protected Field |
getField()
Returns the field which will store the database value
|
String |
getLabel()
Returns the name of the property which is shown to the user.
|
int |
getLength()
Returns the length of the generated column.
|
String |
getName()
Returns the name of the property.
|
String |
getPropertyName()
Returns the effective property name.
|
Object |
getValue(Object entity)
Obtains the field value from the given entity.
|
Object |
getValueAsCopy(Object entity)
For modifyable datatypes like collections, this returns the value as copy so that further modifications
will not change the returned value.
|
Object |
getValueForDatasource(Class<? extends BaseMapper<?,?,?>> mapperType,
Object entity)
Obtains the database value from the given entity.
|
protected Object |
getValueFromField(Object target)
Obtains the value from the field in the given target object
|
int |
hashCode() |
protected sirius.kernel.health.HandledException |
illegalFieldValue(sirius.kernel.commons.Value value)
Creates an exception which represents an illegal value for this property
|
boolean |
isNullable()
Determines if this property accepts null values
|
protected void |
link()
Links this property.
|
protected void |
onAfterDelete(Object entity)
Invoked after an entity was deleted from the database
|
protected void |
onAfterSave(Object entity)
Invoked after an entity was written to the database
|
protected void |
onBeforeDelete(Object entity)
Invoked before an entity is deleted from the database
|
protected void |
onBeforeSave(Object entity)
Invoked before an entity is written to the database.
|
protected void |
onBeforeSaveChecks(Object entity)
Invoked before an entity is written to the database.
|
void |
parseValue(Object e,
sirius.kernel.commons.Value value)
Parses the given value and applies it to the given entity if possible.
|
protected void |
setValue(Object entity,
Object object)
Applies the given value to the given entity.
|
protected void |
setValueFromDatasource(Class<? extends BaseMapper<?,?,?>> mapperType,
Object entity,
sirius.kernel.commons.Value data)
Applies the given database value to the given entity.
|
protected void |
setValueToField(Object value,
Object target)
Applies the given value to the field in the given target object
|
String |
toString() |
protected Object |
transformFromDatasource(Class<? extends BaseMapper<?,?,?>> mapperType,
sirius.kernel.commons.Value object)
Converts the database value to the appropriate field value.
|
protected Object |
transformFromElastic(sirius.kernel.commons.Value object)
Loads a value from an Elasticsearch database.
|
protected Object |
transformFromJDBC(sirius.kernel.commons.Value object)
Loads a value from a JDBC datasource.
|
protected Object |
transformFromMongo(sirius.kernel.commons.Value object)
Loads a value from a MongoDB datasource.
|
protected Object |
transformToDatasource(Class<? extends BaseMapper<?,?,?>> mapperType,
Object object)
Converts the Java object which resides in the associated field to the database value which is to be
written into the database.
|
protected Object |
transformToElastic(Object object)
Generates a value for an Elasticsearch database.
|
protected Object |
transformToJDBC(Object object)
Generates a value for a JDBC datasource.
|
protected Object |
transformToMongo(Object object)
Generates a value for a MongoDB datasource.
|
abstract Object |
transformValue(sirius.kernel.commons.Value value)
Converts the given value, which most probably contains user input as string into the target type of this
property.
|
is, makeNewInstance, tryAs
protected String name
Mapping.SUBFIELD_SEPARATOR
which is a _.protected String propertyName
name
but can be re-written
to support legacy table and column names.protected String label
getLabel()
protected String propertyKey
getLabel()
protected String alternativePropertyKey
getLabel()
protected EntityDescriptor descriptor
protected AccessPath accessPath
protected Field field
protected String defaultValue
protected int length
protected boolean nullable
protected Property(@Nonnull EntityDescriptor descriptor, @Nonnull AccessPath accessPath, @Nonnull Field field)
descriptor
- the descriptor which owns the propertyaccessPath
- the access path required to obtain the target object which contains the fieldfield
- the field which stores the database valueprotected void determineDefaultValue()
DefaultValue
annotation on the field.protected void determineLengths()
Length
annotation on the field.protected void determineNullability()
NullAllowed
annotation on the field.
Note that subclasses might overwrite this value if they do not accept null values (like properties for primitive types).
public <A extends Annotation> Optional<A> getAnnotation(Class<A> type)
A
- the annotation to fetchtype
- the type of the annotation to fetchpublic String getName()
public String getPropertyName()
@Nonnull protected Field getField()
public String getLabel()
This can be used in error messages or for labelling in forms.
The label can be set in three ways:
protected String getDefinition()
This is mainly used to report errors for duplicate names etc.
protected void setValue(Object entity, Object object)
The internal access path will be used to find the target object which contains the field.
entity
- the entity to write toobject
- the value to write to the fieldprotected void setValueToField(Object value, Object target)
value
- the database value to storetarget
- the target object determined by the access pathprotected void setValueFromDatasource(Class<? extends BaseMapper<?,?,?>> mapperType, Object entity, sirius.kernel.commons.Value data)
The internal access path will be used to find the target object which contains the field.
If the underlying field of this property is primitive, but the given value is null or transformed to null, this will be ignored. An scenario like this might happen, if we join-fetch a value, which is not present.
mapperType
- the mapper which is currently active. This can be used to determine which kind of database is
active and therefore which data format will be available.entity
- the entity to write todata
- the database value to storeprotected Object transformFromDatasource(Class<? extends BaseMapper<?,?,?>> mapperType, sirius.kernel.commons.Value object)
mapperType
- the mapper which is currently active. This can be used to determine which kind of database is
active and therefore which data format will be available.object
- the database valueprotected Object transformFromJDBC(sirius.kernel.commons.Value object)
object
- the database valueprotected Object transformFromElastic(sirius.kernel.commons.Value object)
object
- the database valueprotected Object transformFromMongo(sirius.kernel.commons.Value object)
object
- the database valuepublic Object getValue(Object entity)
The internal access path will be used to find the target object which contains the field.
entity
- the entity to fetch the value frompublic Object getValueAsCopy(Object entity)
entity
- the entity to fetch the value fromprotected Object getValueFromField(Object target)
target
- the target object determined by the access pathpublic Object getValueForDatasource(Class<? extends BaseMapper<?,?,?>> mapperType, Object entity)
The internal access path will be used to find the target object which contains the field.
mapperType
- the mapper which is currently active. This can be used to determine which kind of database is
active and therefore which data format will be required.entity
- the entity to write toprotected Object transformToDatasource(Class<? extends BaseMapper<?,?,?>> mapperType, Object object)
mapperType
- the mapper which is currently active. This can be used to determine which kind of database is
active and therefore which data format will be required.object
- the current field valueprotected Object transformToJDBC(Object object)
object
- the database valueprotected Object transformToElastic(Object object)
object
- the database valueprotected Object transformToMongo(Object object)
object
- the database valuepublic void parseValue(Object e, sirius.kernel.commons.Value value)
e
- the entity to receive the parsed valuevalue
- the value to parse and applypublic abstract Object transformValue(sirius.kernel.commons.Value value)
value
- the value to convertprotected sirius.kernel.health.HandledException illegalFieldValue(sirius.kernel.commons.Value value)
value
- the illegal value itselfpublic EntityDescriptor getDescriptor()
protected final void onBeforeSave(Object entity)
Checks the nullability and uniqueness of the property.
entity
- the entity to checkprotected void onBeforeSaveChecks(Object entity)
This method is intended to be overwritten with custom logic.
entity
- the entity to checkprotected void checkNullability(Object propertyValue)
propertyValue
- the value to checkprotected void checkUniqueness(Object entity, Object propertyValue)
Unique
annotation is presententity
- the entity to checkpropertyValue
- the value to checkprotected void onAfterSave(Object entity)
entity
- the entity which was written to the databaseprotected void onBeforeDelete(Object entity)
entity
- the entity to be deletedprotected void onAfterDelete(Object entity)
entity
- the entity which was deletedprotected void link()
This is invoked once all EntityDescriptors are loaded and can be used to build references to other descriptors / properties.
public boolean isNullable()
public int getLength()
public String getDefaultValue()
Copyright © 2018. All rights reserved.