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

package de.ivu.elect.business.konfiguration.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.konfiguration.entity.Konfiguration;
import de.ivu.elect.business.konfiguration.entity.KonfigurationUK;

public abstract class AbstractKonfigurationDAO extends de.ivu.elect.jee.entity.AbstractDAO<Konfiguration,
        de.ivu.elect.business.konfiguration.entity.KonfigurationPK
    , KonfigurationUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractKonfigurationDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public Konfiguration findByPrimaryKey(
        de.ivu.elect.business.konfiguration.entity.KonfigurationPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT k FROM Konfiguration k<br>
      * SQL: <br>
      */
     public java.util.List<Konfiguration> findByAll() {
            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("KONFIGURATION.All")
                                                            .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("Konfiguration, findByAll - NonUniqueResultException, Parameter: "
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: MandantNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT OBJECT(o) FROM Konfiguration o WHERE o.mandant=?1<br>
      * SQL: <br>
      */
     public java.util.List<Konfiguration> findByMandant( @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("KONFIGURATION.Mandant")
                              .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("findByMandant - 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("Konfiguration, findByMandant - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: MandantSchluesselNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT k FROM Konfiguration k WHERE k.mandant=?1 AND k.schluessel = ?2<br>
      * SQL: <br>
      */
     public Konfiguration findByMandantSchluessel( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull java.lang.String schluessel) {
            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.lang.String schluesselCheckedAndAttached =  schluessel ;
		  
                              return
                    (Konfiguration)
                              getEntityManager().
                                createNamedQuery("KONFIGURATION.MandantSchluessel")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, schluesselCheckedAndAttached)
                                                                      .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("findByMandantSchluessel - 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("Konfiguration, findByMandantSchluessel - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", schluessel:'"+schluessel+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: SystemSchluesselNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT k FROM Konfiguration k WHERE k.schluessel = ?1<br>
      * SQL: <br>
      */
     public Konfiguration findBySystemSchluessel( @javax.annotation.Nonnull java.lang.String schluessel) {
            long start = 0L;

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

    try {

        int versuch = 0;
      while (true) {
        versuch++;
                try {

          		  			  java.lang.String schluesselCheckedAndAttached =  schluessel ;
		  
                              return
                    (Konfiguration)
                              getEntityManager().
                                createNamedQuery("KONFIGURATION.SystemSchluessel")
                              .setParameter(1, schluesselCheckedAndAttached)
                                                                      .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("findBySystemSchluessel - 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("Konfiguration, findBySystemSchluessel - NonUniqueResultException, Parameter: "
           + ", schluessel:'"+schluessel+"'"
    
        );
    throw nure;
            }
            }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.konfiguration.entity.KonfigurationUK uk,
        de.ivu.elect.business.konfiguration.entity.KonfigurationPK
      pk

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

   public de.ivu.elect.business.konfiguration.entity.Konfiguration findByUK(de.ivu.elect.business.konfiguration.entity.KonfigurationUK uk){

    try {
     return (de.ivu.elect.business.konfiguration.entity.Konfiguration) getEntityManager().createNamedQuery("KONFIGURATION.UK")
                  .setParameter(1, uk
                .getSchluessel ()  
              )
             .setParameter(2, uk
                .getMandantUK ()  
                .getNummer ()  
              )
          .getSingleResult()
     ;
     } catch (NonUniqueResultException nure) {
        LOG.warn("Konfiguration, findByUK - NonUniqueResultException, UK: " + uk);
        throw nure;
     }
  }
    
}