package core.dao;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import javax.annotation.Resource;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.beanutils.PropertyUtils;
import org.apache.log4j.Logger;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Projections;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import core.extjs.ExtJSBaseParameter;
import core.support.BaseParameter;
import core.support.QueryResult;
import ypc.zwz.model.SysUser;
/**
* @author Yang Tian
* @email 1298588579@qq.com
*/
public class BaseDao<E> implements Dao<E> {
protected final Logger log = Logger.getLogger(BaseDao.class);
private static Map<String, Method> MAP_METHOD = new HashMap<String, Method>();
private SessionFactory sessionFactory;
public SessionFactory getSessionFactory() {
return sessionFactory;
}
public void setSessionFactory(SessionFactory sessionFactory) {
this.sessionFactory = sessionFactory;
}
public Session getSession() {
return sessionFactory.getCurrentSession();
}
@Resource(name = "sessionFactory")
public void setSF(SessionFactory sessionFactory) {
setSessionFactory(sessionFactory);
}
protected Class<E> entityClass;
public BaseDao(Class<E> entityClass) {
this.entityClass = entityClass;
}
public void persist(E entity) {
getSession().save(entity);
}
/**
* 获取属性类型(type),属性名(name),属性值(value)的map组成的list
* */
private ArrayList getFiledsInfo(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
ArrayList list = new ArrayList();
Map infoMap=null;
for(int i=0;i<fields.length;i++){
infoMap = new HashMap();
infoMap.put("type", fields[i].getType().toString());
infoMap.put("name", fields[i].getName());
infoMap.put("value", getFieldValueByName(fields[i].getName(), o));
list.add(infoMap);
}
return list;
}
/**
* 根据属性名获取属性值
* */
private Object getFieldValueByName(String fieldName, Object o) {
try {
String firstLetter = fieldName.substring(0, 1).toUpperCase();
String getter = "get" + firstLetter + fieldName.substring(1);
Method method = o.getClass().getMethod(getter, new Class[] {});
Object value = method.invoke(o, new Object[] {});
return value;
} catch (Exception e) {
log.error(e.getMessage(),e);
return null;
}
}
/**
* 获取属性名数组
* */
private String[] getFiledName(Object o){
Field[] fields=o.getClass().getDeclaredFields();
String[] fieldNames=new String[fields.length];
for(int i=0;i<fields.length;i++){
System.out.println(fields[i].getType());
fieldNames[i]=fields[i].getName();
}
return fieldNames;
}
public static java.sql.Date StringToDate(String sDate) {
/**
*str转date方法
*/
String str = sDate;
SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
java.util.Date d = null;
try {
d = format.parse(str);
} catch (Exception e) {
e.printStackTrace();
}
java.sql.Date date = new java.sql.Date(d.getTime());
return date;
}
public void updateQmm(Object entity){
String[] conditionName= {"id"};
Long[] conditionValue= new Long[1];
ArrayList<String> arrPropertyName=new ArrayList();
ArrayList<String> arrPropertyValue=new ArrayList();
ArrayList<String> arrPropertyType=new ArrayList();
ArrayList<Map> fieldsInfo=getFiledsInfo(entity);
for(int i=0;i<fieldsInfo.size();i++) {
Map m=(Map)fieldsInfo.get(i);
String type=(String)m.get("type");
String name=(String)m.get("name");
if(!name.equals("serialVersionUID")) {
if(m.get("value")!=null) {
String value=String.valueOf(m.get("value"));
if(((String)m.get("name")).equals("id")){
conditionValue[0]=Long.valueOf(value);
}else {
arrPropertyName.add(name);
arrPropertyValue.add(value);
arrPropertyType.add(type);
}
}
}
}
String[] propertyName= new String[arrPropertyName.size()];
Object[] propertyValue= new Object[arrPropertyValue.size()];
for(int i=0;i<arrPropertyName.size();i++) {
String name=(String)arrPropertyName.get(i);
String type=(String)arrPropertyType.get(i);
String value=(String)arrPropertyValue.get(i);
propertyName[i]=name;
if(type.equals("class java.lang.Long")){
propertyValue[i]=Long.valueOf(value);
}else
if(type.equals("class java.lang.String")){
propertyValue[i]=String.valueOf(value);
}else
if(type.equals("class java.sql.Date")){
propertyValue[i]=StringToDate((String)value);
}else
if(type.equals("class java.util.Date")){
propertyValue[i]=StringToDate((String)value);
}
}
updateByProperties(conditionName,conditionValue,propertyName,propertyValue);
}
public boolean deleteByPK(Serializable... id) {
boolean result = false;
if (id != null && id.length > 0) {
for (int i = 0; i < id.length; i++) {
E entity = get(id[i]);
if (entity != null) {
getSession().delete(entity);
result = true;
}
}
}
return result;
}
public void deleteByProperties(String[] propName, Object[] propValue) {
if (propName != null && propName.length > 0 && propValue != null && propValue.length > 0 && propValue.length == propName.length) {
StringBuffer sb = new StringBuffer("delete from " + entityClass.getName() + " o where 1=1 ");
appendQL(sb, propName, propValue);
Query query = getSession().createQuery(sb.toString());
setParameter(query, propName, propValue);
query.executeUpdate();
}
}
public void delete(E entity) {
getSession().delete(entity);
}
public void deleteByProperties(String propName, Object propValue) {
deleteByProperties(new String[] { propName }, new Object[] { propValue });
}
public void updateByProperties(String[] conditionName, Object[] conditionValue, String[] propertyName, Object[] propertyValue) {
if (propertyName != null && propertyName.length > 0 && propertyValue != null && propertyValue.length > 0 && propertyName.length == propertyValue.length && conditionValue != null && conditionValue.length > 0) {
StringBuffer sb = new StringBuffer();
sb.append("update " + entityClass.getName() + " o set ");
for (int i = 0; i < propertyName.length; i++) {
sb.append(propertyName[i] + " = :p_" + propertyName[i] + ",");
}
sb.deleteCharAt(sb.length() - 1);
sb.append(" where 1=1 ");
appendQL(sb, conditionName, conditionValue);
Query query = getSession().createQuery(sb.toString());
for (int i = 0; i < propertyName.length; i++) {
query.setParameter("p_" + propertyName[i], propertyValue[i]);
}
setParameter(query, conditionName, conditionValue);
query.executeUpdate();
} else {
throw new IllegalArgumentException("Method updateByProperties in BaseDao argument is illegal!");
}
}
public void updateByProperties(String[] conditionName, Object[] conditionValue, String propertyName, Object propertyValue) {
updateByProperties(conditionName, conditionValue, new String[] { propertyName }, new Object[] { propertyValu