org.beangle.struts2.helper.QueryHelper.java Source code

Java tutorial

Introduction

Here is the source code for org.beangle.struts2.helper.QueryHelper.java

Source

/* Copyright c 2005-2012.
 * Licensed under GNU  LESSER General Public License, Version 3.
 * http://www.gnu.org/licenses
 */
package org.beangle.struts2.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;
import org.beangle.commons.collection.CollectUtils;
import org.beangle.commons.collection.page.Page;
import org.beangle.commons.collection.page.PageLimit;
import org.beangle.commons.lang.StrUtils;
import org.beangle.model.entity.Model;
import org.beangle.model.entity.types.EntityType;
import org.beangle.model.query.builder.Condition;
import org.beangle.model.query.builder.OqlBuilder;
import org.beangle.web.util.CookieUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueryHelper {

    protected static final Logger logger = LoggerFactory.getLogger(QueryHelper.class);

    public static final String PAGENO = "pageNo";

    public static final String PAGESIZE = "pageSize";

    public static boolean RESERVED_NULL = true;

    public static void populateConditions(OqlBuilder<?> builder) {
        builder.where(extractConditions(builder.getEntityClass(), builder.getAlias(), null));
    }

    /**
     * entity alias??????.<br>
     * 
     * @param entityQuery
     * @param exclusiveAttrNames
     *            entityQueryalias
     */
    public static void populateConditions(OqlBuilder<?> entityQuery, String exclusiveAttrNames) {
        entityQuery
                .where(extractConditions(entityQuery.getEntityClass(), entityQuery.getAlias(), exclusiveAttrNames));
    }

    public static void populateIds(OqlBuilder<?> entityQuery, String paramName) {
        Long[] ids = StrUtils.splitToLong(Params.get(paramName + "s"));
        if (ids != null && ids.length > 0) {
            entityQuery.where(paramName + " in(:" + paramName.replaceAll("\\.", "") + "s)", Arrays.asList(ids));
        }
    }

    /**
     * ????
     * 
     * @param clazz
     * @param prefix
     * @param exclusiveAttrNames
     * @return
     */
    public static List<Condition> extractConditions(Class<?> clazz, String prefix, String exclusiveAttrNames) {
        Object entity = null;
        try {
            if (clazz.isInterface()) {
                EntityType entityType = Model.getEntityType(clazz.getName());
                clazz = entityType.getEntityClass();
            }
            entity = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException("[RequestUtil.extractConditions]: error in in initialize " + clazz);
        }
        List<Condition> conditions = CollectUtils.newArrayList();
        Map<String, Object> params = Params.sub(prefix, exclusiveAttrNames);
        for (Map.Entry<String, Object> entry : params.entrySet()) {
            String attr = entry.getKey();
            String strValue = entry.getValue().toString().trim();
            // 
            if (StringUtils.isNotEmpty(strValue)) {
                try {
                    if (RESERVED_NULL && "null".equals(strValue)) {
                        conditions.add(new Condition(prefix + "." + attr + " is null"));
                    } else {
                        StringBuilder sb = new StringBuilder();
                        String[] keys = attr.split("\\|");
                        Condition condition = new Condition(null);
                        for (String key : keys) {
                            if (sb.length() > 0) {
                                sb.append(" or ");
                            }
                            ;
                            String namedParam = key.replace('.', '_').replace('|', '_');
                            Model.getPopulator().populateValue(entity, key, strValue);
                            Object settedValue = PropertyUtils.getProperty(entity, key);
                            if (null == settedValue)
                                continue;
                            if (settedValue instanceof String) {
                                sb.append(prefix).append(".").append(key).append(" like :").append(namedParam);
                                settedValue = "%" + settedValue + "%";
                            } else {
                                sb.append(prefix).append(".").append(key).append(" = :").append(namedParam);
                            }
                            condition.param(settedValue);
                        }
                        condition.setContent(sb.toString());
                        conditions.add(condition);
                    }
                } catch (Exception e) {
                    logger.debug(
                            "[populateFromParams]:error in populate entity " + prefix + "'s attribute " + attr);
                }
            }
        }
        return conditions;
    }

    /**
     * ?cookie(?)??
     * 
     * @return
     */
    public static PageLimit getPageLimit() {
        PageLimit limit = new PageLimit();
        limit.setPageNo(getPageNo());
        limit.setPageSize(getPageSize());
        return limit;
    }

    /**
     * ?
     * 
     * @return
     */
    public static int getPageNo() {
        String pageNo = Params.get(PAGENO);
        if (StringUtils.isNotBlank(pageNo) && StringUtils.isNumeric(pageNo)) {
            return Integer.valueOf(pageNo.trim()).intValue();
        } else {
            return Page.DEFAULT_PAGE_NUM;
        }
    }

    /**
     * 
     * 
     * @return
     */
    public static int getPageSize() {
        String pageSize = Params.get(PAGESIZE);
        if (StringUtils.isNotBlank(pageSize) && StringUtils.isNumeric(pageSize)) {
            return Integer.valueOf(pageSize.trim()).intValue();
        } else {
            HttpServletRequest request = ServletActionContext.getRequest();
            pageSize = CookieUtils.getCookieValue(request, PAGESIZE);
            if (StringUtils.isNotEmpty(pageSize) && StringUtils.isNumeric(pageSize)) {
                return Integer.valueOf(pageSize).intValue();
            } else
                return Page.DEFAULT_PAGE_SIZE;
        }
    }

    public static void addDateIntervalCondition(OqlBuilder<?> query, String attr, String beginOn, String endOn) {
        addDateIntervalCondition(query, query.getAlias(), attr, beginOn, endOn);
    }

    /**
     * ?
     * 
     * @param
     * @param query
     * @param alias
     * @param attr
     *            ?
     * @param beginOn
     *            ??(??)
     * @param endOn
     *            ???(??)
     * @throws ParseException
     */
    public static void addDateIntervalCondition(OqlBuilder<?> query, String alias, String attr, String beginOn,
            String endOn) {
        String stime = Params.get(beginOn);
        String etime = Params.get(endOn);
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date sdate = null, edate = null;
        if (StringUtils.isNotBlank(stime)) {
            try {
                sdate = df.parse(stime);
            } catch (Exception e) {
                logger.debug("wrong date format:" + stime);
            }

        }
        // 
        if (StringUtils.isNotBlank(etime)) {
            try {
                edate = df.parse(etime);
            } catch (Exception e) {
                logger.debug("wrong date format:" + etime);
            }
            if (null != edate) {
                Calendar gc = new GregorianCalendar();
                gc.setTime(edate);
                gc.set(Calendar.DAY_OF_YEAR, gc.get(Calendar.DAY_OF_YEAR) + 1);
                edate = gc.getTime();
            }
        }
        String objAttr = ((null == alias) ? query.getAlias() : alias) + "." + attr;
        if (null != sdate && null == edate) {
            query.where(objAttr + " >=:sdate", sdate);
        } else if (null != sdate && null != edate) {
            query.where(objAttr + " >=:sdate and " + objAttr + " <:edate", sdate, edate);
        } else if (null == sdate && null != edate) {
            query.where(objAttr + " <:edate", edate);
        }
    }
}