org.faster.orm.criteria.GenericCriteria.java Source code

Java tutorial

Introduction

Here is the source code for org.faster.orm.criteria.GenericCriteria.java

Source

/*
 * Copyright (c) 2013 @iSQWEN. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.faster.orm.criteria;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.apache.commons.lang3.builder.ToStringStyle;
import org.faster.util.DateTimes;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import javax.ws.rs.DefaultValue;
import javax.ws.rs.QueryParam;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlTransient;
import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

/**
 * ?
 *
 * @author sqwen
 */
@XmlAccessorType(XmlAccessType.FIELD)
public class GenericCriteria<PO> {

    public static final String ALL = "<ALL>";

    public static final String NULL = "<NULL>";

    public static final String NOT_NULL = "<NOTNULL>";

    // ?: ???-desc|asc?????
    @QueryParam("sort")
    @XmlAttribute
    @NoQuery
    protected String sort;

    // ?1
    @QueryParam("page")
    @DefaultValue("1")
    @XmlAttribute
    @NoQuery
    protected int page = 1;

    // ??
    @QueryParam("limit")
    @DefaultValue("10")
    @XmlAttribute
    @NoQuery
    protected int limit = 10;

    // 
    @QueryParam("fields")
    @XmlAttribute
    @NoQuery
    protected String fields;

    /**
     * 
     * <p>
     * yes - ?
     * <p>
     * no - ??
     * <p>
     * delete/flush - 
     */
    @QueryParam("cache")
    @DefaultValue("YES")
    @NoQuery
    protected transient String cache;

    @XmlTransient
    @NoQuery
    protected final Class<PO> persistClass;

    // --------------------
    // 
    // --------------------

    @SuppressWarnings("unchecked")
    public GenericCriteria() {
        persistClass = (Class<PO>) ((ParameterizedType) getClass().getGenericSuperclass())
                .getActualTypeArguments()[0];
    }

    /**
     * ???????
     * <p>
     * ?????
     */
    protected void beforeBuildCriteria() {
    }

    public DetachedCriteria buildCriteria() {
        beforeBuildCriteria();
        DetachedCriteria dc = DetachedCriteria.forClass(persistClass);
        List<Field> queryFields = QueryHelper.getQueryFields(this.getClass());
        Set<String> aliases = new HashSet<String>(); // ??
        for (Field f : queryFields) {
            try {
                Object obj = f.get(this);
                boolean ignoreNull = QueryHelper.isIgnoreNull(f);
                if (obj == null) {
                    if (ignoreNull) {
                        continue;
                    }
                }

                String fieldName = QueryHelper.getFieldName(f);
                if (fieldName.contains(".")) {
                    String[] subFieldNames = fieldName.split("\\.");
                    String fieldPath = subFieldNames[0];
                    String alias = subFieldNames[0];
                    if (!aliases.contains(alias)) {
                        dc.createAlias(fieldPath, alias);
                        aliases.add(alias);
                    }

                    // ?alias
                    for (int i = 1; i < subFieldNames.length - 1; i++) {
                        fieldPath += "." + subFieldNames[i];
                        alias += "_" + subFieldNames[i];
                        if (!aliases.contains(alias)) {
                            dc.createAlias(fieldPath, alias);
                            aliases.add(alias);
                        }
                    }

                    if (subFieldNames.length > 2) {
                        fieldName = alias + "." + subFieldNames[subFieldNames.length - 1];
                    }
                }

                if (obj == null && !ignoreNull) {
                    dc.add(Restrictions.isNull(fieldName));
                    continue;
                }

                String stringValue = String.valueOf(obj);
                boolean ignoreEmptyOrZero = QueryHelper.isIgnoreEmptyOrZero(f);
                if (ignoreEmptyOrZero && isBlank(stringValue)) {
                    continue;
                }

                if (stringValue.startsWith(NULL)) {
                    dc.add(Restrictions.isNull(fieldName));
                    continue;
                }

                if (stringValue.startsWith(NOT_NULL)) {
                    dc.add(Restrictions.isNotNull(fieldName));
                    continue;
                }

                String delimiter = QueryHelper.getDelimiter(f);
                DataType dt = QueryHelper.getDataType(f);
                MatchMode mm = QueryHelper.getMatchMode(f);

                switch (dt) {
                case STRING:
                    switch (mm) {
                    case EQ:
                        dc.add(Restrictions.eq(fieldName, stringValue));
                        continue;
                    case LIKE:
                        dc.add(Restrictions.like(fieldName, stringValue,
                                org.hibernate.criterion.MatchMode.ANYWHERE));
                        continue;
                    case LIKE_START:
                        dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.START));
                        continue;
                    case LIKE_END:
                        dc.add(Restrictions.like(fieldName, stringValue, org.hibernate.criterion.MatchMode.END));
                        continue;
                    case ILIKE:
                        dc.add(Restrictions.ilike(fieldName, stringValue,
                                org.hibernate.criterion.MatchMode.ANYWHERE));
                        continue;
                    case ILIKE_START:
                        dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.START));
                        continue;
                    case ILIKE_END:
                        dc.add(Restrictions.ilike(fieldName, stringValue, org.hibernate.criterion.MatchMode.END));
                        continue;
                    case IN:
                        CriteriaRender.renderFieldByStringFilterValues(dc, fieldName, stringValue, delimiter);
                        continue;
                    default:
                        throw new IllegalArgumentException("Invalid MatchMode for String: " + mm);
                    }
                case INTEGER:
                    if (ignoreEmptyOrZero && stringValue.equals("0")) {
                        continue;
                    }

