
package de.ivu.elect.jee.entity;

import java.io.Serializable;

import javax.annotation.PostConstruct;
import javax.ejb.Stateful;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.enterprise.context.RequestScoped;
import javax.inject.Named;
import javax.persistence.FlushModeType;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

@Named(ExtendedDAO.COMPONENT_NAME)
@Stateful(passivationCapable = false)
@RequestScoped
@TransactionAttribute(TransactionAttributeType.SUPPORTS) // zur Anaylse von Transaktionsproblemen hier "NEVER" eintragen...
public class ExtendedDAO implements Serializable {

  private static final long serialVersionUID = 1L;

  public static final String COMPONENT_NAME = "extendedDAO";

  @PersistenceContext(type = PersistenceContextType.EXTENDED)
  private transient javax.persistence.EntityManager entityManager;

  @PostConstruct
  @TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
  public void init() {
    entityManager.setFlushMode(FlushModeType.COMMIT);
  }

  public javax.persistence.EntityManager getEntityManager() {
    return entityManager;
  }

  @TransactionAttribute(TransactionAttributeType.REQUIRED)
  public void flush() {
    getEntityManager().flush();
  }

  @TransactionAttribute(TransactionAttributeType.REQUIRES_NEW)
  public void flushNewTransaction() {
    getEntityManager().flush();
  }

  public void resetCache(){
    entityManager.getEntityManagerFactory().getCache().evictAll();
  }

}
