Interface EntityEditModel

All Known Implementing Classes:
AbstractEntityEditModel, SwingEntityEditModel

public interface EntityEditModel
Specifies a class for editing Entity instances.
  • Field Details

    • PERSIST_FOREIGN_KEYS

      static final PropertyValue<Boolean> PERSIST_FOREIGN_KEYS
      Specifies whether writable foreign key values should persist when the model is cleared or set to null
      Value type: Boolean
      Default value: true
    • WARN_ABOUT_UNSAVED_DATA

      static final PropertyValue<Boolean> WARN_ABOUT_UNSAVED_DATA
      Indicates whether the application should ask for confirmation when exiting if some data is unsaved
      and whether it should warn when unsaved data is about to be lost, i.e. due to selection changes. Value type: Boolean
      Default value: false
    • EDIT_EVENTS

      static final PropertyValue<Boolean> EDIT_EVENTS
      Specifies whether edit models post their insert, update and delete events to EntityEditEvents
      Value type: Boolean
      Default value: true
  • Method Details

    • entityType

      EntityType entityType()
      Returns:
      the type of the entity this edit model is based on
    • connectionProvider

      EntityConnectionProvider connectionProvider()
      Returns:
      the connection provider used by this edit model
    • setDefaults

      void setDefaults()
      Populates this edit model with default values.
      See Also:
    • set

      void set(Entity entity)
      Copies the values from the given Entity into the underlying Entity being edited by this edit model. If entity is null the effect is the same as calling setDefaults().
      Parameters:
      entity - the entity
      See Also:
    • refreshEntity

      void refreshEntity()
      Refreshes the active Entity from the database, discarding all changes. If the active Entity is new then calling this method has no effect.
    • entity

      Entity entity()
      Returns:
      an immutable version of the Entity instance being edited
      See Also:
    • isNull

      StateObserver isNull(Attribute<?> attribute)
      Parameters:
      attribute - the attribute
      Returns:
      a StateObserver indicating whether the value of the given attribute is null
    • isNotNull

      StateObserver isNotNull(Attribute<?> attribute)
      Parameters:
      attribute - the attribute
      Returns:
      a StateObserver indicating whether the value of the given attribute is not null
    • nullable

      boolean nullable(Attribute<?> attribute)
      Parameters:
      attribute - the attribute
      Returns:
      true if this value is allowed to be null in the underlying entity
    • put

      <T> T put(Attribute<T> attribute, T value)
      Sets the given value in the underlying Entity
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute to associate the given value with
      value - the value to associate with the given attribute
      Returns:
      the previous value, if any
    • remove

      <T> T remove(Attribute<T> attribute)
      Removes the given value from the underlying Entity
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      Returns:
      the value, if any
    • get

      <T> T get(Attribute<T> attribute)
      Returns the value associated with the given attribute
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      Returns:
      the value associated with the given attribute
    • optional

      <T> Optional<T> optional(Attribute<T> attribute)
      Returns the value associated with the given attribute
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      Returns:
      the value associated with the given attribute, an empty Optional in case it is null
    • referencedEntity

      Entity referencedEntity(ForeignKey foreignKey)
      Returns the value associated with the given foreign key.
      Parameters:
      foreignKey - the foreign key
      Returns:
      the foreign key value
    • value

      <T> Value<T> value(Attribute<T> attribute)
      Returns a Value based on attribute in this edit model, note that subsequent calls for the same attribute return the same value instance.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      Returns:
      a Value based on the given edit model value
    • entities

      Entities entities()
      Returns:
      the underlying domain entities
    • entityDefinition

      EntityDefinition entityDefinition()
      Returns:
      the definition of the underlying entity
    • overwriteWarning

      State overwriteWarning()
      Returns:
      a state controlling whether this edit model triggers a warning before overwriting unsaved data
      See Also:
    • readOnly

      State readOnly()
      Making this edit model read-only prevents any changes from being persisted to the database, trying to insert, update or delete will cause an exception being thrown, it does not prevent editing. Use insertEnabled(), updateEnabled() and deleteEnabled() to configure the enabled state of those specific actions.
      Returns:
      the State controlling whether this model is read only
    • insertEnabled

      State insertEnabled()
      Disabling insert causes an exception being thrown when inserting.
      Returns:
      the state controlling whether inserting is enabled via this edit model
    • updateEnabled

      State updateEnabled()
      Disabling update causes an exception being thrown when updating.
      Returns:
      the state controlling whether updating is enabled via this edit model
    • updateMultipleEnabled

      State updateMultipleEnabled()
      Disabling updating multiple entities causes an exception being thrown when trying to update multiple entities at a time.
      Returns:
      the state controlling whether updating multiple entities is enabled
    • deleteEnabled

      State deleteEnabled()
      Disabling delete causes an exception being thrown when deleting.
      Returns:
      the state controlling whether deleting is enabled via this edit model
    • createForeignKeySearchModel

      EntitySearchModel createForeignKeySearchModel(ForeignKey foreignKey)
      Creates a EntitySearchModel for looking up entities referenced by the given foreign key, using the search attributes defined for that entity type.
      Parameters:
      foreignKey - the foreign key for which to create a EntitySearchModel
      Returns:
      a EntitySearchModel for looking up entities of the type referenced by the given foreign key attribute,
      Throws:
      IllegalStateException - in case no searchable attributes can be found for the entity type referenced by the given foreign key
    • containsSearchModel

      boolean containsSearchModel(ForeignKey foreignKey)
      Returns true if this edit model contains a EntitySearchModel for the given foreign key
      Parameters:
      foreignKey - the foreign key
      Returns:
      true if a EntitySearchModel has been initialized for the given foreign key
    • foreignKeySearchModel

      EntitySearchModel foreignKeySearchModel(ForeignKey foreignKey)
      Parameters:
      foreignKey - the foreign key for which to retrieve the EntitySearchModel
      Returns:
      the EntitySearchModel associated with the foreignKey, if no search model has been initialized for the given foreign key, a new one is created, associated with the foreign key and returned.
    • setDefault

      <T> void setDefault(Attribute<T> attribute, Supplier<T> defaultValue)
      Sets the default value supplier for the given attribute. Used when the underlying value is not persistent. Use setDefaults() or set(Entity) with a null parameter to populate the model with the default values.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      defaultValue - the default value supplier
      See Also:
    • editEvents

      State editEvents()
      Returns:
      a state controlling whether this edit model posts insert, update and delete events on the EntityEditEvents event bus.
      See Also:
    • persist

      State persist(Attribute<?> attribute)
      Returns a State controlling whether the last used value for this attribute should persist when the model is cleared.
      Parameters:
      attribute - the attribute
      Returns:
      a State controlling whether the given attribute value should persist when the model is cleared
      See Also:
    • insert

      Performs an insert on the active entity, sets the primary key values of the active entity according to the primary key of the inserted entity
      Returns:
      the inserted entity
      Throws:
      DatabaseException - in case of a database exception
      ValidationException - in case validation fails
      IllegalStateException - in case inserting is not enabled
      See Also:
    • insert

      Performs an insert on the given entities, returns silently on receiving an empty collection.
      Parameters:
      entities - the entities to insert
      Returns:
      a list containing the inserted entities
      Throws:
      DatabaseException - in case of a database exception
      ValidationException - in case validation fails
      IllegalStateException - in case inserting is not enabled
      See Also:
    • update

      Performs an update on the active entity
      Returns:
      the updated entity
      Throws:
      DatabaseException - in case of a database exception
      RecordModifiedException - in case the entity has been modified since it was loaded
      ValidationException - in case validation fails
      IllegalStateException - in case updating is not enabled
      UpdateException - in case the active entity is not modified
      See Also:
    • update

      Updates the given entities. If the entities are unmodified or the collection is empty this method returns silently.
      Parameters:
      entities - the entities to update
      Returns:
      the updated entities
      Throws:
      DatabaseException - in case of a database exception
      RecordModifiedException - in case an entity has been modified since it was loaded
      ValidationException - in case validation fails
      IllegalStateException - in case updating is not enabled
      See Also:
    • delete

      void delete() throws DatabaseException
      Deletes the active entity
      Throws:
      DatabaseException - in case of a database exception
      IllegalStateException - in case deleting is not enabled
      See Also:
    • delete

      void delete(Collection<? extends Entity> entities) throws DatabaseException
      Deletes the given entities, returns silently on receiving an empty collection
      Parameters:
      entities - the entities to delete
      Throws:
      DatabaseException - in case of a database exception
      IllegalStateException - in case deleting is not enabled
      See Also:
    • refresh

      void refresh()
      Refreshes all data models used by this edit model, combo box models f.ex.
    • add

      void add(ForeignKey foreignKey, Collection<Entity> entities)
      Adds the given entities to all foreign key models based on that entity type
      Parameters:
      foreignKey - the foreign key
      entities - the values
    • remove

      void remove(ForeignKey foreignKey, Collection<Entity> entities)
      Removes the given entities from all foreign key models based on that entity type and clears any foreign key values referencing them.
      Parameters:
      foreignKey - the foreign key
      entities - the values
    • replace

      void replace(ForeignKey foreignKey, Collection<Entity> entities)
      For every field referencing the given foreign key values, replaces that foreign key instance with the corresponding entity from entities, useful when attribute values have been changed in the referenced entity that must be reflected in the edit model.
      Parameters:
      foreignKey - the foreign key
      entities - the foreign key entities
    • validator

      EntityValidator validator()
      Returns:
      the validator
    • validate

      void validate(Attribute<?> attribute) throws ValidationException
      Validates the value associated with the given attribute, using the underlying validator.
      Parameters:
      attribute - the attribute the value is associated with
      Throws:
      ValidationException - if the given value is not valid for the given attribute
      See Also:
    • validate

      void validate() throws ValidationException
      Validates the current state of the entity
      Throws:
      ValidationException - in case the entity is invalid
    • validate

      void validate(Collection<? extends Entity> entities) throws ValidationException
      Validates the given entities, using the underlying validator. For entities of a type other than this edit model is based on, their respective validators are used.
      Parameters:
      entities - the entities to validate
      Throws:
      ValidationException - on finding the first invalid entity
      See Also:
    • validate

      void validate(Entity entity) throws ValidationException
      Validates the given entity, using the underlying validator. For entities of a type other than this edit model is based on, their respective validators are used.
      Parameters:
      entity - the entity to validate
      Throws:
      ValidationException - in case the entity is invalid
      NullPointerException - in case the entity is null
      See Also:
    • valid

      StateObserver valid()
      Returns:
      a StateObserver indicating the valid status of the underlying Entity.
      See Also:
    • valid

      StateObserver valid(Attribute<?> attribute)
      Parameters:
      attribute - the attribute
      Returns:
      a StateObserver indicating the valid status of the given attribute.
      See Also:
    • modified

      StateObserver modified()
      Returns a StateObserver indicating when and if any values in the underlying Entity have been modified.
      Returns:
      a StateObserver indicating the modified state of this edit model
    • modified

      StateObserver modified(Attribute<?> attribute)
      Returns a StateObserver instance indicating whether the value of the given attribute has been modified.
      Parameters:
      attribute - the attribute
      Returns:
      a StateObserver indicating the modified state of the value of the given attribute
      Throws:
      IllegalArgumentException - in case attribute is not part of the underlying entity
      See Also:
    • exists

      StateObserver exists()
      Returns:
      a StateObserver indicating whether the active entity exists in the database
    • primaryKeyNull

      StateObserver primaryKeyNull()
      Returns:
      a StateObserver indicating whether the primary key of the active entity is null
    • refreshing

      StateObserver refreshing()
      Returns:
      a StateObserver active while data models (such as combo box models) are being refreshed
    • addEditListener

      <T> void addEditListener(Attribute<T> attribute, Consumer<T> listener)
      Adds a listener notified each time the value associated with the given attribute is edited via put(Attribute, Object) or remove(Attribute), note that this event is only fired if the value actually changes.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute for which to monitor value edits
      listener - a listener notified each time the value of the given attribute is edited via this model
    • removeEditListener

      <T> void removeEditListener(Attribute<T> attribute, Consumer<T> listener)
      Removes the given listener.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute
      listener - the listener to remove
    • addValueListener

      <T> void addValueListener(Attribute<T> attribute, Consumer<T> listener)
      Adds a listener notified each time the value associated with the given attribute changes, either via editing or when the active entity is set.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute for which to monitor value changes
      listener - a listener notified each time the value of the attribute changes
      See Also:
    • removeValueListener

      <T> void removeValueListener(Attribute<T> attribute, Consumer<T> listener)
      Removes the given listener.
      Type Parameters:
      T - the value type
      Parameters:
      attribute - the attribute for which to remove the listener
      listener - the listener to remove
    • addValueChangeListener

      void addValueChangeListener(Consumer<Attribute<?>> listener)
      Parameters:
      listener - a listener notified each time a value changes, providing the attribute
    • removeValueChangeListener

      void removeValueChangeListener(Consumer<Attribute<?>> listener)
      Parameters:
      listener - the listener to remove
    • addEntityListener

      void addEntityListener(Consumer<Entity> listener)
      Notified each time the entity is set via set(Entity) or setDefaults().
      Parameters:
      listener - a listener notified each time the entity is set, possibly to null
      See Also:
    • removeEntityListener

      void removeEntityListener(Consumer<Entity> listener)
      Removes the given listener.
      Parameters:
      listener - the listener to remove
    • addBeforeInsertListener

      void addBeforeInsertListener(Consumer<Collection<Entity>> listener)
      Parameters:
      listener - a listener to be notified before an insert is performed
    • removeBeforeInsertListener

      void removeBeforeInsertListener(Consumer<Collection<Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addAfterInsertListener

      void addAfterInsertListener(Consumer<Collection<Entity>> listener)
      Parameters:
      listener - a listener to be notified each time insert has been performed
    • removeAfterInsertListener

      void removeAfterInsertListener(Consumer<Collection<Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addBeforeUpdateListener

      void addBeforeUpdateListener(Consumer<Map<Entity.Key,Entity>> listener)
      Parameters:
      listener - a listener to be notified before an update is performed
    • removeBeforeUpdateListener

      void removeBeforeUpdateListener(Consumer<Map<Entity.Key,Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addAfterUpdateListener

      void addAfterUpdateListener(Consumer<Map<Entity.Key,Entity>> listener)
      Parameters:
      listener - a listener to be notified each time an update has been performed, with the updated entities, mapped to their respective original primary keys, that is, the primary keys before the update was performed
    • removeAfterUpdateListener

      void removeAfterUpdateListener(Consumer<Map<Entity.Key,Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addBeforeDeleteListener

      void addBeforeDeleteListener(Consumer<Collection<Entity>> listener)
      Parameters:
      listener - a listener to be notified before a delete is performed
    • removeBeforeDeleteListener

      void removeBeforeDeleteListener(Consumer<Collection<Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addAfterDeleteListener

      void addAfterDeleteListener(Consumer<Collection<Entity>> listener)
      Parameters:
      listener - a listener to be notified each time delete has been performed
    • removeAfterDeleteListener

      void removeAfterDeleteListener(Consumer<Collection<Entity>> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addRefreshListener

      void addRefreshListener(Runnable listener)
      Parameters:
      listener - a listener to be notified each time a refresh has been performed
      See Also:
    • removeRefreshListener

      void removeRefreshListener(Runnable listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addInsertUpdateOrDeleteListener

      void addInsertUpdateOrDeleteListener(Runnable listener)
      Parameters:
      listener - a listener notified each time one or more entities are updated, inserted or deleted via this model
    • removeInsertUpdateOrDeleteListener

      void removeInsertUpdateOrDeleteListener(Runnable listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove
    • addConfirmOverwriteListener

      void addConfirmOverwriteListener(Consumer<State> listener)
      Parameters:
      listener - a listener notified each time the active entity is about to be set
    • removeConfirmOverwriteListener

      void removeConfirmOverwriteListener(Consumer<State> listener)
      Removes the given listener.
      Parameters:
      listener - a listener to remove