package net.shangtech.framework.dao.hibernate;

import java.lang.reflect.ParameterizedType;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import net.shangtech.framework.dao.IBaseDao;
import net.shangtech.framework.dao.support.MapHolder;
import net.shangtech.framework.dao.support.Pagination;

import org.apache.commons.lang3.StringUtils;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.orm.hibernate4.HibernateCallback;
import org.springframework.orm.hibernate4.support.HibernateDaoSupport;
import org.springframework.util.CollectionUtils;
@SuppressWarnings("unchecked")
public class BaseDao<T> extends HibernateDaoSupport implements IBaseDao<T> {

	@Override
    public void save(T entity) {
	    getHibernateTemplate().save(entity);
    }

	@Override
    public void delete(long id) {
		getHibernateTemplate().delete(find(id));
    }

	@Override
    public void update(T entity) {
		getHibernateTemplate().update(entity);
    }

    @Override
    public T find(Long id) {
	    return (T) getHibernateTemplate().get(getEntityClass(), id);
    }

	@Override
    public List<T> findAll() {
	    return findAll(null);
    }
	
	public List<T> findAll(String orderBy){
		String queryString = "from " + getEntityClass().getSimpleName();
		if (StringUtils.isNotBlank(orderBy)) {
			queryString += " order by " + orderBy;
		}
		return (List<T>) getHibernateTemplate().find(queryString);
	}
	
	public List<?> findByProperties(MapHolder<String> holder){
		return findByProperties(holder, null);
	}
	
	public Object gatherByProperties(final String queryString, final Object...values){
		return getHibernateTemplate().execute(new HibernateCallback<Object>(){
			@Override
			public Object doInHibernate(Session session) throws HibernateException {
				Query query = session.createQuery(queryString);
				for(int i = 0; i < values.length; i++){
					query.setParameter(i, values[i]);
				}
				List<Object> list = query.list();
				if(CollectionUtils.isEmpty(list)){
					return null;
				}
				return list.get(0);
			}
		});
	}
	
	public List<?> findByProperties(MapHolder<String> holder, String orderBy){
		Map<String, Object> properties = holder.getMap();
		String queryString = "from " + getEntityClass().getSimpleName() + " where 1=1 ";
		List<Object> values = new ArrayList<Object>();
		for(Entry<String, Object> entry : properties.entrySet()){
			queryString += " and " + entry.getKey() + "=?";
		}
		if(StringUtils.isNotBlank(orderBy)){
			queryString += " order by " + orderBy;
		}
		return getHibernateTemplate().find(queryString, values.toArray());
	}
	
	public Pagination<T> findPageByProperties(MapHolder<String> holder, String orderBy, Pagination<T> page){
		
		return page;
	}
	
	private Class<?> getEntityClass() {
		Class<?> entityClass = (Class<?>) ((ParameterizedType) getClass()
				.getGenericSuperclass()).getActualTypeArguments()[0];
		return entityClass;
	}

}

@@ -2514,19 +2514,53 @@
 () + %22=?
+ 
 %22;%0A
+%09%09%09values.add(entry.getValue());%0A
 %09%09%7D%0A%09%09if
@@ -2820,18 +2820,857 @@
  page)%7B%0A
-
 %09%09
+String queryString = %22from %22 + getEntityClass().getSimpleName() + %22 where 1=1 %22;%0A%09%09String countString = %22select count(o) from %22 + getEntityClass().getSimpleName() + %22 where 1=1 %22;%0A%09%09String whereString = %22 where 1=1 %22;%0A%09%09List%3CObject%3E values = new ArrayList%3C%3E();%0A%09%09for(Entry%3CString, Object%3E entry : holder.getMap().entrySet())%7B%0A%09%09%09whereString = %22 and %22 + entry.getKey() + %22=? %22;%0A%09%09%09values.add(entry.getValue());%0A%09%09%7D%0A%09%09queryString += whereString;%0A%09%09countString += whereString;%0A%09%09if(StringUtils.isNoneBlank(orderBy))%7B%0A%09%09%09queryString += %22 order by %22 + orderBy;%0A%09%09%7D%0A%09%09List%3CT%3E list = (List%3CT%3E) getHibernateTemplate().find(countString, values.toArray());%0A%09%09page.setItems(list);%0A%09%09Object total = gatherByProperties(queryString, values.toArray());%0A%09%09if(total == null)%7B%0A%09%09%09page.setTotalCount(0);%0A%09%09%7D%0A%09%09else%7B%0A%09%09%09page.setTotalPage((Integer) total);%0A%09%09%7D
 %0A%09%09retur

improve base dao