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

package de.ivu.elect.business.geodata.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.geodata.entity.GeodataPlz;
import de.ivu.elect.business.geodata.entity.GeodataPlzUK;

public abstract class AbstractGeodataPlzDAO extends de.ivu.elect.jee.entity.AbstractDAO<GeodataPlz,
        de.ivu.elect.business.geodata.entity.GeodataPlzPK
    , GeodataPlzUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractGeodataPlzDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public GeodataPlz findByPrimaryKey(
        de.ivu.elect.business.geodata.entity.GeodataPlzPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM GeodataPlz obj<br>
      * SQL: <br>
      */
     public java.util.List<GeodataPlz> findByAll() {
            long start = 0L;

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

    try {

            try {

          		  
                              return
                              getEntityManager().
                                createNamedQuery("GEODATA_PLZ.All")
                                                            .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("GeodataPlz, 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: PlzNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM GeodataPlz obj WHERE UPPER(obj.plz) = UPPER(?1)<br>
      * SQL: <br>
      */
     public GeodataPlz findByPlz( @javax.annotation.Nonnull String plz) {
            long start = 0L;

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

    try {

            try {

          		  			  String plzCheckedAndAttached =  checkAndAttach(plz, String.class, "plz"); ;
		  
                              return
                    (GeodataPlz)
                              getEntityManager().
                                createNamedQuery("GEODATA_PLZ.Plz")
                              .setParameter(1, plzCheckedAndAttached)
                                                                      .getSingleResult();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("GeodataPlz, findByPlz - NonUniqueResultException, Parameter: "
           + ", plz:'"+plz+"'"
    
        );
    throw nure;
        }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.geodata.entity.GeodataPlzUK uk,
        de.ivu.elect.business.geodata.entity.GeodataPlzPK
      pk

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

   public de.ivu.elect.business.geodata.entity.GeodataPlz findByUK(de.ivu.elect.business.geodata.entity.GeodataPlzUK uk){

    try {
     return (de.ivu.elect.business.geodata.entity.GeodataPlz) getEntityManager().createNamedQuery("GEODATA_PLZ.UK")
                  .setParameter(1, uk
                .getPlz ()  
              )
          .getSingleResult()
     ;
     } catch (NonUniqueResultException nure) {
        LOG.warn("GeodataPlz, findByUK - NonUniqueResultException, UK: " + uk);
        throw nure;
     }
  }
    
}