/*
 * 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.Wahltermin;
import de.ivu.elect.business.wahl.entity.WahlterminUK;

public abstract class AbstractWahlterminDAO extends de.ivu.elect.jee.entity.AbstractDAO<Wahltermin,
        de.ivu.elect.business.wahl.entity.WahlterminPK
    , WahlterminUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractWahlterminDAO() {
    super();
  }

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

    return false;
  }

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

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahltermin o WHERE o.mandant=?1<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> 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("WAHLTERMIN.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("Wahltermin, 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: AllNummerWithNummernNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o.nummer FROM Wahltermin o WHERE o.mandant=?1 AND o.nummer IN ?2<br>
      * SQL: <br>
      */
     public java.util.List<java.lang.String> findByAllNummerWithNummern( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.util.Collection<String> nummern) {
            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.Collection<String> nummernCheckedAndAttached =  nummern ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.AllNummerWithNummern")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, nummernCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Wahltermin, findByAllNummerWithNummern - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", nummern:'"+nummern+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: AllSystemweitNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahltermin o<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> findByAllSystemweit() {
            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("WAHLTERMIN.AllSystemweit")
                                                            .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("findByAllSystemweit - 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("Wahltermin, findByAllSystemweit - NonUniqueResultException, Parameter: "
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: AllWithNummernNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahltermin o WHERE o.mandant=?1 AND o.nummer IN ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> findByAllWithNummern( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.util.Collection<String> nummern) {
            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.Collection<String> nummernCheckedAndAttached =  nummern ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.AllWithNummern")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, nummernCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Wahltermin, findByAllWithNummern - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", nummern:'"+nummern+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: IdPrefetchWahlenNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahltermin o LEFT JOIN FETCH o.wahlsByWahltermin wahl LEFT JOIN FETCH wahl.wahlart wahlart WHERE o.id = ?1<br>
      * SQL: <br>
      */
     public Wahltermin findByIdPrefetchWahlen( @javax.annotation.Nonnull java.lang.Long id) {
            long start = 0L;

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

    try {

            try {

          		  			  java.lang.Long idCheckedAndAttached =  id ;
		  
                              return
                    (Wahltermin)
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.IdPrefetchWahlen")
                              .setParameter(1, idCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Wahltermin, findByIdPrefetchWahlen - NonUniqueResultException, Parameter: "
           + ", id:'"+id+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: NameNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahltermin o WHERE o.mandant=?1 AND o.name = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> findByName( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull String name) {
            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"); ;
		  			  String nameCheckedAndAttached =  checkAndAttach(name, String.class, "name"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.Name")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, nameCheckedAndAttached)
                                                                      .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("findByName - 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("Wahltermin, findByName - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", 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 o FROM Wahltermin o WHERE o.mandant=?1 AND o.status = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> findByStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.WahlterminStatus 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.WahlterminStatus statusCheckedAndAttached =  checkAndAttach(status, de.ivu.elect.business.wahl.entity.WahlterminStatus.class, "status"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.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("Wahltermin, 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: WahldatumAndNummerNoFlush<br>
      * Beschreibung: Nummer wegen ELECTMUC-2011<br>
      * EJBQL: SELECT o FROM Wahltermin o WHERE o.mandant=?1 AND o.wahldatum = ?2 AND o.nummer = ?3<br>
      * SQL: <br>
      */
     public Wahltermin findByWahldatumAndNummer( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.util.Date wahldatum,  @javax.annotation.Nonnull java.lang.String nummer) {
            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"); ;
		  			  java.util.Date wahldatumCheckedAndAttached =  wahldatum ;
		  			  java.lang.String nummerCheckedAndAttached =  nummer ;
		  
                              return
                    (Wahltermin)
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.WahldatumAndNummer")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, wahldatumCheckedAndAttached, javax.persistence.TemporalType.TIMESTAMP)
                                        .setParameter(3, nummerCheckedAndAttached)
                                                                      .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("findByWahldatumAndNummer - 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("Wahltermin, findByWahldatumAndNummer - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", wahldatum:'"+wahldatum+"'"
           + ", nummer:'"+nummer+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: WahldatumDeprecatedNoFlush<br>
      * Beschreibung: @Deprecated // mit Nummer verwenden<br>
      * EJBQL: SELECT o FROM Wahltermin o WHERE o.mandant=?1 AND o.wahldatum = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Wahltermin> findByWahldatumDeprecated( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.util.Date wahldatum) {
            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"); ;
		  			  java.util.Date wahldatumCheckedAndAttached =  wahldatum ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WAHLTERMIN.WahldatumDeprecated")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, wahldatumCheckedAndAttached, javax.persistence.TemporalType.TIMESTAMP)
                                                                      .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("findByWahldatumDeprecated - 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("Wahltermin, findByWahldatumDeprecated - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", wahldatum:'"+wahldatum+"'"
    
        );
    throw nure;
            }
            }

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



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

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

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

   public de.ivu.elect.business.wahl.entity.Wahltermin findByUK(de.ivu.elect.business.wahl.entity.WahlterminUK uk){

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