Class JPAContainer<T>

java.lang.Object
com.vaadin.featurepack.data.jpacontainer.JPAContainer<T>
All Implemented Interfaces:
Buffered, Container, Container.Filterable, Container.Hierarchical, Container.Indexed, Container.ItemSetChangeNotifier, Container.Ordered, Container.Sortable, AdvancedFilterable, EntityContainer<T>, EntityProviderChangeListener<T>, HierarchicalEntityContainer<T>, Serializable

public class JPAContainer<T> extends Object implements EntityContainer<T>, EntityProviderChangeListener<T>, HierarchicalEntityContainer<T>, Container.Indexed
This is the main container class of JPAContainer (and the default implementation of EntityContainer). You can use it in your applications like so:
 EntityContainer<MyEntity> container = new JPAContainer<MyEntity>(
   MyEntity.class);
 container.setEntityProvider(myEntityProvider);
 ...
 myTable.setContainerDataSource(container);
 
In the example code, myEntityProvider is an instance of an EntityProvider that, like the name suggests, is responsible for providing the entities to the container. If the container should be writable, the entity provider must implement the MutableEntityProvider interface and if buffering is desired (i.e. write-through turned off) the BatchableEntityProvider interface as well. There are ready-made implementations of all these interfaces that can be used out-of-the-box (check the See Also section of this Javadoc).

All sorting and filtering is handled by the entity provider, which in turn normally delegates it to the database. Therefore, only persistent properties can be filtered and/or sorted by.

It is possible to use JPAContainer as a hierarchical container if the entities in the container can be related to each other by means of a parent property. For example:

 
 @Entity
 public class Node {
   ...
   @ManyToOne
   private Node parent;
   ...
 }
 
 
Note, however, that the implementation of HierarchicalEntityContainer is still experimental and has some limitations. For example, the data is always read directly from the entity provider regardless of whether buffering is used or not. Therefore, this feature should be used with care in production systems.

Buffering

Here follows some notes on how buffering has been implemented in this class. If you are not going to use buffering, you can skip this section.

When using buffered mode, the following rules apply:

  • All operations that add, update or remove entities are recorded internally.
  • If an item is added and then removed later within the same transaction, the add operation will be removed and no update operation will be recorded.
  • If an item is added and then updated later within the same transaction, only the add operation will be recorded.
  • If an item is updated and then removed later within the same transaction, only the remove operation will be recorded.
  • In the case of an update or edit all changes are applied at once.
  • When the changes are committed, all recorded operations are carried out on the BatchableEntityProvider in the same order that they were recorded.

Please note, that if an entity is modified twice or more, updates are "merged". This is something that implementations of BatchableEntityProvider need to be aware of.

Also note, that it is possible to use buffered mode even if the entities returned from the entity provider are not explicitly detached (see EntityProvider.isEntitiesDetached()), but this should be avoided unless you know what you are doing.

