日期:2014-05-16  浏览次数:20501 次

liferay 返回 json,jquery处理

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.json.JSONObject;
import com.autotoll.jv.erp.proxy.PortletActionBeanProxy;
import com.autotoll.jv.erp.util.ErpStatus;
import com.autotoll.jv.pojo.impl.UserInfo;
import com.liferay.portal.json.JSONArrayImpl;
import com.liferay.portal.json.JSONObjectImpl;
import com.liferay.portal.kernel.json.JSONArray;
import com.liferay.portal.model.User;
import com.liferay.portal.struts.AJAXAction;
import com.liferay.portal.theme.ThemeDisplay;
import com.liferay.portal.util.WebKeys;

public class GetCustNodeAjax extends AJAXAction{

	@SuppressWarnings("unchecked")
	public String getText(ActionMapping arg0, ActionForm arg1,
			HttpServletRequest req, HttpServletResponse response) throws Exception {
		
		ThemeDisplay themeDisplay = 
			(ThemeDisplay) req.getAttribute(WebKeys.THEME_DISPLAY);
		User user = themeDisplay.getUser();
		UserInfo userInfo = PortletActionBeanProxy.
				getInstance().getUserInfoSvc().
				findUserInfoById(user.getUuid());
		
		String lanageId = themeDisplay.getLanguageId();
		String custId = req.getParameter("custId");
		//货源地
		String s_type = ErpStatus.COMP_NODE_HUOYUAN;
		List<Map<String, Object>> s_cityList = PortletActionBeanProxy.
				getInstance().getCityTableSvc().
				findCityTableByNodeId(new Integer(custId), 
				userInfo.getCompInfo().getCompId(), new Short(s_type));
		//目的地
		String d_type = ErpStatus.COMP_NODE_MUDIDI;
		List<Map<String, Object>> d_cityList = PortletActionBeanProxy.
				getInstance().getCityTableSvc().
				findCityTableByNodeId(new Integer(custId), 
				userInfo.getCompInfo().getCompId(), new Short(d_type));
		
		JSONObject jsonObj = new JSONObject();
		//货源地和目的地
		jsonObj = createJson(s_cityList,d_cityList,lanageId);
		//System.out.println("json----"+jsonObj.toString());
		return jsonObj.toString();
	}
	/**
	 * 创建json
	 * @param list
	 * @param lanageId
	 */
	private JSONObject createJson(List<Map<String,Object>> s_list,
			List<Map<String,Object>> d_list,String lanageId){
		
		JSONObject jsonObj = new JSONObject();
		JSONObjectImpl jsonImpl = new JSONObjectImpl();
		JSONArray s_jsonArrId = new JSONArrayImpl();
		JSONArray s_jsonArrAdr = new JSONArrayImpl();
		//货源地
		if (s_list !=null && s_list.size()>0){
			for(int i=0;i<s_list.size();i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map = s_list.get(i);
				String custNodeId = String.valueOf(map.get("0"));
				String proName_tc = String.valueOf(map.get("1"));
				String proName_en = String.valueOf(map.get("2"));
				String proName_sc = String.valueOf(map.get("3"));
				String cityName_tc = String.valueOf(map.get("4"));
				String cityName_en = String.valueOf(map.get("5"));
				String cityName_sc = String.valueOf(map.get("6"));
				String nodeAdr = String.valueOf(map.get("7"));

				s_jsonArrId.put(custNodeId);
				jsonImpl.put("s_custNodeId", s_jsonArrId);
				if (lanageId.equals("zh_CN")) {
					s_jsonArrAdr.put(proName_sc+cityName_sc+nodeAdr);
				} else if (lanageId.equals("zh_TW")){
					s_jsonArrAdr.put(proName_tc+cityName_tc+nodeAdr);
				}else {
					s_jsonArrAdr.put(proName_en+cityName_en+nodeAdr);
				}
			}
		}
		//目的地
		JSONArray d_jsonArrId = new JSONArrayImpl();
		JSONArray d_jsonArrAdr = new JSONArrayImpl();
		
		if (d_list !=null && d_list.size()>0){ 
			for(int i=0;i<d_list.size();i++) {
				Map<String, Object> map = new HashMap<String, Object>();
				map = d_list.get(i);
				String custNodeId = String.valueOf(map.get("0"));
				String proName_tc = String.valueOf(map.get("1"));
				String proName_en = String.valueOf(map.get("2"));
				String proName_sc = String.valueOf(map.get