
com.cedarsoft.gdao.caching.CachingDao Maven / Gradle / Ivy
The newest version!
package com.cedarsoft.gdao.caching;
import com.cedarsoft.gdao.AbstractGenericDao;
import com.cedarsoft.gdao.Finder;
import com.cedarsoft.gdao.GenericDao;
import com.cedarsoft.history.ClusteredElementsCollection;
import com.cedarsoft.history.ClusteredObservableObjectAccess;
import com.cedarsoft.history.ElementVisistor;
import com.cedarsoft.history.ElementsListener;
import com.cedarsoft.history.NoElementFoundException;
import org.hibernate.Session;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.EmptyResultDataAccessException;
import javax.swing.SwingUtilities;
import java.util.List;
import java.util.concurrent.locks.ReadWriteLock;
/**
* A dao that caches the objects (in memory) and is backed up by a dao that is called asynchronously.
* This class is *not* thread safe
*/
public class CachingDao extends AbstractGenericDao implements ClusteredObservableObjectAccess {
@NotNull
protected final ClusteredElementsCollection cache = new ClusteredElementsCollection();
@NotNull
private final GenericDao backingDao;
public CachingDao( @NotNull GenericDao backingDao ) {
this.backingDao = backingDao;
}
@NotNull
public ReadWriteLock getLock() {
throw new UnsupportedOperationException(); //todo implement(?)
}
@NotNull
public ClusteredElementsCollection getCache() {
return cache;
}
public void initializeCache( @NotNull List elements ) {
cache.clear();
for ( T element : elements ) {
cache.add( element );
}
}
@NotNull
public GenericDao getBackingDao() {
return backingDao;
}
@NotNull
public Long save( @NotNull LT newInstance ) throws DataAccessException {
ensureNotEDT();
Long id = backingDao.save( newInstance );
cache.add( newInstance );
return id;
}
public void update( @NotNull LT transientObject ) throws DataAccessException {
ensureNotEDT();
backingDao.update( transientObject );
cache.commit( transientObject );
}
private static void ensureNotEDT() {
if ( SwingUtilities.isEventDispatchThread() ) {
throw new IllegalThreadStateException( "Must not be called from EDT" );
}
}
@Override
public void commit( @NotNull T element ) {
update( element );
}
public void saveOrUpdate( @NotNull LT object ) throws DataAccessException {
ensureNotEDT();
backingDao.saveOrUpdate( object );
if ( cache.contains( object ) ) {
cache.commit( object );
} else {
cache.add( object );
}
}
public void delete( @NotNull LT persistentObject ) throws DataAccessException {
ensureNotEDT();
backingDao.delete( persistentObject );
cache.remove( persistentObject );
}
@NotNull
public T findById( @NotNull Long id ) throws EmptyResultDataAccessException {
throw new UnsupportedOperationException();
}
@NotNull
public R find( @NotNull final Finder finder ) throws EmptyResultDataAccessException {
return getBackingDao().find( new Finder() {
@NotNull
public R find( @NotNull Session session ) throws DataAccessException {
ensureNotEDT();
for ( T t : cache.getElements() ) {
session.update( t );
}
return finder.find( session );
}
} );
}
@NotNull
public List findAll() throws DataAccessException {
return cache.getElements();
}
public int getCount() throws DataAccessException {
return cache.size();
}
public void addElementListener( @NotNull ElementsListener listener ) {
cache.addElementListener( listener );
}
public void addElementListener( @NotNull ElementsListener listener, boolean isTransient ) {
cache.addElementListener( listener, isTransient );
}
public void removeElementListener( @NotNull ElementsListener listener ) {
cache.removeElementListener( listener );
}
@NotNull
public List> getTransientElementListeners() {
return cache.getTransientElementListeners();
}
@NotNull
public List findElements( @NotNull ElementVisistor elementVisistor ) {
return cache.findElements( elementVisistor );
}
@NotNull
public T findFirstElement( @NotNull ElementVisistor elementVisistor ) throws NoElementFoundException {
return cache.findFirstElement( elementVisistor );
}
@Nullable
public T findFirstElementNullable( @NotNull ElementVisistor elementVisistor ) {
return cache.findFirstElementNullable( elementVisistor );
}
public boolean contains( @NotNull T element ) {
return cache.contains( element );
}
public boolean contains( @NotNull ElementVisistor elementVisistor ) throws NoElementFoundException {
return cache.contains( elementVisistor );
}
public void shutdown() {
backingDao.shutdown();
}
}