package com.jgeppert.struts2.jquery.grid.showcase.dao;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.Hibernate;
import org.hibernate.HibernateException;
import org.hibernate.SQLQuery;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Projections;

import com.jgeppert.struts2.jquery.grid.showcase.model.Customers;

public class CustomersDao extends AbstractSimpleGenericDao<Customers, Integer> {

  private static final Log log = LogFactory.getLog(CustomersDao.class);

  @SuppressWarnings("unchecked")
  public List<Customers> findByCriteria(DetachedCriteria dc, int from, int size)
  {
    if (log.isDebugEnabled()) log.debug("Return Customers from " + from + " to " + size);

    try
    {
      Criteria criteria = dc.getExecutableCriteria(hSession);
      criteria.setFirstResult(from);
      criteria.setMaxResults(size);
      return criteria.list();
    }
    catch (HibernateException e)
    {
      log.error(e.getMessage(), e);
      throw e;
    }
  }

  public int countByCriteria(DetachedCriteria dc)
  {
    if (log.isDebugEnabled()) log.debug("count Customers");

    try
    {
      Criteria criteria = dc.getExecutableCriteria(hSession);
      criteria.setProjection(Projections.rowCount());
      return ((Long) criteria.list().get(0)).intValue();
    }
    catch (HibernateException e)
    {
      log.error(e.getMessage(), e);
      throw e;
    }
  }

  @SuppressWarnings("unchecked")
  public List<String> findCountrys()
  {
    if (log.isDebugEnabled()) log.debug("find all countrys");

    try
    {
      String queryString = "SELECT DISTINCT c.country FROM CLASSICMODELS.CUSTOMERS c where c.country is not null";
      SQLQuery q = hSession.createSQLQuery(queryString);
      q.setCacheable(true);
      q.addScalar("country", Hibernate.STRING);
      return q.list();
    }
    catch (HibernateException e)
    {
      log.error(e.getMessage(), e);
      throw e;
    }
  }

  public int nextCustomerNumber()
  {
    if (log.isDebugEnabled()) log.debug("find next customer number");

    try
    {
      DetachedCriteria dc = DetachedCriteria.forClass(Customers.class);
      Criteria criteria = dc.getExecutableCriteria(hSession);
      criteria.setProjection(Projections.max("customernumber"));
      return ((Integer) criteria.list().get(0)).intValue() + 1;
    }
    catch (HibernateException e)
    {
      log.error(e.getMessage(), e);
      throw e;
    }
  }
}

@@ -200,41 +200,8 @@
 a;%0D%0A
-import org.hibernate.Hibernate;%0D%0A
 impo
@@ -364,16 +364,55 @@
 ctions;%0D
+%0Aimport org.hibernate.type.StringType;%0D
 %0A%0D%0Aimpor
@@ -1968,24 +1968,27 @@
 y%22, 
-Hibernate.STRING
+StringType.INSTANCE
 );%0D%0A

Fix deprecated warning for Hibernate.STRING