com.ewcms.publication.freemarker.directive.IncludeDirective.java Source code

Java tutorial

Introduction

Here is the source code for com.ewcms.publication.freemarker.directive.IncludeDirective.java

Source

/**
 * Copyright (c)2010-2011 Enterprise Website Content Management System(EWCMS), All rights reserved.
 * EWCMS PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * http://www.ewcms.com
 */

package com.ewcms.publication.freemarker.directive;

import java.io.IOException;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ewcms.common.lang.EmptyUtil;
import com.ewcms.core.site.model.Channel;
import com.ewcms.core.site.model.Site;
import com.ewcms.publication.freemarker.FreemarkerUtil;
import com.ewcms.publication.freemarker.GlobalVariable;
import com.ewcms.publication.service.ChannelPublishServiceable;
import com.ewcms.publication.service.TemplatePublishServiceable;

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 wangwei
 */
public class IncludeDirective implements TemplateDirectiveModel {

    private final static Logger logger = LoggerFactory.getLogger(IncludeDirective.class);

    private final static String PATH_PARAM_NAME = "path";
    private final static String PARSE_PARAM_NAME = "parse";
    private final static String CHANNEL_PARAM_NAME = "channel";
    private final static String NAME_PARAM_NAME = "name";

    private ChannelPublishServiceable channelService;
    private TemplatePublishServiceable templateService;

    private String pathParam = PATH_PARAM_NAME;
    private String parseParam = PARSE_PARAM_NAME;
    private String channelParam = CHANNEL_PARAM_NAME;
    private String nameParam = NAME_PARAM_NAME;

    public IncludeDirective(ChannelPublishServiceable channelService, TemplatePublishServiceable templateService) {
        this.channelService = channelService;
        this.templateService = templateService;
    }

    @SuppressWarnings("rawtypes")
    @Override
    public void execute(final Environment env, final Map params, final TemplateModel[] loopVars,
            final TemplateDirectiveBody body) throws TemplateException, IOException {
        String path = getPathValue(params);
        Integer siteId = getSiteIdValue(env);
        String uniquePath = null;
        if (EmptyUtil.isNotNull(path)) {
            uniquePath = getUniqueTemplatePath(siteId, path);
        } else {
            String name = getNameValue(params);
            if (EmptyUtil.isNotNull(name)) {
                Integer channelId = getChannelIdValue(env, params, siteId);
                uniquePath = getChannelTemplatePath(siteId, channelId, name);
            }
        }
        if (EmptyUtil.isNull(uniquePath)) {
            logger.error("Include template path is null");
            throw new TemplateModelException("Include template path is null");
        }
        logger.debug("Include template path is {}", uniquePath);
        boolean parse = getParseValue(params);
        env.include(uniquePath, env.getConfiguration().getDefaultEncoding(), parse);
    }

    /**
     * Freemarker?
     * 
     * @param env freemarker
     * @return
     * @throws TemplateException
     */
    private Integer getSiteIdValue(Environment env) throws TemplateException {
        Site site = (Site) FreemarkerUtil.getBean(env, GlobalVariable.SITE.toString());
        if (EmptyUtil.isNull(site)) {
            logger.error("Site is null in freemarker variable");
            throw new TemplateModelException("Site is null in freemarker variable");
        }
        logger.debug("Site is {}", site);
        return site.getId();
    }

    /**
     * ?
     * 
     * @param params ?
     * @return
     * @throws TemplateException
     */
    @SuppressWarnings("rawtypes")
    private String getPathValue(Map params) throws TemplateException {
        String path = FreemarkerUtil.getString(params, pathParam);
        logger.debug("Path is {}", path);
        return path;
    }

    /**
     * ??
     * 
     * @param env freemarker
     * @param params ?
     * @param siteId ?
     * @return ??
     * @throws TemplateException
     */
    @SuppressWarnings("rawtypes")
    private Integer getChannelIdValue(Environment env, Map params, Integer siteId) throws TemplateException {
        Channel channel = (Channel) FreemarkerUtil.getBean(params, channelParam);
        if (EmptyUtil.isNotNull(channel)) {
            logger.debug("Get channel is {}", channel);
            return channel.getId();
        }

        Integer id = FreemarkerUtil.getInteger(params, channelParam);
        if (EmptyUtil.isNotNull(id)) {
            logger.debug("Get channel id is {}", id);
            return id;
        }

        String path = FreemarkerUtil.getString(params, channelParam);
        logger.debug("Get channel by {}", path);
        if (EmptyUtil.isNotNull(path)) {
            channel = channelService.getChannelByUrlOrPath(siteId, path);
        }

        String name = GlobalVariable.CHANNEL.toString();
        logger.debug("Get value param is {}", name);
        channel = (Channel) FreemarkerUtil.getBean(env, name);

        if (EmptyUtil.isNull(channel)) {
            logger.error("Channel is null in freemarker variable");
            throw new TemplateModelException("Channel is null in freemarker variable");
        }

        return channel.getId();
    }

    /**
     * ???
     * 
     * @param params ??
     * @return
     * @throws TemplateException
     */
    @SuppressWarnings("rawtypes")
    private String getNameValue(Map params) throws TemplateException {
        String name = FreemarkerUtil.getString(params, nameParam);
        logger.debug("name is {}", name);
        return name;
    }

    /**
     * ???
     * 
     * @param params ??
     * @return
     * @throws TemplateModelException
     */
    @SuppressWarnings("rawtypes")
    private Boolean getParseValue(Map params) throws TemplateException {
        Boolean enabled = FreemarkerUtil.getBoolean(params, parseParam);
        return EmptyUtil.isNull(enabled) ? Boolean.TRUE : enabled;
    }

    /**
     * ?
     * 
     * <p>?+?</p>
     * 
     * @param siteId ?
     * @param path ?
     * @return
     */
    String getUniqueTemplatePath(Integer siteId, String path) {
        String nPath = StringUtils.removeStart(path, "/");
        String uPath = StringUtils.join(new Object[] { siteId, nPath }, "/");
        logger.debug("Include path is {}", uPath);
        return uPath;
    }

    /**
     * ?
     * 
     * @param siteId ?
     * @param path 
     * @param name ???
     * @return
     */
    String getChannelTemplatePath(Integer siteId, Integer channelId, String name) {
        return templateService.getUniquePathOfChannelTemplate(siteId, channelId, name);
    }

    /**
     * ????
     * 
     * @param pathParam ???
     */
    public void setPathParam(String pathParam) {
        this.pathParam = pathParam;
    }

    /**
     * ????
     * 
     * @param parseParam ????
     */
    public void setParseParam(String parseParam) {
        this.parseParam = parseParam;
    }

    /**
     * ????
     * 
     * @param channelParam ????
     */
    public void setChannelParam(String channelParam) {
        this.channelParam = channelParam;
    }

    /**
     * ????
     * 
     * @param nameParam ????
     */
    public void setNameParam(String nameParam) {
        this.nameParam = nameParam;
    }
}