package de.ivu.elect.business.system.boundary;

import de.ivu.elect.business.system.control.DefaultCacheReset;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.ejb.Stateless;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import java.io.Serializable;

/**
 *  on 04.12.2015.
 */
@Stateless
public class CacheResetSession implements Serializable {

  private static final long serialVersionUID = 1L;

  private static final Logger LOG = LoggerFactory.getLogger(CacheResetSession.class);

  private static DefaultCacheReset CACHE_RESET = DefaultCacheReset.INSTANCE;

  @javax.persistence.PersistenceContext
  private EntityManager entityManager;

  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public void resetCache() {
    reset(entityManager);
  }

  // http://stackoverflow.com/questions/1603846/hibernate-2nd-level-cache-invalidation-when-another-process-modifies-the-databas
  public static void reset(EntityManager entityManager) {
    CACHE_RESET.reset(entityManager);
  }

  public static void setCacheReset(DefaultCacheReset cacheReset) {
    CACHE_RESET = cacheReset;
    LOG.info("Cache reset gesetzt");
  }
}
