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

package de.ivu.elect.business.ui.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.ui.entity.UITableColumnOrder;
import de.ivu.elect.business.ui.entity.UITableColumnOrderUK;

public abstract class AbstractUITableColumnOrderDAO extends de.ivu.elect.jee.entity.AbstractDAO<UITableColumnOrder,
        de.ivu.elect.business.ui.entity.UITableColumnOrderPK
    , UITableColumnOrderUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractUITableColumnOrderDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public UITableColumnOrder findByPrimaryKey(
        de.ivu.elect.business.ui.entity.UITableColumnOrderPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: BenutzerKeyAndColumnBuilderKeyNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT o FROM UITableColumnOrder o WHERE o.columnBuilderKey = ?2 AND o.benutzerKey=?1<br>
      * SQL: <br>
      */
     public java.util.List<UITableColumnOrder> findByBenutzerKeyAndColumnBuilderKey( @javax.annotation.Nonnull String benutzerKey,  @javax.annotation.Nonnull String columnBuilderKey) {
            long start = 0L;

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

    try {

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

          		  			  String benutzerKeyCheckedAndAttached =  checkAndAttach(benutzerKey, String.class, "benutzerKey"); ;
		  			  String columnBuilderKeyCheckedAndAttached =  checkAndAttach(columnBuilderKey, String.class, "columnBuilderKey"); ;
		  
                              return
                              getEntityManager().
                                createNamedQuery("U_I_TABLE_COLUMN_ORDER.BenutzerKeyAndColumnBuilderKey")
                              .setParameter(1, benutzerKeyCheckedAndAttached)
                                        .setParameter(2, columnBuilderKeyCheckedAndAttached)
                                                                      .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("findByBenutzerKeyAndColumnBuilderKey - 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("UITableColumnOrder, findByBenutzerKeyAndColumnBuilderKey - NonUniqueResultException, Parameter: "
           + ", benutzerKey:'"+benutzerKey+"'"
           + ", columnBuilderKey:'"+columnBuilderKey+"'"
    
        );
    throw nure;
            }
            }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.ui.entity.UITableColumnOrderUK uk,
        de.ivu.elect.business.ui.entity.UITableColumnOrderPK
      pk

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

    public de.ivu.elect.business.ui.entity.UITableColumnOrder findByUK(de.ivu.elect.business.ui.entity.UITableColumnOrderUK uk){
        throw new UnsupportedOperationException("Kein UK definiert");
    }


}