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

package de.ivu.elect.business.person.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.person.entity.Personendaten;
import de.ivu.elect.business.person.entity.PersonendatenUK;

public abstract class AbstractPersonendatenDAO extends de.ivu.elect.jee.entity.AbstractDAO<Personendaten,
        de.ivu.elect.business.person.entity.PersonendatenPK
    , PersonendatenUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractPersonendatenDAO() {
    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 Personendaten 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 Personendaten findByPrimaryKey(Long id) {
    if (id == null) {
      throw new IllegalArgumentException("ID ist null! [id = " + id + "]");
    }
  return getEntityManager().find(Personendaten.class, id, CACHE_PROPERTIES);
  }



     /**
      * Name: IdsNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.id IN ?1<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> 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("PERSONENDATEN.Ids")
                              .setParameter(1, idsCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, 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: PersonStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.personStatus = ?1<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> findByPersonStatus( @javax.annotation.Nonnull de.ivu.elect.business.person.entity.PersonStatus personStatus,  int limit) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.person.entity.PersonStatus personStatusCheckedAndAttached =  checkAndAttach(personStatus, de.ivu.elect.business.person.entity.PersonStatus.class, "personStatus"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.PersonStatus")
                              .setParameter(1, personStatusCheckedAndAttached)
                                                  .setMaxResults(limit)
                                        .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, findByPersonStatus - NonUniqueResultException, Parameter: "
           + ", personStatus:'"+personStatus+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlMitOrdnungsmerkmalNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.wahl=?1 AND o.externesOrdnungsmerkmal IS NOT NULL<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> findByWahlMitOrdnungsmerkmal( @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 {

            try {

          		  			  de.ivu.elect.business.wahl.entity.Wahl wahlCheckedAndAttached =  checkAndAttach(wahl, de.ivu.elect.business.wahl.entity.Wahl.class, "wahl"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.WahlMitOrdnungsmerkmal")
                              .setParameter(1, wahlCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, findByWahlMitOrdnungsmerkmal - NonUniqueResultException, Parameter: "
           + ", wahl:'"+wahl+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.wahl=?1<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> 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("PERSONENDATEN.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("Personendaten, 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");
    }
    }
     }

     /**
      * Name: WahlterminAndExterneIdNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.wahltermin = ?1 AND o.externeId IN ?2<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> findByWahlterminAndExterneId( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahltermin wahltermin,  @javax.annotation.Nonnull java.util.Collection<java.lang.String> externeIds) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.wahl.entity.Wahltermin wahlterminCheckedAndAttached =  checkAndAttach(wahltermin, de.ivu.elect.business.wahl.entity.Wahltermin.class, "wahltermin"); ;
		  			  java.util.Collection<java.lang.String> externeIdsCheckedAndAttached =  externeIds ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.WahlterminAndExterneId")
                              .setParameter(1, wahlterminCheckedAndAttached)
                                        .setParameter(2, externeIdsCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, findByWahlterminAndExterneId - NonUniqueResultException, Parameter: "
           + ", wahltermin:'"+wahltermin+"'"
           + ", externeIds:'"+externeIds+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlterminAndKontoverbindungIbanNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM Personendaten obj JOIN obj.kontoverbindung kv WHERE obj.wahltermin = ?1 AND kv.iban = ?2<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> findByWahlterminAndKontoverbindungIban( @javax.annotation.Nonnull de.ivu.elect.business.wahl.entity.Wahltermin wahltermin,  @javax.annotation.Nonnull java.lang.String iban) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.wahl.entity.Wahltermin wahlterminCheckedAndAttached =  checkAndAttach(wahltermin, de.ivu.elect.business.wahl.entity.Wahltermin.class, "wahltermin"); ;
		  			  java.lang.String ibanCheckedAndAttached =  iban ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.WahlterminAndKontoverbindungIban")
                              .setParameter(1, wahlterminCheckedAndAttached)
                                        .setParameter(2, ibanCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, findByWahlterminAndKontoverbindungIban - NonUniqueResultException, Parameter: "
           + ", wahltermin:'"+wahltermin+"'"
           + ", iban:'"+iban+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlterminIdsNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o.id FROM Personendaten o WHERE o.wahltermin=?1<br>
      * SQL: <br>
      */
     public java.util.List<java.lang.Long> findByWahlterminIds( @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 {

            try {

          		  			  de.ivu.elect.business.wahl.entity.Wahltermin wahlterminCheckedAndAttached =  checkAndAttach(wahltermin, de.ivu.elect.business.wahl.entity.Wahltermin.class, "wahltermin"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.WahlterminIds")
                              .setParameter(1, wahlterminCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, findByWahlterminIds - NonUniqueResultException, Parameter: "
           + ", wahltermin:'"+wahltermin+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: WahlterminNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Personendaten o WHERE o.wahltermin=?1<br>
      * SQL: <br>
      */
     public java.util.List<Personendaten> 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 {

            try {

          		  			  de.ivu.elect.business.wahl.entity.Wahltermin wahlterminCheckedAndAttached =  checkAndAttach(wahltermin, de.ivu.elect.business.wahl.entity.Wahltermin.class, "wahltermin"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("PERSONENDATEN.Wahltermin")
                              .setParameter(1, wahlterminCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("Personendaten, 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.person.entity.PersonendatenUK uk) {
    try {
      return findByUK(uk) != null;
    } catch (javax.persistence.NoResultException nre) {
      return false;
    }
  }

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.person.entity.PersonendatenUK 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.person.entity.Personendaten findByUK(de.ivu.elect.business.person.entity.PersonendatenUK uk){
        throw new UnsupportedOperationException("Kein UK definiert");
    }


}