Guest User

createOrderfromJSON

a guest
May 10th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 15.61 KB | None | 0 0
  1. public static Map<String, Object> createOrderfromJSON (DispatchContext dctx, Map<String, ? extends Object> context) {
  2.  
  3.         LocalDispatcher dispatcher = dctx.getDispatcher();
  4.         Delegator delegator = dctx.getDelegator();
  5.  
  6.         String jsonDoc = (String) context.get("orderJSON");
  7.         Debug.logInfo("orderJSON is :" + jsonDoc , module);
  8.  
  9.         JSONObject args = new JSONObject(jsonDoc);
  10.  
  11.         String partyId = args.getString("partyId");
  12.         String orderTypeId = args.getString("orderTypeId");
  13.         String currencyUom = args.getString("currencyUom");
  14.         String productStoreId = args.getString("productStoreId");
  15.  
  16.         Debug.logInfo("partyId is: " + partyId , module);
  17.  
  18.  
  19.  
  20.  
  21.         Map<String, Object> ctx = UtilMisc.<String, Object>toMap("partyId", partyId , "orderTypeId", orderTypeId,
  22.                                                          "currencyUom", currencyUom, "productStoreId", productStoreId);
  23.  
  24.  
  25.         List<GenericValue> orderPaymentInfo = new LinkedList<GenericValue>();
  26.  
  27.         if (args.has("orderPaymentInfo") ) {
  28.  
  29.                 JSONObject orderPaymentInfoJSON = args.getJSONObject("orderPaymentInfo");
  30.  
  31.  
  32.                 if ( orderPaymentInfoJSON.has("OrderContactMech") ) {
  33.                         JSONObject orderContactMechJSON = orderPaymentInfoJSON.getJSONObject("OrderContactMech");
  34.                         GenericValue orderContactMech = delegator.makeValue("OrderContactMech", UtilMisc.toMap (
  35.                                                 "contactMechId"            , orderContactMechJSON.getString("contactMechId") ,
  36.                                                 "contactMechPurposeTypeId" , orderContactMechJSON.getString("contactMechPurposeTypeId" )
  37.                                                 ) ) ;
  38.                         orderPaymentInfo.add(orderContactMech);
  39.                 }
  40.  
  41.  
  42.                 if (orderPaymentInfoJSON.has("OrderPaymentPreference")) {
  43.                         JSONObject orderPaymentPreferenceJSON= orderPaymentInfoJSON.getJSONObject("OrderPaymentPreference");
  44.                         GenericValue orderPaymentPreference = delegator.makeValue("OrderPaymentPreference", UtilMisc.toMap(
  45.                                                 "paymentMethodId", orderPaymentPreferenceJSON.getString("paymentMethodId" ) ,
  46.                                                 "paymentMethodTypeId", orderPaymentPreferenceJSON.getString("paymentMethodTypeId" ),
  47.                                                 "statusId", orderPaymentPreferenceJSON.getString("statusId" ),
  48.                                                 "overflowFlag", orderPaymentPreferenceJSON.getString("overflowFlag"),
  49.                                                 "maxAmount",  orderPaymentPreferenceJSON.getBigDecimal("maxAmount")
  50.                                                 ) );
  51.                         orderPaymentInfo.add(orderPaymentPreference);
  52.                 }
  53.  
  54.                 ctx.put("orderPaymentInfo", orderPaymentInfo);
  55.         }
  56.  
  57.  
  58.         List<GenericValue> orderItemShipGroupInfo = new LinkedList<GenericValue>();
  59.         if (args.has("orderItemShipGroupInfo") ) {
  60.  
  61.                 JSONObject orderItemShipGroupInfoJSON = args.getJSONObject("orderItemShipGroupInfo");
  62.  
  63.                 if (orderItemShipGroupInfoJSON.has("OrderContactMech"))  {
  64.                         JSONObject orderContactMechJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderContactMech");
  65.                         GenericValue orderContactMech = delegator.makeValue("OrderContactMech", UtilMisc.toMap (
  66.                                                 "contactMechId"            , orderContactMechJSON.getString("contactMechId") ,
  67.                                                 "contactMechPurposeTypeId" , orderContactMechJSON.getString("contactMechPurposeTypeId" )
  68.                                                 ) ) ;
  69.                         orderItemShipGroupInfo.add(orderContactMech);
  70.                 }
  71.  
  72.                 if (orderItemShipGroupInfoJSON.has("OrderItemShipGroup")) {
  73.  
  74.                         JSONObject OrderItemShipGroupJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderItemShipGroup");
  75.                         GenericValue orderItemShipGroup = delegator.makeValue("OrderItemShipGroup", UtilMisc.toMap(
  76.                                                         "carrierPartyId"      , OrderItemShipGroupJSON.getString("carrierPartyId") ,
  77.                                                         "contactMechId"       , OrderItemShipGroupJSON.getString("contactMechId"),
  78.                                                         "isGift"              , OrderItemShipGroupJSON.getString("isGift"),
  79.                                                         "shipGroupSeqId"      , OrderItemShipGroupJSON.getString("shipGroupSeqId"),
  80.                                                         "shipmentMethodTypeId", OrderItemShipGroupJSON.getString("shipmentMethodTypeId")
  81.                                                 ));
  82.                         orderItemShipGroupInfo.add(orderItemShipGroup);
  83.                 }
  84.  
  85.                 if (orderItemShipGroupInfoJSON.has("OrderItemShipGroupAssoc")) {
  86.                         JSONObject orderItemShipGroupAssocJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderItemShipGroupAssoc");
  87.                         GenericValue orderItemShipGroupAssoc = delegator.makeValue("OrderItemShipGroupAssoc", UtilMisc.toMap(
  88.                                                         "orderItemSeqId"  , orderItemShipGroupAssocJSON.getString("orderItemSeqId") ,
  89.                                                         "quantity"        , orderItemShipGroupAssocJSON.getBigDecimal("quantity"),
  90.                                                         "shipGroupSeqId"  , orderItemShipGroupAssocJSON.getString("shipGroupSeqId")
  91.                                                         ));
  92.                         orderItemShipGroupInfo.add(orderItemShipGroupAssoc);
  93.  
  94.                 }
  95.  
  96.  
  97.                 if (orderItemShipGroupInfoJSON.has("OrderAdjustment")) {
  98.                         JSONArray OrderAdjustmentsJSON = orderItemShipGroupInfoJSON.getJSONArray("OrderAdjustment") ;
  99.                         for (int i=0 ; i < OrderAdjustmentsJSON.length() ; i++) {
  100.                                 JSONObject item = OrderAdjustmentsJSON.getJSONObject(i);
  101.                                 String orderAdjustmentTypeId = item.getString("orderAdjustmentTypeId");
  102.                                 Debug.logInfo("OrderAdjustment type is:" + orderAdjustmentTypeId , module);
  103.  
  104.                                 GenericValue orderAdjustment = null;
  105.                                 if (orderAdjustmentTypeId.equals("SHIPPING_CHARGES") ) {
  106.                                         orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
  107.                                                         "orderAdjustmentTypeId", orderAdjustmentTypeId ,
  108.                                                         "shipGroupSeqId",        item.getString("shipGroupSeqId") ,
  109.                                                         "amount",                item.getBigDecimal("amount")
  110.                                                         ));
  111.                                         orderItemShipGroupInfo.add(orderAdjustment);
  112.  
  113.                                 } else if (orderAdjustmentTypeId.equals("SALES_TAX") ) {
  114.                                         orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
  115.  
  116.                                                         "orderAdjustmentTypeId", orderAdjustmentTypeId,
  117.                                                         "orderItemSeqId",        item.getString("orderItemSeqId") ,
  118.                                                         "overrideGlAccountId",   item.getString("overrideGlAccountId") ,
  119.                                                         "primaryGeoId",          item.getString("primaryGeoId") ,
  120.                                                         "shipGroupSeqId",        item.getString("shipGroupSeqId") ,
  121.                                                         "sourcePercentage",      item.getBigDecimal("sourcePercentage")
  122.  
  123.                                                         ));
  124.                                         if (item.has("attrs")) {
  125.                                                 JSONObject attrs = item.getJSONObject("attrs") ;
  126.                                                 orderAdjustment.set("taxAuthGeoId",          attrs.getString("taxAuthGeoId") );
  127.                                                 orderAdjustment.set("taxAuthPartyId",        attrs.getString("taxAuthPartyId") );
  128.                                                 orderAdjustment.set("taxAuthorityRateSeqId", attrs.getString("taxAuthorityRateSeqId") );
  129.                                                 orderAdjustment.set("amount",                attrs.getBigDecimal("amount") );
  130.                                                 orderAdjustment.set("comments",              attrs.getString("comments") );
  131.                                         }
  132.                                         orderItemShipGroupInfo.add(orderAdjustment);
  133.                                 } else {
  134.                                         Debug.logError("Invalid OrderAdjustment type is:" + orderAdjustmentTypeId , module);
  135.                                 }
  136.                         }
  137.                 }
  138.  
  139.                 ctx.put("orderItemShipGroupInfo", orderItemShipGroupInfo);
  140.         }
  141.  
  142.         List<GenericValue> orderAdjustments = new LinkedList<GenericValue>();
  143.  
  144.         if (args.has("orderAdjustments") ) {
  145.                 JSONArray OrderAdjustmentsJSON = args.getJSONArray("orderAdjustments") ;
  146.                 for (int i=0 ; i < OrderAdjustmentsJSON.length() ; i++) {
  147.                         JSONObject item = OrderAdjustmentsJSON.getJSONObject(i);
  148.                         String orderAdjustmentTypeId = item.getString("orderAdjustmentTypeId");
  149.                         Debug.logInfo("OrderAdjustment type is:" + orderAdjustmentTypeId , module);
  150.                         GenericValue orderAdjustment = null;
  151.                         if (orderAdjustmentTypeId.equals("PROMOTION_ADJUSTMENT") ) {
  152.  
  153.                                 orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
  154.                                                 "orderAdjustmentTypeId", orderAdjustmentTypeId,
  155.                                                 "productPromoActionSeqId", item.getString("productPromoActionSeqId") ,
  156.                                                 "productPromoId",          item.getString("productPromoId") ,
  157.                                                 "productPromoRuleId",      item.getString("productPromoRuleId") ,
  158.                                                 "amount",                  item.getBigDecimal("amount")
  159.                                                 ));
  160.                                 orderAdjustments.add(orderAdjustment);
  161.  
  162.                         } else {
  163.                                 Debug.logError("Invalid OrderAdjustment type is:" + orderAdjustmentTypeId , module);
  164.                         }
  165.                 }
  166.                 ctx.put("orderAdjustments", orderAdjustments);
  167.         }
  168.  
  169.  
  170.  
  171.         List<GenericValue> orderItems = new LinkedList<GenericValue>();
  172.         if (args.has("orderItems")) {
  173.                 JSONArray orderItemsJSON = args.getJSONArray("orderItems") ;
  174.                 for (int i=0 ; i < orderItemsJSON.length() ; i++) {
  175.                         JSONObject itemObj = orderItemsJSON.getJSONObject(i);
  176.  
  177.                         if (itemObj.has("OrderItem")) {
  178.                                 JSONObject item = itemObj.getJSONObject("OrderItem");
  179.                                 GenericValue orderItem = delegator.makeValue("OrderItem", UtilMisc.toMap(
  180.                                                                  "orderItemSeqId", item.getString("orderItemSeqId") ,
  181.                                                                 "orderItemTypeId", item.getString("orderItemTypeId"),
  182.                                                                   "prodCatalogId", item.getString("prodCatalogId"),
  183.                                                                       "productId", item.getString("productId") ,
  184.                                                                        "quantity", item.getBigDecimal("quantity"),
  185.                                                                  "selectedAmount", item.getBigDecimal("selectedAmount")
  186.                                                                 ));
  187.                                 if (itemObj.has("attrs")) {
  188.                                         JSONObject attrs = itemObj.getJSONObject("attrs") ;
  189.                                                 orderItem.set("isPromo", attrs.getString("isPromo") ) ;
  190.                                         orderItem.set("isModifiedPrice", attrs.getString("isModifiedPrice"));
  191.                                               orderItem.set("unitPrice", attrs.getBigDecimal("unitPrice") );
  192.                                           orderItem.set("unitListPrice", attrs.getBigDecimal("unitListPrice") );
  193.                                                orderItem.set("statusId", attrs.getString("statusId")  );
  194.                                 }
  195.                                 Debug.logInfo("Adding OrderItem..." , module);
  196.                                 orderItems.add(orderItem);
  197.                         }
  198.                 }
  199.                 ctx.put("orderItems", orderItems);
  200.         }
  201.  
  202.         List<GenericValue> orderTerms = new LinkedList<GenericValue>();
  203.         ctx.put("orderTerms", orderTerms);
  204.  
  205.         GenericValue OrderContactMech = delegator.makeValue("OrderContactMech");
  206.         OrderContactMech.set("contactMechPurposeTypeId", "SHIPPING_LOCATION");
  207.         OrderContactMech.set("contactMechId", "10000");
  208.         List<GenericValue> orderContactMechs = new LinkedList<GenericValue>();
  209.         orderContactMechs.add(OrderContactMech);
  210.  
  211.  
  212.         if (args.has("placingCustomerPartyId")) {
  213.                 ctx.put("placingCustomerPartyId", args.getString("placingCustomerPartyId") );
  214.         }
  215.  
  216.         if (args.has("endUserCustomerPartyId")) {
  217.                 ctx.put("endUserCustomerPartyId", args.getString("endUserCustomerPartyId") );
  218.         }
  219.  
  220.         if (args.has("shipToCustomerPartyId")) {
  221.                 ctx.put("shipToCustomerPartyId", args.getString("shipToCustomerPartyId") );
  222.         }
  223.  
  224.         if (args.has("billToCustomerPartyId")) {
  225.                 ctx.put("billToCustomerPartyId", args.getString("billToCustomerPartyId") );
  226.         }
  227.  
  228.         if (args.has("billFromVendorPartyId")) {
  229.                 ctx.put("billFromVendorPartyId", args.getString("billFromVendorPartyId") );
  230.         }
  231.  
  232.  
  233. /* custom code */
  234.  
  235.         ctx.put("login.username",  "admin" );
  236.         ctx.put("login.password",  "ofbiz" );
  237.  
  238.         Map<String, Object> resultMap = ServiceUtil.returnSuccess();
  239.  
  240.         String orderId="";
  241.         String statusId="";
  242.  
  243.         try {
  244.                 Map<String, Object> resp = dispatcher.runSync("storeOrder", ctx);
  245.  
  246.                 orderId = (String) resp.get("orderId");
  247.                 statusId = (String) resp.get("statusId");
  248.  
  249.                 Debug.logInfo("orderId is :" + orderId , module);
  250.                 Debug.logInfo("orderId status is :" + statusId , module);
  251.  
  252.  
  253.  
  254.         } catch (Exception e) {
  255.                 String errMsg = "Error calling storeOrder: " + e.toString();
  256.                 Debug.logError(e, errMsg, module);
  257.         }
  258.  
  259.  
  260.         resultMap.put("orderId", orderId);
  261.         resultMap.put("statusId", statusId);
  262.         return resultMap ;
  263.     }
Add Comment
Please, Sign In to add comment