package org.nutz.mvc.impl.processor;

import javax.servlet.http.HttpServletRequest;

import org.nutz.lang.Lang;
import org.nutz.lang.Strings;
import org.nutz.lang.util.Context;
import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionInfo;
import org.nutz.mvc.NutConfig;
import org.nutz.mvc.View;
import org.nutz.mvc.ViewMaker;
import org.nutz.mvc.ViewMaker2;
import org.nutz.mvc.view.ViewWrapper;
import org.nutz.mvc.view.VoidView;

public class ViewProcessor extends AbstractProcessor {

    protected View view;
    public static final String DEFAULT_ATTRIBUTE = "obj";
    private static final Log log = Logs.get();
    
    @Override
    public void init(NutConfig config, ActionInfo ai) throws Throwable {
        view = evalView(config, ai, ai.getOkView());
    }

    public void process(ActionContext ac) throws Throwable {
        Object re = ac.getMethodReturn();
        Object err = ac.getError();
        if (re != null && re instanceof View) {
            if (re instanceof ViewWrapper)
                putRequestAttribute(ac.getRequest(), ((ViewWrapper)re).getData());
            ((View) re).render(ac.getRequest(), ac.getResponse(), err);
        } else {
            putRequestAttribute(ac.getRequest(), null == re ? err : re);
            view.render(ac.getRequest(), ac.getResponse(), null == re ? err : re);
        }
        doNext(ac);
    }
    
    /**
     * 保存对象到attribute
     */
    public static void putRequestAttribute(HttpServletRequest req, Object re){
        if (null != re){
            if(re instanceof Context){
                Context context = (Context) re;
                for(String key : context.keys()){
                    req.setAttribute(key, context.get(key));
                }
            } else {
                req.setAttribute(ViewProcessor.DEFAULT_ATTRIBUTE, re);
            }
        }
    }

    public static View evalView(NutConfig config, ActionInfo ai, String viewType) {
        if (Strings.isBlank(viewType))
            return new VoidView();

        String str = viewType;
        int pos = str.indexOf(':');
        String type, value;
        if (pos > 0) {
            type = Strings.trim(str.substring(0, pos).toLowerCase());
            value = Strings.trim(pos >= (str.length() - 1) ? null : str.substring(pos + 1));
        } else {
            type = str;
            value = null;
        }
        
        //需要特别提醒一下使用jsonView,但方法的返回值是String的!!
        if("json".equals(type) && String.class.equals(ai.getMethod().getReturnType())) {
            log.warn("Not a good idea : Return String ,and using JsonView!! (Using @Ok(\"raw\") or return map/list/pojo)--> " + Lang.simpleMetodDesc(ai.getMethod()));
        }
        
        for (ViewMaker maker : ai.getViewMakers()) {
        	if (maker instanceof ViewMaker2) {
        		View view = ((ViewMaker2)maker).make(config, ai, type, value);
        		if (view != null)
        			return view;
        	}
        	View view = maker.make(config.getIoc(), type, value);
            if (null != view)
                return view;
        }
        throw Lang.makeThrow("Can not eval %s(\"%s\") View for %s", viewType, str, ai.getMethod());
    }
}

@@ -752,32 +752,355 @@
 ows Throwable %7B%0A
+        //%E9%9C%80%E8%A6%81%E7%89%B9%E5%88%AB%E6%8F%90%E9%86%92%E4%B8%80%E4%B8%8B%E4%BD%BF%E7%94%A8jsonView,%E4%BD%86%E6%96%B9%E6%B3%95%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC%E6%98%AFString%E7%9A%84!!%0A        if(%22json%22.equals(ai.getOkView()) && String.class.equals(ai.getMethod().getReturnType())) %7B%0A            log.warn(%22Not a good idea : Return String ,and using JsonView!! (Using @Ok(%5C%22raw%5C%22) or return map/list/pojo)--%3E %22 + Lang.simpleMetodDesc(ai.getMethod()));%0A        %7D%0A
         view = e
@@ -2753,330 +2753,8 @@
    %0A
-        //%E9%9C%80%E8%A6%81%E7%89%B9%E5%88%AB%E6%8F%90%E9%86%92%E4%B8%80%E4%B8%8B%E4%BD%BF%E7%94%A8jsonView,%E4%BD%86%E6%96%B9%E6%B3%95%E7%9A%84%E8%BF%94%E5%9B%9E%E5%80%BC%E6%98%AFString%E7%9A%84!!%0A        if(%22json%22.equals(type) && String.class.equals(ai.getMethod().getReturnType())) %7B%0A            log.warn(%22Not a good idea : Return String ,and using JsonView!! (Using @Ok(%5C%22raw%5C%22) or return map/list/pojo)--%3E %22 + Lang.simpleMetodDesc(ai.getMethod()));%0A        %7D%0A        %0A
     

fix issue #533 : 仅对@Ok进行JsonView返回值检测