/*
 * JPA DAO Template
 *
 *
 * Takatuka - Template fuer JPA DAO
 *
 */

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

import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.EntityNotFoundException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import de.ivu.elect.business.email.entity.EmailVorgang;
import de.ivu.elect.business.email.entity.EmailVorgangUK;

public abstract class AbstractEmailVorgangDAO extends de.ivu.elect.jee.entity.AbstractDAO<EmailVorgang,
        de.ivu.elect.business.email.entity.EmailVorgangPK
    , EmailVorgangUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractEmailVorgangDAO() {
    super();
  }

  public boolean isEntityWithPKPresent(
            de.ivu.elect.business.email.entity.EmailVorgangPK
     pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk) != null;
    }

    return false;
  }

  @Override
  public EmailVorgang findByPrimaryKey(
        de.ivu.elect.business.email.entity.EmailVorgangPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

    throw new IllegalArgumentException("PK oder PK.getId() ist null! [pk = " + pk + "]");
  }


  @Override
  protected EmailVorgang findByPrimaryKey(Long id) {
    if (id == null) {
      throw new IllegalArgumentException("ID ist null! [id = " + id + "]");
    }
  return getEntityManager().find(EmailVorgang.class, id, CACHE_PROPERTIES);
  }



     /**
      * Name: CountEmailLayoutNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from EmailVorgang o where o.emailLayout=?1<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountEmailLayout( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailLayout emailLayout) {
            long start = 0L;

            if (de.ivu.elect.business.system.control.LogQueries.LOG_QUERIES) {
               start = System.currentTimeMillis();
            }

    try {

        int versuch = 0;
      while (true) {
        versuch++;
                try {

          		  			  de.ivu.elect.business.email.entity.EmailLayout emailLayoutCheckedAndAttached =  checkAndAttach(emailLayout, de.ivu.elect.business.email.entity.EmailLayout.class, "emailLayout"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("EMAIL_VORGANG.CountEmailLayout")
                              .setParameter(1, emailLayoutCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (EntityNotFoundException enfe) {
      // ELECTMUC-2455
       if (de.ivu.elect.business.system.control.InvalidateCacheOnEntityNotFound.INVALIDATE_CACHE &&
       versuch < de.ivu.elect.business.system.control.InvalidateCacheOnEntityNotFound.MAX_VERSUCHE ) {
       LOG.warn("findByCountEmailLayout - EntityNotFoundException: " +enfe.getMessage()+", Versuch: " +versuch+" - lösche Cache - wiederhole");
        try {
         de.ivu.elect.business.system.boundary.CacheResetSession.reset(getEntityManager());
        } catch (Exception e) {
          LOG.warn("Fehler beim Zurücksetzen des Cache: "+e.getMessage());

        }
       } else {
        throw enfe;
       }
           } catch (NonUniqueResultException nure) {
    LOG.warn("EmailVorgang, findByCountEmailLayout - NonUniqueResultException, Parameter: "
           + ", emailLayout:'"+emailLayout+"'"
    
        );
    throw nure;
            }
            }

    } finally {
    if (de.ivu.elect.business.system.control.LogQueries.LOG_QUERIES) {
    LOG.info("Query: " + getClass().getName()+".findByCountEmailLayout(): "+(System.currentTimeMillis()-start)+ " ms");
    }
    }
     }

     /**
      * Name: CountEmailVorlageNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from EmailVorgang o where o.emailVorlage=?1<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountEmailVorlage( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorlage emailVorlage) {
            long start = 0L;

            if (de.ivu.elect.business.system.control.LogQueries.LOG_QUERIES) {
               start = System.currentTimeMillis();
            }

    try {

        int versuch = 0;
      while (true) {
        versuch++;
                try {

          		  			  de.ivu.elect.business.email.entity.EmailVorlage emailVorlageCheckedAndAttached =  checkAndAttach(emailVorlage, de.ivu.elect.business.email.entity.EmailVorlage.class, "emailVorlage"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("EMAIL_VORGANG.CountEmailVorlage")
                              .setParameter(1, emailVorlageCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (EntityNotFoundException enfe) {
      // ELECTMUC-2455
       if (de.ivu.elect.business.system.control.InvalidateCacheOnEntityNotFound.INVALIDATE_CACHE &&
       versuch < de.ivu.elect.business.system.control.InvalidateCacheOnEntityNotFound.MAX_VERSUCHE ) {
       LOG.warn("findByCountEmailVorlage - EntityNotFoundException: " +enfe.getMessage()+", Versuch: " +versuch+" - lösche Cache - wiederhole");
        try {
         de.ivu.elect.business.system.boundary.CacheResetSession.reset(getEntityManager());
        } catch (Exception e) {
          LOG.warn("Fehler beim Zurücksetzen des Cache: "+e.getMessage());

        }
       } else {
        throw enfe;
       }
           } catch (NonUniqueResultException nure) {
    LOG.warn("EmailVorgang, findByCountEmailVorlage - NonUniqueResultException, Parameter: "
           + ", emailVorlage:'"+emailVorlage+"'"
    
        );
    throw nure;
            }
            }

    } finally {
    if (de.ivu.elect.business.system.control.LogQueries.LOG_QUERIES) {
    LOG.info("Query: " + getClass().getName()+".findByCountEmailVorlage(): "+(System.currentTimeMillis()-start)+ " ms");
    }
    }
     }



  @Override
  public boolean isEntityWithUKPresent(de.ivu.elect.business.email.entity.EmailVorgangUK uk) {
    try {
      return findByUK(uk) != null;
    } catch (javax.persistence.NoResultException nre) {
      return false;
    }
  }

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.email.entity.EmailVorgangUK uk,
        de.ivu.elect.business.email.entity.EmailVorgangPK
      pk

  ) {
    try {
      return findByUK(uk).getPk().equals(pk);
    } catch (NoResultException ignored) {
      // Do nothing - be happy.
    }
    return true;
  }

    public de.ivu.elect.business.email.entity.EmailVorgang findByUK(de.ivu.elect.business.email.entity.EmailVorgangUK uk){
        throw new UnsupportedOperationException("Kein UK definiert");
    }


}