smartcrud.common.orm.PropertyFilter.java Source code

Java tutorial

Introduction

Here is the source code for smartcrud.common.orm.PropertyFilter.java

Source

/**
 * 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 smartcrud.common.orm;

import java.util.Date;

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

import smartcrud.common.utils.ReflectionUtils;

/**
 * ORM??.
 * 
 * PropertyFilter?????,HibernateCriterion??.
 * 
 * @author calvin
 */
public class PropertyFilter {

    /** OR. */
    public static final String OR_SEPARATOR = "_OR_";

    /** . */
    public enum MatchType {
        EQ, NE, LIKE, LT, GT, LE, GE, IS, ISN;
    }

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