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

java.lang.Object
  extended by com.vaadin.addon.jpacontainer.provider.LocalEntityProvider<T>
All Implemented Interfaces:
EntityProvider<T>, Serializable
Direct Known Subclasses:
CachingLocalEntityProvider, EntityProvider, MutableLocalEntityProvider

public class LocalEntityProvider<T>
extends Object
implements EntityProvider<T>, Serializable

A read-only entity provider that works with a local EntityManager. Most important features and limitations:

This entity provider does not perform very well, as every method call results in at least one query being sent to the entity manager. If speed is desired, CachingLocalEntityProvider should be used instead. However, this entity provider consumes less memory than the caching provider.

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

Constructor Summary
LocalEntityProvider(Class<T> entityClass)
          Creates a new LocalEntityProvider.
LocalEntityProvider(Class<T> entityClass, javax.persistence.EntityManager entityManager)
          Creates a new LocalEntityProvider.
LocalEntityProvider(Class<T> entityClass, EntityManagerProvider entityManagerProvider)
          Creates a new LocalEntityProvider with the specified EntityManagerProvider.
 
Method Summary
protected  List<SortBy> addPrimaryKeyToSortList(List<SortBy> original)
          Creates a copy of original and adds an entry for the primary key to the end of the list.
 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.
protected  javax.persistence.TypedQuery<Object> createFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy, boolean swapSortOrder)
          Creates a filtered, optionally sorted, query.
protected  javax.persistence.TypedQuery<Object> createSiblingQuery(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy, boolean backwards)
          This method creates a query that can be used to fetch the siblings of a specific entity.
protected  javax.persistence.TypedQuery<Object> createUnsortedFilteredQuery(EntityContainer<T> container, List<String> fieldsToSelect, com.vaadin.data.Container.Filter filter)
          Creates a filtered query that does not do any sorting.
protected  T detachEntity(T entity)
          Detaches entity from the entity manager.
protected  boolean doContainsEntity(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter)
           
