/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights reserved. <a>http://www.crunii.com</a>
 */
package com.crunii.micro.common.template;

import com.crunii.micro.common.utils.ResUtil;
import freemarker.cache.StringTemplateLoader;
import freemarker.template.Configuration;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;

/**
 * <pre>
 * 使用方法如下：模板文件放到resources/temps下面
 * @Configuration
 * public class TemplateConfiguration {
 *
 *    @Bean("tempConfiguration")
 *    public FreemarkerTempConfiguration regTempConfiguration() {
 * 		FreemarkerTempConfiguration conf = new FreemarkerTempConfiguration();
 * 		conf.setResourcePath("classpath:temps/*");
 * 		return conf;
 *    }
 * }
 *
 * 使用范例
 *    @Resource(name = "tempConfiguration")
 * 	  private Configuration configuration;
 *
 *    @Test
 *    public void testTemplate() throws Exception {
 * 		Template template = configuration.getTemplate("salerQuery.ftl");
 * 		Map<String, Object> map = new HashMap<>();
 * 		map.put("queryObj", "1");
 * 		map.put("queryType", "1");
 * 		StringWriter sw = new StringWriter(256);
 * 		template.process(map, sw);
 * 		String body = sw.toString();
 * 		String url = "http://8.8.8.8:10030/a/b/c";
 * 		StringEntity entity = new StringEntity(body, ContentType.APPLICATION_SOAP_XML);
 * 		String recvXml = HttpClientUtil.reqStrByEntity(url, entity, "POST");
 * 		//获取某个节点的xml内容
 * 		String nodeXml = XmlUtil.getNodeXml(recvXml, "return");
 * 		//xml直接转对象
 * 		ChannelQuerySaleInfoResp rsp = JaxbUtil.xml2Bean(nodeXml, ChannelQuerySaleInfoResp.class);
 * 		System.out.println(rsp);
 *    }
 * 	本地测试实例参见
 *  com.crunii.micro.intf.httpdemo.test.TempTest
 * </pre>
 *
 * @author 田平 create 2018年10月12日下午3:54:13
 */
@Slf4j
public class FreemarkerTempConfiguration implements FactoryBean<Configuration> {

	@Autowired
	private ResourcePatternResolver resR;

	private String resourcePath;

	/**
	 * @return the resourcePath
	 */
	public String getResourcePath() {
		return resourcePath;
	}

	/**
	 * @param resourcePath the resourcePath to set
	 */
	public void setResourcePath(String resourcePath) {
		this.resourcePath = resourcePath;
	}

	public ResourcePatternResolver getResR() {
		return resR;
	}

	public void setResR(ResourcePatternResolver resR) {
		this.resR = resR;
	}

	@Override
	public Configuration getObject() {
		Configuration cfg = new Configuration(Configuration.VERSION_2_3_25);
		cfg.setDefaultEncoding("UTF-8");
		StringTemplateLoader loader = new StringTemplateLoader();

		org.springframework.core.io.Resource[] resources;
		try {
			resources = resR.getResources(resourcePath);
			for (org.springframework.core.io.Resource res : resources) {
				loader.putTemplate(res.getFilename(), ResUtil.getContent(res.getInputStream()));
			}
		} catch (IOException e) {
			log.error("initial Configuration error!", e);
			throw new RuntimeException(e);
		}
		cfg.setTemplateLoader(loader);
		return cfg;
	}

	/**
	 * @see FactoryBean#getObjectType()
	 */
	@Override
	public Class<?> getObjectType() {

		return Configuration.class;
	}

	/**
	 * @see FactoryBean#isSingleton()
	 */
	@Override
	public boolean isSingleton() {
		return false;
	}

}
