com.vaadin.addon.jpacontainer.provider
Class CachingMutableLocalEntityProvider<T>

java.lang.Object
  extended by com.vaadin.addon.jpacontainer.provider.LocalEntityProvider<T>
      extended by com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider<T>
          extended by com.vaadin.addon.jpacontainer.provider.CachingMutableLocalEntityProvider<T>
All Implemented Interfaces:
CachingEntityProvider<T>, EntityProvider<T>, EntityProviderChangeNotifier<T>, MutableEntityProvider<T>, Serializable
Direct Known Subclasses:
CachingBatchableLocalEntityProvider, CachingMutableEntityProvider

public class CachingMutableLocalEntityProvider<T>
extends MutableLocalEntityProvider<T>
implements CachingEntityProvider<T>

En extended version of MutableLocalEntityProvider that also implements the CachingEntityProvider interface.

This provider can be used in applications in the same manner as MutableLocalEntityProvider, with a few exceptions. By default, the cache is turned off which means that this provider effectively works as a MutableLocalEntityProvider. The cache can be turned on using setCacheEnabled(boolean).

If you are going to edit the entities returned by the container, you should check the setCloneCachedEntities(boolean) before continuing.

Since:
1.0
Author:
Petter Holmström (Vaadin Ltd)
See Also:
Serialized Form

Constructor Summary
CachingMutableLocalEntityProvider(Class<T> entityClass)
          Creates a new CachingLocalEntityProvider.
CachingMutableLocalEntityProvider(Class<T> entityClass, javax.persistence.EntityManager entityManager)
          Creates a new CachingMutableLocalEntityProvider.
 
