/*
 * 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.GeodataOrt;
import de.ivu.elect.business.geodata.entity.GeodataOrtUK;

public abstract class AbstractGeodataOrtDAO extends de.ivu.elect.jee.entity.AbstractDAO<GeodataOrt,
        de.ivu.elect.business.geodata.entity.GeodataOrtPK
    , GeodataOrtUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractGeodataOrtDAO() {
    super();
  }

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

    return false;
  }

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

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


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



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

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

    try {

            try {

          		  
                              return
                              getEntityManager().
                                createNamedQuery("GEODATA_ORT.All")
                                                            .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("GeodataOrt, 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: OrtsnameAndOrtsteilnameNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT obj FROM GeodataOrt obj WHERE (obj.ortsname IS NULL AND LENGTH(?1) = 0 OR UPPER(obj.ortsname) = UPPER(?1)) AND (obj.ortsteilname IS NULL AND LENGTH(?2) = 0 OR UPPER(obj.ortsteilname) = UPPER(?2))<br>
      * SQL: <br>
      */
     public java.util.List<GeodataOrt> findByOrtsnameAndOrtsteilname( @javax.annotation.Nonnull String ortsname,  @javax.annotation.Nonnull String ortsteilname) {
            long start = 0L;

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

    try {

            try {

          		  			  String ortsnameCheckedAndAttached =  checkAndAttach(ortsname, String.class, "ortsname"); ;
		  			  String ortsteilnameCheckedAndAttached =  checkAndAttach(ortsteilname, String.class, "ortsteilname"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("GEODATA_ORT.OrtsnameAndOrtsteilname")
                              .setParameter(1, ortsnameCheckedAndAttached)
                                        .setParameter(2, ortsteilnameCheckedAndAttached)
                                                                      .getResultList();
                                        
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("GeodataOrt, findByOrtsnameAndOrtsteilname - NonUniqueResultException, Parameter: "
           + ", ortsname:'"+ortsname+"'"
           + ", ortsteilname:'"+ortsteilname+"'"
    
        );
    throw nure;
        }

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



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

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

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

   public de.ivu.elect.business.geodata.entity.GeodataOrt findByUK(de.ivu.elect.business.geodata.entity.GeodataOrtUK uk){

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