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

public abstract class AbstractWahlartDAO extends de.ivu.elect.jee.entity.AbstractDAO<Wahlart,
        de.ivu.elect.business.wahl.entity.WahlartPK
    , WahlartUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractWahlartDAO() {
    super();
  }

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

    return false;
  }

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

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


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



     /**
      * Name: AllOrderByNameNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahlart o WHERE o.mandant=?1 ORDER BY o.name<br>
      * SQL: <br>
      */
     public java.util.List<Wahlart> findByAllOrderByName( @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("WAHLART.AllOrderByName")
                              .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("findByAllOrderByName - 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("Wahlart, findByAllOrderByName - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: NameNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM Wahlart o WHERE o.mandant=?1 AND o.name=?2<br>
      * SQL: <br>
      */
     public Wahlart 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
                    (Wahlart)
                              getEntityManager().
                                createNamedQuery("WAHLART.Name")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, nameCheckedAndAttached)
                                                                      .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("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("Wahlart, 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");
    }
    }
     }



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

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

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

   public de.ivu.elect.business.wahl.entity.Wahlart findByUK(de.ivu.elect.business.wahl.entity.WahlartUK uk){

    try {
     return (de.ivu.elect.business.wahl.entity.Wahlart) getEntityManager().createNamedQuery("WAHLART.UK")
                  .setParameter(1, uk
                .getName ()

              )
             .setParameter(2, uk
                .getMandantUK ()

                .getNummer ()

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