Class TemplateEntity
- All Implemented Interfaces:
EntityReference
,Entity
TemplateEntity
is a special Entity
in that it is a view
of any other normal Entity
. It always has an underlying "base entity"
which contributes its entity model with a few exceptions (i.e. without the
keys) and a subset of its relations to this entity model. The data of this
template entity is serialized into a textual representation and stored in a
single field of type `text`.
When loading, the representation is read and fields are populated according
to the current type of the field of the underlying base model. Relations are
loaded from the current database and linked back to this template entity.-
Nested Class Summary
Nested classes/interfaces inherited from interface ch.tocco.nice2.persist.core.api.entity.Entity
Entity.State
-
Constructor Summary
ConstructorDescriptionTemplateEntity
(Entity templateEntity, TemplateEntityManager entityManager, TypeManager typeManager, CommandExecutor executor, Set<TemplateEntityFieldHandler> fieldHandlers, TemplateBlacklist templateBlacklist) -
Method Summary
Modifier and TypeMethodDescriptionprotected EntityModel
void
delete()
Delete the entity.protected void
doJoinTx
(Transaction tx) Executed exactly once for any TemplateEntity instance, if one of the `joinTx` methods is invoked.protected TemplateEntityManager
protected Object
fromString
(String field, String value) boolean
Returns all field names for fields with values differing from initially loaded value in current transaction.protected FieldModel
getFieldModel
(String field) @Nullable PrimaryKey
getKey()
Get the primary key of this entity.org.slf4j.Logger
Provides access to the entity's logger.Get the manager that manages this entity.getModel()
Get the entity model.<T> T
getOldValue
(String field, Class<T> targetType) Get the old value of a field.getRelatedEntity
(String relName) Returns the single related entity for the given relation name, or throws.@Nullable Entity
getRelatedEntityOrNull
(String relName) Returns the single related entity for the given relation name, null if none, or throws.getState()
Get the current state of the entity.<T> T
getTemplateValue
(FieldModel fieldModel, Type<T> type) protected TypeManager
TypedValue
<?> getValue
(FieldModel model) Get the value and (nominal) type of a field.<T> T
getValue
(FieldModel fieldModel, Type<T> type) Get the value of a field as the specified type, converting the value if neccessary.<T> T
getValue
(FieldModel fieldModel, Class<T> targetClass) Get the value of a field, converting it if necessary.TypedValue
<?> Get the value and (nominal) type of a field.<T> T
Get the value of a field as the specified type, converting the value if neccessary.<T> T
Get the value of a field, converting it if necessary.Get all values of the entity as a map.boolean
isFieldChanged
(String field) Returns true if the current field value differs from initially loaded value in current transaction.boolean
isFieldTouched
(String field) static boolean
isTemplateField
(String name) boolean
Determine whether the entity is updatable.void
joinTx()
Joins the current transaction.protected Type
<?> requireType
(String field) Resolve a relation.void
setAutoFieldsDisabled
(boolean disabled) Disables fields listedEntityAutoField
(like version, update-user and -timestamps).void
setTemplateValue
(String field, Object value) void
Set the value of a field.static String
stripTemplatePrefix
(String name) protected String
validate
(boolean asynchronous) Validates this entity by running the validators of this entity's fields.Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface ch.tocco.nice2.persist.core.api.entity.Entity
getBool, getDate, getDateTime, getDecimal, getDouble, getFloat, getInt, getLong, getShort, getString, getTime, hasKey, requireBool, requireDate, requireDateTime, requireDecimal, requireDouble, requireFloat, requireInt, requireKey, requireLong, requireShort, requireString, requireTime, requireValue, requireValue, requireValue, requireValue, requireValue, requireValue, validate
-
Constructor Details
-
TemplateEntity
public TemplateEntity(Entity templateEntity, TemplateEntityManager entityManager, TypeManager typeManager, CommandExecutor executor, Set<TemplateEntityFieldHandler> fieldHandlers, TemplateBlacklist templateBlacklist)
-
-
Method Details
-
setValue
Description copied from interface:Entity
Set the value of a field.In the current implementation no content validation is made on this method call - other than null on a non-nullable field. Validation happens when storing, when all values have been set.
The value must either be in the correct data type, or convertible. To be convertible there must be a
ch.tocco.nice2.types.spi.Converter
contributed. Note that not all kinds of conversions that seem obvious at first are registered, for example there is no string to integer converter and thus a value like "" won't work and set 0 on an int field. Setting an int 0 on a long field works though because there is an int to long type converter.- Parameters:
field
- The name of the field.value
- The new value in the expected data type, or in a conversible type of it. Throwsch.tocco.nice2.security.UnauthorizedException
If the user does not have the permission to set/change the value.
-
setTemplateValue
-
getTemplateValue
-
getValue
Description copied from interface:Entity
Get the value of a field as the specified type, converting the value if neccessary.- Parameters:
fieldModel
- The field model.type
- The target type.- Returns:
- The value.
-
delete
public void delete()Description copied from interface:Entity
Delete the entity. -
doJoinTx
Executed exactly once for any TemplateEntity instance, if one of the `joinTx` methods is invoked. -
isFieldTouched
-
getChangedFields
Description copied from interface:Entity
Returns all field names for fields with values differing from initially loaded value in current transaction. Note: This method throws an UnauthorizedException when at least one changed field is not readable.- See Also:
-
isFieldChanged
Description copied from interface:Entity
Returns true if the current field value differs from initially loaded value in current transaction. -
getTouchedFields
-
getTouchedRelations
- Returns:
- all touched relations, unmodifiableSet
-
resolve
Description copied from interface:Entity
Resolve a relation. Throws if there is no such relation, check the model if you're unsure!The returned RelationQuery may be modified by interceptors already when you receive it. For example, in the entity system, there is (as of now) the NiceModelEntityInterceptorFactory which appends the default ordering rules of the EntityModel on query.
- Parameters:
relation
- The name of the relation- Returns:
- The executable
RelationQuery
.
-
getValueMap
Description copied from interface:Entity
Get all values of the entity as a map.Changes to that map will not be reflected in the entity.
The data includes pk fields as well as nice-specific fields (version etc).
- Returns:
- A map containing all values of the entity (shallow copy, modifiable).
-
getModel
Description copied from interface:Entity
Get the entity model.- Returns:
- The entity model.
-
getManager
Description copied from interface:Entity
Get the manager that manages this entity. -
baseModel
-
getTemplateEntity
-
entityManager
-
getTypeManager
-
requireType
-
fromString
- Throws:
StringConversionException
-
toString
-
isTemplateField
-
stripTemplatePrefix
-
getKey
Description copied from interface:Entity
Get the primary key of this entity.- Specified by:
getKey
in interfaceEntity
- Specified by:
getKey
in interfaceEntityReference
- Returns:
- The primary key of this entity, or null if in CONCEPTION state.
- See Also:
-
getValue
Description copied from interface:Entity
Get the value of a field as the specified type, converting the value if neccessary. -
getValue
Description copied from interface:Entity
Get the value and (nominal) type of a field. If the underlying value isnull
, it returns the type associated with the value ofnull
.- Specified by:
getValue
in interfaceEntity
- Parameters:
model
- The name of the field- Returns:
- A
TypedValue
.
-
getValue
Description copied from interface:Entity
Get the value and (nominal) type of a field. If the value of the specified field isnull
, typed value with the field type and an associated value ofnull
is returned.- Specified by:
getValue
in interfaceEntity
- Parameters:
field
- The name of the field- Returns:
- A
TypedValue
.
-
getValue
Description copied from interface:Entity
Get the value of a field, converting it if necessary. -
getValue
Description copied from interface:Entity
Get the value of a field, converting it if necessary. -
getOldValue
Description copied from interface:Entity
Get the old value of a field.- Specified by:
getOldValue
in interfaceEntity
- Parameters:
field
- The field to get the old value from.targetType
- The target Java type.- Returns:
- the old value of the field (cached value read from database).
-
getFieldModel
-
joinTx
public void joinTx()Description copied from interface:Entity
Joins the current transaction. -
isUpdatable
public boolean isUpdatable()Description copied from interface:Entity
Determine whether the entity is updatable.- Specified by:
isUpdatable
in interfaceEntity
- Returns:
true
, if the entity is updatable,false
, if it's read-only.
-
getState
Description copied from interface:Entity
Get the current state of the entity. -
getContext
- Specified by:
getContext
in interfaceEntity
-
validate
Description copied from interface:Entity
Validates this entity by running the validators of this entity's fields. Such validation includes business logic. Data type validation has been made way before, before setting the values on this entity object. Validators that throw a WARN level error may be ignored, which means they won't be run at all, and thus won't stop a persist operation. The DataOperator uses this functionality. The GUI can show a warning to the user, and he may change his input, or ignore the warning and submit again. If asynchronous is set to true, only validators that are deemed save for asynchronous validation (e.g. validation of an entity before everything is filled in / before listeners) are run. If it is set to false, all validators will be run. -
validate
-
getLogger
public org.slf4j.Logger getLogger()Description copied from interface:Entity
Provides access to the entity's logger. This may be useful to report errors on certain operation on an entity. -
getRelatedEntity
Description copied from interface:Entity
Returns the single related entity for the given relation name, or throws.- Specified by:
getRelatedEntity
in interfaceEntity
- Parameters:
relName
- The relation name.- Returns:
- The single related entity, loaded eagerly.
- See Also:
-
getRelatedEntityOrNull
Description copied from interface:Entity
Returns the single related entity for the given relation name, null if none, or throws.Like
Entity.getRelatedEntity(java.lang.String)
but returnsnull
if none.- Specified by:
getRelatedEntityOrNull
in interfaceEntity
- Parameters:
relName
- The relation name.- Returns:
- The single related entity loaded eagerly, or
null
if none. - See Also:
-
setAutoFieldsDisabled
public void setAutoFieldsDisabled(boolean disabled) Description copied from interface:Entity
Disables fields listedEntityAutoField
(like version, update-user and -timestamps). Useful for background-tasks which update things which can be re-written when overwritten by someone else. Important: Use this only when you exactly know why you are doing it!- Specified by:
setAutoFieldsDisabled
in interfaceEntity
-
getAutoFieldsDisabled
public boolean getAutoFieldsDisabled()- Specified by:
getAutoFieldsDisabled
in interfaceEntity
- See Also:
-