com.dp2345.template.directive.BaseDirective.java Source code

Java tutorial

Introduction

Here is the source code for com.dp2345.template.directive.BaseDirective.java

Source

/*
 * Copyright 2013-2015 cetvision.com. All rights reserved.
 * Support: http://www.cetvision.com
 * License: http://www.cetvision.com/license
 */
package com.dp2345.template.directive;

import java.beans.PropertyDescriptor;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.beanutils.PropertyUtils;
import org.apache.commons.lang.ArrayUtils;
import org.apache.commons.lang.StringUtils;

import com.dp2345.Filter;
import com.dp2345.Order;
import com.dp2345.Order.Direction;
import com.dp2345.util.FreemarkerUtils;

import freemarker.core.Environment;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateDirectiveModel;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;
import freemarker.template.TemplateModelException;

/**
 * ? - 
 * 
 * @author CETVISION CORP
 * @version 2.0.3
 */
public abstract class BaseDirective implements TemplateDirectiveModel {

    /** ""??? */
    private static final String USE_CACHE_PARAMETER_NAME = "useCache";
    /** ""??? */
    private static final String CACHE_REGION_PARAMETER_NAME = "cacheRegion";
    /** "ID"??? */
    private static final String ID_PARAMETER_NAME = "id";
    /** "?"??? */
    private static final String COUNT_PARAMETER_NAME = "count";
    /** "?"??? */
    private static final String ORDER_BY_PARAMETER_NAME = "orderBy";
    /** ? */
    private static final String ORDER_BY_ITEM_SEPARATOR = "\\s*,\\s*";
    /** ? */
    private static final String ORDER_BY_FIELD_SEPARATOR = "\\s+";

    /**
     * 
     * 
     * @param env
     *            Environment
     * @param params
     *            ?
     * @return 
     */
    protected boolean useCache(Environment env, Map<String, TemplateModel> params) throws TemplateModelException {
        Boolean useCache = FreemarkerUtils.getParameter(USE_CACHE_PARAMETER_NAME, Boolean.class, params);
        return useCache != null ? useCache : true;
    }

    /**
     * ?
     * 
     * @param env
     *            Environment
     * @param params
     *            ?
     * @return 
     */
    protected String getCacheRegion(Environment env, Map<String, TemplateModel> params)
            throws TemplateModelException {
        String cacheRegion = FreemarkerUtils.getParameter(CACHE_REGION_PARAMETER_NAME, String.class, params);
        return cacheRegion != null ? cacheRegion : env.getTemplate().getName();
    }

    /**
     * ?ID
     * 
     * @param params
     *            ?
     * @return ID
     */
    protected Long getId(Map<String, TemplateModel> params) throws TemplateModelException {
        return FreemarkerUtils.getParameter(ID_PARAMETER_NAME, Long.class, params);
    }

    /**
     * ??
     * 
     * @param params
     *            ?
     * @return ?
     */
    protected Integer getCount(Map<String, TemplateModel> params) throws TemplateModelException {
        return FreemarkerUtils.getParameter(COUNT_PARAMETER_NAME, Integer.class, params);
    }

    /**
     * ?
     * 
     * @param params
     *            ?
     * @param type
     *            ?
     * @param ignoreProperties
     *            
     * @return 
     */
    protected List<Filter> getFilters(Map<String, TemplateModel> params, Class<?> type, String... ignoreProperties)
            throws TemplateModelException {
        List<Filter> filters = new ArrayList<Filter>();
        PropertyDescriptor[] propertyDescriptors = PropertyUtils.getPropertyDescriptors(type);
        for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
            String propertyName = propertyDescriptor.getName();
            Class<?> propertyType = propertyDescriptor.getPropertyType();
            if (!ArrayUtils.contains(ignoreProperties, propertyName) && params.containsKey(propertyName)) {
                Object value = FreemarkerUtils.getParameter(propertyName, propertyType, params);
                filters.add(Filter.eq(propertyName, value));
            }
        }
        return filters;
    }

    /**
     * ??
     * 
     * @param params
     *            ?
     * @param ignoreProperties
     *            
     * @return ?
     */
    protected List<Order> getOrders(Map<String, TemplateModel> params, String... ignoreProperties)
            throws TemplateModelException {
        String orderBy = StringUtils
                .trim(FreemarkerUtils.getParameter(ORDER_BY_PARAMETER_NAME, String.class, params));
        List<Order> orders = new ArrayList<Order>();
        if (StringUtils.isNotEmpty(orderBy)) {
            String[] orderByItems = orderBy.split(ORDER_BY_ITEM_SEPARATOR);
            for (String orderByItem : orderByItems) {
                if (StringUtils.isNotEmpty(orderByItem)) {
                    String property = null;
                    Direction direction = null;
                    String[] orderBys = orderByItem.split(ORDER_BY_FIELD_SEPARATOR);
                    if (orderBys.length == 1) {
                        property = orderBys[0];
                    } else if (orderBys.length >= 2) {
                        property = orderBys[0];
                        try {
                            direction = Direction.valueOf(orderBys[1]);
                        } catch (IllegalArgumentException e) {
                            continue;
                        }
                    } else {
                        continue;
                    }
                    if (!ArrayUtils.contains(ignoreProperties, property)) {
                        orders.add(new Order(property, direction));
                    }
                }
            }
        }
        return orders;
    }

    /**
     * ??
     * 
     * @param name
     *            ??
     * @param value
     *            ??
     * @param env
     *            Environment
     * @param body
     *            TemplateDirectiveBody
     */
    protected void setLocalVariable(String name, Object value, Environment env, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        TemplateModel sourceVariable = FreemarkerUtils.getVariable(name, env);
        FreemarkerUtils.setVariable(name, value, env);
        body.render(env.getOut());
        FreemarkerUtils.setVariable(name, sourceVariable, env);
    }

    /**
     * ??
     * 
     * @param variables
     *            ??
     * @param env
     *            Environment
     * @param body
     *            TemplateDirectiveBody
     */
    protected void setLocalVariables(Map<String, Object> variables, Environment env, TemplateDirectiveBody body)
            throws TemplateException, IOException {
        Map<String, Object> sourceVariables = new HashMap<String, Object>();
        for (String name : variables.keySet()) {
            TemplateModel sourceVariable = FreemarkerUtils.getVariable(name, env);
            sourceVariables.put(name, sourceVariable);
        }
        FreemarkerUtils.setVariables(variables, env);
        body.render(env.getOut());
        FreemarkerUtils.setVariables(sourceVariables, env);
    }

}