package me.codeboy.common.framework.workflow.core;

import com.google.gson.reflect.TypeToken;
import me.codeboy.common.base.log.CBPrint;
import me.codeboy.common.framework.workflow.bean.CBCommonResult;
import me.codeboy.common.framework.workflow.util.CBGsonUtil;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Type;

/**
 * process and wrap data, then push data to client
 *
 * @author yuedong.li
 */
public class CBResponseController {

    /**
     * process and wrap data
     *
     * @param result result data
     */
    public static <T> void process(T result) {
        process(new CBCommonResult<>(result));
    }

    /**
     * process and push description to client
     *
     * @param description description data
     */
    public static void process(String description) {
        process(new CBCommonResult<>(CBCommonResultCode.SUCCESS, description));
    }

    /**
     * process and push description to client
     *
     * @param description description data
     */
    public static void process(CBCommonResultCode resultCode, String description) {
        process(new CBCommonResult<>(resultCode, description));
    }


    /**
     * process data
     *
     * @param result result data
     */
    public static <T> void process(CBCommonResult<T> result) {
        process(result, new TypeToken<CBCommonResult<T>>() {
        }.getType());
    }


    /**
     * process raw data
     *
     * @param result result
     */
    public static <T> void processRaw(T result) {
        process(result, new TypeToken<T>() {
        }.getType());
    }

    /**
     * process data and push data to client
     *
     * @param result result data
     * @param type   data type
     */
    private static <T> void process(T result, Type type) {
        try {
            HttpServletResponse response = ServletActionContext.getResponse();
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.write(CBGsonUtil.getInstance().toJson(result, type));
            out.flush();
            out.close();
        } catch (IOException e) {
            CBPrint.log(e.getMessage());
            e.printStackTrace();
        }
    }
}
@@ -751,260 +751,8 @@
  %7D%0A%0A
-    /**%0A     * process and push description to client%0A     *%0A     * @param description description data%0A     */%0A    public static void process(String description) %7B%0A        process(new CBCommonResult%3C%3E(CBCommonResultCode.SUCCESS, description));%0A    %7D%0A%0A
     

delete conflict method