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

public abstract class AbstractEmailDAO extends de.ivu.elect.jee.entity.AbstractDAO<Email,
        de.ivu.elect.business.email.entity.EmailPK
    , EmailUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractEmailDAO() {
    super();
  }

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

    return false;
  }

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

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


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



     /**
      * Name: CountEmailVorgangAndStatusInNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from Email o where o.emailVorgang=?1 and o.status in (?2)<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountEmailVorgangAndStatusIn( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorgang emailVorgang,  @javax.annotation.Nonnull java.util.Collection<de.ivu.elect.business.email.entity.EmailStatusType> emailStatusTypes) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailVorgang emailVorgangCheckedAndAttached =  checkAndAttach(emailVorgang, de.ivu.elect.business.email.entity.EmailVorgang.class, "emailVorgang"); ;
		  			  java.util.Collection<de.ivu.elect.business.email.entity.EmailStatusType> emailStatusTypesCheckedAndAttached =  emailStatusTypes ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("EMAIL.CountEmailVorgangAndStatusIn")
                              .setParameter(1, emailVorgangCheckedAndAttached)
                                        .setParameter(2, emailStatusTypesCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, findByCountEmailVorgangAndStatusIn - NonUniqueResultException, Parameter: "
           + ", emailVorgang:'"+emailVorgang+"'"
           + ", emailStatusTypes:'"+emailStatusTypes+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: CountEmailVorgangAndStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from Email o where o.emailVorgang=?1 and o.status=?2<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountEmailVorgangAndStatus( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorgang emailVorgang,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType emailStatusType) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailVorgang emailVorgangCheckedAndAttached =  checkAndAttach(emailVorgang, de.ivu.elect.business.email.entity.EmailVorgang.class, "emailVorgang"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType emailStatusTypeCheckedAndAttached =  checkAndAttach(emailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "emailStatusType"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("EMAIL.CountEmailVorgangAndStatus")
                              .setParameter(1, emailVorgangCheckedAndAttached)
                                        .setParameter(2, emailStatusTypeCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, findByCountEmailVorgangAndStatus - NonUniqueResultException, Parameter: "
           + ", emailVorgang:'"+emailVorgang+"'"
           + ", emailStatusType:'"+emailStatusType+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: CountStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select count(o) from Email o where o.emailVorgang.wahltermin.mandant=?1 AND o.status=?2<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType emailStatusType) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType emailStatusTypeCheckedAndAttached =  checkAndAttach(emailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "emailStatusType"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("EMAIL.CountStatus")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, emailStatusTypeCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, findByCountStatus - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", emailStatusType:'"+emailStatusType+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: SendeversucheStatusAndFehlermeldungByIdNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: UPDATE Email o SET o.letzterSendeversuch = ?2, o.anzahlSendeversuche = ?3, o.status = ?4, o.fehlermeldung = ?5 WHERE o.id = ?1<br>
      * SQL: <br>
      */
     public Integer updateSendeversucheStatusAndFehlermeldungById( @javax.annotation.Nonnull java.lang.Long emailId,  @javax.annotation.Nonnull java.util.Date letzterSendeversuch,  @javax.annotation.Nonnull java.lang.Long anzahlSendeversuche,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusType,  @javax.annotation.Nonnull java.lang.String neueFehlermeldung) {
            long start = 0L;

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

    try {

            try {

          		  			  java.lang.Long emailIdCheckedAndAttached =  emailId ;
		  			  java.util.Date letzterSendeversuchCheckedAndAttached =  letzterSendeversuch ;
		  			  java.lang.Long anzahlSendeversucheCheckedAndAttached =  anzahlSendeversuche ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusTypeCheckedAndAttached =  checkAndAttach(neuerEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "neuerEmailStatusType"); ;
		  			  java.lang.String neueFehlermeldungCheckedAndAttached =  neueFehlermeldung ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("EMAIL.SendeversucheStatusAndFehlermeldungById")
                              .setParameter(1, emailIdCheckedAndAttached)
                                        .setParameter(2, letzterSendeversuchCheckedAndAttached, javax.persistence.TemporalType.TIMESTAMP)
                                        .setParameter(3, anzahlSendeversucheCheckedAndAttached)
                                        .setParameter(4, neuerEmailStatusTypeCheckedAndAttached)
                                        .setParameter(5, neueFehlermeldungCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, updateSendeversucheStatusAndFehlermeldungById - NonUniqueResultException, Parameter: "
           + ", emailId:'"+emailId+"'"
           + ", letzterSendeversuch:'"+letzterSendeversuch+"'"
           + ", anzahlSendeversuche:'"+anzahlSendeversuche+"'"
           + ", neuerEmailStatusType:'"+neuerEmailStatusType+"'"
           + ", neueFehlermeldung:'"+neueFehlermeldung+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: StatusAndFehlermeldungByEmailVorgangAndStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: UPDATE Email o SET o.status = ?3, o.fehlermeldung = ?4 WHERE o.emailVorgang = ?1 AND o.status = ?2<br>
      * SQL: <br>
      */
     public Integer updateStatusAndFehlermeldungByEmailVorgangAndStatus( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorgang emailVorgang,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType alterEmailStatusType,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusType,  @javax.annotation.Nonnull java.lang.String neueFehlermeldung) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailVorgang emailVorgangCheckedAndAttached =  checkAndAttach(emailVorgang, de.ivu.elect.business.email.entity.EmailVorgang.class, "emailVorgang"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType alterEmailStatusTypeCheckedAndAttached =  checkAndAttach(alterEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "alterEmailStatusType"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusTypeCheckedAndAttached =  checkAndAttach(neuerEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "neuerEmailStatusType"); ;
		  			  java.lang.String neueFehlermeldungCheckedAndAttached =  neueFehlermeldung ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("EMAIL.StatusAndFehlermeldungByEmailVorgangAndStatus")
                              .setParameter(1, emailVorgangCheckedAndAttached)
                                        .setParameter(2, alterEmailStatusTypeCheckedAndAttached)
                                        .setParameter(3, neuerEmailStatusTypeCheckedAndAttached)
                                        .setParameter(4, neueFehlermeldungCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, updateStatusAndFehlermeldungByEmailVorgangAndStatus - NonUniqueResultException, Parameter: "
           + ", emailVorgang:'"+emailVorgang+"'"
           + ", alterEmailStatusType:'"+alterEmailStatusType+"'"
           + ", neuerEmailStatusType:'"+neuerEmailStatusType+"'"
           + ", neueFehlermeldung:'"+neueFehlermeldung+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: StatusAndFehlermeldungByEmailVorgangNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: UPDATE Email o SET o.status = ?2, o.fehlermeldung = ?3 WHERE o.emailVorgang = ?1<br>
      * SQL: <br>
      */
     public Integer updateStatusAndFehlermeldungByEmailVorgang( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorgang emailVorgang,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusType,  @javax.annotation.Nonnull java.lang.String neueFehlermeldung) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailVorgang emailVorgangCheckedAndAttached =  checkAndAttach(emailVorgang, de.ivu.elect.business.email.entity.EmailVorgang.class, "emailVorgang"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusTypeCheckedAndAttached =  checkAndAttach(neuerEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "neuerEmailStatusType"); ;
		  			  java.lang.String neueFehlermeldungCheckedAndAttached =  neueFehlermeldung ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("EMAIL.StatusAndFehlermeldungByEmailVorgang")
                              .setParameter(1, emailVorgangCheckedAndAttached)
                                        .setParameter(2, neuerEmailStatusTypeCheckedAndAttached)
                                        .setParameter(3, neueFehlermeldungCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, updateStatusAndFehlermeldungByEmailVorgang - NonUniqueResultException, Parameter: "
           + ", emailVorgang:'"+emailVorgang+"'"
           + ", neuerEmailStatusType:'"+neuerEmailStatusType+"'"
           + ", neueFehlermeldung:'"+neueFehlermeldung+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: StatusByEmailVorgangAndStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: UPDATE Email o SET o.status = ?3 WHERE o.emailVorgang = ?1 AND o.status = ?2<br>
      * SQL: <br>
      */
     public Integer updateStatusByEmailVorgangAndStatus( @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailVorgang emailVorgang,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType alterEmailStatusType,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusType) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.email.entity.EmailVorgang emailVorgangCheckedAndAttached =  checkAndAttach(emailVorgang, de.ivu.elect.business.email.entity.EmailVorgang.class, "emailVorgang"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType alterEmailStatusTypeCheckedAndAttached =  checkAndAttach(alterEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "alterEmailStatusType"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType neuerEmailStatusTypeCheckedAndAttached =  checkAndAttach(neuerEmailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "neuerEmailStatusType"); ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("EMAIL.StatusByEmailVorgangAndStatus")
                              .setParameter(1, emailVorgangCheckedAndAttached)
                                        .setParameter(2, alterEmailStatusTypeCheckedAndAttached)
                                        .setParameter(3, neuerEmailStatusTypeCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, updateStatusByEmailVorgangAndStatus - NonUniqueResultException, Parameter: "
           + ", emailVorgang:'"+emailVorgang+"'"
           + ", alterEmailStatusType:'"+alterEmailStatusType+"'"
           + ", neuerEmailStatusType:'"+neuerEmailStatusType+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: StatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: select o from Email o where o.emailVorgang.wahltermin.mandant=?1 AND o.status=?2<br>
      * SQL: <br>
      */
     public java.util.List<Email> findByStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.email.entity.EmailStatusType emailStatusType,  int limit) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  			  de.ivu.elect.business.email.entity.EmailStatusType emailStatusTypeCheckedAndAttached =  checkAndAttach(emailStatusType, de.ivu.elect.business.email.entity.EmailStatusType.class, "emailStatusType"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("EMAIL.Status")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, emailStatusTypeCheckedAndAttached)
                                                  .setMaxResults(limit)
                                        .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Email, findByStatus - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", emailStatusType:'"+emailStatusType+"'"
    
        );
    throw nure;
        }

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



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

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

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

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


}