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

package de.ivu.elect.business.system.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.system.entity.SystemLog;
import de.ivu.elect.business.system.entity.SystemLogUK;

public abstract class AbstractSystemLogDAO extends de.ivu.elect.jee.entity.AbstractDAO<SystemLog,
        de.ivu.elect.business.system.entity.SystemLogPK
    , SystemLogUK> {

  private static final long serialVersionUID = 1L;

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

  public AbstractSystemLogDAO() {
    super();
  }

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

    return false;
  }

  @Override
  public SystemLog findByPrimaryKey(
        de.ivu.elect.business.system.entity.SystemLogPK
      pk) {
    if (pk != null && pk.getId() != null) {
      return findByPrimaryKey(pk.getId());
    }

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


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



     /**
      * Name: AllNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: DELETE FROM SystemLog o WHERE o.mandant=?1<br>
      * SQL: <br>
      */
     public Integer deleteAll( @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 {

            try {

          		  			  de.ivu.elect.business.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("SYSTEM_LOG.All")
                              .setParameter(1, mandantCheckedAndAttached)
                                                                                .executeUpdate();
          
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("SystemLog, deleteAll - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
    
        );
    throw nure;
        }

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

     /**
      * Name: CountStatusArtNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT COUNT(o) FROM SystemLog o WHERE o.mandant=?1 AND o.status = ?2 AND o.logNachrichtArt=?3<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountStatusArt( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.system.entity.SystemLogStatus status,  @javax.annotation.Nonnull de.ivu.elect.business.system.entity.LogNachrichtArt art) {
            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"); ;
		  			  de.ivu.elect.business.system.entity.SystemLogStatus statusCheckedAndAttached =  checkAndAttach(status, de.ivu.elect.business.system.entity.SystemLogStatus.class, "status"); ;
		  			  de.ivu.elect.business.system.entity.LogNachrichtArt artCheckedAndAttached =  checkAndAttach(art, de.ivu.elect.business.system.entity.LogNachrichtArt.class, "art"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("SYSTEM_LOG.CountStatusArt")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, statusCheckedAndAttached)
                                        .setParameter(3, artCheckedAndAttached)
                                                                      .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("findByCountStatusArt - 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("SystemLog, findByCountStatusArt - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", status:'"+status+"'"
           + ", art:'"+art+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: CountStatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: SELECT COUNT(o) FROM SystemLog o WHERE o.mandant=?1 AND o.status = ?2<br>
      * SQL: <br>
      */
     public java.lang.Long findByCountStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.system.entity.SystemLogStatus status) {
            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"); ;
		  			  de.ivu.elect.business.system.entity.SystemLogStatus statusCheckedAndAttached =  checkAndAttach(status, de.ivu.elect.business.system.entity.SystemLogStatus.class, "status"); ;
		  
                              return
                    (java.lang.Long)
                              getEntityManager().
                                createNamedQuery("SYSTEM_LOG.CountStatus")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, statusCheckedAndAttached)
                                                                      .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("findByCountStatus - 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("SystemLog, findByCountStatus - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", status:'"+status+"'"
    
        );
    throw nure;
            }
            }

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

     /**
      * Name: StatusNoFlush<br>
      * Beschreibung: <br>
      * EJBQL: UPDATE SystemLog o SET o.status = ?2 WHERE o.mandant=?1<br>
      * SQL: <br>
      */
     public Integer updateStatus( @javax.annotation.Nonnull de.ivu.elect.business.mandant.entity.Mandant mandant,  @javax.annotation.Nonnull de.ivu.elect.business.system.entity.SystemLogStatus status) {
            long start = 0L;

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

    try {

            try {

          		  			  de.ivu.elect.business.mandant.entity.Mandant mandantCheckedAndAttached =  checkAndAttach(mandant, de.ivu.elect.business.mandant.entity.Mandant.class, "mandant"); ;
		  			  de.ivu.elect.business.system.entity.SystemLogStatus statusCheckedAndAttached =  checkAndAttach(status, de.ivu.elect.business.system.entity.SystemLogStatus.class, "status"); ;
		  
                              return
                    (Integer)
                              getEntityManager().
                                createNamedQuery("SYSTEM_LOG.Status")
                              .setParameter(1, mandantCheckedAndAttached)
                                        .setParameter(2, statusCheckedAndAttached)
                                                                      .executeUpdate();
                    
                    
        } catch (NonUniqueResultException nure) {
    LOG.warn("SystemLog, updateStatus - NonUniqueResultException, Parameter: "
           + ", mandant:'"+mandant+"'"
           + ", status:'"+status+"'"
    
        );
    throw nure;
        }

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



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

  @Override
  public boolean isEntityWithUKHasPK(de.ivu.elect.business.system.entity.SystemLogUK uk,
        de.ivu.elect.business.system.entity.SystemLogPK
      pk

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

    public de.ivu.elect.business.system.entity.SystemLog findByUK(de.ivu.elect.business.system.entity.SystemLogUK uk){
        throw new UnsupportedOperationException("Kein UK definiert");
    }


}