Java tutorial
/* * Copyright 2005-2013 shopxx.net. All rights reserved. * Support: http://www.shopxx.net * License: http://www.shopxx.net/license */ package cn.shengyuan.yun.admin.web.template.directive; import cn.shengyuan.basic.model.Order; import cn.shengyuan.basic.model.Order.Direction; import cn.shengyuan.tools.util.FreemarkerUtils; import freemarker.core.Environment; import freemarker.template.*; import org.apache.commons.lang.ArrayUtils; import org.apache.commons.lang.StringUtils; import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; /** * ? - * * @author SHOP++ Team * @version 3.0 */ 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 PAGENO_PARAMETER_NAME = "pageNo"; /** "?"??? */ 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 * ? * @return ? */ protected Integer getPageNo(Map<String, TemplateModel> params) throws TemplateModelException { Integer pageNo = FreemarkerUtils.getParameter(PAGENO_PARAMETER_NAME, Integer.class, params); if (pageNo == null) { pageNo = 1; } return pageNo; } /** * ?? * * @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); } }