Since:
1.0
Author:
Petter Holmström (Vaadin Ltd)
See Also:
  • Constructor Details

    • JPAContainer

      public JPAContainer(Class<T> entityClass)
      Creates a new JPAContainer instance for entities of class entityClass. An entity provider must be provided using the setEntityProvider(EntityProvider) before the container can be used.
      Parameters:
      entityClass - the class of the entities that will reside in this container (must not be null).
  • Method Details

    • setAdditionalFilterablePropertyIds

      public void setAdditionalFilterablePropertyIds(String... propertyIds)
      Sometimes, it may be necessary to filter by properties that do not show up in the container. This method can be used to add additional property IDs to the getFilterablePropertyIds() collection. This method performs no propertyId validation, so it is up to the client to make sure the propertyIds are valid.
      Parameters:
      propertyIds - an array of additional propertyIds, may be null.
    • updateFilterablePropertyIds

      protected void updateFilterablePropertyIds()
    • getEntityClassMetadata

      protected EntityClassMetadata<T> getEntityClassMetadata()
      Gets the mapping metadata of the entity class.
      Returns:
      the metadata (never null).
      See Also:
    • addListener

      public void addListener(Container.ItemSetChangeListener listener)
    • removeListener

      public void removeListener(Container.ItemSetChangeListener listener)
    • fireContainerItemSetChange

      protected void fireContainerItemSetChange(Container.ItemSetChangeEvent event)
      Publishes event to all registered ItemSetChangeListeners.
      Parameters:
      event - the event to publish (must not be null).
    • setFireContainerItemSetChangeEvents

      public void setFireContainerItemSetChangeEvents(boolean value)
      Specifies whether the container should fire an item set change event when it detects a change in the entity provider (such as an entity being added, updated or deleted).
    • isFireContainerItemSetChangeEvents

      public boolean isFireContainerItemSetChangeEvents()
      Tests whether the container should fire an item set change event when it detects a change in the entity provider (such as an entity being added, updated or deleted).
      Returns:
      true if item set change events should be fired (default), false otherwise.
    • addNestedContainerProperty

      public void addNestedContainerProperty(String nestedProperty) throws UnsupportedOperationException
      Description copied from interface: EntityContainer
      Adds the nested property nestedProperty to the set of properties. An asterisk can be used as a wildcard to indicate all leaf-properties.

      For example, let's say there is a property named address and that this property's type in turn has the properties street, postalCode and city.

      If we want to be able to access the street property directly, we can add the nested property address.street using this method.

      However, if we want to add all the address properties, we can also use address.*. This will cause the nested properties address.street, address.postalCode and address.city to be added to the set of properties.

      Note, that the wildcard cannot be used in the middle of a chain of property names. E.g. myprop.*.something is illegal.

      Nested properties may be transient, but only persistent nested properties (i.e. embedded properties or ManyToOne references) may be used for filtering or sorting.

      Specified by:
      addNestedContainerProperty in interface EntityContainer<T>
      Parameters:
      nestedProperty - the nested property to add (must not be null).
      Throws:
      UnsupportedOperationException - if nested properties are not supported by the container.
    • getEntityClass

      public Class<T> getEntityClass()
      Description copied from interface: EntityContainer
      Gets the class of the entities that are/can be contained in this container.
      Specified by:
      getEntityClass in interface EntityContainer<T>
      Returns:
      the entity class.
    • getEntityProvider

      public EntityProvider<T> getEntityProvider()
      Description copied from interface: EntityContainer
      Gets the entity provider that is used for fetching and storing entities.
      Specified by:
      getEntityProvider in interface EntityContainer<T>
      Returns:
      the entity provider, or null if this container has not yet been properly initialized.
    • doGetEntityProvider

      protected EntityProvider<T> doGetEntityProvider() throws IllegalStateException
      Checks that the entity provider is not null and returns it.
      Returns:
      the entity provider (never null).
      Throws:
      IllegalStateException - if the entity provider was null.
    • isReadOnly

      public boolean isReadOnly()
      Description copied from interface: EntityContainer
      Returns whether the container is read only or writable.
      Specified by:
      isReadOnly in interface EntityContainer<T>
      Returns:
      true if read only, false if writable.
    • setEntityProvider

      public void setEntityProvider(EntityProvider<T> entityProvider)
      Description copied from interface: EntityContainer
      Sets the entity provider to use for fetching and storing entities. The EntityProvider can be changed once set, if necessary.
      Specified by:
      setEntityProvider in interface EntityContainer<T>
      Parameters:
      entityProvider - the entity provider to use (must not be null).
    • setFireItemSetChangeOnProviderChange

      protected void setFireItemSetChangeOnProviderChange(boolean fireItemSetChangeOnProviderChange)
      Specifies whether the container should fire an ItemSetChangeEvent when an EntityProviderChangeEvent is received. This is used to prevent clients from receiving duplicate ItemSetChangeEvents when the container modifies data and wants to handle ItemSetChangeEvents itself.
      Parameters:
      fireItemSetChangeOnProviderChange - true fo fire an ItemSetChangeEvent when the provider changes, false not to.
    • isFireItemSetChangeOnProviderChange

      protected boolean isFireItemSetChangeOnProviderChange()
      Returns:
      true if an ItemSetChangeEvent should be fired when the provider changes, false if it should not.
      See Also:
    • entityProviderChange

      public void entityProviderChange(EntityProviderChangeEvent<T> event)
      Description copied from interface: EntityProviderChangeListener
      Notifies the client that event has occurred.
      Specified by:
      entityProviderChange in interface EntityProviderChangeListener<T>
      Parameters:
      event - the occurred event (never null).
    • setReadOnly

      public void setReadOnly(boolean readOnly) throws UnsupportedOperationException
      Description copied from interface: EntityContainer
      Changes the read only state of the container, if possible.
      Specified by:
      setReadOnly in interface EntityContainer<T>
      Parameters:
      readOnly - true to make the container read only, false to make it writable.
      Throws:
      UnsupportedOperationException - if the read only state cannot be changed.
    • setSortProperty

      public void setSortProperty(String propertyId, String sortProperty) throws IllegalArgumentException
      Configures a property to be sortable based on another property, normally a sub-property of the main property to sort.

      For example, let's say there is a property named address and that this property's type in turn has the property street. Addresses are not directly sortable as they are not simple properties.

      If we want to be able to sort addresses based on the street property, we can set the sort property for address to be address.street using this method.

      Normally the sort property should be of the form propertyId + "." + subPropertyId. Sort properties must be persistent and usable in JPQL, but need not be registered as separate properties in the container.

      Note that the sort property is not checked when this method is called. If it is not a valid sort property, an exception will be thrown when trying to sort a container.

      Parameters:
      propertyId - property for which sorting should be configured
      sortProperty - property or other JPQL string that should be used when sorting by propertyId is requested, typically a sub-property propertyId
      Throws:
      IllegalArgumentException - if the property propertyId is not in the container
      Since:
      1.2.1
    • getSortableContainerPropertyIds

      public Collection<String> getSortableContainerPropertyIds()
      Description copied from interface: Container.Sortable
      Gets the container property IDs which can be used to sort the items.
      Specified by:
      getSortableContainerPropertyIds in interface Container.Sortable
      Returns:
      the IDs of the properties that can be used for sorting the container
    • sort

      public void sort(Object[] propertyId, boolean[] ascending)
      Description copied from interface: Container.Sortable
      Sorts the container items.

      Sorting a container can irreversibly change the order of its items or only change the order temporarily, depending on the container.

      Specified by:
      sort in interface Container.Sortable
      Parameters:
      propertyId - Array of container property IDs, whose values are used to sort the items in container as primary, secondary, ... sorting criterion. All of the item IDs must be in the collection returned by Container.Sortable.getSortableContainerPropertyIds()
      ascending - Array of sorting order flags corresponding to each property ID used in sorting. If this array is shorter than propertyId array, ascending order is assumed for items where the order is not specified. Use true to sort in ascending order, false to use descending order.
    • getSortByList

      protected List<SortBy> getSortByList()
      Gets all the properties that the items should be sorted by, if any.
      Returns:
      an unmodifiable, possible empty list of SortBy instances (never null).
    • addItemAfter

      public Object addItemAfter(Object previousItemId) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Adds a new item after the given item.

      Adding an item after null item adds the item as first item of the ordered container.

      Specified by:
      addItemAfter in interface Container.Ordered
      Parameters:
      previousItemId - Id of the visible item in ordered container after which to insert the new item.
      Returns:
      item id the the created new item or null if the operation fails.
      Throws:
      UnsupportedOperationException - if the operation is not supported by the container
      See Also:
    • addItemAfter

      public Item addItemAfter(Object previousItemId, Object newItemId) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Adds a new item after the given item.

      Adding an item after null item adds the item as first item of the ordered container.

      Specified by:
      addItemAfter in interface Container.Ordered
      Parameters:
      previousItemId - Id of the visible item in ordered container after which to insert the new item.
      newItemId - Id of the new item to be added.
      Returns:
      new item or null if the operation fails.
      Throws:
      UnsupportedOperationException - if the operation is not supported by the container
      See Also:
    • firstItemId

      public Object firstItemId()
      Description copied from interface: Container.Ordered
      Gets the ID of the first Item in the Container.
      Specified by:
      firstItemId in interface Container.Ordered
      Returns:
      ID of the first visible Item in the Container
    • isFirstId

      public boolean isFirstId(Object itemId)
      Description copied from interface: Container.Ordered
      Tests if the Item corresponding to the given Item ID is the first Item in the Container.
      Specified by:
      isFirstId in interface Container.Ordered
      Parameters:
      itemId - ID of an Item in the Container
      Returns:
      true if the Item is first visible item in the Container, false if not
    • isLastId

      public boolean isLastId(Object itemId)
      Description copied from interface: Container.Ordered
      Tests if the Item corresponding to the given Item ID is the last Item in the Container.
      Specified by:
      isLastId in interface Container.Ordered
      Returns:
      true if the Item is last visible item in the Container, false if not
    • lastItemId

      public Object lastItemId()
      Description copied from interface: Container.Ordered
      Gets the ID of the last Item in the Container..
      Specified by:
      lastItemId in interface Container.Ordered
      Returns:
      ID of the last visible Item in the Container
    • nextItemId

      public Object nextItemId(Object itemId)
      Description copied from interface: Container.Ordered
      Gets the ID of the Item following the Item that corresponds to itemId. If the given Item is the last or not found in the Container, null is returned.
      Specified by:
      nextItemId in interface Container.Ordered
      Parameters:
      itemId - ID of a visible Item in the Container
      Returns:
      ID of the next visible Item or null
    • prevItemId

      public Object prevItemId(Object itemId)
      Description copied from interface: Container.Ordered
      Gets the ID of the Item preceding the Item that corresponds to itemId. If the given Item is the first or not found in the Container, null is returned.
      Specified by:
      prevItemId in interface Container.Ordered
      Parameters:
      itemId - ID of a visible Item in the Container
      Returns:
      ID of the previous visible Item or null
    • addContainerProperty

      public boolean addContainerProperty(Object propertyId, Class<?> type, Object defaultValue) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Adds a new Property to all Items in the Container. The Property ID, data type and default value of the new Property are given as parameters.

      This functionality is optional.

      Specified by:
      addContainerProperty in interface Container
      Parameters:
      propertyId - ID of the Property
      type - Data type of the new Property
      defaultValue - The value all created Properties are initialized to
      Returns:
      true if the operation succeeded, false if not
      Throws:
      UnsupportedOperationException - if the container does not support explicitly adding container properties
    • addItem

      public Item addItem(Object itemId) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Creates a new Item with the given ID in the Container.

      The new Item is returned, and it is ready to have its Properties modified. Returns null if the operation fails or the Container already contains a Item with the given ID.

      This functionality is optional.

      Specified by:
      addItem in interface Container
      Parameters:
      itemId - ID of the Item to be created
      Returns:
      Created new Item, or null in case of a failure
      Throws:
      UnsupportedOperationException - if adding an item with an explicit item ID is not supported by the container
    • addItem

      public Object addItem() throws UnsupportedOperationException
      This functionality is not fully supported by this implementation. The implementation tries to call empty parameter constructor and add entity as such to database. If identifiers are not autogenerated or empty parameter constructor does not exist, the operation will fail and throw UnSupportedOperationException.

      Creates a new Item into the Container, and assign it an automatic ID.

      The new ID is returned, or null if the operation fails. After a successful call you can use the getItemmethod to fetch the Item.

      This functionality is optional.

      Specified by:
      addItem in interface Container
      Returns:
      ID of the newly created Item, or null in case of a failure
      Throws:
      UnsupportedOperationException - if adding an item without an explicit item ID is not supported by the container
    • containsId

      public boolean containsId(Object itemId)
      Description copied from interface: Container
      Tests if the Container contains the specified Item.

      Filtering can hide items so that they will not be visible through the container API, and this method should respect visibility of items (i.e. only indicate visible items as being in the container) if feasible for the container.

      Specified by:
      containsId in interface Container
      Parameters:
      itemId - ID the of Item to be tested
      Returns:
      boolean indicating if the Container holds the specified Item
    • isContainsIdFiresItemSetChangeIfNotFound

      public boolean isContainsIdFiresItemSetChangeIfNotFound()
      Returns whether the containsId(java.lang.Object) method will fire an item set change event if it returns false. This may be necessary when using the container together with a
      invalid reference
      com.vaadin.ui.Table
      and there are multiple users modifying the same data source.

      When a user selects an item in a Table, the table checks with the container if the item exists or not. If it does not exist, nothing happens. Normally, the item should always exist, but if the container has been changed after the initial set of items were fetched and cached by the table, there may be items in the Table that are not present in the container.

      By enabling this flag, the Table will repaint itself if it tries to select a nonexistent item, causing the item to dissapear from the table as well.

    • setContainsIdFiresItemSetChangeIfNotFound

      public void setContainsIdFiresItemSetChangeIfNotFound(boolean value)
      Parameters:
      value -
    • doContainsId

      protected boolean doContainsId(Object itemId)
      See Also:
    • getContainerProperty

      public Property<?> getContainerProperty(Object itemId, Object propertyId)
      Description copied from interface: Container
      Gets the Property identified by the given itemId and propertyId from the Container. If the Container does not contain the item or it is filtered out, or the Container does not have the Property, null is returned.
      Specified by:
      getContainerProperty in interface Container
      Parameters:
      itemId - ID of the visible Item which contains the Property
      propertyId - ID of the Property to retrieve
      Returns:
      Property with the given ID or null
    • getContainerPropertyIds

      public Collection<String> getContainerPropertyIds()
      Description copied from interface: Container
      Gets the ID's of all Properties stored in the Container. The ID's cannot be modified through the returned collection.
      Specified by:
      getContainerPropertyIds in interface Container
      Returns:
      unmodifiable collection of Property IDs
    • getItem

      public EntityItem<T> getItem(Object itemId)
      Gets the Item with the given Item ID from the Container. If the Container does not contain the requested Item, null is returned.

      Containers should not return Items that are filtered out.

      Please note, that this method will create a new instance of EntityItem upon every execution. That is, two subsequent calls to this method with the same itemId will not return the same EntityItem instance. The actual entity instance may still be the same though, depending on the implementation of the entity provider.

      Specified by:
      getItem in interface Container
      Specified by:
      getItem in interface EntityContainer<T>
      Parameters:
      itemId - ID of the Item to retrieve
      Returns:
      the Item with the given ID or null if the Item is not found in the Container
    • getItemIds

      public Collection<Object> getItemIds()
      This impementation does not use lazy loading and performs bad when the number of items is large! Do not use unless you absolutely have to!

      Gets the ID's of all visible (after filtering and sorting) Items stored in the Container. The ID's cannot be modified through the returned collection.

      If the container is Container.Ordered, the collection returned by this method should follow that order. If the container is Container.Sortable, the items should be in the sorted order.

      Calling this method for large lazy containers can be an expensive operation and should be avoided when practical.

      Specified by:
      getItemIds in interface Container
      Returns:
      unmodifiable collection of Item IDs
    • createEntityItem

      public EntityItem<T> createEntityItem(T entity)
      Description copied from interface: EntityContainer
      Creates a new EntityItem for entity without adding it to the container. This makes it possible to use the same
      invalid reference
      com.vaadin.ui.Form
      for editing both new entities and existing entities.

      To add the entity to the container, EntityContainer.addEntity(java.lang.Object) should be used.

      Specified by:
      createEntityItem in interface EntityContainer<T>
      Parameters:
      entity - the entity for which an item should be created.
      Returns:
      the entity item (never null).
      See Also:
    • getType

      public Class<?> getType(Object propertyId)
      Description copied from interface: Container
      Gets the data type of all Properties identified by the given Property ID.
      Specified by:
      getType in interface Container
      Parameters:
      propertyId - ID identifying the Properties
      Returns:
      data type of the Properties
    • removeContainerProperty

      public boolean removeContainerProperty(Object propertyId) throws UnsupportedOperationException
      Description copied from interface: Container
      Removes a Property specified by the given Property ID from the Container. Note that the Property will be removed from all Items in the Container.

      This functionality is optional.

      Specified by:
      removeContainerProperty in interface Container
      Parameters:
      propertyId - ID of the Property to remove
      Returns:
      true if the operation succeeded, false if not
      Throws:
      UnsupportedOperationException - if the container does not support removing container properties
    • size

      public int size()
      Description copied from interface: Container
      Gets the number of visible Items in the Container.

      Filtering can hide items so that they will not be visible through the container API.

      Specified by:
      size in interface Container
      Returns:
      number of Items in the Container
    • getAppliedFiltersAsConjunction

      protected Container.Filter getAppliedFiltersAsConjunction()
      Returns a conjunction (filter1 AND filter2 AND ... AND filterN) of all the applied filters. If there are no applied filters, this method returns null.
      Returns:
      a conjunction filter or null.
      See Also:
    • getFilterablePropertyIds

      public Collection<Object> getFilterablePropertyIds()
      Description copied from interface: AdvancedFilterable
      Gets the IDs of all the properties that are filterable.
      Specified by:
      getFilterablePropertyIds in interface AdvancedFilterable
      Returns:
      an unmodifiable collection of property IDs (never null).
    • isFilterable

      public boolean isFilterable(Object propertyId)
      Description copied from interface: AdvancedFilterable
      Checks if propertyId is filterable.
      Specified by:
      isFilterable in interface AdvancedFilterable
      Parameters:
      propertyId - the property ID to check (must not be null).
      Returns:
      true if the property is filterable, false otherwise.
    • getFilters

      public List<Container.Filter> getFilters()
      Description copied from interface: AdvancedFilterable
      Gets the list of filters to apply. The filters will be applied as a conjunction (i.e. AND) in the order they appear in.
      Specified by:
      getFilters in interface AdvancedFilterable
      Returns:
      an unmodifiable list of filters (never null).
    • getAppliedFilters

      public List<Container.Filter> getAppliedFilters()
      Description copied from interface: AdvancedFilterable
      Gets the list of filters that are currently applied. If AdvancedFilterable.isApplyFiltersImmediately() returns true, this list will be the same as the one returned by AdvancedFilterable.getFilters().
      Specified by:
      getAppliedFilters in interface AdvancedFilterable
      Returns:
      an unmodifiable list of filters (never null).
    • setApplyFiltersImmediately

      public void setApplyFiltersImmediately(boolean applyFiltersImmediately)
      Description copied from interface: AdvancedFilterable
      Sets whether the filters should be applied immediately when a filter is added or removed.
      Specified by:
      setApplyFiltersImmediately in interface AdvancedFilterable
      Parameters:
      applyFiltersImmediately - true to apply filters immediately, false to apply when AdvancedFilterable.applyFilters() is called.
      See Also:
    • isApplyFiltersImmediately

      public boolean isApplyFiltersImmediately()
      Description copied from interface: AdvancedFilterable
      Returns whether the filters should be applied immediately when a filter is added or removed. Default is true. If false, AdvancedFilterable.applyFilters() has to be called to apply the filters and update the container.
      Specified by:
      isApplyFiltersImmediately in interface AdvancedFilterable
      Returns:
      true if the filters are applied immediately, false otherwise.
      See Also:
    • applyFilters

      public void applyFilters()
      Description copied from interface: AdvancedFilterable
      Applies the filters to the data, possibly causing the items in the container to change.
      Specified by:
      applyFilters in interface AdvancedFilterable
      See Also:
    • hasUnappliedFilters

      public boolean hasUnappliedFilters()
      Description copied from interface: AdvancedFilterable
      Checks if there are filters that have not yet been applied, or applied filters that have been removed using
      invalid reference
      #removeAllFilters()
      or
      invalid reference
      #removeFilter(com.vaadin.addon.jpacontainer.Filter)
      .

      If AdvancedFilterable.isApplyFiltersImmediately() is true, this method always returns false.

      Specified by:
      hasUnappliedFilters in interface AdvancedFilterable
      Returns:
      true if there are unapplied filters, false otherwise.
      See Also:
    • addContainerFilter

      public void addContainerFilter(Object propertyId, String filterString, boolean ignoreCase, boolean onlyMatchPrefix)
      Description copied from interface: EntityContainer

      This method creates a new

      invalid reference
      StringComparisonFilter
      for the specified parameters and applies the filter immediately, regardless of the state of AdvancedFilterable.isApplyFiltersImmediately().
      Specified by:
      addContainerFilter in interface EntityContainer<T>
      See Also:
    • removeAllContainerFilters

      public void removeAllContainerFilters()
      Description copied from interface: Container.Filterable
      Remove all active filters from the container.
      Specified by:
      removeAllContainerFilters in interface Container.Filterable
    • removeContainerFilters

      public void removeContainerFilters(Object propertyId)
      Description copied from interface: EntityContainer

      The container is updated immediately regardless of the state of AdvancedFilterable.isApplyFiltersImmediately().

      Specified by:
      removeContainerFilters in interface EntityContainer<T>
    • addContainerFilter

      public void addContainerFilter(Container.Filter filter) throws UnsupportedFilterException
      Adds a filter for the container.

      If a container has multiple filters, only items accepted by all filters are visible.

      Note that JPAContainer don't support custom Container.Filters as filtering is done on DB level. Only basic Filter implementations are supported. If more complex filtering is needed, developers should tend to QueryModifierDelegate that allows developers to use JPA Criteria API to modify queries.

      Specified by:
      addContainerFilter in interface Container.Filterable
      Throws:
      UnsupportedFilterException - if the filter is not supported by the container
      See Also:
      • invalid reference
        com.vaadin.data.Container.Filterable#addContainerFilter(com.vaadin.data.Container.Filter)
    • removeContainerFilter

      public void removeContainerFilter(Container.Filter filter)
      Description copied from interface: Container.Filterable
      Removes a filter from the container.

      This requires that the equals() method considers the filters as equivalent (same instance or properly implemented equals() method).

      Specified by:
      removeContainerFilter in interface Container.Filterable
    • addItemAt

      public Object addItemAt(int index) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Adds a new item at given index (in the filtered view).

      The indices of the item currently in the given position and all the following items are incremented.

      This method should apply filters to the added item after inserting it, possibly hiding it immediately. If the container is being sorted, the item may be added at the correct sorted position instead of the given position. See Container.Indexed, Container.Ordered, Container.Filterable and Container.Sortable for more information.

      Specified by:
      addItemAt in interface Container.Indexed
      Parameters:
      index - Index (in the filtered and sorted view) to add the new item.
      Returns:
      item id of the created item or null if the operation fails.
      Throws:
      UnsupportedOperationException - if the operation is not supported by the container
    • addItemAt

      public Item addItemAt(int index, Object newItemId) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Adds a new item at given index (in the filtered view).

      The indexes of the item currently in the given position and all the following items are incremented.

      This method should apply filters to the added item after inserting it, possibly hiding it immediately. If the container is being sorted, the item may be added at the correct sorted position instead of the given position. See Container.Indexed, Container.Filterable and Container.Sortable for more information.

      Specified by:
      addItemAt in interface Container.Indexed
      Parameters:
      index - Index (in the filtered and sorted view) at which to add the new item.
      newItemId - Id of the new item to be added.
      Returns:
      new Item or null if the operation fails.
      Throws:
      UnsupportedOperationException - if the operation is not supported by the container
    • getIdByIndex

      public Object getIdByIndex(int index)
      Description copied from interface: Container.Indexed
      Get the item id for the item at the position given by index.

      Specified by:
      getIdByIndex in interface Container.Indexed
      Parameters:
      index - the index of the requested item id
      Returns:
      the item id of the item at the given index
    • indexOfId

      public int indexOfId(Object itemId)
      This implementation does not use lazy loading and performs extremely bad when the number of items is large! Do not use unless you absolutely have to!

      Gets the index of the Item corresponding to the itemId. The following is true for the returned index: 0 invalid input: '<'= index invalid input: '<' size(), or index = -1 if there is no visible item with that id in the container.

      Specified by:
      indexOfId in interface Container.Indexed
      Parameters:
      itemId - ID of an Item in the Container
      Returns:
      index of the Item, or -1 if (the filtered and sorted view of) the Container does not include the Item
    • requireWritableContainer

      protected void requireWritableContainer() throws IllegalStateException, UnsupportedOperationException
      Checks that the container is writable, i.e. the entity provider implements the MutableEntityProvider interface and the container is not marked as read only.
      Throws:
      IllegalStateException - if the container is read only.
      UnsupportedOperationException - if the entity provider does not support editing.
    • addEntity

      public Object addEntity(T entity) throws UnsupportedOperationException, IllegalStateException
      Description copied from interface: EntityContainer
      Adds a new entity to the container. The corresponding EntityItem can then be accessed by calling EntityContainer.getItem(java.lang.Object) using the entity identifier returned by this method.

      If EntityContainer.isAutoCommit() is activated, the returned identifier is always the actual entity ID. Otherwise, the returned identifier may, depending on the ID generation strategy, be either the actual entity ID or a temporary ID that is changed to the real ID once the changes have been committed using Buffered.commit().

      Specified by:
      addEntity in interface EntityContainer<T>
      Parameters:
      entity - the entity to add (must not be null).
      Returns:
      the identifier of the entity (never null).
      Throws:
      UnsupportedOperationException - if the container does not support adding new entities at all.
      IllegalStateException - if the container supports adding entities, but is currently in read only mode.
    • removeAllItems

      public boolean removeAllItems()
      This feature is not well optimized. Using direct access to db is much faster.

      Removes all Items from the Container.

      Note that Property ID and type information is preserved. This functionality is optional.

      Specified by:
      removeAllItems in interface Container
      Returns:
      true if the operation succeeded, false if not
    • removeItem

      public boolean removeItem(Object itemId) throws UnsupportedOperationException
      Description copied from interface: Container
      Removes the Item identified by ItemId from the Container.

      Containers that support filtering should also allow removing an item that is currently filtered out.

      This functionality is optional.

      Specified by:
      removeItem in interface Container
      Specified by:
      removeItem in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item to remove
      Returns:
      true if the operation succeeded, false if not
      Throws:
      UnsupportedOperationException - if the container does not support removing individual items
    • commit

      Description copied from interface: Buffered
      Updates all changes since the previous commit to the data source. The value stored in the object will always be updated into the data source when commit is called.
      Specified by:
      commit in interface Buffered
      Throws:
      Buffered.SourceException - if the operation fails because of an exception is thrown by the data source. The cause is included in the exception.
      Validator.InvalidValueException - if the operation fails because validation is enabled and the values do not validate
    • discard

      public void discard() throws Buffered.SourceException
      Description copied from interface: Buffered
      Discards all changes since last commit. The object updates its value from the data source.
      Specified by:
      discard in interface Buffered
      Throws:
      Buffered.SourceException - if the operation fails because of an exception is thrown by the data source. The cause is included in the exception.
    • isModified

      public boolean isModified()
      Description copied from interface: Buffered
      Tests if the value stored in the object has been modified since it was last updated from the data source.
      Specified by:
      isModified in interface Buffered
      Returns:
      true if the value in the object has been modified since the last data source update, false if not.
    • isReadThrough

      public boolean isReadThrough()
    • isWriteThrough

      public boolean isWriteThrough()
    • setReadThrough

      public void setReadThrough(boolean readThrough) throws Buffered.SourceException
      This functionality is not supported by this implementation.

      Throws:
      Buffered.SourceException
    • setWriteThrough

      public void setWriteThrough(boolean writeThrough) throws Buffered.SourceException, Validator.InvalidValueException

      Note, that write-through mode can only be turned off if the entity provider implements the BatchableEntityProvider interface.

      Throws:
      Buffered.SourceException
      Validator.InvalidValueException
    • setAutoCommit

      public void setAutoCommit(boolean autoCommit) throws Buffered.SourceException, Validator.InvalidValueException
      Description copied from interface: EntityContainer
      Alias of
      invalid reference
      Buffered#setWriteThrough(boolean)
      .
      Specified by:
      setAutoCommit in interface EntityContainer<T>
      Throws:
      Buffered.SourceException
      Validator.InvalidValueException
    • isAutoCommit

      public boolean isAutoCommit()
      Description copied from interface: EntityContainer
      Alias of
      invalid reference
      Buffered#isWriteThrough()
      .
      Specified by:
      isAutoCommit in interface EntityContainer<T>
    • getParentProperty

      public String getParentProperty()
      Description copied from interface: HierarchicalEntityContainer
      Gets the name of the persistent property that contains the reference to the parent entity.
      Specified by:
      getParentProperty in interface HierarchicalEntityContainer<T>
      Returns:
      the name of the parent property, or null if not specified.
    • setParentProperty

      public void setParentProperty(String parentProperty)
      Description copied from interface: HierarchicalEntityContainer
      Sets the persistent property (may be nested) that contains the reference to the parent entity. The parent entity is expected to be of the same type as the child entity.
      Specified by:
      setParentProperty in interface HierarchicalEntityContainer<T>
      Parameters:
      parentProperty - the name of the parent property.
    • areChildrenAllowed

      public boolean areChildrenAllowed(Object itemId)
      Description copied from interface: Container.Hierarchical
      Tests if the Item with given ID can have children.
      Specified by:
      areChildrenAllowed in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item in the container whose child capability is to be tested
      Returns:
      true if the specified Item exists in the Container and it can have children, false if it's not found from the container or it can't have children.
    • getChildren

      public Collection<?> getChildren(Object itemId)
      Description copied from interface: Container.Hierarchical
      Gets the IDs of all Items that are children of the specified Item. The returned collection is unmodifiable.
      Specified by:
      getChildren in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item whose children the caller is interested in
      Returns:
      An unmodifiable collection containing the IDs of all other Items that are children in the container hierarchy; null if item does not have any children.
    • getParent

      public Object getParent(Object itemId)
      Description copied from interface: Container.Hierarchical
      Gets the ID of the parent Item of the specified Item.
      Specified by:
      getParent in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item whose parent the caller wishes to find out.
      Returns:
      the ID of the parent Item. Will be null if the specified Item is a root element.
    • hasChildren

      public boolean hasChildren(Object itemId)
      Description copied from interface: Container.Hierarchical

      Tests if the Item specified with itemId has child Items or if it is a leaf. The Container.Hierarchical.getChildren(Object itemId) method always returns null for leaf Items.

      Note that being a leaf does not imply whether or not an Item is allowed to have children.

      Specified by:
      hasChildren in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item to be tested
      Returns:
      true if the specified Item has children, false if not (is a leaf)
    • isRoot

      public boolean isRoot(Object itemId)
      Description copied from interface: Container.Hierarchical
      Tests if the Item specified with itemId is a root Item. The hierarchical container can have more than one root and must have at least one unless it is empty. The Container.Hierarchical.getParent(Object itemId) method always returns null for root Items.
      Specified by:
      isRoot in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item whose root status is to be tested
      Returns:
      true if the specified Item is a root, false if not
    • rootItemIds

      public Collection<?> rootItemIds()
      Description copied from interface: Container.Hierarchical
      Gets the IDs of all Items in the container that don't have a parent. Such items are called root Items. The returned collection is unmodifiable.
      Specified by:
      rootItemIds in interface Container.Hierarchical
      Returns:
      An unmodifiable collection containing IDs of all root elements of the container
    • setChildrenAllowed

      public boolean setChildrenAllowed(Object itemId, boolean areChildrenAllowed) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Sets the given Item's capability to have children. If the Item identified with itemId already has children and Container.Hierarchical.areChildrenAllowed(Object) is false this method fails and false is returned.

      The children must be first explicitly removed with Container.Hierarchical.setParent(Object itemId, Object newParentId)or Container.removeItem(Object itemId).

      This operation is optional. If it is not implemented, the method always returns false.

      Specified by:
      setChildrenAllowed in interface Container.Hierarchical
      Parameters:
      itemId - ID of the Item in the container whose child capability is to be set
      areChildrenAllowed - boolean value specifying if the Item can have children or not
      Returns:
      true if the operation succeeded, false if not
      Throws:
      UnsupportedOperationException
    • setParent

      public boolean setParent(Object itemId, Object newParentId) throws UnsupportedOperationException
      This functionality is not supported by this implementation.

      Sets the parent of an Item. The new parent item must exist and be able to have children. ( Container.Hierarchical.areChildrenAllowed(Object) == true ). It is also possible to detach a node from the hierarchy (and thus make it root) by setting the parent null.

      This operation is optional.

      Specified by:
      setParent in interface Container.Hierarchical
      Parameters:
      itemId - ID of the item to be set as the child of the Item identified with newParentId
      newParentId - ID of the Item that's to be the new parent of the Item identified with itemId
      Returns:
      true if the operation succeeded, false if not
      Throws:
      UnsupportedOperationException
    • getPropertyKind

      public PropertyKind getPropertyKind(Object propertyId)
      Specified by:
      getPropertyKind in interface EntityContainer<T>
      Parameters:
      propertyId -
      Returns:
      the type of property indentified by given propertyId
    • refreshItem

      public void refreshItem(Object itemId)
      Description copied from interface: EntityContainer
      Causes all EntityItems created from this container and identified by given entityId to be refreshed.
      Specified by:
      refreshItem in interface EntityContainer<T>
      Parameters:
      itemId -
      See Also:
    • refresh

      public void refresh()
      Description copied from interface: EntityContainer
      Refreshes the entire container from the database. All unsaved data will be lost.
      Specified by:
      refresh in interface EntityContainer<T>
    • getQueryModifierDelegate

      public QueryModifierDelegate getQueryModifierDelegate()
      Description copied from interface: EntityContainer
      Gets the QueryModifierDelegate set for this container. EntityProviders will the delegate returned by this method in their queries.
      Specified by:
      getQueryModifierDelegate in interface EntityContainer<T>
      Returns:
      QueryModifierDelegate to control queries built by this container
    • setQueryModifierDelegate

      public void setQueryModifierDelegate(QueryModifierDelegate queryModifierDelegate)
      Sets the QueryModifierDelegate, which is called in the different stages that the EntityProvider builds a criteria query.
      Parameters:
      queryModifierDelegate - the delegate.
    • setBuffered

      public void setBuffered(boolean buffered)
      Description copied from interface: Buffered
      Sets the buffered mode to the specified status.

      When in buffered mode, an internal buffer will be used to store changes until Buffered.commit() is called. Calling Buffered.discard() will revert the internal buffer to the value of the data source.

      When in non-buffered mode both read and write operations will be done directly on the data source. In this mode the Buffered.commit() and Buffered.discard() methods serve no purpose.

      Specified by:
      setBuffered in interface Buffered
      Parameters:
      buffered - true if buffered mode should be turned on, false otherwise
    • isBuffered

      public boolean isBuffered()
      Description copied from interface: Buffered
      Checks the buffered mode
      Specified by:
      isBuffered in interface Buffered
      Returns:
      true if buffered mode is on, false otherwise
    • addItemSetChangeListener

      public void addItemSetChangeListener(Container.ItemSetChangeListener listener)
      Description copied from interface: Container.ItemSetChangeNotifier
      Adds an Item set change listener for the object.
      Specified by:
      addItemSetChangeListener in interface Container.ItemSetChangeNotifier
      Parameters:
      listener - listener to be added
    • removeItemSetChangeListener

      public void removeItemSetChangeListener(Container.ItemSetChangeListener listener)
      Description copied from interface: Container.ItemSetChangeNotifier
      Removes the Item set change listener from the object.
      Specified by:
      removeItemSetChangeListener in interface Container.ItemSetChangeNotifier
      Parameters:
      listener - listener to be removed
    • getItemIds

      public List<?> getItemIds(int startIndex, int numberOfItems)
      Description copied from interface: Container.Indexed
      Get numberOfItems consecutive item ids from the container, starting with the item id at startIndex.

      Implementations should return at most numberOfItems item ids, but can contain less if the container has less items than required to fulfill the request. The returned list must hence contain all of the item ids from the range:

      startIndex to max(startIndex + (numberOfItems-1), container.size()-1).

      For quick migration to new API see: ContainerHelpers.getItemIdsUsingGetIdByIndex(int, int, Indexed)

      Specified by:
      getItemIds in interface Container.Indexed
      Parameters:
      startIndex - the index for the first item which id to include
      numberOfItems - the number of consecutive item ids to get from the given start index, must be >= 0
      Returns:
      List containing the requested item ids or empty list if numberOfItems == 0; not null
    • getContainerFilters

      public Collection<Container.Filter> getContainerFilters()
      Description copied from interface: Container.Filterable
      Returns the filters which have been applied to the container
      Specified by:
      getContainerFilters in interface Container.Filterable
      Returns:
      A collection of filters which have been applied to the container. An empty collection if no filters have been applied.