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

package de.ivu.elect.business.wahl.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.wahl.entity.Wahl;
import de.ivu.elect.business.wahl.entity.WahlUK;

public abstract class AbstractWahlDAO extends de.ivu.elect.jee.entity.AbstractDAO<Wahl,
        de.ivu.elect.business.wahl.entity.WahlPK
    , WahlUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractWahlDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public Wahl findByPrimaryKey(
        de.ivu.elect.business.wahl.entity.WahlPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.wahltermin.mandant=?1<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByAll( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant) {
            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.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.All")
                              .setParameter(1, mandantCheckedAndAttached)
                                                                      .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("Wahl, findByAll - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: BerichtetAnAndMandantNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.berichtetAn=?1 AND o.wahltermin.mandant = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByBerichtetAnAndMandant( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahl wahl,  @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant) {
            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"); ;
		  			  de.ivu.elect.business.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.BerichtetAnAndMandant")
                              .setParameter(1, wahlCheckedAndAttached)
                                        .setParameter(2, mandantCheckedAndAttached)
                                                                      .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("findByBerichtetAnAndMandant - 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("Wahl, findByBerichtetAnAndMandant - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: BerichtetAnNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.berichtetAn=?1<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByBerichtetAn( @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("WAHL.BerichtetAn")
                              .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("findByBerichtetAn - 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("Wahl, findByBerichtetAn - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: CountWahlartNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT COUNT(o) FROM Wahl o WHERE o.wahlart=?1<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountWahlart( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahlart wahlart) {
            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.Wahlart wahlartCheckedAndAttached =  checkAndAttach(wahlart, de.ivu.elect.business.wahl.entity.Wahlart.class, "wahlart"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("WAHL.CountWahlart")
                              .setParameter(1, wahlartCheckedAndAttached)
                                                                      .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("findByCountWahlart - 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("Wahl, findByCountWahlart - NonUniqueResultException, Parameter: "
           + ", wahlart:'"+wahlart+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: NachDatumNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.wahltermin.mandant=?1 AND o.wahltermin.wahldatum >= ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByNachDatum( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.util.Date zeitpunkt) {
            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"); ;
		  			  java.util.Date zeitpunktCheckedAndAttached =  zeitpunkt ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.NachDatum")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, zeitpunktCheckedAndAttached, javax.persistence.TemporalType.TIMESTAMP)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Wahl, findByNachDatum - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", zeitpunkt:'"+zeitpunkt+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: NameNoFlush<br>
      * Beschreibung: Automatisch generierter Finder<br>
      * EJBQL: SELECT OBJECT(o) FROM Wahl o WHERE o.name=?1<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByName( @javax.annotation.Nonnull String name) {
            long start = 0L;

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

    try {

            try {

          		  			  String nameCheckedAndAttached =  checkAndAttach(name, String.class, "name"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.Name")
                              .setParameter(1, nameCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Wahl, findByName - NonUniqueResultException, Parameter: "
           + ", name:'"+name+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: StatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT w FROM Wahl w WHERE w.wahltermin.mandant=?1 AND w.status = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.WahlStatus status) {
            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.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  			  de.ivu.elect.business.wahl.entity.WahlStatus statusCheckedAndAttached =  checkAndAttach(status, de.ivu.elect.business.wahl.entity.WahlStatus.class, "status"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.Status")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, statusCheckedAndAttached)
                                                                      .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("findByStatus - 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("Wahl, findByStatus - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", status:'"+status+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: UnterstuetzerErfassbarNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.wahltermin.mandant=?1 AND o.unterstuetzerErfassbar = TRUE<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByUnterstuetzerErfassbar( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant) {
            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.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHL.UnterstuetzerErfassbar")
                              .setParameter(1, mandantCheckedAndAttached)
                                                                      .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("findByUnterstuetzerErfassbar - 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("Wahl, findByUnterstuetzerErfassbar - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: WahlterminNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahl o WHERE o.wahltermin=?1<br>
      * SQL: <br>
      */
     public java.util.List<Wahl> findByWahltermin( @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("WAHL.Wahltermin")
                              .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("findByWahltermin - 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("Wahl, findByWahltermin - NonUniqueResultException, Parameter: "
           + ", wahltermin:'"+wahltermin+"'"
    
        );
    throw nure;
            }
            }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.wahl.entity.WahlUK uk,
        de.ivu.elect.business.wahl.entity.WahlPK
      pk

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

   public de.ivu.elect.business.wahl.entity.Wahl findByUK(de.ivu.elect.business.wahl.entity.WahlUK uk){

    try {
     return (de.ivu.elect.business.wahl.entity.Wahl) getEntityManager().createNamedQuery("WAHL.UK")
                  .setParameter(1, uk
                .getWahlartUK ()  
                .getName ()  
              )
             .setParameter(2, uk
                .getWahlartUK ()  
                .getMandantUK ()  
                .getNummer ()  
              )
             .setParameter(3, uk
                .getWahlterminUK ()  
                .getNummer ()  
              )
             .setParameter(4, uk
                .getWahlterminUK ()  
                .getWahldatum ()  , javax.persistence.TemporalType.DATE /*ELECT-13069*/ 
              )
             .setParameter(5, uk
                .getWahlterminUK ()  
                .getMandantUK ()  
                .getNummer ()  
              )
          .getSingleResult()
     ;
     } catch (NonUniqueResultException nure) {
        LOG.warn("Wahl, findByUK - NonUniqueResultException, UK: " + uk);
        throw nure;
     }
  }
    
}