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

package de.ivu.elect.business.kandidatur.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.kandidatur.entity.Listenkandidatur;
import de.ivu.elect.business.kandidatur.entity.ListenkandidaturUK;

public abstract class AbstractListenkandidaturDAO extends de.ivu.elect.jee.entity.AbstractDAO<Listenkandidatur,
      de.ivu.elect.business.person.entity.PersonendatenPK
  , ListenkandidaturUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractListenkandidaturDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public Listenkandidatur findByPrimaryKey(
      de.ivu.elect.business.person.entity.PersonendatenPK
    pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: GruppeAndUngleichStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Listenkandidatur o WHERE o.liste.gruppeImGebiet.gruppe = ?1 AND o.kandidaturStatus != ?2<br>
      * SQL: <br>
      */
     public java.util.List<Listenkandidatur> findByGruppeAndUngleichStatus( @javax.annotation.Nonnull de.ivu.elect.business.gruppe.entity.Gruppe gruppe,  @javax.annotation.Nonnull de.ivu.elect.business.kandidatur.entity.KandidaturStatus kandidaturStatus) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.gruppe.entity.Gruppe gruppeCheckedAndAttached =  checkAndAttach(gruppe, de.ivu.elect.business.gruppe.entity.Gruppe.class, "gruppe"); ;
		  			  de.ivu.elect.business.kandidatur.entity.KandidaturStatus kandidaturStatusCheckedAndAttached =  checkAndAttach(kandidaturStatus, de.ivu.elect.business.kandidatur.entity.KandidaturStatus.class, "kandidaturStatus"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("LISTENKANDIDATUR.GruppeAndUngleichStatus")
                              .setParameter(1, gruppeCheckedAndAttached)
                                        .setParameter(2, kandidaturStatusCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Listenkandidatur, findByGruppeAndUngleichStatus - NonUniqueResultException, Parameter: "
           + ", gruppe:'"+gruppe+"'"
           + ", kandidaturStatus:'"+kandidaturStatus+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: GruppeNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Listenkandidatur o WHERE o.liste.gruppeImGebiet.gruppe = ?1<br>
      * SQL: <br>
      */
     public java.util.List<Listenkandidatur> findByGruppe( @javax.annotation.Nonnull de.ivu.elect.business.gruppe.entity.Gruppe gruppe) {
            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.gruppe.entity.Gruppe gruppeCheckedAndAttached =  checkAndAttach(gruppe, de.ivu.elect.business.gruppe.entity.Gruppe.class, "gruppe"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("LISTENKANDIDATUR.Gruppe")
                              .setParameter(1, gruppeCheckedAndAttached)
                                                                      .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("findByGruppe - 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("Listenkandidatur, findByGruppe - NonUniqueResultException, Parameter: "
           + ", gruppe:'"+gruppe+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: IdsNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Listenkandidatur o WHERE o.id IN (?1)<br>
      * SQL: <br>
      */
     public java.util.List<Listenkandidatur> findByIds( @javax.annotation.Nonnull java.util.Collection<Long> ids) {
            long start = 0L;

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

    try {

            try {

          		  			  java.util.Collection<Long> idsCheckedAndAttached =  ids ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("LISTENKANDIDATUR.Ids")
                              .setParameter(1, idsCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Listenkandidatur, findByIds - NonUniqueResultException, Parameter: "
           + ", ids:'"+ids+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: ListeOrderByEingereichtePositionNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM Listenkandidatur obj where obj.liste = ?1 ORDER BY obj.eingereichtePosition<br>
      * SQL: <br>
      */
     public java.util.List<Listenkandidatur> findByListeOrderByEingereichtePosition( @javax.annotation.Nonnull de.ivu.elect.business.wahlvorschlag.entity.Liste liste) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.wahlvorschlag.entity.Liste listeCheckedAndAttached =  checkAndAttach(liste, de.ivu.elect.business.wahlvorschlag.entity.Liste.class, "liste"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("LISTENKANDIDATUR.ListeOrderByEingereichtePosition")
                              .setParameter(1, listeCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Listenkandidatur, findByListeOrderByEingereichtePosition - NonUniqueResultException, Parameter: "
           + ", liste:'"+liste+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: ListeOrderByRelativePositionNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM Listenkandidatur obj where obj.liste = ?1 ORDER BY obj.relativePosition<br>
      * SQL: <br>
      */
     public java.util.List<Listenkandidatur> findByListeOrderByRelativePosition( @javax.annotation.Nonnull de.ivu.elect.business.wahlvorschlag.entity.Liste liste) {
            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.wahlvorschlag.entity.Liste listeCheckedAndAttached =  checkAndAttach(liste, de.ivu.elect.business.wahlvorschlag.entity.Liste.class, "liste"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("LISTENKANDIDATUR.ListeOrderByRelativePosition")
                              .setParameter(1, listeCheckedAndAttached)
                                                                      .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("findByListeOrderByRelativePosition - 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("Listenkandidatur, findByListeOrderByRelativePosition - NonUniqueResultException, Parameter: "
           + ", liste:'"+liste+"'"
    
        );
    throw nure;
            }
            }

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

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

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.kandidatur.entity.ListenkandidaturUK uk,
      de.ivu.elect.business.person.entity.PersonendatenPK
    pk

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

    public de.ivu.elect.business.kandidatur.entity.Listenkandidatur findByUK(de.ivu.elect.business.kandidatur.entity.ListenkandidaturUK uk){
        throw new UnsupportedOperationException("Kein UK definiert");
    }


}