/*	JsonUpdateBridge.java

	Purpose:
		
	Description:
		
	History:
		2013/6/25 by Dennis

	Copyright (C) 2010 Potix Corporation. All Rights Reserved.
*/
package org.zkoss.zss.jsp;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.zkoss.json.JSONObject;
import org.zkoss.zk.ui.Desktop;
import org.zkoss.zkplus.embed.Bridge;
import org.zkoss.zssex.ui.UpdateBridge;

/**
 * A abstract util wrapping {@link Bridge} provide easier functionality for a servlet
 * @author dennis
 * @since 3.0.0
 */
public abstract class JsonUpdateBridge extends UpdateBridge{
	private static final long serialVersionUID = -4680674868593365029L;
	
	protected String _zkjsFieldName = "_zssjspjs";
	
	public JsonUpdateBridge(ServletContext servletContext, HttpServletRequest req, HttpServletResponse resp,String desktopId){
		super(servletContext,req,resp,desktopId);
	}
	public String getJsFieldName(){
		return _zkjsFieldName;
	}

	/**
	 * Process the bridge, find the desktop and call {@link #process(Desktop)}, and set the zk ajax update script to field (return by {@link #getJsFieldName()})
	 * of the result json object
	 */
	public void process(JSONObject result){
		if(result==null){
			throw new IllegalArgumentException("json object is null");
		}
		String zkjs = process();
		result.put(getJsFieldName(), zkjs);
	}
	
}
