Java tutorial
package com.bsi.summer.core.dao; /** * Copyright (c) 2005-20010 springside.org.cn * * Licensed under the Apache License, Version 2.0 (the "License"); * * $Id: PropertyFilter.java 1205 2010-09-09 15:12:17Z calvinxiu $ */ import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import org.apache.commons.lang.StringUtils; import org.springframework.util.Assert; import com.bsi.summer.core.common.QueryConstants; import com.bsi.summer.core.util.ConvertUtils; import com.bsi.summer.core.util.ServletUtils; /** * ORM??, ?????. * * @author calvin */ public class PropertyFilter { public static int indexOf(List<PropertyFilter> filters, PropertyFilter newFilter) { int i = -1; for (PropertyFilter filter : filters) { i++; if (filter.getPropertyName().equals(newFilter.getPropertyName())) { return i; } } return -1; } public static PropertyFilter create(String filterName, final String value) { return new PropertyFilter(filterName, value); } /** OR. */ public static final String OR_SEPARATOR = "_OR_"; /** . */ public enum MatchType { EQ, LIKE, LIKESTART, LIKEND, LT, GT, LE, GE, BETWEEN, NE, OR, NULL, NOTNULL, EMPTY, NOTEMPTY, IN; } /** * ? * @param zz * @return */ public static PropertyType getPropertyClass(Class<?> zz) { for (PropertyType type : PropertyType.values()) { if (type.getValue() == zz) { return type; } } return PropertyType.S; } /** * ?class * @param clazz * @return */ public static MatchType getMatchTypeByProType(Class<?> clazz) { PropertyType type = getPropertyClass(clazz); MatchType reType = MatchType.EQ; switch (type) { case S: reType = MatchType.LIKE; break; } return reType; } /** ?. */ public enum PropertyType { S(String.class), I(Integer.class), L(Long.class), N(Double.class), D(Date.class), B(Boolean.class); private Class<?> clazz; /* * */ private PropertyType(Class<?> clazz) { this.clazz = clazz; } public Class<?> getValue() { return clazz; } } private MatchType matchType = null; private Object matchValue = null; //? private Object matchBetweenValue = null; //?between2 private PropertyType propertyClass = null; private String[] propertyNames = null; public PropertyFilter() { } /** * @param filterName ,???. * FILTER_LIKES_NAME_OR_LOGIN_NAME * * value "SYS_" ??? * * @param value . */ public PropertyFilter(final String filterName, final String value) { String firstPart = StringUtils.substringBefore(filterName, "_"); String matchTypeCode = StringUtils.substring(firstPart, 0, firstPart.length() - 1); String propertyTypeCode = StringUtils.substring(firstPart, firstPart.length() - 1, firstPart.length()); try { matchType = Enum.valueOf(MatchType.class, matchTypeCode); } catch (RuntimeException e) { throw new IllegalArgumentException( "filter??" + filterName + ",.", e); } try { propertyClass = Enum.valueOf(PropertyType.class, propertyTypeCode); } catch (RuntimeException e) { throw new IllegalArgumentException( "filter??" + filterName + ",.", e); } String propertyNameStr = StringUtils.substringAfter(filterName, "_"); Assert.isTrue(StringUtils.isNotBlank(propertyNameStr), "filter??" + filterName + ",??."); propertyNames = StringUtils.splitByWholeSeparator(propertyNameStr, PropertyFilter.OR_SEPARATOR); if (StringUtils.split(value, "-").length == 1) { this.matchValue = getValue(value); } else { this.matchValue = getValue(StringUtils.split(value, "-")[0]); this.matchBetweenValue = getValue(StringUtils.split(value, "-")[1]); } } public Object getValue(String value) { if (value.startsWith("SYS_")) { return getRealObj(StringUtils.substring(value, 3)); } else { return ConvertUtils.convertStringToObject(value, propertyClass.getValue()); } } public void setMatchType(MatchType matchType) { this.matchType = matchType; } public void setMatchValue(Object matchValue) { this.matchValue = matchValue; } // public void setPropertyClass(Class<?> propertyClass) { // this.propertyClass = propertyClass; // } public void setPropertyNames(String[] propertyNames) { this.propertyNames = propertyNames; } /** * HttpRequestPropertyFilter, Filter???filter. * * @see #buildFromHttpRequest(HttpServletRequest, String) */ public static List<PropertyFilter> buildFromHttpRequest(final HttpServletRequest request) { return buildFromHttpRequest(request, "filter"); } /** * HttpRequestPropertyFilter * PropertyFilter??Filter?__??. * * eg. * filter_EQS_name * filter_LIKES_name_OR_email */ public static List<PropertyFilter> buildFromHttpRequest(final HttpServletRequest request, final String filterPrefix) { List<PropertyFilter> filterList = new ArrayList<PropertyFilter>(); //request??????,?????Map. Map<String, Object> filterParamMap = ServletUtils.getParametersStartingWith(request, filterPrefix + "_"); //??Map,PropertyFilter for (Map.Entry<String, Object> entry : filterParamMap.entrySet()) { String filterName = entry.getKey(); String value = null; try { value = (String) entry.getValue(); } catch (RuntimeException e) { throw new IllegalArgumentException( "filter??" + filterName + ",????2", e); } //value,filter. if (StringUtils.isNotBlank(value)) { PropertyFilter filter = new PropertyFilter(filterName, value); filterList.add(filter); } } return filterList; } // /** // * ?. // */ // public Class<?> getPropertyClass() { // return propertyClass; // } /** * ??. */ public MatchType getMatchType() { return matchType; } /** * ?. */ public Object getMatchValue() { return matchValue; } /** * ???. */ public String[] getPropertyNames() { return propertyNames; } /** * ???. */ public String getPropertyName() { Assert.isTrue(propertyNames.length == 1, "There are not only one property in this filter."); return propertyNames[0]; } /** * ?. */ public boolean hasMultiProperties() { return (propertyNames.length > 1); } QueryConstants stans; public void setStans(QueryConstants stans) { this.stans = stans; } public Object getRealObj(String key) { Object obj = null; return obj; } public PropertyType getPropertyClass() { return propertyClass; } public void setPropertyClass(PropertyType propertyClass) { this.propertyClass = propertyClass; } public Object getMatchBetweenValue() { return matchBetweenValue; } public void setMatchBetweenValue(Object matchBetweenValue) { this.matchBetweenValue = matchBetweenValue; } public static void main(String[] args) { System.out.println(StringUtils.split("312321321", "-").length); } }