                    Integer intValue = 0;
                    if (mm != MatchMode.IN) {
                        try {
                            intValue = Integer.valueOf(stringValue);
                        } catch (Exception e) {
                            throw new IllegalArgumentException(
                                    stringValue + " is not valid int value and can't use MatchMode: " + mm);
                        }
                    }

                    switch (mm) {
                    case EQ:
                        dc.add(Restrictions.eq(fieldName, intValue));
                        continue;
                    case NE:
                        dc.add(Restrictions.ne(fieldName, intValue));
                        continue;
                    case IN:
                        CriteriaRender.renderFieldByIntegerFilterValues(dc, fieldName, stringValue, delimiter);
                        continue;
                    case GT:
                        dc.add(Restrictions.gt(fieldName, intValue));
                        continue;
                    case GE:
                        dc.add(Restrictions.ge(fieldName, intValue));
                        continue;
                    case LT:
                        dc.add(Restrictions.lt(fieldName, intValue));
                        continue;
                    case LE:
                        dc.add(Restrictions.le(fieldName, intValue));
                        continue;
                    default:
                        throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm);
                    }
                case LONG:
                    if (ignoreEmptyOrZero && stringValue.equals("0")) {
                        continue;
                    }

                    Long longValue = 0L;
                    if (mm != MatchMode.IN) {
                        try {
                            longValue = Long.valueOf(stringValue);
                        } catch (Exception e) {
                            throw new IllegalArgumentException(
                                    stringValue + " is not valid long value and can't use MatchMode: " + mm);
                        }
                    }

                    switch (mm) {
                    case EQ:
                        dc.add(Restrictions.eq(fieldName, longValue));
                        continue;
                    case NE:
                        dc.add(Restrictions.ne(fieldName, longValue));
                        continue;
                    case IN:
                        CriteriaRender.renderFieldByLongFilterValues(dc, fieldName, stringValue, delimiter);
                        continue;
                    case GT:
                        dc.add(Restrictions.gt(fieldName, longValue));
                        continue;
                    case GE:
                        dc.add(Restrictions.ge(fieldName, longValue));
                        continue;
                    case LT:
                        dc.add(Restrictions.lt(fieldName, longValue));
                        continue;
                    case LE:
                        dc.add(Restrictions.le(fieldName, longValue));
                        continue;
                    default:
                        throw new IllegalArgumentException("Invalid MatchMode for Long: " + mm);
                    }
                case DATE:
                    // TODO ??????
                    Date date = DateTimes.parseStringToDate(stringValue);
                    switch (mm) {
                    case EQ:
                        dc.add(Restrictions.eq(fieldName, date));
                        continue;
                    case NE:
                        dc.add(Restrictions.ne(fieldName, date));
                        continue;
                    case GT:
                        dc.add(Restrictions.gt(fieldName, date));
                        continue;
                    case GE:
                        dc.add(Restrictions.ge(fieldName, date));
                        continue;
                    case LT:
                        dc.add(Restrictions.lt(fieldName, date));
                        continue;
                    case LE:
                        dc.add(Restrictions.le(fieldName, date));
                        continue;
                    default:
                        throw new IllegalArgumentException("Invalid MatchMode for Date: " + mm);
                    }
                case BOOLEAN:
                    Boolean bool = BooleanUtils.toBooleanObject(stringValue);
                    switch (mm) {
                    case EQ:
                        dc.add(Restrictions.eq(fieldName, bool));
                        continue;
                    case NE:
                        dc.add(Restrictions.ne(fieldName, bool));
                        continue;
                    default:
                        throw new IllegalArgumentException("Invalid MatchMode for Boolean: " + mm);
                    }
                case LatLong:
                    LatLngBound b = new LatLngBound(stringValue);
                    if (b.isValid()) {
                        String minLatitude = b.getMinLatitude() == null ? "0.0" : b.getMinLatitude().toString();
                        String maxLatitude = b.getMaxLatitude() == null ? "0.0" : b.getMaxLatitude().toString();
                        String minLongitude = b.getMinLongitude() == null ? "0.0" : b.getMinLongitude().toString();
                        String maxLongitude = b.getMaxLongitude() == null ? "0.0" : b.getMaxLongitude().toString();
                        if ("area".equalsIgnoreCase(fieldName)) {
                            dc.add(Restrictions.between("latitude", minLatitude, maxLatitude));
                            dc.add(Restrictions.between("longitude", minLongitude, maxLongitude));
                        } else {
                            if (fieldName.contains(".")) {
                                String alias = fieldName.replace(".", "_");
                                if (!aliases.contains(alias)) {
                                    dc.createAlias(fieldName, alias);
                                    aliases.add(alias);
                                }
                                fieldName = alias;
                            } else {
                                if (!aliases.contains(fieldName)) {
                                    dc.createAlias(fieldName, fieldName);
                                    aliases.add(fieldName);
                                }
                            }
                            dc.add(Restrictions.between(fieldName + ".latitude", minLatitude, maxLatitude));
                            dc.add(Restrictions.between(fieldName + ".longitude", minLongitude, maxLongitude));
                        }
                    }
                    continue;
                default:
                    throw new IllegalArgumentException("Unsupported DataType: " + dt);
                }
            } catch (Exception e) {
                throw new IllegalArgumentException(e);
            }
        }

        renderCriteria(dc);

        if (isNotBlank(sort)) {
            String[] fields = sort.split("-");
            if (fields.length < 2 || "desc".equalsIgnoreCase(fields[1])) {
                dc.addOrder(Order.desc(fields[0]));
            } else {
                dc.addOrder(Order.asc(fields[0]));
            }
        }

        return dc;
    }

    /**
     * ?DetachedCriteria?
     *
     * @param dc
     *            ??
     */
    protected void renderCriteria(DetachedCriteria dc) {
    }

    public String buildCacheKey() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    @Override
    public String toString() {
        return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);
    }

    /**
     * ?
     */
    public boolean isPagination() {
        return limit > 0;
    }

    // --------------------
    // getter/setter
    // --------------------

    public int getPage() {
        return page;
    }

    public void setPage(int pageNo) {
        this.page = pageNo;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int pageSize) {
        this.limit = pageSize;
    }

    public String getSort() {
        return sort;
    }

    public void setSort(String sort) {
        this.sort = sort;
    }

    public String getCacheStrategy() {
        return cache;
    }

    public void setCacheStrategy(String cacheStrategy) {
        this.cache = cacheStrategy;
    }

    public String getFields() {
        return fields;
    }

    public void setFields(String fields) {
        this.fields = fields;
    }
}