Author: jacopoc
Date: Mon Oct 27 16:43:03 2014 New Revision: 1634590 URL: http://svn.apache.org/r1634590 Log: Refactored Ajax category tree to use the common request json response rather than a custom method. Thanks to Amardeep Singh Jhajj for the help on the jQuery modification required. This change removes the dependency on a specific JSON library from CategoryServices. Modified: ofbiz/branches/json-integration-refactoring/applications/product/src/org/ofbiz/product/category/CategoryServices.java ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/WEB-INF/controller.xml ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/category/CategoryTree.ftl Modified: ofbiz/branches/json-integration-refactoring/applications/product/src/org/ofbiz/product/category/CategoryServices.java URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/applications/product/src/org/ofbiz/product/category/CategoryServices.java?rev=1634590&r1=1634589&r2=1634590&view=diff ============================================================================== --- ofbiz/branches/json-integration-refactoring/applications/product/src/org/ofbiz/product/category/CategoryServices.java (original) +++ ofbiz/branches/json-integration-refactoring/applications/product/src/org/ofbiz/product/category/CategoryServices.java Mon Oct 27 16:43:03 2014 @@ -18,9 +18,6 @@ *******************************************************************************/ package org.ofbiz.product.category; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.io.Writer; import java.sql.Timestamp; import java.util.List; import java.util.Locale; @@ -31,7 +28,6 @@ import javax.servlet.http.HttpServletRes import javolution.util.FastList; import javolution.util.FastMap; -import net.sf.json.JSONObject; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.GeneralException; @@ -433,7 +429,7 @@ public class CategoryServices { // Please note : the structure of map in this function is according to the JSON data map of the jsTree @SuppressWarnings("unchecked") - public static void getChildCategoryTree(HttpServletRequest request, HttpServletResponse response){ + public static String getChildCategoryTree(HttpServletRequest request, HttpServletResponse response){ Delegator delegator = (Delegator) request.getAttribute("delegator"); String productCategoryId = request.getParameter("productCategoryId"); String isCatalog = request.getParameter("isCatalog"); @@ -527,42 +523,13 @@ public class CategoryServices { categoryList.add(josonMap); } List<Map<Object, Object>> sortedCategoryList = UtilMisc.sortMaps(categoryList, sortList); - toJsonObjectList(sortedCategoryList,response); + request.setAttribute("treeData", sortedCategoryList); } } } catch (GenericEntityException e) { e.printStackTrace(); + return "error"; } - } - - public static void toJsonObjectList(List attrList, HttpServletResponse response){ - StringBuilder jsonBuilder = new StringBuilder("["); - for (Object attrMap : attrList) { - JSONObject json = JSONObject.fromObject(attrMap); - jsonBuilder.append(json.toString()); - jsonBuilder.append(','); - } - jsonBuilder.append("{ } ]"); - String jsonStr = jsonBuilder.toString(); - if (UtilValidate.isEmpty(jsonStr)) { - Debug.logError("JSON Object was empty; fatal error!",module); - } - // set the X-JSON content type - response.setContentType("application/json"); - // jsonStr.length is not reliable for unicode characters - try { - response.setContentLength(jsonStr.getBytes("UTF8").length); - } catch (UnsupportedEncodingException e) { - Debug.logError("Problems with Json encoding",module); - } - // return the JSON String - Writer out; - try { - out = response.getWriter(); - out.write(jsonStr); - out.flush(); - } catch (IOException e) { - Debug.logError("Unable to get response writer",module); - } + return "success"; } } Modified: ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/WEB-INF/controller.xml URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/WEB-INF/controller.xml?rev=1634590&r1=1634589&r2=1634590&view=diff ============================================================================== --- ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/WEB-INF/controller.xml (original) +++ ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/WEB-INF/controller.xml Mon Oct 27 16:43:03 2014 @@ -3022,7 +3022,8 @@ under the License. <request-map uri="getChild"> <security auth="false" https="true"/> <event type="java" path="org.ofbiz.product.category.CategoryServices" invoke="getChildCategoryTree"/> - <response name="success" type="none"/> + <response name="success" type="request" value="json"/> + <response name="error" type="request" value="json"/> </request-map> <request-map uri="listMiniproduct"> <security auth="false" https="true"/> Modified: ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/category/CategoryTree.ftl URL: http://svn.apache.org/viewvc/ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/category/CategoryTree.ftl?rev=1634590&r1=1634589&r2=1634590&view=diff ============================================================================== --- ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/category/CategoryTree.ftl (original) +++ ofbiz/branches/json-integration-refactoring/applications/product/webapp/catalog/category/CategoryTree.ftl Mon Oct 27 16:43:03 2014 @@ -62,20 +62,24 @@ var rawdata = [ $.cookie('jstree_select', "<#if productCategoryId??>${productCategoryId}<#elseif prodCatalogId??>${prodCatalogId}<#elseif showProductCategoryId??>${showProductCategoryId}</#if>"); </#if> jQuery("#tree").jstree({ - "plugins" : [ "themes", "json_data","ui" ,"cookies", "types"], + "plugins" : [ "themes", "json_data","ui" ,"cookies", "types"], "json_data" : { "data" : rawdata, - "ajax" : { "url" : "<@ofbizUrl>getChild</@ofbizUrl>", "type" : "POST", - "data" : function (n) { - return { - "isCategoryType" : n.attr ? n.attr("isCatalog").replace("node_","") : 1 , - "isCatalog" : n.attr ? n.attr("isCatalog").replace("node_","") : 1 , - "productCategoryId" : n.attr ? n.attr("id").replace("node_","") : 1 , - "additionParam" : "','category" , - "hrefString" : "EditCategory?productCategoryId=" , - "onclickFunction" : "callDocument" - }; - } + "ajax" : { "url" : "<@ofbizUrl>getChild</@ofbizUrl>", + "type" : "POST", + "data" : function (n) { + return { + "isCategoryType" : n.attr ? n.attr("isCatalog").replace("node_","") : 1 , + "isCatalog" : n.attr ? n.attr("isCatalog").replace("node_","") : 1 , + "productCategoryId" : n.attr ? n.attr("id").replace("node_","") : 1 , + "additionParam" : "','category" , + "hrefString" : "EditCategory?productCategoryId=" , + "onclickFunction" : "callDocument" + }; + }, + Â Â Â Â success : function(data) { + return data.treeData; + Â Â Â Â } } }, "types" : { |
Free forum by Nabble | Edit this page |