protected  List<Object> doGetAllEntityIdentifiers(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
           
protected  T doGetEntity(Object entityId)
           
protected  int doGetEntityCount(EntityContainer<T> container, com.vaadin.data.Container.Filter filter)
           
protected  Object doGetEntityIdentifierAt(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy, int index)
           
protected  javax.persistence.EntityManager doGetEntityManager()
          Gets the entity manager.
protected  Object doGetFirstEntityIdentifier(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
           
protected  Object doGetLastEntityIdentifier(EntityContainer<T> container, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
           
protected  Object doGetNextEntityIdentifier(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
           
protected  Object doGetPreviousEntityIdentifier(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy)
           
 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.
protected  EntityClassMetadata<T> getEntityClassMetadata()
          Gets the metadata for the entity class.
 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.
 javax.persistence.EntityManager getEntityManager()
          Gets the entity manager.
 EntityManagerProvider getEntityManagerProvider()
          Gets the EntityManagerProvider that is used to find the current entity manager unless one is specified using setEntityManager(javax.persistence.EntityManager).
 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 getIdentifier(T entity)
          Returns identifier for given entity
 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.
 LazyLoadingDelegate getLazyLoadingDelegate()
           
 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.
 QueryModifierDelegate getQueryModifierDelegate()
           
protected  Object getSibling(EntityContainer<T> container, Object entityId, com.vaadin.data.Container.Filter filter, List<SortBy> sortBy, boolean backwards)
          If backwards is false, this method will return the identifier of the entity next to the entity identified by entityId.
 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.
protected  Object readResolve()
           
 void refresh()
          Clears all caches and refreshes any loaded that cannot be discarded entities.
 T refreshEntity(T entity)
          Refreshes an entity from DB.
 void setEntitiesDetached(boolean detached)
          Specifies whether the entities returned by the entity provider should be explicitly detached or not.
 void setEntityManager(javax.persistence.EntityManager entityManager)
          Sets the entity manager.
 void setEntityManagerProvider(EntityManagerProvider entityManagerProvider)
          Sets the EntityManagerProvider that is used to find the current entity manager unless set using setEntityManager(javax.persistence.EntityManager)
 void setLazyLoadingDelegate(LazyLoadingDelegate delegate)
          Set the delegate used for lazy loading.
 void setQueryModifierDelegate(QueryModifierDelegate delegate)
          Sets the QueryModifierDelegate, which is called in the different stages that the EntityProvider builds a criteria query.
protected  javax.persistence.criteria.Order translateSortBy(SortBy sortBy, boolean swapSortOrder, javax.persistence.criteria.CriteriaBuilder cb, javax.persistence.criteria.Root<T> root)
          Translates SortBy instances, which possibly contain nested properties (e.g.
protected  Object writeReplace()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LocalEntityProvider

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

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

LocalEntityProvider

public LocalEntityProvider(Class<T> entityClass)
Creates a new LocalEntityProvider. The entity manager or an entity manager provider must be set using setEntityManager(javax.persistence.EntityManager) or setEntityManagerProvider(com.vaadin.addon.jpacontainer.EntityManagerProvider) respectively.

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

LocalEntityProvider

public LocalEntityProvider(Class<T> entityClass,
                           EntityManagerProvider entityManagerProvider)
Creates a new LocalEntityProvider with the specified EntityManagerProvider.

Parameters:
entityClass -
entityManagerProvider -
Method Detail

writeReplace

protected Object writeReplace()
                       throws ObjectStreamException
Throws:
ObjectStreamException

readResolve

protected Object readResolve()
                      throws ObjectStreamException
Throws:
ObjectStreamException

setEntityManagerProvider

public void setEntityManagerProvider(EntityManagerProvider entityManagerProvider)
Sets the EntityManagerProvider that is used to find the current entity manager unless set using setEntityManager(javax.persistence.EntityManager)

Specified by:
setEntityManagerProvider in interface EntityProvider<T>
Parameters:
entityManagerProvider - The entity manager provider to set.

getEntityManagerProvider

public EntityManagerProvider getEntityManagerProvider()
Gets the EntityManagerProvider that is used to find the current entity manager unless one is specified using setEntityManager(javax.persistence.EntityManager).

Specified by:
getEntityManagerProvider in interface EntityProvider<T>
Returns:
the entity manager provider,

setEntityManager

public void setEntityManager(javax.persistence.EntityManager entityManager)
Sets the entity manager.

Specified by:
setEntityManager in interface EntityProvider<T>
Parameters:
entityManager - the entity manager to set.

getEntityClassMetadata

protected EntityClassMetadata<T> getEntityClassMetadata()
Gets the metadata for the entity class.

Returns:
the metadata (never null).

getEntityManager

public javax.persistence.EntityManager getEntityManager()
Gets the entity manager. If no entity manager has been set, the one returned by the registered entity manager provider is returned.

Specified by:
getEntityManager in interface EntityProvider<T>
Returns:
the entity manager.

doGetEntityManager

protected javax.persistence.EntityManager doGetEntityManager()
                                                      throws IllegalStateException
Gets the entity manager.

Returns:
the entity manager (never null).
Throws:
IllegalStateException - if no entity manager is set.

addPrimaryKeyToSortList

protected List<SortBy> addPrimaryKeyToSortList(List<SortBy> original)
Creates a copy of original and adds an entry for the primary key to the end of the list.

Parameters:
original - the original list of sorting instructions (must not be null, but may be empty).
Returns:
a new list with the added entry for the primary key.

translateSortBy

protected javax.persistence.criteria.Order translateSortBy(SortBy sortBy,
                                                           boolean swapSortOrder,
                                                           javax.persistence.criteria.CriteriaBuilder cb,
                                                           javax.persistence.criteria.Root<T> root)
Translates SortBy instances, which possibly contain nested properties (e.g. name.firstName, name.lastName) into Order instances which can be used in a CriteriaQuery.

Parameters:
sortBy - the SortBy instance to translate
swapSortOrder - swaps the specified sort order if true.
cb - the CriteriaBuilder to use
root - the CriteriaQuery Root to be used.
Returns:

createUnsortedFilteredQuery

protected javax.persistence.TypedQuery<Object> createUnsortedFilteredQuery(EntityContainer<T> container,
                                                                           List<String> fieldsToSelect,
                                                                           com.vaadin.data.Container.Filter filter)
Creates a filtered query that does not do any sorting.

Parameters:
fieldsToSelect - the fields to select (must not be null).
filter - the filter to apply, or null if no filters should be applied.
Returns:
the query (never null).
See Also:
createFilteredQuery(com.vaadin.addon.jpacontainer.EntityContainer, java.util.List, com.vaadin.data.Container.Filter, java.util.List, boolean)

createFilteredQuery

protected javax.persistence.TypedQuery<Object> createFilteredQuery(EntityContainer<T> container,
                                                                   List<String> fieldsToSelect,
                                                                   com.vaadin.data.Container.Filter filter,
                                                                   List<SortBy> sortBy,
                                                                   boolean swapSortOrder)
Creates a filtered, optionally sorted, query.

Parameters:
fieldsToSelect - the fields to select (must not be null).
filter - the filter to apply, or null if no filters should be applied.
sortBy - the fields to sort by (must include at least one field), or null if the result should not be sorted at all.
swapSortOrder - true to swap the sort order, false to use the sort order specified in sortBy. Only applies if sortBy is not null.
Returns:
the query (never null).

doContainsEntity

protected boolean doContainsEntity(EntityContainer<T> container,
                                   Object entityId,
                                   com.vaadin.data.Container.Filter filter)

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>
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.

doGetEntity

protected T doGetEntity(Object entityId)

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>
entityId - the entity identifier (must not be null).
Returns:
the entity, or null if not found.

doGetEntityIdentifierAt

protected Object doGetEntityIdentifierAt(EntityContainer<T> container,
                                         com.vaadin.data.Container.Filter filter,
                                         List<SortBy> sortBy,
                                         int index)

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>
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.

doGetEntityCount

protected int doGetEntityCount(EntityContainer<T> container,
                               com.vaadin.data.Container.Filter filter)

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>
filter - the filter that should be used to filter the entities (may be null).
Returns:
the number of matches.

doGetFirstEntityIdentifier

protected Object doGetFirstEntityIdentifier(EntityContainer<T> container,
                                            com.vaadin.data.Container.Filter filter,
                                            List<SortBy> sortBy)

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>
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.

doGetLastEntityIdentifier

protected Object doGetLastEntityIdentifier(EntityContainer<T> container,
                                           com.vaadin.data.Container.Filter filter,
                                           List<SortBy> sortBy)

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>
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.

getSibling

protected Object getSibling(EntityContainer<T> container,
                            Object entityId,
                            com.vaadin.data.Container.Filter filter,
                            List<SortBy> sortBy,
                            boolean backwards)
If backwards is false, this method will return the identifier of the entity next to the entity identified by entityId. If true, this method will return the identifier of the entity previous to the entity identified by entityId. filter and sortBy is used to define and limit the list of entities to be used for determining the sibling.

Parameters:
entityId - the identifier of the entity whose sibling to retrieve (must not be null).
filter - an optional filter to limit the entities (may be null).
sortBy - the order in which the list should be sorted (must not be null).
backwards - true to fetch the previous sibling, false to fetch the next sibling.
Returns:
the identifier of the "sibling".

createSiblingQuery

protected javax.persistence.TypedQuery<Object> createSiblingQuery(EntityContainer<T> container,
                                                                  Object entityId,
                                                                  com.vaadin.data.Container.Filter filter,
                                                                  List<SortBy> sortBy,
                                                                  boolean backwards)
This method creates a query that can be used to fetch the siblings of a specific entity. If backwards is false, the query will begin with the entity next to the entity identified by entityId. If backwards is false, the query will begin with the entity prior to the entity identified by entityId.

Parameters:
entityId - the identifier of the entity whose sibling to retrieve (must not be null).
filter - an optional filter to limit the entities (may be null).
sortBy - the order in which the list should be sorted (must not be null).
backwards - true to fetch the previous sibling, false to fetch the next sibling.
Returns:
the query that will return the sibling and all the subsequent entities unless limited.

doGetNextEntityIdentifier

protected Object doGetNextEntityIdentifier(EntityContainer<T> container,
                                           Object entityId,
                                           com.vaadin.data.Container.Filter filter,
                                           List<SortBy> sortBy)

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>
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.

doGetPreviousEntityIdentifier

protected Object doGetPreviousEntityIdentifier(EntityContainer<T> container,
                                               Object entityId,
                                               com.vaadin.data.Container.Filter filter,
                                               List<SortBy> sortBy)

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>
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.

detachEntity

protected T detachEntity(T entity)
Detaches entity from the entity manager. If entity is null, then null is returned. If isEntitiesDetached() is false, entity is returned directly.

Parameters:
entity - the entity to detach.
Returns:
the detached entity.

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 EntityProvider<T>
Returns:
true if the entities are explicitly detached, false otherwise.
See Also:
EntityProvider.setEntitiesDetached(boolean)

setEntitiesDetached

public void setEntitiesDetached(boolean detached)
                         throws UnsupportedOperationException
Description copied from interface: EntityProvider
Specifies whether the entities returned by the entity provider should be explicitly detached or not. See EntityProvider.isEntitiesDetached() for a more detailed description of the consequences.

Specified by:
setEntitiesDetached in interface EntityProvider<T>
Parameters:
detached - true to request explicitly detached entities, false otherwise.
Throws:
UnsupportedOperationException - if the implementation does not allow the user to change the way entities are returned.

doGetAllEntityIdentifiers

protected List<Object> doGetAllEntityIdentifiers(EntityContainer<T> container,
                                                 com.vaadin.data.Container.Filter filter,
                                                 List<SortBy> sortBy)

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>
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).

setQueryModifierDelegate

public void setQueryModifierDelegate(QueryModifierDelegate delegate)
Description copied from interface: EntityProvider
Sets the QueryModifierDelegate, which is called in the different stages that the EntityProvider builds a criteria query.

Specified by:
setQueryModifierDelegate in interface EntityProvider<T>
Parameters:
delegate - the delegate.

getQueryModifierDelegate

public QueryModifierDelegate getQueryModifierDelegate()
Specified by:
getQueryModifierDelegate in interface EntityProvider<T>
Returns:
the registered QueryModifierDelegate.

getIdentifier

public Object getIdentifier(T entity)
Description copied from interface: EntityProvider
Returns identifier for given entity

Specified by:
getIdentifier in interface EntityProvider<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>
Returns:
the refreshed entity or null

setLazyLoadingDelegate

public void setLazyLoadingDelegate(LazyLoadingDelegate delegate)
Description copied from interface: EntityProvider
Set the delegate used for lazy loading.

Specified by:
setLazyLoadingDelegate in interface EntityProvider<T>
Parameters:
delegate - the LazyLoadingDelegate to use.

getLazyLoadingDelegate

public LazyLoadingDelegate getLazyLoadingDelegate()
Specified by:
getLazyLoadingDelegate in interface EntityProvider<T>
Returns:
the LazyLoadingDelegate in use or null if none registered.

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>


Copyright © 2013 Vaadin Ltd. All rights reserved.