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

com.cedarsoft.gdao.caching.CachingDao Maven / Gradle / Ivy

There is a newer version: 1.0.1
Show newest version
/**
 * Copyright (C) cedarsoft GmbH.
 *
 * Licensed under the GNU General Public License version 3 (the "License")
 * with Classpath Exception; you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at
 *
 *         http://www.cedarsoft.org/gpl3ce
 *         (GPL 3 with Classpath Exception)
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 3 only, as
 * published by the Free Software Foundation. cedarsoft GmbH designates this
 * particular file as subject to the "Classpath" exception as provided
 * by cedarsoft GmbH in the LICENSE file that accompanied this code.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 3 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 3 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
 * Please contact cedarsoft GmbH, 72810 Gomaringen, Germany,
 * or visit www.cedarsoft.com if you need additional information or
 * have any questions.
 */

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.ElementVisitor;
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.lang.Override;
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;
  }

  @Override
  @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;
  }

  @Override
  @NotNull
  public  Long save( @NotNull LT newInstance ) throws DataAccessException {
    ensureNotEDT();

    Long id = backingDao.save( newInstance );
    cache.add( newInstance );
    return id;
  }

  @Override
  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 );
  }

  @Override
  public  void saveOrUpdate( @NotNull LT object ) throws DataAccessException {
    ensureNotEDT();

    backingDao.saveOrUpdate( object );
    if ( cache.contains( object ) ) {
      cache.commit( object );
    } else {
      cache.add( object );
    }
  }

  @Override
  public  void delete( @NotNull LT persistentObject ) throws DataAccessException {
    ensureNotEDT();

    backingDao.delete( persistentObject );
    cache.remove( persistentObject );
  }

  @Override
  @NotNull
  public T findById( @NotNull Long id ) throws EmptyResultDataAccessException {
    throw new UnsupportedOperationException();
  }

  @Override
  @NotNull
  public  R find( @NotNull final Finder finder ) throws EmptyResultDataAccessException {
    return getBackingDao().find( new Finder() {
      @Override
      @NotNull
      public R find( @NotNull Session session ) throws DataAccessException {
        ensureNotEDT();

        for ( T t : cache.getElements() ) {
          session.update( t );
        }
        return finder.find( session );
      }
    } );
  }

  @Override
  @NotNull
  public List findAll() throws DataAccessException {
    return cache.getElements();
  }

  @Override
  public int getCount() throws DataAccessException {
    return cache.size();
  }

  @Override
  public void addElementListener( @NotNull ElementsListener listener ) {
    cache.addElementListener( listener );
  }

  @Override
  public void addElementListener( @NotNull ElementsListener listener, boolean isTransient ) {
    cache.addElementListener( listener, isTransient );
  }

  @Override
  public void removeElementListener( @NotNull ElementsListener listener ) {
    cache.removeElementListener( listener );
  }

  @Override
  @NotNull
  public List> getTransientElementListeners() {
    return cache.getTransientElementListeners();
  }

  @NotNull
  public List findElements( @NotNull ElementVisitor elementVisitor ) {
    return cache.findElements( elementVisitor );
  }

  @NotNull
  public T findFirstElement( @NotNull ElementVisitor elementVisitor ) throws NoElementFoundException {
    return cache.findFirstElement( elementVisitor );
  }

  @Nullable
  public T findFirstElementNullable( @NotNull ElementVisitor elementVisitor ) {
    return cache.findFirstElementNullable( elementVisitor );
  }

  public boolean contains( @NotNull T element ) {
    return cache.contains( element );
  }

  public boolean contains( @NotNull ElementVisitor elementVisitor ) throws NoElementFoundException {
    return cache.contains( elementVisitor );
  }

  @Override
  public void shutdown() {
    backingDao.shutdown();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy