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

package de.ivu.elect.business.wertelisten.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.wertelisten.entity.Wertelisten;
import de.ivu.elect.business.wertelisten.entity.WertelistenUK;

public abstract class AbstractWertelistenDAO extends de.ivu.elect.jee.entity.AbstractDAO<Wertelisten,
        de.ivu.elect.business.wertelisten.entity.WertelistenPK
    , WertelistenUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractWertelistenDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public Wertelisten findByPrimaryKey(
        de.ivu.elect.business.wertelisten.entity.WertelistenPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: CountWahlAndWertelistenArtNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT COUNT(obj) FROM Wertelisten obj WHERE obj.wahl = ?1 AND obj.wertelistenArt IN(?2)<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountWahlAndWertelistenArt( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahl wahl,  @javax.annotation.Nonnull java.util.Collection<de.ivu.elect.business.wertelisten.entity.WertelistenArt> wertelistenArt) {
            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"); ;
		  			  java.util.Collection<de.ivu.elect.business.wertelisten.entity.WertelistenArt> wertelistenArtCheckedAndAttached =  wertelistenArt ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("WERTELISTEN.CountWahlAndWertelistenArt")
                              .setParameter(1, wahlCheckedAndAttached)
                                        .setParameter(2, wertelistenArtCheckedAndAttached)
                                                                      .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("findByCountWahlAndWertelistenArt - 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("Wertelisten, findByCountWahlAndWertelistenArt - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
           + ", wertelistenArt:'"+wertelistenArt+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: WahlAndWertelistenArtNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM Wertelisten obj WHERE obj.wahl = ?1 AND obj.wertelistenArt IN (?2)<br>
      * SQL: <br>
      */
     public java.util.List<Wertelisten> findByWahlAndWertelistenArt( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahl wahl,  @javax.annotation.Nonnull java.util.Collection<de.ivu.elect.business.wertelisten.entity.WertelistenArt> wertelistenArt) {
            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"); ;
		  			  java.util.Collection<de.ivu.elect.business.wertelisten.entity.WertelistenArt> wertelistenArtCheckedAndAttached =  wertelistenArt ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("WERTELISTEN.WahlAndWertelistenArt")
                              .setParameter(1, wahlCheckedAndAttached)
                                        .setParameter(2, wertelistenArtCheckedAndAttached)
                                                                      .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("findByWahlAndWertelistenArt - 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("Wertelisten, findByWahlAndWertelistenArt - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
           + ", wertelistenArt:'"+wertelistenArt+"'"
    
        );
    throw nure;
            }
            }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.wertelisten.entity.WertelistenUK uk,
        de.ivu.elect.business.wertelisten.entity.WertelistenPK
      pk

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

   public de.ivu.elect.business.wertelisten.entity.Wertelisten findByUK(de.ivu.elect.business.wertelisten.entity.WertelistenUK uk){

    try {
     return (de.ivu.elect.business.wertelisten.entity.Wertelisten) getEntityManager().createNamedQuery("WERTELISTEN.UK")
                  .setParameter(1, uk
                .getName ()

              )
             .setParameter(2, uk
                .getWertelistenArt ()

              )
             .setParameter(3, uk
                .getWahlUK ()

                .getWahlartUK ()

                .getName ()

              )
             .setParameter(4, uk
                .getWahlUK ()

                .getWahlartUK ()

                .getMandantUK ()

                .getNummer ()

              )
             .setParameter(5, uk
                .getWahlUK ()

                .getWahlterminUK ()

                .getNummer ()

              )
             .setParameter(6, uk
                .getWahlUK ()

                .getWahlterminUK ()

                .getWahldatum ()

              )
             .setParameter(7, uk
                .getWahlUK ()

                .getWahlterminUK ()

                .getMandantUK ()

                .getNummer ()

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