package org.ssh.app.util;

import java.io.PrintWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.util.PropertyFilter;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;

public class JsonViewUtil {
    private static Logger logger = LoggerFactory.getLogger(JsonViewUtil.class);

    /**
     * Generates modelMap to return in the modelAndView
     * @param contacts
     * @return
     */
    public static ModelAndView getModelMap(List<? extends Object> data){

        Map<String,Object> modelMap = new HashMap<String,Object>(3);
        modelMap.put("total", data.size());
        modelMap.put("data", data);
        modelMap.put("success", true);

        logger.info("json,getModelMap...");

        return new ModelAndView("jsonView", modelMap);
    }

    /**
     * Generates modelMap to return in the modelAndView in case
     * of exception
     * @param msg message
     * @return
     */
    public static ModelAndView getModelMapError(String msg){

        Map<String,Object> modelMap = new HashMap<String,Object>(2);
        modelMap.put("message", msg);
        modelMap.put("success", false);

        return new ModelAndView("jsonView",modelMap);
    }

    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * 'totalCount':Grid的JSONReader的totalProperty属性必须设置为"totalCount" <br>
     * 'rows':Grid的JSONReader的root属性必须设置为"rows"
     */
    public static void buildJSONDataResponse(HttpServletResponse response,
            String jsonString, Long total) throws Exception {
        StringBuffer sb = new StringBuffer();
        sb.append("{\"totalCount\":" + total + ",\"rows\":");
        sb.append(jsonString);
        sb.append("}");

        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(sb.toString());
    }

    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * 'totalCount':Grid的JSONReader的totalProperty属性必须设置为"totalCount" <br>
     * 'rows':Grid的JSONReader的root属性必须设置为"rows"
     */
    public static void buildJSONDataResponse(HttpServletResponse response,
            List<? extends Object> data, Long total) throws Exception {
        //JsonConfig cfg = new JsonConfig();

//        cfg.setJsonPropertyFilter(new PropertyFilter() {
//            @Override
//            public boolean apply(Object arg0, String arg1, Object arg2) {
//                System.out.println("oo:" + arg1);
//
//                if (arg1.equals("childs") || arg1.equals("parent")) {
//                    return true;
//                } else {
//                    return false;
//                }
//            }
//        });
        //cfg.setExcludes(new String[]{"handler","hibernateLazyInitializer"});
        //cfg.registerJsonValueProcessor(java.util.Date.class, new JsonValueProcessorImpl());
        //cfg.registerJsonValueProcessor(java.sql.Date.class, new JsonValueProcessorImpl());

        JSONArray jsonArray = JSONArray.fromObject(data);//, cfg);
        StringBuffer sb = new StringBuffer();
        sb.append("{\"totalCount\":" + total + ",\"rows\":");
        sb.append(jsonArray.toString());
        sb.append("}");

        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(sb.toString());
    }

    /**
     * 响应生成Grid格式的JSON数据集 <br>
     * JSON对象的属性可以根据需要自定义设置，通过Map添加键值即可<br>
     * 如: Map<String,Object> map = new HashMap<String,Object>();<br>
     * map.put("totalCount",125);<br>
     * map.put("success",true);<br>
     * map.put("data",list);
     */
    public static void buildCustomJSONDataResponse(
            HttpServletResponse response, Map<String, ? extends Object> data)
            throws Exception {
        JSONObject jsonObject = JSONObject.fromObject(data);
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(jsonObject.toString());
    }

    /**
     * 响应非Grid形式的JSON数据集的请求
     */
    public static void buildJSONResponse(HttpServletResponse response,
            List<? extends Object> data) throws Exception {
        JSONArray jsonArray = JSONArray.fromObject(data);
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(jsonArray.toString());
    }

    /**
     * 响应非Grid形式的JSON数据集的请求
     */
    public static void buildJSONObjectResponse(HttpServletResponse response,
            JSONObject data) throws Exception {
        response.setContentType("text/json; charset=UTF-8");
        PrintWriter out = response.getWriter();
        out.write(data.toString());
    }


}

@@ -2544,18 +2544,16 @@
         
-//
 JsonConf
@@ -2582,18 +2582,16 @@
 g();%0D%0A%0D%0A
-//
         
@@ -2644,35 +2644,8 @@
  %7B%0D%0A
-//            @Override%0D%0A//
     
@@ -2711,26 +2711,24 @@
 ct arg2) %7B%0D%0A
-//
             
@@ -2770,14 +2770,10 @@
 );%0D%0A
-//%0D%0A//
+%0D%0A
     
@@ -2835,26 +2835,24 @@
 arent%22)) %7B%0D%0A
-//
             
@@ -2869,26 +2869,24 @@
 turn true;%0D%0A
-//
             
@@ -2899,18 +2899,16 @@
 else %7B%0D%0A
-//
         
@@ -2934,18 +2934,16 @@
 false;%0D%0A
-//
         
@@ -2945,34 +2945,32 @@
              %7D%0D%0A
-//
             %7D%0D%0A/
@@ -2968,18 +2968,16 @@
      %7D%0D%0A
-//
         
@@ -3311,12 +3311,8 @@
 data
-);//
 , cf

update jsonviewUtil.java