Method Summary
 T addEntity(T entity)
          Adds entity to the persistence storage.
 boolean containsEntity(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter)
          Checks if the persistence storage contains an entity identified by entityId that is also matched by filter.
 void flush()
          Flushes the cache, forcing all entities to be loaded from the persistence storage upon next request.
 List<Object> getAllEntityIdentifiers(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
          Gets the identifiers of all items that match filter.
 T getEntity(EntityContainer<T> container, Object entityId)
          Loads the entity identified by entityId from the persistence storage.
 int getEntityCacheMaxSize()
          Gets the maximum number of entity instances to store in the cache.
 int getEntityCount(EntityContainer<T> container, com.vaadin.data.Container.Filter filter)
          Gets the number of entities that are matched by filter.
 Object getEntityIdentifierAt(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy, int index)
          Gets the identifier of the entity at position index in the result set determined from filter and sortBy.
 Object getFirstEntityIdentifier(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
          Gets the identifier of the first item in the list of entities determined by filter and sortBy.
 Object getLastEntityIdentifier(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
          Gets the identifier of the last item in the list of entities determined by filter and sortBy.
 Object getNextEntityIdentifier(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
          Gets the identifier of the item next to the item identified by entityId in the list of entities determined by filter and sortBy.
 Object getPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
          Gets the identifier of the item previous to the item identified by entityId in the list of entities determined by filter and sortBy.
 boolean isCacheEnabled()
          Returns whether the entity provider currently has the internal cache enabled.
 boolean isCloneCachedEntities()
          Returns whether entities found in the cache should be cloned before they are returned or not.
 boolean isEntitiesDetached()
          If this method returns true, all entities returned from this entity provider are explicitly detached from the persistence context before returned, regardless of whether the persistence context is extended or transaction-scoped.
 void refresh()
          Clears all caches and refreshes any loaded that cannot be discarded entities.
 T refreshEntity(T entity)
          Refreshes an entity from DB.
 void removeEntity(Object entityId)
          Removes the entity identified by entityId.
 void setCacheEnabled(boolean cacheInUse)
          Turns the cache on or off.
 void setCloneCachedEntities(boolean clone)
          Changes the value of the CachingEntityProvider.isCloneCachedEntities() flag.
 void setEntityCacheMaxSize(int maxSize)
          Sets the maximum number of entity instances to store in the cache.
 T updateEntity(T entity)
          Saves the changes made to entity to the persistence storage.
 void updateEntityProperty(Object entityId, String propertyName, Object propertyValue)
          Updates a single property value of a specific entity.
 boolean usesCache()
          Returns whether the entity provider is currently using the internal cache, which will be the case if both the caching is enabled ( CachingEntityProvider.setCacheEnabled(boolean) and there is no filter modifiying QueryModifierDelegate in use.
 
Methods inherited from class com.vaadin.addon.jpacontainer.provider.MutableLocalEntityProvider
addListener, fireEntityProviderChangeEvent, isFireEntityProviderChangeEvent, isTransactionsHandledByProvider, removeListener, runInTransaction, setFireEntityProviderChangeEvents, setTransactionsHandledByProvider
 
Methods inherited from class com.vaadin.addon.jpacontainer.provider.LocalEntityProvider
addPrimaryKeyToSortList, createFilteredQuery, createSiblingQuery, createUnsortedFilteredQuery, detachEntity, doContainsEntity, doGetAllEntityIdentifiers, doGetEntity, doGetEntityCount, doGetEntityIdentifierAt, doGetEntityManager, doGetFirstEntityIdentifier, doGetLastEntityIdentifier, doGetNextEntityIdentifier, doGetPreviousEntityIdentifier, getEntityClassMetadata, getEntityManager, getEntityManagerProvider, getIdentifier, getLazyLoadingDelegate, getQueryModifierDelegate, getSibling, readResolve, setEntitiesDetached, setEntityManager, setEntityManagerProvider, setLazyLoadingDelegate, setQueryModifierDelegate, translateSortBy, writeReplace
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface com.vaadin.addon.jpacontainer.CachingEntityProvider
setEntitiesDetached
 
Methods inherited from interface com.vaadin.addon.jpacontainer.EntityProvider
getEntityManager, getEntityManagerProvider, getIdentifier, getLazyLoadingDelegate, getQueryModifierDelegate, setEntityManager, setEntityManagerProvider, setLazyLoadingDelegate, setQueryModifierDelegate
 

Constructor Detail

CachingMutableLocalEntityProvider

public CachingMutableLocalEntityProvider(Class<T> entityClass,
                                         javax.persistence.EntityManager entityManager)
Creates a new CachingMutableLocalEntityProvider.

Parameters:
entityClass - the entity class (must not be null).
entityManager - the entity manager to use (must not be null).

CachingMutableLocalEntityProvider

public CachingMutableLocalEntityProvider(Class<T> entityClass)
Creates a new CachingLocalEntityProvider. The entity manager must be set using LocalEntityProvider.setEntityManager(javax.persistence.EntityManager).

Parameters:
entityClass - the entity class (must not be null).
Method Detail

flush

public void flush()
Description copied from interface: CachingEntityProvider
Flushes the cache, forcing all entities to be loaded from the persistence storage upon next request. This feature should be implemented by all caching entity providers.

Specified by:
flush in interface CachingEntityProvider<T>

getEntityCacheMaxSize

public int getEntityCacheMaxSize()
Description copied from interface: CachingEntityProvider
Gets the maximum number of entity instances to store in the cache. The default value is implementation specific.

Specified by:
getEntityCacheMaxSize in interface CachingEntityProvider<T>
Returns:
the max size, or -1 for unlimited size.

isCacheEnabled

public boolean isCacheEnabled()
Description copied from interface: CachingEntityProvider
Returns whether the entity provider currently has the internal cache enabled. By default, caching should be enabled.

NOTE! If a QueryModifierDelegate is in use and it modifies the filters through the QueryModifierDelegate.filtersWillBeAdded(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.CriteriaQuery, java.util.List) method, caching will NOT be enabled.

Specified by:
isCacheEnabled in interface CachingEntityProvider<T>
Returns:
true if the cache is in use, false otherwise.

setCacheEnabled

public void setCacheEnabled(boolean cacheInUse)
Description copied from interface: CachingEntityProvider
Turns the cache on or off. When the cache is turned off, it is automatically flushed.

NOTE! If a QueryModifierDelegate is in use and it modifies the filters through the QueryModifierDelegate.filtersWillBeAdded(javax.persistence.criteria.CriteriaBuilder, javax.persistence.criteria.CriteriaQuery, java.util.List) method, caching will NOT be enabled.

Specified by:
setCacheEnabled in interface CachingEntityProvider<T>
Parameters:
cacheInUse - true to turn the cache on, false to turn it off.

usesCache

public boolean usesCache()
Description copied from interface: CachingEntityProvider
Returns whether the entity provider is currently using the internal cache, which will be the case if both the caching is enabled ( CachingEntityProvider.setCacheEnabled(boolean) and there is no filter modifiying QueryModifierDelegate in use.

Specified by:
usesCache in interface CachingEntityProvider<T>
Returns:
true if the cache is actually in use, false otherwise.

setEntityCacheMaxSize

public void setEntityCacheMaxSize(int maxSize)
Description copied from interface: CachingEntityProvider
Sets the maximum number of entity instances to store in the cache. The implementation may decide what to do when the cache is full, but a full cache may never cause an exception. This feature is optional.

Specified by:
setEntityCacheMaxSize in interface CachingEntityProvider<T>
Parameters:
maxSize - the new maximum size, or -1 for unlimited size.

containsEntity

public boolean containsEntity(EntityContainer<T> container,
                              Object entityId,
                              com.vaadin.data.Container.Filter filter)
Description copied from interface: EntityProvider
Checks if the persistence storage contains an entity identified by entityId that is also matched by filter.

Specified by:
containsEntity in interface EntityProvider<T>
Overrides:
containsEntity in class LocalEntityProvider<T>
entityId - the entity identifier (must not be null).
filter - the filter that the entity should match (may be null).
Returns:
true if the entity exists, false if not.

getAllEntityIdentifiers

public List<Object> getAllEntityIdentifiers(EntityContainer<T> container,
                                            com.vaadin.data.Container.Filter filter,
                                            List<SortBy> sortBy)
Description copied from interface: EntityProvider
Gets the identifiers of all items that match filter. This method only exists to speed up Container.getItemIds(), which in turn is used by AbstractSelect and its subclasses (e.g. ComboBox). Using this method is not recommended, as it does not use lazy loading.

Specified by:
getAllEntityIdentifiers in interface EntityProvider<T>
Overrides:
getAllEntityIdentifiers in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
Returns:
an unmodifiable list of entity identifiers (never null).

getEntity

public T getEntity(EntityContainer<T> container,
                   Object entityId)
Description copied from interface: EntityProvider
Loads the entity identified by entityId from the persistence storage.

Specified by:
getEntity in interface EntityProvider<T>
Overrides:
getEntity in class LocalEntityProvider<T>
entityId - the entity identifier (must not be null).
Returns:
the entity, or null if not found.

isEntitiesDetached

public boolean isEntitiesDetached()
Description copied from interface: EntityProvider
If this method returns true, all entities returned from this entity provider are explicitly detached from the persistence context before returned, regardless of whether the persistence context is extended or transaction-scoped. Thus, no lazy-loaded associations will work and any changes made to the entities will not be reflected in the persistence context unless the entity is merged.

If this method returns false, the entities returned may be managed or detached, depending on the scope of the persistence context.

The default value is implementation specific.

Specified by:
isEntitiesDetached in interface CachingEntityProvider<T>
Specified by:
isEntitiesDetached in interface EntityProvider<T>
Overrides:
isEntitiesDetached in class LocalEntityProvider<T>
Returns:
true if the entities are explicitly detached, false otherwise.
See Also:
EntityProvider.setEntitiesDetached(boolean)

isCloneCachedEntities

public boolean isCloneCachedEntities()
Description copied from interface: CachingEntityProvider
Returns whether entities found in the cache should be cloned before they are returned or not. If this flag is false, two subsequent calls to #getEntity(java.lang.Object) with the same entity ID and without flushing the cache in between may return the same entity instance. This could be a problem if the instance is modified, as the cache would then contain the locally modified entity instance and not the one that was fetched from the persistence storage.

If the entity instances are serialized and deserialized before they reach the container, or the container is read-only, entities need not be cloned.

It is undefined what happens if this flag is true and the entities are not cloneable.

The default value of this flag is implementation dependent.

Specified by:
isCloneCachedEntities in interface CachingEntityProvider<T>
Returns:
true if cached entities should be cloned before they are returned, false to return them directly.
See Also:
CachingEntityProvider.setCloneCachedEntities(boolean)

setCloneCachedEntities

public void setCloneCachedEntities(boolean clone)
                            throws UnsupportedOperationException
Description copied from interface: CachingEntityProvider
Changes the value of the CachingEntityProvider.isCloneCachedEntities() flag.

Specified by:
setCloneCachedEntities in interface CachingEntityProvider<T>
Parameters:
clone - true to clone cached entities before returning them, false to return them directly.
Throws:
UnsupportedOperationException - if the implementation does not support changing the state of this flag.

getEntityCount

public int getEntityCount(EntityContainer<T> container,
                          com.vaadin.data.Container.Filter filter)
Description copied from interface: EntityProvider
Gets the number of entities that are matched by filter. If no filter has been specified, the total number of entities is returned.

Specified by:
getEntityCount in interface EntityProvider<T>
Overrides:
getEntityCount in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
Returns:
the number of matches.

getEntityIdentifierAt

public Object getEntityIdentifierAt(EntityContainer<T> container,
                                    com.vaadin.data.Container.Filter filter,
                                    List<SortBy> sortBy,
                                    int index)
Description copied from interface: EntityProvider
Gets the identifier of the entity at position index in the result set determined from filter and sortBy.

Specified by:
getEntityIdentifierAt in interface EntityProvider<T>
Overrides:
getEntityIdentifierAt in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
index - the index of the entity to fetch.
Returns:
the entity identifier, or null if not found.

getFirstEntityIdentifier

public Object getFirstEntityIdentifier(EntityContainer<T> container,
                                       com.vaadin.data.Container.Filter filter,
                                       List<SortBy> sortBy)
Description copied from interface: EntityProvider
Gets the identifier of the first item in the list of entities determined by filter and sortBy.

Specified by:
getFirstEntityIdentifier in interface EntityProvider<T>
Overrides:
getFirstEntityIdentifier in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
Returns:
the identifier of the first entity, or null if there are no entities matching filter.

getLastEntityIdentifier

public Object getLastEntityIdentifier(EntityContainer<T> container,
                                      com.vaadin.data.Container.Filter filter,
                                      List<SortBy> sortBy)
Description copied from interface: EntityProvider
Gets the identifier of the last item in the list of entities determined by filter and sortBy.

Specified by:
getLastEntityIdentifier in interface EntityProvider<T>
Overrides:
getLastEntityIdentifier in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
Returns:
the identifier of the last entity, or null if there are no entities matching filter.

getNextEntityIdentifier

public Object getNextEntityIdentifier(EntityContainer<T> container,
                                      Object entityId,
                                      com.vaadin.data.Container.Filter filter,
                                      List<SortBy> sortBy)
Description copied from interface: EntityProvider
Gets the identifier of the item next to the item identified by entityId in the list of entities determined by filter and sortBy.

Specified by:
getNextEntityIdentifier in interface EntityProvider<T>
Overrides:
getNextEntityIdentifier in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
Returns:
the identifier of the next entity, or null if there are no entities matching filter or entityId is the last item.

getPreviousEntityIdentifier

public Object getPreviousEntityIdentifier(EntityContainer<T> container,
                                          Object entityId,
                                          com.vaadin.data.Container.Filter filter,
                                          List<SortBy> sortBy)
Description copied from interface: EntityProvider
Gets the identifier of the item previous to the item identified by entityId in the list of entities determined by filter and sortBy.

Specified by:
getPreviousEntityIdentifier in interface EntityProvider<T>
Overrides:
getPreviousEntityIdentifier in class LocalEntityProvider<T>
filter - the filter that should be used to filter the entities (may be null).
sortBy - the properties to sort by, if any (may be null).
Returns:
the identifier of the previous entity, or null if there are no entities matching filter or entityId is the first item.

addEntity

public T addEntity(T entity)
Description copied from interface: MutableEntityProvider
Adds entity to the persistence storage. This method returns the same entity after adding to make it possible for the client to access the entity identifier. Note, however, that depending on the implementation of the entity provider and the state of EntityProvider.isEntitiesDetached(), this may or may not be the same instance as entity. Therefore, if EntityProvider.isEntitiesDetached() is true, clients should always assume that entity != returnedEntity.

Specified by:
addEntity in interface MutableEntityProvider<T>
Overrides:
addEntity in class MutableLocalEntityProvider<T>
Parameters:
entity - the entity to add (must not be null).
Returns:
the added entity.

removeEntity

public void removeEntity(Object entityId)
Description copied from interface: MutableEntityProvider
Removes the entity identified by entityId. If no entity is found, nothing happens.

Specified by:
removeEntity in interface MutableEntityProvider<T>
Overrides:
removeEntity in class MutableLocalEntityProvider<T>
Parameters:
entityId - the identifier of the entity to remove.

updateEntity

public T updateEntity(T entity)
Description copied from interface: MutableEntityProvider
Saves the changes made to entity to the persistence storage. This method returns the same entity after saving the changes. Note, however, that depending on the implementation of the entity provider and the state of EntityProvider.isEntitiesDetached(), this may or may not be the same instance as entity. Therefore, if EntityProvider.isEntitiesDetached() is true, clients should always assume that entity != returnedEntity.

Specified by:
updateEntity in interface MutableEntityProvider<T>
Overrides:
updateEntity in class MutableLocalEntityProvider<T>
Parameters:
entity - the entity to update (must not be null).
Returns:
the updated entity.

updateEntityProperty

public void updateEntityProperty(Object entityId,
                                 String propertyName,
                                 Object propertyValue)
                          throws IllegalArgumentException
Description copied from interface: MutableEntityProvider
Updates a single property value of a specific entity. If the entity is not found, nothing happens.

Specified by:
updateEntityProperty in interface MutableEntityProvider<T>
Overrides:
updateEntityProperty in class MutableLocalEntityProvider<T>
Parameters:
entityId - the identifier of the entity (must not be null).
propertyName - the name of the property to update (must not be null).
propertyValue - the new property value.
Throws:
IllegalArgumentException - if propertyName is not a valid property name.

refresh

public void refresh()
Description copied from interface: EntityProvider
Clears all caches and refreshes any loaded that cannot be discarded entities.

Specified by:
refresh in interface EntityProvider<T>
Overrides:
refresh in class LocalEntityProvider<T>

refreshEntity

public T refreshEntity(T entity)
Description copied from interface: EntityProvider
Refreshes an entity from DB. If entity no more exists, null is returned.

Specified by:
refreshEntity in interface EntityProvider<T>
Overrides:
refreshEntity in class LocalEntityProvider<T>
Returns:
the refreshed entity or null


Copyright © 2013 Vaadin Ltd. All rights reserved.