Interface Entity

All Superinterfaces:
EntityReference
All Known Subinterfaces:
EntityImplementor
All Known Implementing Classes:
AbstractDirtyCheckingEntity, AbstractEntityWrapper, AbstractHibernateEntity, AbstractPojoEntity, AbstractSessionOnlyEntity, ConditionalEntityWrapper, NullCounterEntityWrapper, NullKeyEntityWrapper, TemplateEntity, UnmodifiableEntityWrapper

public interface Entity extends EntityReference
  • Method Details

    • getKey

      @Nullable @Nullable PrimaryKey getKey()
      Get the primary key of this entity.
      Specified by:
      getKey in interface EntityReference
      Returns:
      The primary key of this entity, or null if in CONCEPTION state.
      See Also:
    • requireKey

      default PrimaryKey requireKey() throws IllegalStateException
      Get the primary key of this already persisted entity.

      Like getKey() but throws if the key is null (state CONCEPTION). Use this if you know the key has been set (ie the entity has been stored before).

      Returns:
      The primary key of this entity.
      Throws:
      IllegalStateException - if the pk is [still] null.
    • hasKey

      default boolean hasKey()
      Returns:
      true if the pk has been set. This is usually the case when the entity is not in CONCEPTION state anymore. false if the entity has a pk with value null.
    • setValue

      void setValue(String field, @Nullable @Nullable Object value)
      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. Throws ch.tocco.nice2.security.UnauthorizedException If the user does not have the permission to set/change the value.
    • getValue

      TypedValue<?> getValue(String field)
      Get the value and (nominal) type of a field. If the value of the specified field is null, typed value with the field type and an associated value of null is returned.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • requireValue

      default TypedValue<?> requireValue(String field)
      Get the value and (nominal) type of a field. If the value of the specified field is null, typed value with the field type and an associated value of null is returned.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
      Throws:
      IllegalStateException - if value is null
    • getValue

      @Nullable <T> T getValue(String field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • requireValue

      default <T> T requireValue(String field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
      Throws:
      IllegalStateException - if value is null
    • getValue

      @Nullable <T> T getValue(String field, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
    • requireValue

      default <T> T requireValue(String field, Type<T> type)
      Get the value of a field as the specified type, converting the value if neccessary.
      Parameters:
      field - The name of the Field
      type - The target type.
      Returns:
      The value.
      Throws:
      IllegalStateException - if value is null
    • getValue

      TypedValue<?> getValue(FieldModel field)
      Get the value and (nominal) type of a field. If the underlying value is null, it returns the type associated with the value of null.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
    • requireValue

      default TypedValue<?> requireValue(FieldModel field)
      Get the value and (nominal) type of a field. If the underlying value is null, it returns the type associated with the value of null.
      Parameters:
      field - The name of the field
      Returns:
      A TypedValue.
      Throws:
      IllegalStateException - if value is null
    • getValue

      @Nullable <T> T getValue(FieldModel field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
    • requireValue

      default <T> T requireValue(FieldModel field, Class<T> targetType)
      Get the value of a field, converting it if necessary.
      Type Parameters:
      T - The target Java type.
      Parameters:
      field - The name of a field.
      targetType - The target Java type.
      Returns:
      The value of the field.
      Throws:
      IllegalStateException - if value is null
    • getValue

      @Nullable <T> T getValue(FieldModel fieldModel, Type<T> type)
      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.
    • requireValue

      default <T> T requireValue(FieldModel fieldModel, Type<T> type)
      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.
      Throws:
      IllegalStateException - if value is null
    • delete

      void delete()
      Delete the entity.
    • getString

      @Nullable default @Nullable String getString(String field)
      Shortcut for getValue(field, String.class).
    • requireString

      default String requireString(String field)
      Shortcut for requireValue(field, String.class).
    • getDate

      @Nullable default @Nullable org.joda.time.LocalDate getDate(String field)
      Shortcut for getValue(field, LocalDate.class).
    • requireDate

      default org.joda.time.LocalDate requireDate(String field)
      Shortcut for requireValue(field, LocalDate.class).
    • getDateTime

      @Nullable default @Nullable org.joda.time.DateTime getDateTime(String field)
      Shortcut for getValue(field, DateTime.class).
    • requireDateTime

      default org.joda.time.DateTime requireDateTime(String field)
      Shortcut for requireValue(field, DateTime.class).
    • getTime

      @Nullable default @Nullable org.joda.time.LocalTime getTime(String field)
      Shortcut for getValue(field, LocalTime.class).
    • requireTime

      default org.joda.time.LocalTime requireTime(String field)
      Shortcut for requireValue(field, LocalTime.class).
    • getDecimal

      @Nullable default @Nullable BigDecimal getDecimal(String field)
      Shortcut for getValue(field, BigDecimal.class).
    • requireDecimal

      default BigDecimal requireDecimal(String field)
      Shortcut for requireValue(field, BigDecimal.class).
    • getInt

      @Nullable default @Nullable Integer getInt(String field)
      Shortcut for getValue(field, Integer.class).
    • requireInt

      default Integer requireInt(String field)
      Shortcut for requireValue(field, Integer.class).
    • getShort

      @Nullable default @Nullable Short getShort(String field)
      Shortcut for getValue(field, Short.class).
    • requireShort

      @Nullable default @Nullable Short requireShort(String field)
      Shortcut for requireValue(field, Short.class).
    • getLong

      @Nullable default @Nullable Long getLong(String field)
      Shortcut for getValue(field, Long.class).
    • requireLong

      default Long requireLong(String field)
      Shortcut for requireValue(field, Long.class).
    • getDouble

      @Nullable default @Nullable Double getDouble(String field)
      Shortcut for getValue(field, Double.class).
    • requireDouble

      default Double requireDouble(String field)
      Shortcut for requireValue(field, Double.class).
    • getFloat

      @Nullable default @Nullable Float getFloat(String field)
      Shortcut for getValue(field, Float.class).
    • requireFloat

      default Float requireFloat(String field)
      Shortcut for requireValue(field, Float.class).
    • getBool

      @Nullable default @Nullable Boolean getBool(String field)
      Shortcut for getValue(field, Boolean.class).
    • requireBool

      default Boolean requireBool(String field)
      Shortcut for requireValue(field, Boolean.class).
    • isFieldChanged

      boolean isFieldChanged(String field)
      Returns true if the current field value differs from initially loaded value in current transaction.
    • getChangedFields

      Set<String> getChangedFields()
      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:
    • isFieldTouched

      @Deprecated boolean isFieldTouched(String field) throws ModelException
      Deprecated.
      Throws:
      ModelException
    • getTouchedFields

      @Deprecated Set<String> getTouchedFields()
      Deprecated.
      use getChangedFields() (String)}
    • getTouchedRelations

      Set<String> getTouchedRelations()
      Returns:
      all touched relations, unmodifiableSet
    • getOldValue

      @Nullable <T> T getOldValue(String field, Class<T> targetType)
      Get the old value of a field.
      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).
    • resolve

      RelationQuery resolve(String relation)
      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.
    • getRelatedEntity

      Entity getRelatedEntity(String relName)
      Returns the single related entity for the given relation name, or throws.
      Parameters:
      relName - The relation name.
      Returns:
      The single related entity, loaded eagerly.
      See Also:
    • getRelatedEntityOrNull

      @Nullable @Nullable Entity getRelatedEntityOrNull(String relName)
      Returns the single related entity for the given relation name, null if none, or throws.

      Like getRelatedEntity(java.lang.String) but returns null if none.

      Parameters:
      relName - The relation name.
      Returns:
      The single related entity loaded eagerly, or null if none.
      See Also:
    • joinTx

      void joinTx()
      Joins the current transaction.
    • getValueMap

      Map<String,Object> getValueMap()
      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).
    • isUpdatable

      boolean isUpdatable()
      Determine whether the entity is updatable.
      Returns:
      true, if the entity is updatable, false, if it's read-only.
    • getModel

      EntityModel getModel()
      Get the entity model.
      Specified by:
      getModel in interface EntityReference
      Returns:
      The entity model.
    • getManager

      EntityManager getManager()
      Get the manager that manages this entity.
    • getState

      Entity.State getState()
      Get the current state of the entity.
      Returns:
      The current state of the entity.
    • getContext

      Context getContext()
    • setAutoFieldsDisabled

      void setAutoFieldsDisabled(boolean disabled)
      Disables fields listed EntityAutoField (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!
    • getAutoFieldsDisabled

      boolean getAutoFieldsDisabled()
      See Also:
    • validate

      CompleteEntityValidationResult validate(boolean asynchronous)
      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

      default CompleteEntityValidationResult validate()
      Is used for backwards compatibility. Calls validate(boolean)} with asynchronous = false.
    • getLogger

      org.slf4j.Logger getLogger()
      Provides access to the entity's logger. This may be useful to report errors on certain operation on an entity.