Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static Map<String, Object> createOrderfromJSON (DispatchContext dctx, Map<String, ? extends Object> context) {
- LocalDispatcher dispatcher = dctx.getDispatcher();
- Delegator delegator = dctx.getDelegator();
- String jsonDoc = (String) context.get("orderJSON");
- Debug.logInfo("orderJSON is :" + jsonDoc , module);
- JSONObject args = new JSONObject(jsonDoc);
- String partyId = args.getString("partyId");
- String orderTypeId = args.getString("orderTypeId");
- String currencyUom = args.getString("currencyUom");
- String productStoreId = args.getString("productStoreId");
- Debug.logInfo("partyId is: " + partyId , module);
- Map<String, Object> ctx = UtilMisc.<String, Object>toMap("partyId", partyId , "orderTypeId", orderTypeId,
- "currencyUom", currencyUom, "productStoreId", productStoreId);
- List<GenericValue> orderPaymentInfo = new LinkedList<GenericValue>();
- if (args.has("orderPaymentInfo") ) {
- JSONObject orderPaymentInfoJSON = args.getJSONObject("orderPaymentInfo");
- if ( orderPaymentInfoJSON.has("OrderContactMech") ) {
- JSONObject orderContactMechJSON = orderPaymentInfoJSON.getJSONObject("OrderContactMech");
- GenericValue orderContactMech = delegator.makeValue("OrderContactMech", UtilMisc.toMap (
- "contactMechId" , orderContactMechJSON.getString("contactMechId") ,
- "contactMechPurposeTypeId" , orderContactMechJSON.getString("contactMechPurposeTypeId" )
- ) ) ;
- orderPaymentInfo.add(orderContactMech);
- }
- if (orderPaymentInfoJSON.has("OrderPaymentPreference")) {
- JSONObject orderPaymentPreferenceJSON= orderPaymentInfoJSON.getJSONObject("OrderPaymentPreference");
- GenericValue orderPaymentPreference = delegator.makeValue("OrderPaymentPreference", UtilMisc.toMap(
- "paymentMethodId", orderPaymentPreferenceJSON.getString("paymentMethodId" ) ,
- "paymentMethodTypeId", orderPaymentPreferenceJSON.getString("paymentMethodTypeId" ),
- "statusId", orderPaymentPreferenceJSON.getString("statusId" ),
- "overflowFlag", orderPaymentPreferenceJSON.getString("overflowFlag"),
- "maxAmount", orderPaymentPreferenceJSON.getBigDecimal("maxAmount")
- ) );
- orderPaymentInfo.add(orderPaymentPreference);
- }
- ctx.put("orderPaymentInfo", orderPaymentInfo);
- }
- List<GenericValue> orderItemShipGroupInfo = new LinkedList<GenericValue>();
- if (args.has("orderItemShipGroupInfo") ) {
- JSONObject orderItemShipGroupInfoJSON = args.getJSONObject("orderItemShipGroupInfo");
- if (orderItemShipGroupInfoJSON.has("OrderContactMech")) {
- JSONObject orderContactMechJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderContactMech");
- GenericValue orderContactMech = delegator.makeValue("OrderContactMech", UtilMisc.toMap (
- "contactMechId" , orderContactMechJSON.getString("contactMechId") ,
- "contactMechPurposeTypeId" , orderContactMechJSON.getString("contactMechPurposeTypeId" )
- ) ) ;
- orderItemShipGroupInfo.add(orderContactMech);
- }
- if (orderItemShipGroupInfoJSON.has("OrderItemShipGroup")) {
- JSONObject OrderItemShipGroupJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderItemShipGroup");
- GenericValue orderItemShipGroup = delegator.makeValue("OrderItemShipGroup", UtilMisc.toMap(
- "carrierPartyId" , OrderItemShipGroupJSON.getString("carrierPartyId") ,
- "contactMechId" , OrderItemShipGroupJSON.getString("contactMechId"),
- "isGift" , OrderItemShipGroupJSON.getString("isGift"),
- "shipGroupSeqId" , OrderItemShipGroupJSON.getString("shipGroupSeqId"),
- "shipmentMethodTypeId", OrderItemShipGroupJSON.getString("shipmentMethodTypeId")
- ));
- orderItemShipGroupInfo.add(orderItemShipGroup);
- }
- if (orderItemShipGroupInfoJSON.has("OrderItemShipGroupAssoc")) {
- JSONObject orderItemShipGroupAssocJSON = orderItemShipGroupInfoJSON.getJSONObject("OrderItemShipGroupAssoc");
- GenericValue orderItemShipGroupAssoc = delegator.makeValue("OrderItemShipGroupAssoc", UtilMisc.toMap(
- "orderItemSeqId" , orderItemShipGroupAssocJSON.getString("orderItemSeqId") ,
- "quantity" , orderItemShipGroupAssocJSON.getBigDecimal("quantity"),
- "shipGroupSeqId" , orderItemShipGroupAssocJSON.getString("shipGroupSeqId")
- ));
- orderItemShipGroupInfo.add(orderItemShipGroupAssoc);
- }
- if (orderItemShipGroupInfoJSON.has("OrderAdjustment")) {
- JSONArray OrderAdjustmentsJSON = orderItemShipGroupInfoJSON.getJSONArray("OrderAdjustment") ;
- for (int i=0 ; i < OrderAdjustmentsJSON.length() ; i++) {
- JSONObject item = OrderAdjustmentsJSON.getJSONObject(i);
- String orderAdjustmentTypeId = item.getString("orderAdjustmentTypeId");
- Debug.logInfo("OrderAdjustment type is:" + orderAdjustmentTypeId , module);
- GenericValue orderAdjustment = null;
- if (orderAdjustmentTypeId.equals("SHIPPING_CHARGES") ) {
- orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
- "orderAdjustmentTypeId", orderAdjustmentTypeId ,
- "shipGroupSeqId", item.getString("shipGroupSeqId") ,
- "amount", item.getBigDecimal("amount")
- ));
- orderItemShipGroupInfo.add(orderAdjustment);
- } else if (orderAdjustmentTypeId.equals("SALES_TAX") ) {
- orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
- "orderAdjustmentTypeId", orderAdjustmentTypeId,
- "orderItemSeqId", item.getString("orderItemSeqId") ,
- "overrideGlAccountId", item.getString("overrideGlAccountId") ,
- "primaryGeoId", item.getString("primaryGeoId") ,
- "shipGroupSeqId", item.getString("shipGroupSeqId") ,
- "sourcePercentage", item.getBigDecimal("sourcePercentage")
- ));
- if (item.has("attrs")) {
- JSONObject attrs = item.getJSONObject("attrs") ;
- orderAdjustment.set("taxAuthGeoId", attrs.getString("taxAuthGeoId") );
- orderAdjustment.set("taxAuthPartyId", attrs.getString("taxAuthPartyId") );
- orderAdjustment.set("taxAuthorityRateSeqId", attrs.getString("taxAuthorityRateSeqId") );
- orderAdjustment.set("amount", attrs.getBigDecimal("amount") );
- orderAdjustment.set("comments", attrs.getString("comments") );
- }
- orderItemShipGroupInfo.add(orderAdjustment);
- } else {
- Debug.logError("Invalid OrderAdjustment type is:" + orderAdjustmentTypeId , module);
- }
- }
- }
- ctx.put("orderItemShipGroupInfo", orderItemShipGroupInfo);
- }
- List<GenericValue> orderAdjustments = new LinkedList<GenericValue>();
- if (args.has("orderAdjustments") ) {
- JSONArray OrderAdjustmentsJSON = args.getJSONArray("orderAdjustments") ;
- for (int i=0 ; i < OrderAdjustmentsJSON.length() ; i++) {
- JSONObject item = OrderAdjustmentsJSON.getJSONObject(i);
- String orderAdjustmentTypeId = item.getString("orderAdjustmentTypeId");
- Debug.logInfo("OrderAdjustment type is:" + orderAdjustmentTypeId , module);
- GenericValue orderAdjustment = null;
- if (orderAdjustmentTypeId.equals("PROMOTION_ADJUSTMENT") ) {
- orderAdjustment = delegator.makeValue("OrderAdjustment", UtilMisc.toMap(
- "orderAdjustmentTypeId", orderAdjustmentTypeId,
- "productPromoActionSeqId", item.getString("productPromoActionSeqId") ,
- "productPromoId", item.getString("productPromoId") ,
- "productPromoRuleId", item.getString("productPromoRuleId") ,
- "amount", item.getBigDecimal("amount")
- ));
- orderAdjustments.add(orderAdjustment);
- } else {
- Debug.logError("Invalid OrderAdjustment type is:" + orderAdjustmentTypeId , module);
- }
- }
- ctx.put("orderAdjustments", orderAdjustments);
- }
- List<GenericValue> orderItems = new LinkedList<GenericValue>();
- if (args.has("orderItems")) {
- JSONArray orderItemsJSON = args.getJSONArray("orderItems") ;
- for (int i=0 ; i < orderItemsJSON.length() ; i++) {
- JSONObject itemObj = orderItemsJSON.getJSONObject(i);
- if (itemObj.has("OrderItem")) {
- JSONObject item = itemObj.getJSONObject("OrderItem");
- GenericValue orderItem = delegator.makeValue("OrderItem", UtilMisc.toMap(
- "orderItemSeqId", item.getString("orderItemSeqId") ,
- "orderItemTypeId", item.getString("orderItemTypeId"),
- "prodCatalogId", item.getString("prodCatalogId"),
- "productId", item.getString("productId") ,
- "quantity", item.getBigDecimal("quantity"),
- "selectedAmount", item.getBigDecimal("selectedAmount")
- ));
- if (itemObj.has("attrs")) {
- JSONObject attrs = itemObj.getJSONObject("attrs") ;
- orderItem.set("isPromo", attrs.getString("isPromo") ) ;
- orderItem.set("isModifiedPrice", attrs.getString("isModifiedPrice"));
- orderItem.set("unitPrice", attrs.getBigDecimal("unitPrice") );
- orderItem.set("unitListPrice", attrs.getBigDecimal("unitListPrice") );
- orderItem.set("statusId", attrs.getString("statusId") );
- }
- Debug.logInfo("Adding OrderItem..." , module);
- orderItems.add(orderItem);
- }
- }
- ctx.put("orderItems", orderItems);
- }
- List<GenericValue> orderTerms = new LinkedList<GenericValue>();
- ctx.put("orderTerms", orderTerms);
- GenericValue OrderContactMech = delegator.makeValue("OrderContactMech");
- OrderContactMech.set("contactMechPurposeTypeId", "SHIPPING_LOCATION");
- OrderContactMech.set("contactMechId", "10000");
- List<GenericValue> orderContactMechs = new LinkedList<GenericValue>();
- orderContactMechs.add(OrderContactMech);
- if (args.has("placingCustomerPartyId")) {
- ctx.put("placingCustomerPartyId", args.getString("placingCustomerPartyId") );
- }
- if (args.has("endUserCustomerPartyId")) {
- ctx.put("endUserCustomerPartyId", args.getString("endUserCustomerPartyId") );
- }
- if (args.has("shipToCustomerPartyId")) {
- ctx.put("shipToCustomerPartyId", args.getString("shipToCustomerPartyId") );
- }
- if (args.has("billToCustomerPartyId")) {
- ctx.put("billToCustomerPartyId", args.getString("billToCustomerPartyId") );
- }
- if (args.has("billFromVendorPartyId")) {
- ctx.put("billFromVendorPartyId", args.getString("billFromVendorPartyId") );
- }
- /* custom code */
- ctx.put("login.username", "admin" );
- ctx.put("login.password", "ofbiz" );
- Map<String, Object> resultMap = ServiceUtil.returnSuccess();
- String orderId="";
- String statusId="";
- try {
- Map<String, Object> resp = dispatcher.runSync("storeOrder", ctx);
- orderId = (String) resp.get("orderId");
- statusId = (String) resp.get("statusId");
- Debug.logInfo("orderId is :" + orderId , module);
- Debug.logInfo("orderId status is :" + statusId , module);
- } catch (Exception e) {
- String errMsg = "Error calling storeOrder: " + e.toString();
- Debug.logError(e, errMsg, module);
- }
- resultMap.put("orderId", orderId);
- resultMap.put("statusId", statusId);
- return resultMap ;
- }
Add Comment
Please, Sign In to add comment