org.beanfuse.struts2.action.helper.QueryHelper.java Source code

Java tutorial

Introduction

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

Source

//$Id:QueryHelper.java 2009-1-20 ?06:53:48 chaostone Exp $
/*
 * Copyright c 2005-2009.
 * 
 * Licensed under the GPL License, Version 2.0 (the "License")
 * http://www.gnu.org/licenses/gpl-2.0.html
 * 
 */
package org.beanfuse.struts2.action.helper;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
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.beanfuse.collection.page.Page;
import org.beanfuse.collection.page.PageLimit;
import org.beanfuse.entity.Model;
import org.beanfuse.entity.types.EntityType;
import org.beanfuse.query.Condition;
import org.beanfuse.query.EntityQuery;
import org.beanfuse.utils.web.CookieUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class QueryHelper extends ParamHelper {

    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(EntityQuery entityQuery) {
        entityQuery.add(extractConditions(entityQuery.getEntityClass(), entityQuery.getAlias(), null));
    }

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

    /**
     * ????
     * 
     * @param clazz
     * @param prefix
     * @param exclusiveAttrNames
     * @return
     */
    public static List 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 conditions = new ArrayList();
        HttpServletRequest request = ServletActionContext.getRequest();
        Map params = ParamHelper.getParams(prefix, exclusiveAttrNames);
        for (Iterator iter = params.keySet().iterator(); iter.hasNext();) {
            String attr = (String) iter.next();
            String strValue = ((String) params.get(attr)).trim();
            // 
            if (StringUtils.isNotEmpty(strValue)) {
                try {
                    if (RESERVED_NULL && "null".equals(strValue)) {
                        conditions.add(new Condition(prefix + "." + attr + " is null"));
                    } else {
                        Model.getPopulator().populateValue(attr, strValue, entity);
                        Object settedValue = PropertyUtils.getProperty(entity, attr);
                        if (null == settedValue)
                            continue;
                        if (settedValue instanceof String) {
                            conditions.add(new Condition(prefix + "." + attr + " like :" + attr.replace('.', '_'),
                                    "%" + (String) settedValue + "%"));
                        } else {
                            conditions.add(new Condition(prefix + "." + attr + "=:" + attr.replace('.', '_'),
                                    settedValue));
                        }
                    }
                } 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 = get(PAGENO);
        if (StringUtils.isNotEmpty(pageNo)) {
            return Integer.valueOf(pageNo).intValue();
        } else {
            return Page.DEFAULT_PAGE_NUM;
        }
    }

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

    /**
     * ??
     * 
     * @param objs
     */
    public static void addPage(Page objs) {
        Page page = (Page) objs;
        put(PAGENO, new Integer(page.getPageNo()));
        put(PAGESIZE, new Integer(page.getPageSize()));
        put("previousPageNo", new Integer(page.getPreviousPageNo()));
        put("nextPageNo", new Integer(page.getNextPageNo()));
        put("maxPageNo", new Integer(page.getMaxPageNo()));
        put("thisPageSize", new Integer(page.size()));
        put("totalSize", new Integer(page.getTotal()));
    }

    public static void addDateIntervalCondition(EntityQuery 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(EntityQuery query, String alias, String attr, String beginOn,
            String endOn) {
        String stime = get(beginOn);
        String etime = 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);
            }
            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.add(new Condition(objAttr + " >=:sdate", sdate));
        } else if (null != sdate && null != edate) {
            query.add(new Condition(objAttr + " >=:sdate and " + objAttr + " <:edate", sdate, edate));
        } else if (null == sdate && null != edate) {
            query.add(new Condition(objAttr + " <:edate", edate));
        }
    }
}