/*
 * 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.EmailVorlage;
import de.ivu.elect.business.email.entity.EmailVorlageUK;

public abstract class AbstractEmailVorlageDAO extends de.ivu.elect.jee.entity.AbstractDAO<EmailVorlage,
        de.ivu.elect.business.email.entity.EmailVorlagePK
    , EmailVorlageUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractEmailVorlageDAO() {
    super();
  }

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

    return false;
  }

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

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select o from EmailVorlage o<br>
      * SQL: <br>
      */
     public java.util.List<EmailVorlage> findByAll() {
            long start = 0L;

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

    try {

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

          		  
                              return
                              getEntityManager().
                                createNamedQuery("EMAIL_VORLAGE.All")
                                                            .getResultList();
                                        
                    
        } 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("findByAll - 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("EmailVorlage, findByAll - NonUniqueResultException, Parameter: "
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: CountEmailLayoutNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from EmailVorlage 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_VORLAGE.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("EmailVorlage, 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: EmailLayoutForAllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: update EmailVorlage o set o.emailLayout=?1<br>
      * SQL: <br>
      */
     public Integer updateEmailLayoutForAll( @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 {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailLayout emailLayoutCheckedAndAttached =  checkAndAttach(emailLayout, de.ivu.elect.business.email.entity.EmailLayout.class, "emailLayout"); ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("EMAIL_VORLAGE.EmailLayoutForAll")
                              .setParameter(1, emailLayoutCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("EmailVorlage, updateEmailLayoutForAll - NonUniqueResultException, Parameter: "
           + ", emailLayout:'"+emailLayout+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlOrderByBezeichnungNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select o from EmailVorlage o where o.wahl=?1 order by o.bezeichnung<br>
      * SQL: <br>
      */
     public java.util.List<EmailVorlage> findByWahlOrderByBezeichnung( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahl wahl) {
            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.wahl.entity.Wahl wahlCheckedAndAttached =  checkAndAttach(wahl, de.ivu.elect.business.wahl.entity.Wahl.class, "wahl"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("EMAIL_VORLAGE.WahlOrderByBezeichnung")
                              .setParameter(1, wahlCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } 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("findByWahlOrderByBezeichnung - 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("EmailVorlage, findByWahlOrderByBezeichnung - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: WahlterminOrderByBezeichnungNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select o from EmailVorlage o where o.wahltermin=?1 order by o.bezeichnung<br>
      * SQL: <br>
      */
     public java.util.List<EmailVorlage> findByWahlterminOrderByBezeichnung( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahltermin wahltermin) {
            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.wahl.entity.Wahltermin wahlterminCheckedAndAttached =  checkAndAttach(wahltermin, de.ivu.elect.business.wahl.entity.Wahltermin.class, "wahltermin"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("EMAIL_VORLAGE.WahlterminOrderByBezeichnung")
                              .setParameter(1, wahlterminCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } 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("findByWahlterminOrderByBezeichnung - 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("EmailVorlage, findByWahlterminOrderByBezeichnung - NonUniqueResultException, Parameter: "
           + ", wahltermin:'"+wahltermin+"'"
    
        );
    throw nure;
            }
            }

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



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

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

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

   public de.ivu.elect.business.email.entity.EmailVorlage findByUK(de.ivu.elect.business.email.entity.EmailVorlageUK uk){

    try {
     return (de.ivu.elect.business.email.entity.EmailVorlage) getEntityManager().createNamedQuery("EMAIL_VORLAGE.UK")
                  .setParameter(1, uk
                .getBezeichnung ()

              )
             .setParameter(2, uk
                .getWahlterminUK ()

                .getNummer ()

              )
             .setParameter(3, uk
                .getWahlterminUK ()

                .getWahldatum ()

              )
             .setParameter(4, uk
                .getWahlterminUK ()

                .getMandantUK ()

                .getNummer ()

              )
          .getSingleResult()
     ;
     } catch (NonUniqueResultException nure) {
        LOG.warn("EmailVorlage, findByUK - NonUniqueResultException, UK: " + uk);
        throw nure;
     }
  }
    
}