package com.barcode.core.orm;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
/**
* 与具体ORM实现无关的属性过滤条件封装类 PropertyFilter主要记录页面中简单的搜索过滤条件,比Hibernate的Criterion要简单很多
* 可按项目扩展其他对比方式如大于、小于及其他数据类型如数字和日期
*
* @author Administrator
*
*/
public class PropertyFilter {
/**
* 多个属性间OR关系的分隔符:"__"
*/
public static final String OR_SEPARATOR = "__";
/**
* 时间格式化函数:"yyyy-MM-dd"
*/
public static final DateFormat dateFmter = new SimpleDateFormat(
"yyyy-MM-dd");
/**
* 时间格式化函数:"yyyy-MM-dd hh:mm"
*/
public static final DateFormat tsFmter = new SimpleDateFormat(
"yyyy-MM-dd hh:mm");
/**
* 属性比较类型
*
*/
public enum MatchType {
EQ, LIKE, GT, LT, GE, LE, FROM, TO, NOT, IN, NOTLIKE, NOTIN, IS_NULL, IS_NOT_NULL;
}
/**
* 属性类型
*/
public enum ValueType {
String, Integer, Double, Date, Timestamp;
}
/**
* 属性名
*/
private String propertyName;
/**
* 属性值
*/
private Object value;
/**
* 属性值的字符串
*/
private String strValue;
/**
* 比较类型:默认为MatchType.EQ
*/
private MatchType matchType = MatchType.EQ;
/**
* 属性类型
*/
private ValueType valueType;
/**
* 空构造函数
*/
public PropertyFilter() {
}
/**
* 根据属性名,属性值,比较类型构造过滤条件
*
* @param propertyName
* 属性名
* @param value
* 属性值
* @param matchType
* 比较类型
*/
public PropertyFilter(final String propertyName, final Object value,
final MatchType matchType) {
this.propertyName = propertyName;
this.value = value;
this.valueType = null;
this.matchType = matchType;
}
/**
* 根据属性名,属性值的字符串,属性类型,比较类型构造过滤条件
*
* @param propertyName
* 属性名
* @param strValue
* 属性值的字符串
* @param valueType
* 属性类型
* @param matchType
* 比较类型
*/
public PropertyFilter(final String propertyName, final String strValue,
final ValueType valueType, final MatchType matchType) {
this.propertyName = propertyName;
this.strValue = strValue;
this.valueType = valueType;
this.matchType = matchType;
}
/**
* 获取属性名称,可用'__'分隔多个属性,此时属性间是or的关系.
*
* @return 属性名称
*/
public String getPropertyName() {
// 将propertyName以_Begin或者_End结尾的字符去除
if (this.propertyName != null
&& (this.propertyName.endsWith("_Begin") || this.propertyName
.endsWith("_End"))) {
this.propertyName = this.propertyName.replaceAll(
"((_End)|(_Begin))$", "");
}
return propertyName;
}
/**
* 设置属性名称,可用'__'分隔多个属性,此时属性间是or的关系
*
* @param propertyName
* 属性名称
*/
public void setPropertyName(final String propertyName) {
this.propertyName = propertyName;
}
/**
* 得到属性的值,如果属性值为空则返回其对应字符串的值
*
* @return 属性的值
*/
public Object getValue() {
if (value == null && strValue != null && this.valueType != null) {
switch (this.valueType) {
case Integer:
if (strValue.matches("^-?\\d+$")) {
value = Integer.valueOf(strValue);
}
break;
case String:
value = strValue;
break;
case Double:
if (strValue.matches("^-?\\d+(\\.\\d+)?$")) {
value = Double.valueOf(strValue);
}
break;
case Date:
if (strValue.matches("^\\d{4,4}-\\d{1,2}-\\d{1,2}$")) {
try {
value = dateFmter.parse(strValue);
} catch (ParseException e) {
throw new IllegalArgumentException("过滤条件" + strValue
+ "不符合yyyy-MM-dd形式");
}
}
break;
case Timestamp:
if (strValue
.matches("^\\d{4,4}-\\d{1,2}-\\d{1,2} \\d{1,2}:\\d{1,2}$")) {
try {
value = new Timestamp(tsFmter.parse(strValue).getTime());
} catch (ParseException e) {
throw new IllegalArgumentException("过滤条件" + strValue
+ "不符合yyyy-MM-dd hh:mm形式");
}
} else if (strValue.matches("^\\d{4,4}-\\d{1,2}-\\d{1,2}$")) {
try {
value = dateFmter.parse(strValue);
} catch (ParseException e) {
throw new IllegalArgumentException("过滤条件" + strValue
+ "不符合yyyy-MM-dd形式");
}
}
break;
default:
break;
}
}
// 如果属性值为空则返回其对应字符串的值
return value == null ? strValue : value;
}
/**
* 设置属性值
*
* @param value
* 属性值
*/
public void setValue(final Object value) {
this.value = value;
}
/**
* 得到比较类型,MatchType.FROM等价于MatchType.GE,MatchType.TO等价于MatchType.LE
*
* @return 比较类型
*
*/
public MatchType getMatchType() {
if (this.matchType == MatchType.FROM) {
return MatchType.GE;
}
if (this.matchType == MatchType.TO) {
return MatchType.LE;
}
return matchType;
}
/**
* 设置比较类型
*
* @param matchType
* 比较类型
*/
public void setMatchType(final MatchType matchType) {
this.matchType = matchType;
}
/**
* 得到属性类型
*
* @return 属性类型
*/
public ValueType getValueType() {
return valueType;
}
/**
* 设置属性类型
*
* @param valueType
* 属性类型
*/
public void setValueType(ValueType valueType) {
this.valueType = valueType;
}
/**
* 得到属性值的字符串
*
* @return 属性值的字符串
*/
public String getStrValue() {
return strValue;
}
/**
* 设置属性值的字符串
*
* @param strValue
* 属性值的字符串
*/
public void setStrValue(String strValue) {
this.strValue = strValue;
}
}
替代hibernate的cretira的方法
猜你喜欢
转载自blog.csdn.net/sinat_31908303/article/details/52856927
今日推荐
周排行