package com.htsoft.core.command;
import org.hibernate.Criteria;
public interface CriteriaCommand {
public static final String SORT_DESC = "desc";
public static final String SORT_ASC = "asc";
public Criteria execute(Criteria paramCriteria);
}
package com.htsoft.core.command;
import org.hibernate.Criteria;
import org.hibernate.criterion.Example;
public class ExampleCommandImpl implements CriteriaCommand {
private Object pojoExample = null;
public void setPojoExample(Object pojoEx) {
this.pojoExample = pojoEx;
}
public ExampleCommandImpl(Object pojoExample) {
this.pojoExample = pojoExample;
}
public Criteria execute(Criteria criteria) {
if (this.pojoExample != null) {
Example exampleRestriction = Example.create(this.pojoExample);
criteria.add(exampleRestriction);
}
return criteria;
}
}
package com.htsoft.core.command;
import java.util.List;
import java.util.Set;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Criteria;
import org.hibernate.criterion.Restrictions;
import org.hibernate.criterion.SimpleExpression;
/**
* 对HQL语句中传进来的参数进行判断
* @author Administrator
*
*/
public class FieldCommandImpl implements CriteriaCommand {
private static Log logger = LogFactory.getLog(CriteriaCommand.class);
private String property;
private Object value;
private String operation;
private QueryFilter filter;
public FieldCommandImpl(String property, Object value, String operation,
QueryFilter filter) {
this.property = property;
this.value = value;
this.operation = operation;
this.filter = filter;
}
public String getProperty() {
return this.property;
}
public void setProperty(String property) {
this.property = property;
}
public Object getValue() {
return this.value;
}
public void setValue(Object value) {
this.value = value;
}
public String getOperation() {
return this.operation;
}
public void setOperation(String operation) {
this.operation = operation;
}
public Criteria execute(Criteria criteria) {
String[] propertys = this.property.split("[.]");
if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))) {
for (int i = 0; i < propertys.length - 1; ++i) {
if (!this.filter.getAliasSet().contains(propertys[i])) {
criteria.createAlias(propertys[i], propertys[i]);
this.filter.getAliasSet().add(propertys[i]);
}
}
}
if ("LT".equals(this.operation))
criteria.add(Restrictions.lt(this.property, this.value));
else if ("GT".equals(this.operation))
criteria.add(Restrictions.gt(this.property, this.value));
else if ("LE".equals(this.operation))
criteria.add(Restrictions.le(this.property, this.value));
else if ("GE".equals(this.operation))
criteria.add(Restrictions.ge(this.property, this.value));
else if ("LK".equals(this.operation))
criteria.add(Restrictions.like(this.property,
"%" + this.value + "%").ignoreCase());
else if ("LFK".equals(this.operation))
criteria.add(Restrictions.like(this.property, this.value + "%")
.ignoreCase());
else if ("RHK".equals(this.operation))
criteria.add(Restrictions.like(this.property, "%" + this.value)
.ignoreCase());
else if ("NULL".equals(this.operation))
criteria.add(Restrictions.isNull(this.property));
else if ("NOTNULL".equals(this.operation))
criteria.add(Restrictions.isNotNull(this.property));
else if ("EMP".equals(this.operation))
criteria.add(Restrictions.isEmpty(this.property));
else if ("NOTEMP".equals(this.operation))
criteria.add(Restrictions.isNotEmpty(this.property));
else if ("NEQ".equals(this.operation))
criteria.add(Restrictions.ne(this.property, this.value));
else {
criteria.add(Restrictions.eq(this.property, this.value));
}
return criteria;
}
public String getPartHql() {
String[] propertys = this.property.split("[.]");
if ((propertys != null) && (propertys.length > 1)
&& (!"vo".equals(propertys[0]))
&& (!this.filter.getAliasSet().contains(propertys[0]))) {
this.filter.getAliasSet().add(propertys[0]);
}
String partHql = "";
//小于
if ("LT".equals(this.operation)) {
partHql = this.property + " < ? ";
this.filter.getParamValueList().add(this.value);
//大于
} else if ("GT".equals(this.operation)) {
partHql = this.property + " > ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LE".equals(this.operation)) {
partHql = this.property + " <= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("GE".equals(this.operation)) {
partHql = this.property + " >= ? ";
this.filter.getParamValueList().add(this.value);
} else if ("LK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(
"%" + this.value.toString() + "%");
} else if ("LFK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add(this.value.toString() + "%");
} else if ("RHK".equals(this.operation)) {
partHql = this.property + " like ? ";
this.filter.getParamValueList().add("%" + this.value.toString());
} else if ("NULL".equals(this.operation)) {
partHql = this.property + " is null ";
} else if ("NOTNULL".equals(this.operation)) {
partHql = this.property + " is not null ";
} else if ((!"EMP".equals(this.operation))
&& (!"NOTEMP".equals(this.operation))) {
if ("NEQ".equals(this.operation)) {
partHql = this.property + " !=? ";
this.filter.getParamValueList().add(this.value);
} else {
partHql = partHql + this.property + " =? ";
this.filter.getParamValueList().add(this.value);
}
}
return partHql;
}
}
package com.htsoft.core.command;
import java.util.Set;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.Criteria;
import org.hibernate.criterion.Order;
public class SortCommandImpl implements CriteriaCommand {
private String sortName;
private String ascDesc;
private QueryFilter filter;
public Criteria execute(Criteria criteria) {
String[] propertys = this.sortName.split("[.]");
if ((propertys != null) && (propertys.length > 1)) {
for (int i = 0; i < propertys.length - 1; ++i) {
if (!this.filter.getAliasSet().contains(propertys[i])) {
criteria.createAlias(propertys[i], propertys[i]);
this.filter.getAliasSet().add(propertys[i]);
}
}
}
if ("desc".equalsIgnoreCase(this.ascDesc))
criteria.addOrder(Order.desc(this.sortName));
else if ("asc".equalsIgnoreCase(this.ascDesc)) {
criteria.addOrder(Order.asc(this.sortName));
}
return criteria;
}
public SortCommandImpl(String sortName, String ascDesc, QueryFilter filter) {
this.sortName = sortName;
this.ascDesc = ascDesc;
this.filter = filter;
}
public String getSortName() {
return this.sortName;
}
public void setSortName(String sortName) {
this.sortName = sortName;
}
public String getAscDesc() {
return this.ascDesc;
}
public void setAscDesc(String ascDesc) {
this.ascDesc = ascDesc;
}
public int hashCode() {
return new HashCodeBuilder(-82280557, -700257973).append(this.sortName)
.append(this.ascDesc).toHashCode();
}
public String getPartHql() {
return this.sortName + " " + this.ascDesc;
}
}
package com.htsoft.core.command;
import com.htsoft.core.util.ParamUtil;
import com.htsoft.core.web.paging.PagingBean;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
public class QueryFilter {
public static final Log logger = LogFactory.getLog(QueryFilter.class);
public static final String ORDER_DESC = "desc";
public static final String ORDER_ASC = "asc";
private HttpServletRequest request = null;
private String filterName = null;
private List<Object> paramValues = new ArrayList();
private List<CriteriaCommand> commands = new ArrayList();
private Set<String> aliasSet = new HashSet();
private PagingBean pagingBean = null;
public String getFilterName() {
return this.filterName;
}
public void setFilterName(String filterName) {
this.filterName = filterName;
}
public PagingBean getPagingBean() {
return this.pagingBean;
}
public QueryFilter(HttpServletRequest request) {
this.request = request;
Enumeration paramEnu = request.getParameterNames();
while (paramEnu.hasMoreElements()) {
String paramName = (String) paramEnu.nextElement();
if (paramName.startsWith("Q_")) {
String paramValue = request.getParameter(paramName);
addFilter(paramName, paramValue);
}
}
Integer start = Integer.valueOf(0);
Integer limit = PagingBean.DEFAULT_PAGE_SIZE;
String s_start = request.getParameter("start");
String s_limit = request.getParameter("limit");
if (StringUtils.isNotEmpty(s_start)) {
start = new Integer(s_start);
}
if (StringUtils.isNotEmpty(s_limit)) {
limit = new Integer(s_limit);
}
String sort = request.getParameter("sort");
String dir = request.getParameter("dir");
if ((StringUtils.isNotEmpty(sort)) && (StringUtils.isNotEmpty(dir))) {
addSorted(sort, dir);
}
this.pagingBean = new PagingBean(start.intValue(), limit.intValue());
}
public void addFilter(String paramName, String paramValue) {
String[] fieldInfo = paramName.split("[_]");
Object value = null;
if ((fieldInfo != null) && (fieldInfo.length == 4)) {
value = ParamUtil.convertObject(fieldInfo[2], paramValue);
if (value != null) {
FieldCommandImpl fieldCommand = new FieldCommandImpl(
fieldInfo[1], value, fieldInfo[3], this);
this.commands.add(fieldCommand);
}
} else if ((fieldInfo != null) && (fieldInfo.length == 3)) {
FieldCommandImpl fieldCommand = new FieldCommandImpl(fieldInfo[1],
value, fieldInfo[2], this);
this.commands.add(fieldCommand);
} else {
logger.error("Query param name [" + paramName
+ "] is not right format.");
}
}
public void addParamValue(Object value) {
this.paramValues.add(value);
}
public List getParamValueList() {
return this.paramValues;
}
public void addSorted(String orderBy, String ascDesc) {
this.commands.add(new SortCommandImpl(orderBy, ascDesc, this));
}
public void addExample(Object object) {
this.commands.add(new ExampleCommandImpl(object));
}
public List<CriteriaCommand> getCommands() {
return this.commands;
}
public Set<String> getAliasSet() {
return this.aliasSet;
}
}
package com.htsoft.core.command;
import com.htsoft.core.util.ParamUtil;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
public class ReportFilter {
Map<String, Object> variables = new HashMap<String, Object>();
public ReportFilter() {
}
public ReportFilter(HttpServletRequest request) {
Enumeration paramEnu = request.getParameterNames();
while (paramEnu.hasMoreElements()) {
String paramName = (String) paramEnu.nextElement();
if (paramName.startsWith("Q_")) {
String paramValue = request.getParameter(paramName);
addFilter(paramName, paramValue);
}
}
}
public void addFilter(String paramName, String value) {
String[] fieldInfo = paramName.split("[_]");
if (fieldInfo.length == 3)
this.variables.put(fieldInfo[1], ParamUtil.convertObject(
fieldInfo[2], value));
}
public Map<String, Object> getVariables() {
return this.variables;
}
public void setVariables(Map<String, Object> variables) {
this.variables = variables;
}
}
package com.htsoft.core.dao;
public interface BaseDao<T> extends GenericDao<T, Long> {
}
package com.htsoft.core.dao;
import com.htsoft.core.command.QueryFilter;
import com.htsoft.core.web.paging.PagingBean;
import java.io.Serializable;
import java.util.List;
public interface GenericDao<T, PK extends Serializable> {
public T save(T paramT);
public T merge(T paramT);
public T get(PK paramPK);
public void remove(PK paramPK);
public void remove(T paramT);
public void evict(T paramT);
public List<T> getAll();
public List<T> getAll(PagingBean paramPagingBean);
public List<T> getAll(QueryFilter paramQueryFilter);
public List<T> findByHql(String paramString, Object[] paramArrayOfObject);
public List<T> findByHql(String paramString, Object[] paramArrayOfObject,
PagingBean paramPagingBean);
public List<T> findByHql(String paramString, Object[] paramArrayOfObject,
int paramInt1, int paramInt2);
public void flush();
}
package com.htsoft.core.dao.impl;
import com.htsoft.core.dao.BaseDao;
public class BaseDaoImpl<T> extends GenericDaoImpl<T, Long> implements
BaseDao<T> {
public BaseDaoImpl(Class persistType) {
super(persistType);
}
}
本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。