package com.rndchina.aisin.util;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.CycleDetectionStrategy;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * <code>JSONFactory.java<code/>
 * <p>
 * 功能：JSON工具类
 * <p>
 * Copyright 神州锐达 2014 All right reserved.
 * 
 * @Author: Brain.Hou,2014年2月25日上午11:22:59
 */
@SuppressWarnings("all")
public class JSONFactory {

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

	/**
	 * 向前台传送执行结果
	 * 
	 * @param response
	 * @param code
	 * @param message
	 */
	public static void printResult(HttpServletResponse response, int code,
			String message) {
		response.setContentType("text/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out;
		String str = "{code : '" + code + "',message : '" + message + "'}";
		System.out.println("转换后的json串：" + str);
		try {
			out = response.getWriter();
			out.print(str);
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("转换json格式数据异常:", e);
		}
	}

	/**
	 * 将一个对象转换成json传到前台
	 * 
	 * @param response
	 * @param obj
	 * @param root
	 */
	public static void printData(HttpServletResponse response, int code,
			String message, Object obj) {
		response.setContentType("test/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out;
		try {
			out = response.getWriter();
			JSONObject json = new JSONObject();
			json.put("code", code);
			json.put("message", message);
			json.put("body", obj);
			out.print(json.toString());
			System.out.println("转换后的JSON：" + json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("转换json格式数据异常:", e);
		}
	}

	/**
	 * 将一个对象转换成json传到前台
	 * 
	 * @param response
	 * @param obj
	 * @param root
	 */
	public static void printData(HttpServletResponse response, int code,
			String message, Object obj, String root) {
		response.setContentType("test/html;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out;
		try {
			out = response.getWriter();
			JSONObject json = new JSONObject();
			json.put("code", code);
			json.put("message", message);
			if (Util.isNull(root)) {
				json.put(root, obj);
			} else {
				json.put("body", obj);
			}
			out.print(json.toString());
			System.out.println("转换后的JSON：" + json.toString());
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("转换json格式数据异常:", e);
		}
	}

	/**
	 * 向前台返回自定义字符串
	 * 
	 * @param response
	 * @param msg
	 */
	public static void printMsg(HttpServletResponse response, String msg) {
		response.setContentType("text/xml;charset=UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setHeader("Cache-Control", "no-cache");
		PrintWriter out;
		try {
			out = response.getWriter();
			// logger.info("字符串:" + msg);
			out.print(msg);
			out.flush();
			out.close();
		} catch (Exception e) {
			logger.error("转换json格式数据异常:", e);
		}
	}

	/**
	 * 将一个对象以JSON格式返回
	 * 
	 * @param response
	 * @param obj
	 */
	public static void outJSON(HttpServletResponse response, Object obj) {
		response.setContentType("text/plain;charset=UTF-8");
		JsonConfig config = getJsonConfig(new String[] { "createUser" });
		JSONObject jsonObj = JSONObject.fromObject(obj, config);
		PrintWriter pw = null;
		try {
			pw = response.getWriter();
			pw.print(jsonObj);
			logger.error(jsonObj.toString());
		} catch (IOException e) {
			logger.error("转换json格式数据异常:", e);
		}

	}

	private static JsonConfig getJsonConfig(String[] excludes) {
		JsonConfig jsonConfig = new JsonConfig();
		jsonConfig.setIgnoreDefaultExcludes(true);
		jsonConfig.setCycleDetectionStrategy(CycleDetectionStrategy.LENIENT);
		jsonConfig.setExcludes(excludes);
		return jsonConfig;
	}

}