All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.vaadin.addon.jpacontainer.provider.CachingLocalEntityProvider Maven / Gradle / Ivy

The newest version!
/*
JPAContainer
Copyright (C) 2009-2011 Oy Vaadin Ltd

This program is available under GNU Affero General Public License (version
3 or later at your option).

See the file licensing.txt distributed with this software for more
information about licensing.

You should have received a copy of the GNU Affero General Public License
along with this program.  If not, see .
 */
package com.vaadin.addon.jpacontainer.provider;

import com.vaadin.addon.jpacontainer.CachingEntityProvider;
import com.vaadin.addon.jpacontainer.Filter;
import com.vaadin.addon.jpacontainer.SortBy;
import java.util.List;
import javax.persistence.EntityManager;

/**
 * En extended version of {@link LocalEntityProvider} that also implements the {@link CachingEntityProvider} interface.
 * 

* This provider can be used in applications in the same manner as {@link LocalEntityProvider}, with a few exceptions. By default the cache is on. * The cache can be turned off using {@link #setCacheInUse(boolean) }, in which case the provider effectively works as a {@link LocalEntityProvider}. *

* If you are going to edit the entities returned by the container, you should check the {@link #setCloneCachedEntities(boolean) } before * continuing. * * @author Petter Holmström (Vaadin Ltd) * @since 1.0 */ public class CachingLocalEntityProvider extends LocalEntityProvider implements CachingEntityProvider { // TODO Check how well caching works with concurrent users // Maybe some of the collections/maps should be replaced with // concurrent implementations? What about synchronization? private static final long serialVersionUID = 302600441430870363L; private CachingSupport cachingSupport = new CachingSupport(this); /** * Creates a new CachingLocalEntityProvider. The entity manager must * be set using {@link #setEntityManager(javax.persistence.EntityManager) }. * * @param entityClass * the entity class (must not be null). */ public CachingLocalEntityProvider(Class entityClass) { super(entityClass); } /** * Creates a new CachingLocalEntityProvider. * * @param entityClass * the entity class (must not be null). * @param entityManager * the entity manager to use (must not be null). */ public CachingLocalEntityProvider(Class entityClass, EntityManager entityManager) { super(entityClass, entityManager); } public void flush() { cachingSupport.flush(); } public int getEntityCacheMaxSize() { return cachingSupport.getMaxCacheSize(); } public boolean isCacheInUse() { return cachingSupport.isCacheInUse(); } public void setCacheInUse(boolean cacheInUse) { cachingSupport.setCacheInUse(cacheInUse); } public void setEntityCacheMaxSize(int maxSize) { cachingSupport.setMaxCacheSize(maxSize); } @Override public boolean containsEntity(Object entityId, Filter filter) { return cachingSupport.containsEntity(entityId, filter); } @Override public List getAllEntityIdentifiers(Filter filter, List sortBy) { return cachingSupport.getAllEntityIdentifiers(filter, sortBy); } @Override public synchronized T getEntity(Object entityId) { return cachingSupport.getEntity(entityId); } @Override public boolean isEntitiesDetached() { return isCacheInUse() || super.isEntitiesDetached(); } public boolean isCloneCachedEntities() { return cachingSupport.isCloneCachedEntities(); } public void setCloneCachedEntities(boolean clone) throws UnsupportedOperationException { cachingSupport.setCloneCachedEntities(clone); } @Override public int getEntityCount(Filter filter) { return cachingSupport.getEntityCount(filter); } @Override public Object getEntityIdentifierAt(Filter filter, List sortBy, int index) { return cachingSupport.getEntityIdentifierAt(filter, sortBy, index); } @Override public Object getFirstEntityIdentifier(Filter filter, List sortBy) { return cachingSupport.getFirstEntityIdentifier(filter, sortBy); } @Override public Object getLastEntityIdentifier(Filter filter, List sortBy) { return cachingSupport.getLastEntityIdentifier(filter, sortBy); } @Override public Object getNextEntityIdentifier(Object entityId, Filter filter, List sortBy) { return cachingSupport.getNextEntityIdentifier(entityId, filter, sortBy); } @Override public Object getPreviousEntityIdentifier(Object entityId, Filter filter, List sortBy) { return cachingSupport.getPreviousEntityIdentifier(entityId, filter, sortBy); } }