com.abssh.util.PropertyFilter.java Source code

Java tutorial

Introduction

Here is the source code for com.abssh.util.PropertyFilter.java

Source

/**
 * Copyright (c) 2010 www.pub.cn
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * 
 * $Id: PropertyFilter.java,v 1.6 2010/08/22 07:10:52 xp Exp $
 */
package com.abssh.util;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.GregorianCalendar;

import org.apache.commons.lang.StringUtils;
import org.springframework.util.Assert;

import com.abssh.util.ReflectionUtils;

/**
 * ORM??. PropertyFilter?????,HibernateCriterion??.
 * 
 * @author pub
 */
public class PropertyFilter implements Serializable {
    private static final long serialVersionUID = -5892974032551917730L;
    /**
     * OR.
     */
    public static final String OR_SEPARATOR = "_OR_";

    /**
     * .
     */
    public enum MatchType {
        EQ, // =
        LIKE, // like '%XX%'
        SLIKE, // like 'XX%'
        ELIKE, // like '%XX'
        LT, // <
        GT, // >
        LE, // <=
        LEN, //<=
        GE, // >=
        NE, // <>
        ISNULL, // is null
        ISNOTNULL, // is not null
        ISEMPTY, // is empty
        ISNOTEMPTY, // is not empty
        IN, // in
        INS; // in (sql)
    }

    /**
     * ?.
     */
    public enum PropertyType {
        S(String.class), I(Integer.class), L(Long.class), N(Double.class), D(Date.class), B(Boolean.class), M(
                BigDecimal.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 = MatchType.EQ;

    public PropertyFilter() {
    }

    /**
     * @param filterName
     *            ,???. eg. LIKES_NAME_OR_LOGIN_NAME
     * @param value
     *            .
     */
    @SuppressWarnings("unchecked")
    public PropertyFilter(final String filterName, final Object 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 = propertyNameStr.split(PropertyFilter.OR_SEPARATOR);

        Assert.isTrue(propertyNames.length > 0,
                "filter??" + filterName + ",??.");
        // entity property.
        if (value != null && (value.getClass().isArray() || Collection.class.isInstance(value)
                || value.toString().split(",").length > 1)) {
            // IN ?
            Object[] vObjects = null;
            if (value.getClass().isArray()) {
                vObjects = (Object[]) value;
            } else if (Collection.class.isInstance(value)) {
                vObjects = ((Collection) value).toArray();
            } else {
                vObjects = value.toString().split(",");
            }
            this.propertyValue = new Object[vObjects.length];
            for (int i = 0; i < vObjects.length; i++) {
                propertyValue[i] = ReflectionUtils.convertValue(vObjects[i], propertyType);
            }
        } else {
            Object tObject = ReflectionUtils.convertValue(value, propertyType);
            if (tObject != null && matchType == MatchType.LE && propertyType.getName().equals("java.util.Date")) {
                // LED ??tObject2010-08-13 00:00:00
                // ?2010-08-13 23:59:59
                Date leDate = (Date) tObject;
                Calendar c = GregorianCalendar.getInstance();
                c.setTime(leDate);
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                tObject = c.getTime();
            } else if (tObject != null && matchType == MatchType.LEN
                    && propertyType.getName().equals("java.util.Date")) {
                // LED ??tObject2010-08-13 00:00:00
                // ?2010-08-13 23:59:59
                //            Date leDate = (Date) tObject;
                //            Calendar c = GregorianCalendar.getInstance();
                //            c.setTime(leDate);
                //            tObject = c.getTime();
            } else if (tObject != null && matchType == MatchType.LIKE) {
                tObject = ((String) tObject).replace("%", "\\%").replace("_", "\\_");
            }
            this.propertyValue = new Object[] { tObject };
        }
    }

    /**
     * @param filterName
     *            ,???. eg. LIKES_NAME_OR_LOGIN_NAME
     * @param value
     *            .
     */
    @SuppressWarnings("unchecked")
    public PropertyFilter(final String filterName, final Object value, boolean flag) {

        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 = propertyNameStr.split(PropertyFilter.OR_SEPARATOR);

        Assert.isTrue(propertyNames.length > 0,
                "filter??" + filterName + ",??.");
        // entity property.
        if (value != null && (value.getClass().isArray() || Collection.class.isInstance(value))) {
            // IN ?
            Object[] vObjects = null;
            if (value.getClass().isArray()) {
                vObjects = (Object[]) value;
            } else if (Collection.class.isInstance(value)) {
                vObjects = ((Collection) value).toArray();
            } else {
                vObjects = value.toString().split(",");
            }
            this.propertyValue = new Object[vObjects.length];
            for (int i = 0; i < vObjects.length; i++) {
                propertyValue[i] = ReflectionUtils.convertValue(vObjects[i], propertyType);
            }
        } else {
            Object tObject = ReflectionUtils.convertValue(value, propertyType);
            if (tObject != null && matchType == MatchType.LE && propertyType.getName().equals("java.util.Date")) {
                // LED ??tObject2010-08-13 00:00:00
                // ?2010-08-13 23:59:59
                Date leDate = (Date) tObject;
                Calendar c = GregorianCalendar.getInstance();
                c.setTime(leDate);
                c.set(Calendar.HOUR_OF_DAY, 23);
                c.set(Calendar.MINUTE, 59);
                c.set(Calendar.SECOND, 59);
                tObject = c.getTime();
            } else if (tObject != null && matchType == MatchType.LEN
                    && propertyType.getName().equals("java.util.Date")) {
            } else if (tObject != null && matchType == MatchType.LIKE) {
                tObject = ((String) tObject).replace("%", "\\%").replace("_", "\\_");
            }
            this.propertyValue = new Object[] { tObject };
        }
    }

    /**
     * ?.
     */
    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;
    }
}