Java tutorial
package com.sf.integration.warehouse.service; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.io.Writer; import java.math.BigDecimal; import java.rmi.RemoteException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // import javolution.util.FastMap; FastMap is not supported in ofbiz16, use HashMap instead. import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.ofbiz.base.util.Base64; import org.ofbiz.base.util.Debug; import org.ofbiz.base.util.UtilDateTime; import org.ofbiz.base.util.UtilMisc; import org.ofbiz.base.util.UtilProperties; import org.ofbiz.base.util.UtilValidate; import org.ofbiz.entity.Delegator; import org.ofbiz.entity.GenericEntityException; import org.ofbiz.entity.GenericValue; import org.ofbiz.entity.util.EntityUtil; import org.ofbiz.order.order.OrderReadHelper; import org.ofbiz.service.DispatchContext; import org.ofbiz.service.GenericServiceException; import org.ofbiz.service.LocalDispatcher; import org.ofbiz.service.ServiceUtil; import com.sf.integration.warehouse.service.OutsideToLscmServiceServiceStub.OutsideToLscmService; import com.sf.integration.warehouse.service.OutsideToLscmServiceServiceStub.OutsideToLscmServiceE; import com.sf.integration.warehouse.service.OutsideToLscmServiceServiceStub.OutsideToLscmServiceResponse; import com.sf.integration.warehouse.service.OutsideToLscmServiceServiceStub.OutsideToLscmServiceResponseE; import com.thoughtworks.xstream.core.util.Base64Encoder; import com.zuczug.common.ApiUtil; import com.zuczug.party.ZuczugPartyUtil; import javolution.util.FastList; import javolution.util.FastMap; /** * @author * @version 20161229 * @description */ public class SFService { private static final String ACCESS_CODE = UtilProperties.getPropertyValue("zuczugopen", "ACCESS_CODE"); private static final String CHECK_WORD = UtilProperties.getPropertyValue("zuczugopen", "CHECK_WORD"); private static final String COMPANY_CODE = UtilProperties.getPropertyValue("zuczugopen", "COMPANY_CODE"); private static final String WAREHOUSE_CODE = UtilProperties.getPropertyValue("zuczugopen", "WAREHOUSE_CODE"); private static final String MONTHLY_ACCOUNT = UtilProperties.getPropertyValue("zuczugopen", "MONTHLY_ACCOUNT"); private static final Map<String, String> IN_WAREHOUSE_TYPE = UtilMisc.toMap("10", "", "20", "", "30", "", "40", "?", "50", "?", "60", ""); private static final Map<String, String> OUT_WAREHOUSE_TYPE = UtilMisc.toMap("10", "?", "20", "?", "30", "??", "40", "?", "50", "?", "60", "NPR?"); /** * ?() * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> querySFProductBySku(DispatchContext dctx, Map<String, Object> context) { Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "ITEM_QUERY_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element ItemQueryRequest = Body.addElement("ItemQueryRequest"); Element CompanyCode = ItemQueryRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element SkuNoList = ItemQueryRequest.addElement("SkuNoList"); List<String> skuNoList = (List<String>) context.get("skuNoList"); for (String skuNo : skuNoList) { Element SkuNo = SkuNoList.addElement("SkuNo"); SkuNo.setText(skuNo);//?? } String result = ""; String XMLStr = doc.getRootElement().asXML(); try { result = sendSFRequest(XMLStr); } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } return ServiceUtil.returnSuccess(result); } /** * ??() * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> SFProductSync(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); String message = verificationProductSyncParameters(context); if (!message.equals("success")) { return ServiceUtil.returnError(message); } Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "ITEM_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element ItemRequest = Body.addElement("ItemRequest"); Element CompanyCode = ItemRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element Items = ItemRequest.addElement("Items"); for (Map<String, Object> productItem : (List<Map<String, Object>>) context.get("items")) { Element Item = Items.addElement("Item"); Element SkuNo = Item.addElement("SkuNo"); if (UtilValidate.isNotEmpty(productItem.get("skuNo"))) { SkuNo.setText(productItem.get("skuNo").toString());//?? } Element ItemName = Item.addElement("ItemName"); if (UtilValidate.isNotEmpty(productItem.get("itemName"))) { ItemName.setText(productItem.get("itemName").toString());//??? } Element ItemColor = Item.addElement("ItemColor"); if (UtilValidate.isNotEmpty(productItem.get("itemColor"))) { ItemColor.setText(productItem.get("itemColor").toString());// } Element ItemSize = Item.addElement("ItemSize"); if (UtilValidate.isNotEmpty(productItem.get("itemSize"))) { ItemSize.setText(productItem.get("itemSize").toString());//? } Element Containers = Item.addElement("Containers"); Element Container = Containers.addElement("Container"); Element PackUm = Container.addElement("PackUm"); if (UtilValidate.isNotEmpty(productItem.get("packUm"))) {//?? PackUm.setText(productItem.get("packUm").toString()); } Element UmDescr = Container.addElement("UmDescr"); if (UtilValidate.isNotEmpty(productItem.get("umDescr"))) {//?? UmDescr.setText(productItem.get("umDescr").toString()); } } String XMLStr = doc.getRootElement().asXML(); try { String resultStr = sendSFRequest(XMLStr); Document resultDocument = (Document) DocumentHelper.parseText(resultStr); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); SFAuditLogFilter.saveRequestContext(delegator, "ITEM_SERVICE", XMLStr, resultStr); if (HeadElement.getText().equals("ERR")) { Element ErrorElement = ResponseElement.element("Error"); String code = ErrorElement.attributeValue("code"); String errorMessage = ErrorElement.getText(); return ServiceUtil.returnError("ERROR : errorCode={" + code + "} message={" + errorMessage + "}"); } else { Element BodyElement = ResponseElement.element("Body"); Element ItemResponseElement = BodyElement.element("ItemResponse"); Element ItemsElement = ItemResponseElement.element("Items"); Element ItemElement = ItemsElement.element("Item"); Element ResultElement = ItemElement.element("Result"); result.put("resultCode", ResultElement.getText()); return result; } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } @SuppressWarnings("unchecked") /** * ? * @author sven * @param dctx * @param context * @return */ public static Map<String, Object> querySFOutboundListStatuc(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "SALE_ORDER_STATUS_QUERY_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element SaleOrderRequest = Body.addElement("SaleOrderStatusRequest"); Element CompanyCode = SaleOrderRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element SaleOrders = SaleOrderRequest.addElement("SaleOrders"); Element SaleOrder = SaleOrders.addElement("SaleOrder"); Element WarehouseCode = SaleOrder.addElement("WarehouseCode"); WarehouseCode.setText(WAREHOUSE_CODE);//?? Element ErpOrder = SaleOrder.addElement("ErpOrder"); Element ShipmentId = SaleOrder.addElement("ShipmentId"); ErpOrder.setText(context.get("erpOrder").toString());//??? ShipmentId.setText(context.get("shipmentId").toString());//??? String XMLStr = doc.getRootElement().asXML(); try { String resultXML = sendSFRequest(XMLStr); SFAuditLogFilter.saveRequestContext(delegator, "SALE_ORDER_STATUS_QUERY_SERVICE", XMLStr, resultXML); Document resultDocument = (Document) DocumentHelper.parseText(resultXML); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); if (HeadElement.getText().equals("ERR")) { return ServiceUtil.returnError(resultXML); } else { Element BodyElement = ResponseElement.element("Body"); Element SaleOrderStatusResponseElement = BodyElement.element("SaleOrderStatusResponse"); Element SaleOrderStatusElement = SaleOrderStatusResponseElement.element("SaleOrders"); Element SaleOrderStatuElement = SaleOrderStatusElement.element("SaleOrder"); Element ResultElement = SaleOrderStatuElement.element("Result"); Element HeaderElement = SaleOrderStatuElement.element("Header"); Element StepsElement = SaleOrderStatuElement.element("Steps"); Element NoteElement = SaleOrderStatuElement.element("Note"); result.put("resultCode", ResultElement.getText()); if (ResultElement.getText().equals("1")) { Element StepElement = StepsElement.element("Step"); Element StatusElement = StepElement.element("Status"); Element WayBillNoElement = HeaderElement.element("WayBillNo"); result.put("status", StatusElement.getText()); if (UtilValidate.isNotEmpty(WayBillNoElement)) { result.put("trackingNumberId", WayBillNoElement.getText()); } } else { result.put("note", NoteElement.getText()); } return result; } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result; } /** * ?? * ?????? * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> cancelSFOutboundList(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); String message = verificationCancelSFOutboundList(context); if (!message.equals("success")) { return ServiceUtil.returnError(message); } Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "CANCEL_SALE_ORDER_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element SaleOrderRequest = Body.addElement("CancelSaleOrderRequest"); Element CompanyCode = SaleOrderRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element SaleOrders = SaleOrderRequest.addElement("SaleOrders"); Element SaleOrder = SaleOrders.addElement("SaleOrder"); Element ErpOrder = SaleOrder.addElement("ErpOrder"); ErpOrder.setText(context.get("erpOrder").toString());//??? String XMLStr = doc.getRootElement().asXML(); try { String resultXML = sendSFRequest(XMLStr); SFAuditLogFilter.saveRequestContext(delegator, "CANCEL_SALE_ORDER_SERVICE", XMLStr, resultXML); Document resultDocument = (Document) DocumentHelper.parseText(resultXML); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); if (HeadElement.getText().equals("ERR")) { return ServiceUtil.returnError(resultXML); } else { Element BodyElement = ResponseElement.element("Body"); Element PurchaseOrderResponseElement = BodyElement.element("CancelSaleOrderResponse"); Element PurchaseOrdersElement = PurchaseOrderResponseElement.element("SaleOrders"); Element PurchaseOrderElement = PurchaseOrdersElement.element("SaleOrder"); Element ResultElement = PurchaseOrderElement.element("Result"); result.put("resultCode", ResultElement.getText()); result.put("note", PurchaseOrderElement.element("Note").getText()); return result; } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } /** * ????XML?? * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> SFOutboundList(DispatchContext dctx, Map<String, Object> context) { String message = verificationOutboundListParameters(context); String localOrderTypeId = (String) context.get("localOrderTypeId");//?? Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); if (!message.equals("success")) { return ServiceUtil.returnError(message); } Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "SALE_ORDER_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element SaleOrderRequest = Body.addElement("SaleOrderRequest"); Element CompanyCode = SaleOrderRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element SaleOrders = SaleOrderRequest.addElement("SaleOrders"); for (Map<String, Object> POrder : (List<Map<String, Object>>) context.get("saleOrders")) { Element SaleOrder = SaleOrders.addElement("SaleOrder"); Element WarehouseCode = SaleOrder.addElement("WarehouseCode"); WarehouseCode.setText(WAREHOUSE_CODE);//? Element ErpOrder = SaleOrder.addElement("ErpOrder"); String ErpOrderStr = ""; if (UtilValidate.isNotEmpty(POrder.get("erpOrder"))) { ErpOrderStr = POrder.get("erpOrder").toString(); } ErpOrder.setText(ErpOrderStr);//??? Element ErpOrderType = SaleOrder.addElement("ErpOrderType"); String ErpOrderTypeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("ErpOrderType"))) { ErpOrderTypeStr = POrder.get("erpOrderType").toString(); } ErpOrderType.setText(ErpOrderTypeStr);//? Element SFOrderType = SaleOrder.addElement("SFOrderType"); String SFOrderTypeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("SFOrderType"))) { SFOrderTypeStr = OUT_WAREHOUSE_TYPE.get(POrder.get("SFOrderType").toString()); } SFOrderType.setText(SFOrderTypeStr);//? Element OrderTotalAmount = SaleOrder.addElement("OrderTotalAmount"); String OrderTotalAmountStr = ""; if (UtilValidate.isNotEmpty(POrder.get("orderTotalAmount"))) { OrderTotalAmountStr = POrder.get("orderTotalAmount").toString(); } OrderTotalAmount.setText(OrderTotalAmountStr); /* Map<String, Object> orderCarrier = new HashMap<String, Object>(); orderCarrier.put("carrier", "CP");//? orderCarrier.put("carrierProduct", "1"); orderCarrier.put("paymentOfCharge", ""); orderCarrier.put("monthlyAccount", MONTHLY_ACCOUNT); **/ Element orderCarrier = SaleOrder.addElement("OrderCarrier"); orderCarrier.addElement("Carrier").addText("?"); orderCarrier.addElement("CarrierProduct").addText(""); orderCarrier.addElement("PaymentOfCharge").addText(""); Map<String, Object> orderReceiverInfo = (Map<String, Object>) POrder.get("orderReceiverInfo"); Element OrderReceiverInfo = SaleOrder.addElement("OrderReceiverInfo"); Element ReceiverCompany = OrderReceiverInfo.addElement("ReceiverCompany"); String ReceiverCompanyStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverCompany"))) { ReceiverCompanyStr = orderReceiverInfo.get("receiverCompany").toString(); } ReceiverCompany.setText(ReceiverCompanyStr); Element ReceiverName = OrderReceiverInfo.addElement("ReceiverName"); String ReceiverNameStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverName"))) { ReceiverNameStr = orderReceiverInfo.get("receiverName").toString(); } ReceiverName.setText(ReceiverNameStr); Element ReceiverZipCode = OrderReceiverInfo.addElement("ReceiverZipCode"); String ReceiverZipCodeStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverZipCode"))) { ReceiverZipCodeStr = orderReceiverInfo.get("receiverZipCode").toString(); } ReceiverZipCode.setText(ReceiverZipCodeStr); Element ReceiverMobile = OrderReceiverInfo.addElement("ReceiverMobile"); String ReceiverMobileStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverMobile"))) { ReceiverMobileStr = orderReceiverInfo.get("receiverMobile").toString(); } ReceiverMobile.setText(ReceiverMobileStr); Element ReceiverPhone = OrderReceiverInfo.addElement("ReceiverPhone"); String ReceiverPhoneStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverPhone"))) { ReceiverPhoneStr = orderReceiverInfo.get("receiverPhone").toString(); } ReceiverPhone.setText(ReceiverPhoneStr); Element ReceiverCountry = OrderReceiverInfo.addElement("ReceiverCountry"); String ReceiverCountryStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverCountry"))) { ReceiverCountryStr = orderReceiverInfo.get("receiverCountry").toString(); } ReceiverCountry.setText(ReceiverCountryStr); Element ReceiverProvince = OrderReceiverInfo.addElement("ReceiverProvince"); String ReceiverProvinceStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverProvince"))) { //??????hardcode ReceiverProvinceStr = getProvinceNameHardcode(orderReceiverInfo.get("receiverProvince").toString()); } ReceiverProvince.setText(ReceiverProvinceStr); Element ReceiverCity = OrderReceiverInfo.addElement("ReceiverCity"); String ReceiverCityStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverCity"))) { ReceiverCityStr = orderReceiverInfo.get("receiverCity").toString(); } ReceiverCity.setText(ReceiverCityStr); Element ReceiverArea = OrderReceiverInfo.addElement("ReceiverArea"); String ReceiverAreaStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverArea"))) { ReceiverAreaStr = orderReceiverInfo.get("receiverArea").toString(); } ReceiverArea.setText(ReceiverAreaStr); Element ReceiverAddress = OrderReceiverInfo.addElement("ReceiverAddress"); String ReceiverAddressStr = ""; if (UtilValidate.isNotEmpty(orderReceiverInfo.get("receiverAddress"))) { ReceiverAddressStr = orderReceiverInfo.get("receiverAddress").toString(); } ReceiverAddress.setText(ReceiverAddressStr); Element Items = SaleOrder.addElement("OrderItems"); List<Map<String, Object>> orderItems = (List<Map<String, Object>>) POrder.get("orderItems"); for (Map<String, Object> orderItem : orderItems) { Element Item = Items.addElement("OrderItem"); Element ErpOrderLineNum = Item.addElement("ErpOrderLineNum"); String ErpOrderLineNumStr = ""; if (UtilValidate.isNotEmpty(orderItem.get("erpOrderLineNum"))) { ErpOrderLineNumStr = orderItem.get("erpOrderLineNum").toString(); } ErpOrderLineNum.setText(ErpOrderLineNumStr); Element SkuNo = Item.addElement("SkuNo");//?? String SkuNoStr = ""; if (UtilValidate.isNotEmpty(orderItem.get("skuNo"))) { SkuNoStr = orderItem.get("skuNo").toString(); } SkuNo.setText(SkuNoStr); Element ItemQuantity = Item.addElement("ItemQuantity");//?? String ItemQuantityStr = ""; if (UtilValidate.isNotEmpty(orderItem.get("itemQuantity"))) { ItemQuantityStr = orderItem.get("itemQuantity").toString(); } ItemQuantity.setText(ItemQuantityStr); } } String XMLStr = doc.getRootElement().asXML(); List<Map<String, Object>> resultList = FastList.newInstance(); try { String resultStr = sendSFRequest(XMLStr); SFAuditLogFilter.saveRequestContext(delegator, "SALE_ORDER_SERVICE", XMLStr, resultStr); Document resultDocument = (Document) DocumentHelper.parseText(resultStr); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); if (HeadElement.getText().equals("ERR")) { return ServiceUtil.returnError(resultStr); } else { Element BodyElement = ResponseElement.element("Body"); Element PurchaseOrderResponseElement = BodyElement.element("SaleOrderResponse"); Element PurchaseOrdersElement = PurchaseOrderResponseElement.element("SaleOrders"); List<Element> PurchaseOrderElement = PurchaseOrdersElement.elements("SaleOrder"); for (Element itemElement : PurchaseOrderElement) { Map<String, Object> resultMap = FastMap.newInstance(); Element ResultElement = itemElement.element("Result"); Element ShipmentIdElement = itemElement.element("ShipmentId"); Element NoteElement = itemElement.element("Note"); resultMap.put("erpOrder", itemElement.element("ErpOrder").getText()); resultMap.put("resultCode", ResultElement.getText()); if (UtilValidate.isNotEmpty(ShipmentIdElement)) { resultMap.put("shipmentId", ShipmentIdElement.getText()); } if (UtilValidate.isNotEmpty(NoteElement)) { resultMap.put("note", NoteElement.getText()); } resultList.add(resultMap); } result.put("shunfengResult", resultList); return result; } } catch (RemoteException e) { e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (DocumentException e) { e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } //??????hardcode private static String getProvinceNameHardcode(String provinceName) { if ("".equals(provinceName) || "".equals(provinceName) || "?".equals(provinceName) || "".equals(provinceName)) { provinceName += ""; } return provinceName; } @SuppressWarnings("unchecked") public static Map<String, Object> QueryAllSFProductInventory(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> serviceResult = ServiceUtil.returnSuccess(); LocalDispatcher dispatcher = dctx.getDispatcher(); Map<String, Object> resultMap = new HashMap<String, Object>(); String inventoryStatus = "10"; if (UtilValidate.isNotEmpty(context.get("inventoryStatus"))) { inventoryStatus = context.get("inventoryStatus").toString(); } boolean flag = true; int pageIndex = 1; try { while (flag) { Map<String, Object> result = dispatcher.runSync("querySFProductInventory", UtilMisc.toMap("inventoryStatus", inventoryStatus, "pageIndex", pageIndex + "")); Map<String, Object> serviceResultMap = (Map<String, Object>) result.get("resultMap"); int totalPage = Integer.parseInt(serviceResultMap.get("totalPage").toString()); pageIndex = Integer.parseInt(serviceResultMap.get("pageIndex").toString()); List<Map<String, String>> resultList = (List<Map<String, String>>) serviceResultMap .get("resultList"); for (Map<String, String> serviceResultListMap : resultList) { if (!serviceResultListMap.get("quantityOnHandTotal").equals("0")) { resultMap.put(serviceResultListMap.get("productId"), serviceResultListMap); } } if (totalPage >= pageIndex || totalPage == 0) { flag = false; } else { pageIndex++; } } serviceResult.put("resultMap", resultMap); } catch (GenericServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); } return serviceResult; } /** * ? * @param request * @param response * @return */ public static Map<String, Object> QuerySFProductInventory(DispatchContext dctx, Map<String, Object> context) { Delegator delegator = dctx.getDelegator(); Map<String, Object> result = ServiceUtil.returnSuccess(); List<Map<String, String>> resultList = new ArrayList<Map<String, String>>(); Map<String, Object> resultMap = new HashMap<String, Object>(); Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "RT_INVENTORY_QUERY_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element RTInventoryQueryRequest = Body.addElement("RTInventoryQueryRequest"); Element CompanyCode = RTInventoryQueryRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element WarehouseCode = RTInventoryQueryRequest.addElement("WarehouseCode"); WarehouseCode.setText(WAREHOUSE_CODE);//? if (UtilValidate.isNotEmpty(context.get("inventoryStatus"))) { Element InventoryStatus = RTInventoryQueryRequest.addElement("InventoryStatus"); InventoryStatus.setText((String) context.get("inventoryStatus")); } Element RTInventorys = RTInventoryQueryRequest.addElement("RTInventorys"); Element RTInventory = RTInventorys.addElement("RTInventory"); if (UtilValidate.isNotEmpty(context.get("productId"))) { Element SkuNo = RTInventory.addElement("SkuNo"); SkuNo.setText((String) context.get("productId")); } // Element PageIndex = RTInventoryQueryRequest.addElement("PageIndex"); // if (UtilValidate.isNotEmpty(context.get("inventoryStatus"))) { // PageIndex.setText((String) context.get("pageIndex")); // } else { // PageIndex.setText("1"); // } String XMLStr = doc.getRootElement().asXML(); String resultStr; try { resultStr = sendSFRequest(XMLStr); Document resultDocument = (Document) DocumentHelper.parseText(resultStr); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); if (HeadElement.getText().equals("ERR")) { return ServiceUtil.returnError(resultStr); } else { // Element BodyElement = ResponseElement.element("Body"); // Element WmsInventoryBalancePageQueryResponse = BodyElement.element("WmsInventoryBalancePageQueryResponse"); // Element Result = WmsInventoryBalancePageQueryResponse.element("Result"); // Element TotalSize = WmsInventoryBalancePageQueryResponse.element("TotalSize"); // Element TotalPage = WmsInventoryBalancePageQueryResponse.element("TotalPage"); // Element PageIndexElement = WmsInventoryBalancePageQueryResponse.element("PageIndex"); // resultMap.put("totalSize", TotalSize.getText()); // resultMap.put("totalPage", TotalPage.getText()); // resultMap.put("pageIndex", PageIndexElement.getText()); // if (Result.getText().equals("1")) { // Element List = WmsInventoryBalancePageQueryResponse.element("List"); // if (UtilValidate.isNotEmpty(List)) { // @SuppressWarnings("unchecked") // List<Element> items = List.elements("Item"); // for (Element item : items) { // Element SkuNo = item.element("SkuNo"); // Element OnHandQty = item.element("OnHandQty"); // Element InventoryStatus = item.element("InventoryStatus"); // Map<String, String> itemMap = new HashMap<String, String>(); // itemMap.put("productId", SkuNo.getText()); // itemMap.put("quantityOnHandTotal", OnHandQty.getText()); // itemMap.put("inventoryStatus", InventoryStatus.getText()); // resultList.add((itemMap)); // } // } // } resultMap.put("resultList", resultList); result.put("resultMap", resultMap); } return result; } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } /** * ????XML?? * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> SFGodownEntry(DispatchContext dctx, Map<String, Object> context) { try { String message = verificationGodownEntryParameters(context); Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); if (!message.equals("success")) { return ServiceUtil.returnError(message); } Document doc = (Document) DocumentHelper.createDocument(); //Request Element request = doc.addElement("Request"); request.addAttribute("service", "PURCHASE_ORDER_SERVICE"); request.addAttribute("lang", "zh-CN"); //Head Element head = request.addElement("Head"); Element AccessCode = head.addElement("AccessCode"); AccessCode.setText(ACCESS_CODE);//? Element Checkword = head.addElement("Checkword"); Checkword.setText(CHECK_WORD);//?? //Body Element Body = request.addElement("Body"); Element PurchaseOrderRequest = Body.addElement("PurchaseOrderRequest"); Element CompanyCode = PurchaseOrderRequest.addElement("CompanyCode"); CompanyCode.setText(COMPANY_CODE);//? Element PurchaseOrders = PurchaseOrderRequest.addElement("PurchaseOrders"); for (Map<String, Object> POrder : (List<Map<String, Object>>) context.get("purchaseOrders")) { List<Map<String, Object>> productItems = (List<Map<String, Object>>) POrder.get("items"); Element PurchaseOrder = PurchaseOrders.addElement("PurchaseOrder"); Element WarehouseCode = PurchaseOrder.addElement("WarehouseCode"); WarehouseCode.setText(WAREHOUSE_CODE);//? Element ErpOrder = PurchaseOrder.addElement("ErpOrder"); String ErpOrderStr = ""; if (UtilValidate.isNotEmpty(POrder.get("erpOrder"))) { ErpOrderStr = POrder.get("erpOrder").toString(); } ErpOrder.setText(ErpOrderStr);//??? Element ErpOrderType = PurchaseOrder.addElement("ErpOrderType"); String ErpOrderTypeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("erpOrderType"))) { ErpOrderTypeStr = POrder.get("erpOrderType").toString(); } ErpOrderType.setText(ErpOrderTypeStr);//? Element SFOrderType = PurchaseOrder.addElement("SFOrderType"); String SFOrderTypeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("SFOrderType"))) { SFOrderTypeStr = IN_WAREHOUSE_TYPE.get(POrder.get("SFOrderType")).toString(); } SFOrderType.setText(SFOrderTypeStr);//? Element OrderDate = PurchaseOrder.addElement("OrderDate"); String OrderDateStr = ""; if (UtilValidate.isNotEmpty(POrder.get("orderDate"))) { OrderDateStr = POrder.get("orderDate").toString(); } OrderDate.setText(OrderDateStr);//? Element Buyer = PurchaseOrder.addElement("Buyer"); String buyStr = ""; if (UtilValidate.isNotEmpty(POrder.get("buyer"))) { buyStr = POrder.get("buyer").toString(); } Buyer.setText(buyStr);// Element BuyerPhone = PurchaseOrder.addElement("BuyerPhone"); String BuyerPhoneStr = ""; if (UtilValidate.isNotEmpty(POrder.get("buyerPhone"))) { BuyerPhoneStr = POrder.get("buyerPhone").toString(); } BuyerPhone.setText(BuyerPhoneStr);//?? Element ScheduledReceiptDate = PurchaseOrder.addElement("ScheduledReceiptDate"); String ScheduledReceiptDateStr = ""; if (UtilValidate.isNotEmpty(POrder.get("scheduledReceiptDate"))) { ScheduledReceiptDateStr = POrder.get("scheduledReceiptDate").toString(); } ScheduledReceiptDate.setText(ScheduledReceiptDateStr);// Element VendorCode = PurchaseOrder.addElement("VendorCode"); String VendorCodeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("vendorCode"))) { VendorCodeStr = POrder.get("vendorCode").toString(); } VendorCode.setText(VendorCodeStr);//? Element TransferWarehouseCode = PurchaseOrder.addElement("TransferWarehouseCode"); String TransferWarehouseCodeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("transferWarehouseCode"))) { TransferWarehouseCodeStr = POrder.get("transferWarehouseCode").toString(); } TransferWarehouseCode.setText(TransferWarehouseCodeStr);//? Element OriginalOrderNo = PurchaseOrder.addElement("OriginalOrderNo"); String OriginalOrderNoStr = ""; if (UtilValidate.isNotEmpty(POrder.get("originalOrderNo"))) { OriginalOrderNoStr = POrder.get("originalOrderNo").toString(); } OriginalOrderNo.setText(OriginalOrderNoStr);//?? Element Note = PurchaseOrder.addElement("Note"); String NoteStr = ""; if (UtilValidate.isNotEmpty(POrder.get("note"))) { NoteStr = POrder.get("note").toString(); } Note.setText(NoteStr);//? Element ReceiptMode = PurchaseOrder.addElement("ReceiptMode"); String ReceiptModeStr = ""; if (UtilValidate.isNotEmpty(POrder.get("receiptMode"))) { ReceiptModeStr = POrder.get("receiptMode").toString(); } ReceiptMode.setText(ReceiptModeStr);//?? Element LicensePlateNumber = PurchaseOrder.addElement("LicensePlateNumber"); String LicensePlateNumberStr = ""; if (UtilValidate.isNotEmpty(POrder.get("licensePlateNumber"))) { LicensePlateNumberStr = POrder.get("licensePlateNumber").toString(); } LicensePlateNumber.setText(LicensePlateNumberStr);//? Element Driver = PurchaseOrder.addElement("Driver"); String DriverStr = ""; if (UtilValidate.isNotEmpty(POrder.get("driver"))) { DriverStr = POrder.get("driver").toString(); } Driver.setText(DriverStr);//? Element TradePlatform = PurchaseOrder.addElement("TradePlatform"); String TradePlatformStr = ""; if (UtilValidate.isNotEmpty(POrder.get("tradePlatform"))) { TradePlatformStr = POrder.get("tradePlatform").toString(); } TradePlatform.setText(TradePlatformStr);//? Element TradeOrder = PurchaseOrder.addElement("TradeOrder"); String TradeOrderStr = ""; if (UtilValidate.isNotEmpty(POrder.get("tradeOrder"))) { TradeOrderStr = POrder.get("tradeOrder").toString(); } TradeOrder.setText(TradeOrderStr);//?? Element DriverCalls = PurchaseOrder.addElement("DriverCalls"); String DriverCallsStr = ""; if (UtilValidate.isNotEmpty(POrder.get("driverCalls"))) { DriverCallsStr = POrder.get("driverCalls").toString(); } DriverCalls.setText(DriverCallsStr);//?? for (int i = 1; i < 9; i++) { Element lot = PurchaseOrder.addElement("UserDef" + i); lot.setText(""); } Element items = PurchaseOrder.addElement("Items"); for (Map<String, Object> productItem : productItems) { Element item = items.addElement("Item"); Element ErpOrderLineNum = item.addElement("ErpOrderLineNum"); String ErpOrderLineNumStr = ""; if (UtilValidate.isNotEmpty(productItem.get("erpOrderLineNum"))) { ErpOrderLineNumStr = productItem.get("erpOrderLineNum").toString(); } ErpOrderLineNum.setText(ErpOrderLineNumStr); Element SkuNo = item.addElement("SkuNo");//?? String SkuNoStr = ""; if (UtilValidate.isNotEmpty(productItem.get("skuNo"))) { SkuNoStr = productItem.get("skuNo").toString(); } SkuNo.setText(SkuNoStr); Element Qty = item.addElement("Qty");//? String QtyStr = ""; if (UtilValidate.isNotEmpty(productItem.get("qty"))) { QtyStr = productItem.get("qty").toString(); } Qty.setText(QtyStr); Element QtyUm = item.addElement("QtyUm");//??? String QtyUmStr = ""; if (UtilValidate.isNotEmpty(productItem.get("qtyUm"))) { QtyUmStr = productItem.get("qtyUm").toString(); } QtyUm.setText(QtyUmStr); Element Lot = item.addElement("Lot");//? String lotStr = ""; if (UtilValidate.isNotEmpty(productItem.get("lot"))) { lotStr = productItem.get("lot").toString(); } Lot.setText(lotStr); Element itemNote = item.addElement("Note");// String itemNoteStr = ""; if (UtilValidate.isNotEmpty(productItem.get("note"))) { itemNoteStr = productItem.get("note").toString(); } itemNote.setText(itemNoteStr); Element Price = item.addElement("Price");//? String PriceStr = ""; if (UtilValidate.isNotEmpty(productItem.get("price"))) { //PriceStr = productItem.get("price").toString(); PriceStr = "0.00"; } Price.setText(PriceStr); Element InventoryStatus = item.addElement("InventoryStatus"); String InventoryStatusStr = ""; if (UtilValidate.isNotEmpty(productItem.get("inventoryStatus"))) { InventoryStatusStr = productItem.get("inventoryStatus").toString(); } InventoryStatus.setText(InventoryStatusStr); for (int i = 1; i < 7; i++) { Element lot = item.addElement("LotAttr" + i); lot.setText(""); } for (int i = 1; i < 9; i++) { Element lot = item.addElement("UserDef" + i); lot.setText(""); } } } String XMLStr = doc.getRootElement().asXML(); String resultStr = sendSFRequest(XMLStr); SFAuditLogFilter.saveRequestContext(delegator, "PURCHASE_ORDER_SERVICE", XMLStr, resultStr); Document resultDocument = (Document) DocumentHelper.parseText(resultStr); Element ResponseElement = resultDocument.getRootElement(); Element HeadElement = ResponseElement.element("Head"); if (HeadElement.getText().equals("ERR")) { Element ErrorElement = ResponseElement.element("Error"); String code = ErrorElement.attributeValue("code"); String errorMessage = ErrorElement.getText(); return ServiceUtil.returnError("ERROR : errorCode={" + code + "} message={" + errorMessage + "}"); } else { Element BodyElement = ResponseElement.element("Body"); Element PurchaseOrderResponseElement = BodyElement.element("PurchaseOrderResponse"); Element PurchaseOrdersElement = PurchaseOrderResponseElement.element("PurchaseOrders"); Element PurchaseOrderElement = PurchaseOrdersElement.element("PurchaseOrder"); Element ResultElement = PurchaseOrderElement.element("Result"); Element ReceiptIdElement = PurchaseOrderElement.element("ReceiptId"); Element NoteElement = PurchaseOrderElement.element("Note"); result.put("resultCode", ResultElement.getText()); if (UtilValidate.isNotEmpty(ReceiptIdElement)) { result.put("receiptId", ReceiptIdElement.getText()); } if (UtilValidate.isNotEmpty(NoteElement)) { result.put("note", NoteElement.getText()); } return result; } } catch (RemoteException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (DocumentException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } /** * ? * @author sven * @param request * @param response * @return */ public static String SFGodownEntryPush(HttpServletRequest request, HttpServletResponse response) { String logistics_interface = (String) request.getParameter("logistics_interface"); Debug.log("logistics_interface : SFGodownEntryPush:?" + logistics_interface); Delegator delegator = (Delegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); List<Map<String, Object>> shipmentList = FastList.newInstance(); Document responseDoc = (Document) DocumentHelper.createDocument(); Element responseElement = responseDoc.addElement("Response"); responseElement.addAttribute("service", "PURCHASE_ORDER_INBOUND_SERVICE"); responseElement.addAttribute("lang", "zh-CN"); responseElement.addElement("Head").addText("OK"); Element PurchaseOrderInboundResponse = responseElement.addElement("Body") .addElement("PurchaseOrderInboundResponse"); try { GenericValue userLogin = delegator.findOne("UserLogin", true, UtilMisc.toMap("userLoginId", "system")); Document doc = (Document) DocumentHelper.parseText(logistics_interface); Element ResponseElement = doc.getRootElement(); Element BodyElement = ResponseElement.element("Body"); Element PurchaseOrderInboundRequest = BodyElement.element("PurchaseOrderInboundRequest"); List<Element> PurchaseOrderList = PurchaseOrderInboundRequest.elements("PurchaseOrders"); for (Element PurchaseOrder : PurchaseOrderList) { Element ErpOrder = PurchaseOrder.element("PurchaseOrder").element("ErpOrder");//??? Element ReceiptId = PurchaseOrder.element("PurchaseOrder").element("ReceiptId");//?? Element Status = PurchaseOrder.element("PurchaseOrder").element("Status");//? List<Element> items = PurchaseOrder.element("PurchaseOrder").element("Items").elements("Item"); // ReceiptId ??? GenericValue sfOrderAssoc = EntityUtil.getFirst( delegator.findByAnd("SFOrderAssoc", UtilMisc.toMap("SFCallBackId", ReceiptId.getText()))); if (UtilValidate.isEmpty(sfOrderAssoc)) { //? ApiUtil.messageEmailNotification(dispatcher, delegator, "SFRecepitPushError", "receiptId " + ReceiptId.getText() + " not found", null); //return "error"; continue; } if ("SHIPMENT_IN".equals(sfOrderAssoc.getString("localOrderTypeId"))) { //ofbizshipment? for (Element item : items) { Map<String, Object> shipmentMap = UtilMisc.toMap("productId", item.element("SkuNo").getText(), "quantity", new BigDecimal(item.element("PlanQty").getText()), "actualQuantity", new BigDecimal(item.element("ActualQty").getText())); shipmentList.add(shipmentMap); } // ?? dispatcher.runSync("shunfengShipmentReceive", UtilMisc.toMap("shipmentItems", shipmentList, "shipmentId", sfOrderAssoc.getString("shipmentId"), "userLogin", userLogin)); } else if ("ORDER_IN".equals(sfOrderAssoc.getString("localOrderTypeId"))) { //? String returnId = sfOrderAssoc.getString("returnId"); List<Map<String, Object>> reutnItems = FastList.newInstance(); for (Element item : items) { Map<String, Object> temp = UtilMisc.toMap("productId", item.element("SkuNo").getText(), "quantity", new BigDecimal(item.element("PlanQty").getText()), "actualQuantity", new BigDecimal(item.element("ActualQty").getText())); reutnItems.add(temp); } Map<String, Object> returnMap = dispatcher.runSync("OMSReturnReceive", UtilMisc.toMap("returnId", returnId, "itemList", reutnItems, "userLogin", userLogin)); if (ServiceUtil.isError(returnMap)) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFRecepitPushError", ServiceUtil.getErrorMessage(returnMap), null); } } } } catch (DocumentException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFRecepitPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); e.printStackTrace(); } catch (GenericEntityException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFRecepitPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); e.printStackTrace(); } catch (GenericServiceException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFRecepitPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); e.printStackTrace(); } setResponseMsg(PurchaseOrderInboundResponse, "1", ""); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); return "success"; } /** * xml?? * @author sven * @param element * @param result * @param note */ private static void setResponseMsg(Element element, String result, String note) { element.addElement("Result").addText(result); element.addElement("Note").addText(note); } private static void responseToShunfeng(HttpServletRequest request, HttpServletResponse response, String xml) { try { Debug.log("===== ResponseXML ====== " + xml); request.setAttribute("sfResponseXml", xml); response.setContentType("text/xml"); response.setContentLength(xml.getBytes("UTF8").length); Writer out = response.getWriter(); out.write(xml); out.flush(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * ? * @author sven * @param request * @param response * @return */ public static String SFInventoryPush(HttpServletRequest request, HttpServletResponse response) { String logistics_interface = (String) request.getParameter("logistics_interface"); Delegator delegator = (Delegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); Document responseDoc = (Document) DocumentHelper.createDocument(); Element responseElement = responseDoc.addElement("Response"); responseElement.addAttribute("service", "INVENTORY_BALANCE_SERVICE"); responseElement.addAttribute("lang", "zh-CN"); responseElement.addElement("Head").addText("OK"); Element InventoryBalanceResponse = responseElement.addElement("Body") .addElement("InventoryBalanceResponse"); try { Document doc = (Document) DocumentHelper.parseText(logistics_interface); Element ResponseElement = doc.getRootElement(); Element BodyElement = ResponseElement.element("Body"); Element InventoryBalanceRequest = BodyElement.element("InventoryBalanceRequest"); Element InventoryBalances = InventoryBalanceRequest.element("InventoryBalances"); Element Status = InventoryBalanceRequest.element("Status"); // ?1? if (UtilValidate.isEmpty(InventoryBalances) || "1".equals(Status.getText())) { setResponseMsg(InventoryBalanceResponse, "1", ""); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); return "success"; } List<Element> inventoryBalanceList = InventoryBalances.elements("InventoryBalance"); for (Element inventoryBalance : inventoryBalanceList) { Element SkuNo = inventoryBalance.element("SkuNo"); Element Qty = inventoryBalance.element("Qty"); Element InventoryStatus = inventoryBalance.element("InventoryStatus"); GenericValue invTemp = delegator.makeValue("ShunfengInventoryTemp"); invTemp.put("baseId", delegator.getNextSeqId("ShunfengInventoryTemp")); invTemp.put("productId", SkuNo.getText()); invTemp.put("availableToPromiseTotal", new BigDecimal(Qty.getText())); invTemp.put("quantityOnHandTotal", new BigDecimal(Qty.getText())); invTemp.put("inventoryStatus", InventoryStatus.getText()); invTemp.put("pushDateTime", UtilDateTime.nowTimestamp()); invTemp.create(); } setResponseMsg(InventoryBalanceResponse, "1", ""); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); } catch (DocumentException e) { setResponseMsg(InventoryBalanceResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); e.printStackTrace(); } catch (GenericEntityException e) { setResponseMsg(InventoryBalanceResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.getRootElement().asXML()); e.printStackTrace(); } return "success"; } /** * ?? * @author sven * @param request * @param response * @return */ public static String SFOutboundListPush(HttpServletRequest request, HttpServletResponse response) { String logistics_interface = (String) request.getParameter("logistics_interface"); Debug.log("====== logistics_interface SFOutboundListPush ? :" + logistics_interface); Delegator delegator = (Delegator) request.getAttribute("delegator"); LocalDispatcher dispatcher = (LocalDispatcher) request.getAttribute("dispatcher"); StringBuffer errorOrderBody = new StringBuffer(); List<Map<String, Object>> orderList = FastList.newInstance(); List<Map<String, Object>> shipmentList = FastList.newInstance(); Document responseDoc = (Document) DocumentHelper.createDocument(); Element responseElement = responseDoc.addElement("Response"); responseElement.addAttribute("service", "SALE_ORDER_OUTBOUND_DETAIL_SERVICE"); responseElement.addAttribute("lang", "zh-CN"); responseElement.addElement("Head").addText("OK"); Element PurchaseOrderInboundResponse = responseElement.addElement("Body") .addElement("SaleOrderOutboundDetailResponse"); try { GenericValue userLogin = delegator.findOne("UserLogin", true, UtilMisc.toMap("userLoginId", "system")); Document doc = (Document) DocumentHelper.parseText(logistics_interface); Element ResponseElement = doc.getRootElement(); Element BodyElement = ResponseElement.element("Body"); Element PurchaseOrderInboundRequest = BodyElement.element("SaleOrderOutboundDetailRequest"); List<Element> salesOrders = PurchaseOrderInboundRequest.elements("SaleOrders"); for (Element salesOrder : salesOrders) { String sfShipmentId = salesOrder.element("SaleOrder").element("ShipmentId").getText(); String trackingIdNumber = salesOrder.element("SaleOrder").element("WayBillNo").getText(); String status = salesOrder.element("SaleOrder").element("DataStatus").getText(); if (!"2900".equals(status) && !"3900".equals(status)) { errorOrderBody.append("SFOutboundListPush:ErpOrder[" + sfShipmentId + "],status[" + status + "] ???<br/>"); continue; } // ReceiptId ??? GenericValue sfOrderAssoc = EntityUtil.getFirst( delegator.findByAnd("SFOrderAssoc", UtilMisc.toMap("SFCallBackId", sfShipmentId))); if (UtilValidate.isEmpty(sfOrderAssoc)) { errorOrderBody.append("SFOutboundListPush:sfShipmentId not found<br/>"); continue; } if ("SHIPMENT_OUT".equals(sfOrderAssoc.getString("localOrderTypeId"))) { //ofbizshipment? Map<String, Object> shipmentMap = UtilMisc.toMap("shipmentId", sfOrderAssoc.getString("shipmentId"), "trackingIdNumber", trackingIdNumber); shipmentList.add(shipmentMap); } else if ("ORDER_OUT".equals(sfOrderAssoc.getString("localOrderTypeId"))) { Map<String, Object> orderMap = UtilMisc.toMap("orderId", sfOrderAssoc.getString("orderId"), "orderItemShipGroupId", sfOrderAssoc.getString("shipGroupSeqId"), "trackingIdNumber", trackingIdNumber); orderList.add(orderMap); } } if (UtilValidate.isNotEmpty(errorOrderBody)) { //? ApiUtil.messageEmailNotification(dispatcher, delegator, "SFShipmentPushError", errorOrderBody.toString(), null); //return "error"; } if (UtilValidate.isNotEmpty(orderList)) { //??? dispatcher.runSync("OMSOrderShipComplete", UtilMisc.toMap("orderList", orderList, "userLogin", userLogin)); } if (UtilValidate.isNotEmpty(shipmentList)) { //shipment?? dispatcher.runSync("sendShipmentFromShunfeng", UtilMisc.toMap("shipmentList", shipmentList, "userLogin", userLogin)); } setResponseMsg(PurchaseOrderInboundResponse, "1", ""); responseToShunfeng(request, response, responseDoc.asXML()); } catch (DocumentException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFShipmentPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.asXML()); e.printStackTrace(); } catch (GenericEntityException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFShipmentPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.asXML()); e.printStackTrace(); } catch (GenericServiceException e) { ApiUtil.messageEmailNotification(dispatcher, delegator, "SFShipmentPushError", e.getMessage(), null); setResponseMsg(PurchaseOrderInboundResponse, "2", e.getMessage()); responseToShunfeng(request, response, responseDoc.asXML()); e.printStackTrace(); } return "success"; } //??----------------------------------------------------------------------------------------------------------------------------------------------------------- private static String verificationCancelSFOutboundList(Map<String, Object> context) { // TODO Auto-generated method stub if (UtilValidate.isEmpty(context.get("erpOrder"))) { return "important parameters 'orderId' is not found"; } return "success"; } /** * ??? * ???? * @param context * @return success/error message */ @SuppressWarnings("unchecked") private static String verificationOutboundListParameters(Map<String, Object> context) { // TODO Auto-generated method stub List<Map<String, Object>> saleOrders = (List<Map<String, Object>>) context.get("saleOrders"); if (UtilValidate.isEmpty(saleOrders)) { return "important parameters 'saleOrders' is not found"; } for (Map<String, Object> saleOrder : saleOrders) { if (UtilValidate.isEmpty(saleOrder.get("erpOrder"))) { return "Important parameters 'erpOrder' were not found in the 'saleOrders'"; } if (UtilValidate.isEmpty(saleOrder.get("erpOrderType"))) { return "Important parameters 'erpOrderType' were not found in the 'saleOrders'"; } if (UtilValidate.isEmpty(saleOrder.get("SFOrderType"))) { return "Important parameters 'SFOrderType' were not found in the 'saleOrders'"; } Map<String, Object> orderReceiverInfo = (Map<String, Object>) saleOrder.get("orderReceiverInfo"); if (UtilValidate.isEmpty(orderReceiverInfo)) { return "Important parameters 'orderReceiverInfo' were not found in the 'saleOrders'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverCompany"))) { return "Important parameters 'receiverCompany' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverName"))) { return "Important parameters 'receiverName' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverCountry"))) { return "Important parameters 'receiverCountry' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverProvince"))) { return "Important parameters 'receiverProvince' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverCity"))) { return "Important parameters 'receiverCity' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverArea"))) { return "Important parameters 'receiverArea' were not found in the 'orderReceiverInfo'"; } if (UtilValidate.isEmpty(orderReceiverInfo.get("receiverAddress"))) { return "Important parameters 'receiverAddress' were not found in the 'orderReceiverInfo'"; } List<Map<String, Object>> orderItems = (List<Map<String, Object>>) saleOrder.get("orderItems"); for (Map<String, Object> orderItem : orderItems) { if (UtilValidate.isEmpty(orderItem)) { return "Important parameters 'orderItems' were not found in the 'saleOrders'"; } if (UtilValidate.isEmpty(orderItem.get("skuNo"))) { return "Important parameters 'skuNo' were not found in the 'orderItems'"; } if (UtilValidate.isEmpty(orderItem.get("itemQuantity"))) { return "Important parameters 'itemQuantity' were not found in the 'orderItems'"; } } } return "success"; } @SuppressWarnings("unchecked") private static String verificationProductSyncParameters(Map<String, Object> context) { String resultStr = "Error:"; // TODO Auto-generated method stub List<Map<String, Object>> items = (List<Map<String, Object>>) context.get("items"); for (Map<String, Object> item : items) { if (UtilValidate.isEmpty(item.get("skuNo"))) { resultStr += "Important parameters 'skuNo' were not found in the 'items'\r\n"; } if (UtilValidate.isEmpty(item.get("itemName"))) { resultStr += "Important parameters 'itemName' were not found in the 'items'\r\n"; } if (UtilValidate.isEmpty(item.get("packUm"))) { resultStr += "Important parameters 'packUm' were not found in the 'items'\r\n"; } } if (resultStr.equals("Error:")) { return "success"; } else { return resultStr; } } private static String verificationGodownEntryParameters(Map<String, Object> context) { // TODO Auto-generated method stub if (UtilValidate.isEmpty(context.get("purchaseOrders"))) { return "important parameters 'PurchaseOrders' is not found"; } @SuppressWarnings("unchecked") List<Map<String, Object>> PurchaseOrders = (List<Map<String, Object>>) context.get("purchaseOrders"); if (UtilValidate.isEmpty(PurchaseOrders)) { return "Important parameters 'PurchaseOrders'"; } for (Map<String, Object> POrder : PurchaseOrders) { if (UtilValidate.isEmpty(POrder.get("erpOrder"))) { return "Important parameters 'erpOrder' were not found in the 'PurchaseOrders'"; } if (UtilValidate.isEmpty(POrder.get("erpOrderType"))) { return "Important parameters 'ErpOrderType' were not found in the 'PurchaseOrders'"; } /** if (UtilValidate.isEmpty(POrder.get("scheduledReceiptDate"))) { return "Important parameters 'ScheduledReceiptDate' were not found in the 'PurchaseOrders'"; } if (UtilValidate.isEmpty(POrder.get("vendorCode"))) { return "Important parameters 'VendorCode' were not found in the 'PurchaseOrders'"; } **/ @SuppressWarnings("unchecked") List<Map<String, Object>> productItems = (List<Map<String, Object>>) POrder.get("items"); if (UtilValidate.isEmpty(productItems)) { return "Important parameters 'items' were not found in the 'PurchaseOrders'"; } for (Map<String, Object> productItem : productItems) { if (UtilValidate.isEmpty(productItem.get("skuNo"))) { return "Important parameters 'SkuNo' were not found in the 'items'"; } if (UtilValidate.isEmpty(productItem.get("qty"))) { return "Important parameters 'Qty' were not found in the 'items'"; } } } return "success"; } /** * ??XML * @param XML * @return * @throws RemoteException */ public static String sendSFRequest(String XML) throws RemoteException { //XML = "<![CDATA[" + XML + "]]>"; Debug.log("===== SendXML =======:" + XML); OutsideToLscmServiceServiceStub otsss = new OutsideToLscmServiceServiceStub(); OutsideToLscmServiceE otse = new OutsideToLscmServiceE(); OutsideToLscmService ots = new OutsideToLscmService(); ots.setArg0(XML); otse.setOutsideToLscmService(ots); OutsideToLscmServiceResponseE otsre = otsss.outsideToLscmService(otse); OutsideToLscmServiceResponse otsr = otsre.getOutsideToLscmServiceResponse(); String result = otsr.get_return(); Debug.log("===== ResponseXML ======:" + result); return result; } //service----------------------------------------------------------------------------------------------------------------------------------------------------------- /** * ????? * @param dctx * @param context * @return */ public static Map<String, Object> zuczugOutboundList(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); LocalDispatcher dispatcher = dctx.getDispatcher(); Locale locale = (Locale) context.get("locale"); StringBuffer errorMessage = new StringBuffer(); try { Map<String, Object> ctx = new HashMap<String, Object>(); List<Map<String, Object>> saleOrders = new ArrayList<Map<String, Object>>(); GenericValue userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "system")); //?? Map<String, Object> serviceResult = dispatcher.runSync("fixOrderItemShipGroupFacility", null); if (!serviceResult.get("responseMessage").equals("success")) { return ServiceUtil.returnError( "Facility IdShip Group?Order Head??" + serviceResult.get("responseMessage")); } //? List<GenericValue> orderItemShipGroups = delegator.findByAnd("ToSFWaresouseOrder", UtilMisc.toMap("statusId", "ORDER_APPROVED")); for (GenericValue orderItemShipGroup : orderItemShipGroups) { List<GenericValue> isRequested = delegator.findByAnd("SFOrderAssoc", UtilMisc.toMap("localOrderTypeId", "ORDER_OUT", //? "orderId", orderItemShipGroup.get("orderId"), "shipGroupSeqId", orderItemShipGroup.get("shipGroupSeqId"))); if (UtilValidate.isNotEmpty(isRequested)) { Debug.logWarning("? " + orderItemShipGroup.get("orderId") + " , shipGroupSeqId:" + orderItemShipGroup.get("shipGroupSeqId") + ":??????:" + isRequested.get(0).get("SFCallBackId") + "", "ZuczugOutboundList"); continue; } //?orderItem? Map<String, Object> saleOrder = new HashMap<String, Object>(); saleOrder.put("erpOrder", orderItemShipGroup.get("orderId") + "_" + orderItemShipGroup.get("shipGroupSeqId")); saleOrder.put("erpOrderType", "10"); saleOrder.put("SFOrderType", "10"); GenericValue postalAddress = delegator.findOne("PostalAddress", false, UtilMisc.toMap("contactMechId", orderItemShipGroup.get("contactMechId"))); if (UtilValidate.isEmpty(postalAddress)) { errorMessage.append("?" + orderItemShipGroup.getString("orderId") + "?:" + orderItemShipGroup.getString("shipGroupSeqId") + "???"); continue; } Map<String, Object> orderReceiverInfo = new HashMap<String, Object>(); Map<String, Object> addressMap = ZuczugPartyUtil.getPostalAddressString(delegator, postalAddress, locale, true); orderReceiverInfo.put("receiverCompany", addressMap.get("toName")); orderReceiverInfo.put("receiverName", addressMap.get("toName")); orderReceiverInfo.put("receiverPhone", addressMap.get("phoneNumber")); orderReceiverInfo.put("receiverCountry", addressMap.get("country")); orderReceiverInfo.put("receiverProvince", addressMap.get("province")); orderReceiverInfo.put("receiverCity", addressMap.get("city")); orderReceiverInfo.put("receiverArea", addressMap.get("county")); orderReceiverInfo.put("receiverAddress", addressMap.get("address1")); orderReceiverInfo.put("receiverZipCode", addressMap.get("postalCode")); saleOrder.put("orderReceiverInfo", orderReceiverInfo); List<GenericValue> orderItemShipGroupAssocs = delegator.findByAnd("OrderItemShipGroupAssoc", UtilMisc.toMap("orderId", orderItemShipGroup.get("orderId"), "shipGroupSeqId", orderItemShipGroup.get("shipGroupSeqId"))); if (UtilValidate.isEmpty(orderItemShipGroupAssocs)) { errorMessage.append("? " + orderItemShipGroup.get("orderId") + "_" + orderItemShipGroup.get("shipGroupSeqId") + "?OrderItemShipGroupAssoc?"); continue; } List<Map<String, Object>> orderItems = new ArrayList<Map<String, Object>>(); for (GenericValue oisgs : orderItemShipGroupAssocs) { GenericValue oi = delegator.findOne("OrderItem", false, UtilMisc.toMap("orderId", orderItemShipGroup.get("orderId"), "orderItemSeqId", oisgs.getString("orderItemSeqId"))); if (UtilValidate.isEmpty(oi)) { errorMessage.append("? " + orderItemShipGroup.get("orderId") + " , orderItemSeqId:" + oisgs.get("orderItemSeqId") + "?OrderItem?"); continue; } Map<String, Object> orderItem = new HashMap<String, Object>(); orderItem.put("erpOrderLineNum", oisgs.getString("orderItemSeqId")); orderItem.put("skuNo", oi.get("productId")); if (UtilValidate.isEmpty(OrderReadHelper.getOrderItemQuantity(oi))) { if (UtilValidate.isEmpty(oi)) { errorMessage.append("? " + orderItemShipGroup.get("orderId") + " , orderItemSeqId:" + oisgs.getString("orderItemSeqId") + "?????"); continue; } } orderItem.put("itemQuantity", oisgs.getBigDecimal("quantity")); orderItems.add(orderItem); } saleOrder.put("orderItems", orderItems); saleOrders.add(saleOrder); ctx.put("saleOrders", saleOrders); } if (UtilValidate.isEmpty(ctx)) { return ServiceUtil.returnSuccess(); } serviceResult = dispatcher.runSync("SFOutboundList", ctx); List<Map<String, Object>> shunfengResult = (List<Map<String, Object>>) serviceResult .get("shunfengResult"); if (ServiceUtil.isError(serviceResult)) { ApiUtil.messageEmailNotification(dispatcher, delegator, "shunfeng out error in ZuczugOutboundList", ServiceUtil.getErrorMessage(serviceResult), null); return ServiceUtil.returnError(ServiceUtil.getErrorMessage(serviceResult)); } for (Map<String, Object> map : shunfengResult) { String[] orderAndShipment = ((String) map.get("erpOrder")).split("_"); if (map.get("resultCode").equals("1")) { dispatcher.runSync("saveSFOrderAssoc", UtilMisc.toMap("SFCallBackId", map.get("shipmentId"), "localOrderTypeId", "ORDER_OUT", //? "orderId", orderAndShipment[0], "shipGroupSeqId", orderAndShipment[1], "userLogin", userLogin)); } else { Debug.logWarning("? " + orderAndShipment[0] + "_" + orderAndShipment[1] + " ???" + map.get("resultCode") + ":" + map.get("note"), "ZuczugOutboundList"); errorMessage.append("? " + orderAndShipment[0] + "_" + orderAndShipment[1] + " ???" + map.get("resultCode") + ":" + map.get("note") + "<br/>"); } } if (UtilValidate.isNotEmpty(errorMessage)) { ApiUtil.messageEmailNotification(dispatcher, delegator, "shunfeng out error in ZuczugOutboundList", errorMessage.toString(), null); } return result; } catch (GenericEntityException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (GenericServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } } /** * OrderItemShipGroupfaclityidfacilityIdO2O? * @author sven * @param dctx * @param context * @return */ public static Map<String, Object> fixOrderItemShipGroupFacility(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); Delegator delegator = dctx.getDelegator(); try { List<GenericValue> noFacilityShipGroups = delegator.findByAnd("OrderHeaderAndShipGroup", UtilMisc.toMap("facilityId", null, "orderTypeId", "SALES_ORDER")); for (GenericValue noFacilityShipGroup : noFacilityShipGroups) { String orderId = noFacilityShipGroup.get("orderId").toString(); GenericValue orderHeader = delegator.findOne("OrderHeader", false, UtilMisc.toMap("orderId", orderId)); GenericValue shipGroup = delegator.findOne("OrderItemShipGroup", false, UtilMisc.toMap("orderId", noFacilityShipGroup.getString("orderId"), "shipGroupSeqId", noFacilityShipGroup.getString("shipGroupSeqId"))); if (UtilValidate.isEmpty(shipGroup.getString("facilityId"))) { shipGroup.set("facilityId", orderHeader.get("originFacilityId")); delegator.store(shipGroup); Debug.log("====orderShipGroup?=-==="); } } } catch (GenericEntityException e) { e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } return result; } /** * ?? * @param dctx * @param context * @return */ @SuppressWarnings("unchecked") public static Map<String, Object> zuczugToSFProductSync(DispatchContext dctx, Map<String, Object> context) { Map<String, Object> result = ServiceUtil.returnSuccess(); LocalDispatcher dispatcher = dctx.getDispatcher(); Delegator delegator = dctx.getDelegator(); try { GenericValue userLogin = delegator.findByPrimaryKey("UserLogin", UtilMisc.toMap("userLoginId", "system")); List<GenericValue> finishedGoods = delegator.findByAnd("Product", UtilMisc.toMap("productTypeId", "FINISHED_GOOD", "isVirtual", "N")); // Map<String, Object> ctx = FastMap.newInstance(); Map<String, Object> ctx = new HashMap<String, Object>(); List<Map<String, Object>> items = new ArrayList<Map<String, Object>>(); List<Map<String, String>> totalSync = FastList.newInstance(); for (GenericValue finishedGood : finishedGoods) { GenericValue productAttr = delegator.findOne("ProductAttribute", false, UtilMisc.toMap("productId", finishedGood.getString("productId"), "attrName", "SF_SYNC")); if (UtilValidate.isEmpty(productAttr)) { Map<String, String> temp = UtilMisc.toMap("productId", finishedGood.getString("productId"), "productName", finishedGood.getString("productName")); totalSync.add(temp); } } int count = 1; for (Map<String, String> good : totalSync) { if (count == 1) { ctx = FastMap.newInstance(); items = new ArrayList<Map<String, Object>>(); } Map<String, Object> item = FastMap.newInstance(); item.put("skuNo", good.get("productId")); item.put("itemName", good.get("productName")); item.put("packUm", "CS"); items.add(item); dispatcher.runSync("createProductAttribute", UtilMisc.toMap("productId", good.get("productId"), "attrName", "SF_SYNC", "attrValue", "Y", "userLogin", userLogin)); if (count == 20 || count == totalSync.size()) { ctx.put("items", items); dispatcher.runSync("SFProductSync", ctx); count = 1; } else { count++; } } } catch (GenericEntityException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } catch (GenericServiceException e) { // TODO Auto-generated catch block e.printStackTrace(); return ServiceUtil.returnError(e.getMessage()); } return result; } }