SpringSide3 PropertyFilter介紹

PropertyFilter是SpringSide框架中封裝的一個類,可在頁面中簡單定義filter_EQS_name,filter_LIKES_NAME_OR_LOGIN_NAME的請求參數,可通過HibernateWebUtils的 buildPropertyFilter(ServletRequest)函數快速構造出PropertiyFilter列表並傳遞到 HibernateDAO的search(List<PropertyFilter>)方法中,整個過程非常自動化,無需太多的特定編碼,filter的命名規則也一目瞭然。其中比較類型可選 EQ(=), LIKE(模糊匹配), LT(<), GT(>), LE(<=), GE(>=);
比較值類型可選S(String.class), I(Integer.class), L(Long.class), N(Double.class), D(Date.class),B(Boolean.class);
如果要同時比較多個屬性,屬性名之間用_OR_分隔。
源碼如下:
/**
 * Copyright (c) 2005-2009 springside.org.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: PropertyFilter.java 873 2010-01-18 16:38:24Z calvinxiu $
 */
package org.springside.modules.orm;

import java.util.Date;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;
import org.springside.modules.utils.ReflectionUtils;

/**
 * 與具體ORM實現無關的屬性過濾條件封裝類.
 * 
 * PropertyFilter主要記錄頁面中簡單的搜索過濾條件,比Hibernate的Criterion要簡單.
 * 
 * @author calvin
 */
public class PropertyFilter {

/** 多個屬性間OR關係的分隔符. */
public static final String OR_SEPARATOR = "_OR_";

/** 屬性比較類型. */
public enum MatchType {
EQ, LIKE, LT, GT, LE, GE;
}

/** 屬性數據類型. */
public enum PropertyType {
S(String.class), I(Integer.class), L(Long.class), N(Double.class), D(Date.class), B(Boolean.class);

private Class<?> clazz;

PropertyType(Class<?> clazz) {
this.clazz = clazz;
}

public Class<?> getValue() {
return clazz;
}
}

private String[] propertyNames = null;
private Class<?> propertyType = null;
private Object propertyValue = null;
private MatchType matchType = null;

public PropertyFilter() {
}

/**
* @param filterName 比較屬性字符串,含待比較的比較類型、屬性值類型及屬性列表. 
*                   eg. LIKES_NAME_OR_LOGIN_NAME
* @param value 待比較的值.
*/
public PropertyFilter(final String filterName, final String value) {

String matchTypeStr = StringUtils.substringBefore(filterName, "_");
String matchTypeCode = StringUtils.substring(matchTypeStr, 0, matchTypeStr.length() - 1);
String propertyTypeCode = StringUtils.substring(matchTypeStr, matchTypeStr.length() - 1, matchTypeStr.length());
try {
matchType = Enum.valueOf(MatchType.class, matchTypeCode);
} catch (RuntimeException e) {
throw new IllegalArgumentException("filter名稱" + filterName + "沒有按規則編寫,無法得到屬性比較類型.", e);
}

try {
propertyType = Enum.valueOf(PropertyType.class, propertyTypeCode).getValue();
} catch (RuntimeException e) {
throw new IllegalArgumentException("filter名稱" + filterName + "沒有按規則編寫,無法得到屬性值類型.", e);
}

String propertyNameStr = StringUtils.substringAfter(filterName, "_");
propertyNames = StringUtils.split(propertyNameStr, PropertyFilter.OR_SEPARATOR);

Assert.isTrue(propertyNames.length > 0, "filter名稱" + filterName + "沒有按規則編寫,無法得到屬性名稱.");
//按entity property中的類型將字符串轉化爲實際類型.
this.propertyValue = ReflectionUtils.convertStringToObject(value, propertyType);
}

/**
* 是否比較多個屬性.
*/
public boolean isMultiProperty() {
return (propertyNames.length > 1);
}

/**
* 獲取比較屬性名稱列表.
*/
public String[] getPropertyNames() {
return propertyNames;
}

/**
* 獲取唯一的比較屬性名稱.
*/
public String getPropertyName() {
if (propertyNames.length > 1) {
throw new IllegalArgumentException("There are not only one property");
}
return propertyNames[0];
}

/**
* 獲取比較值.
*/
public Object getPropertyValue() {
return propertyValue;
}

/**
* 獲取比較值的類型.
*/
public Class<?> getPropertyType() {
return propertyType;
}

/**
* 獲取比較方式類型.
*/
public MatchType getMatchType() {
return matchType;
}
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章