Search in sources :

Example 11 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class UspsRequestException method uspsAddressValidation.

/*

    Address Standardization Samples: (API=Verify)

    Request:
    <AddressValidateRequest USERID="xxxxxxx" PASSWORD="xxxxxxx">
        <Address ID="0">
            <Address1></Address1>
            <Address2>6406 Ivy Lane</Address2>
            <City>Greenbelt</City>
            <State>MD</State>
            <Zip5></Zip5>
            <Zip4></Zip4>
        </Address>
    </AddressValidateRequest>

    Response:
    <AddressValidateResponse>
        <Address ID="0">
            <Address2>6406 IVY LN</Address2>
            <City>GREENBELT</City>
            <State>MD</State>
            <Zip5>20770</Zip5>
            <Zip4>1440</Zip4>
        </Address>
    </AddressValidateResponse>

    Note:
        The service parameters address1 and addess2 follow the OFBiz naming convention,
        and are converted to USPS conventions internally
        (OFBiz address1 = USPS address2, OFBiz address2 = USPS address1)

    */
public static Map<String, Object> uspsAddressValidation(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
    String resource = (String) context.get("configProps");
    String state = (String) context.get("state");
    String city = (String) context.get("city");
    String zip5 = (String) context.get("zip5");
    Locale locale = (Locale) context.get("locale");
    if (// No state, city or zip5
    (UtilValidate.isEmpty(state) && UtilValidate.isEmpty(city) && UtilValidate.isEmpty(zip5)) || (UtilValidate.isEmpty(zip5) && (UtilValidate.isEmpty(state) || UtilValidate.isEmpty(city)))) {
        // Both state and city are required if no zip5
        Debug.logError("USPS address validation requires either zip5 or city and state", module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsAddressValidationStateAndCityOrZipRqd", locale));
    }
    Document requestDocument = createUspsRequestDocument("AddressValidateRequest", true, delegator, shipmentGatewayConfigId, resource);
    Element addressElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "Address", requestDocument);
    addressElement.setAttribute("ID", "0");
    // 38 chars max
    UtilXml.addChildElementValue(addressElement, "FirmName", (String) context.get("firmName"), requestDocument);
    // 38 chars max
    UtilXml.addChildElementValue(addressElement, "Address1", (String) context.get("address2"), requestDocument);
    // 38 chars max
    UtilXml.addChildElementValue(addressElement, "Address2", (String) context.get("address1"), requestDocument);
    // 15 chars max
    UtilXml.addChildElementValue(addressElement, "City", (String) context.get("city"), requestDocument);
    UtilXml.addChildElementValue(addressElement, "State", (String) context.get("state"), requestDocument);
    UtilXml.addChildElementValue(addressElement, "Zip5", (String) context.get("zip5"), requestDocument);
    UtilXml.addChildElementValue(addressElement, "Zip4", (String) context.get("zip4"), requestDocument);
    Document responseDocument = null;
    try {
        responseDocument = sendUspsRequest("Verify", requestDocument, delegator, shipmentGatewayConfigId, resource, locale);
    } catch (UspsRequestException e) {
        Debug.logInfo(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsAddressValidationSendingError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    Element respAddressElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "Address");
    if (respAddressElement == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsAddressValidationIncompleteResponse", locale));
    }
    Element respErrorElement = UtilXml.firstChildElement(respAddressElement, "Error");
    if (respErrorElement != null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsAddressValidationIncompleteResponse", UtilMisc.toMap("errorString", UtilXml.childElementValue(respErrorElement, "Description")), locale));
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    // Note: a FirmName element is not returned if empty
    String firmName = UtilXml.childElementValue(respAddressElement, "FirmName");
    if (UtilValidate.isNotEmpty(firmName)) {
        result.put("firmName", firmName);
    }
    // Note: an Address1 element is not returned if empty
    String address1 = UtilXml.childElementValue(respAddressElement, "Address1");
    if (UtilValidate.isNotEmpty(address1)) {
        result.put("address2", address1);
    }
    result.put("address1", UtilXml.childElementValue(respAddressElement, "Address2"));
    result.put("city", UtilXml.childElementValue(respAddressElement, "City"));
    result.put("state", UtilXml.childElementValue(respAddressElement, "State"));
    result.put("zip5", UtilXml.childElementValue(respAddressElement, "Zip5"));
    result.put("zip4", UtilXml.childElementValue(respAddressElement, "Zip4"));
    Element returnTextElement = UtilXml.firstChildElement(respAddressElement, "ReturnText");
    if (returnTextElement != null) {
        result.put("returnText", UtilXml.elementValue(returnTextElement));
    }
    return result;
}
Also used : Locale(java.util.Locale) Delegator(org.apache.ofbiz.entity.Delegator) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 12 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class UspsRequestException method uspsCityStateLookup.

/*

    City/State Lookup Samples: (API=CityStateLookup)

    Request:
    <CityStateLookupRequest USERID="xxxxxxx" PASSWORD="xxxxxxx">
        <ZipCode ID="0">
            <Zip5>90210</Zip5>
        </ZipCode>
    </CityStateLookupRequest>

    Response:
    <CityStateLookupResponse>
        <ZipCode ID="0">
            <Zip5>90210</Zip5>
            <City>BEVERLY HILLS</City>
            <State>CA</State>
        </ZipCode>
    </CityStateLookupResponse>

    */
public static Map<String, Object> uspsCityStateLookup(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
    String resource = (String) context.get("configProps");
    Locale locale = (Locale) context.get("locale");
    Document requestDocument = createUspsRequestDocument("CityStateLookupRequest", true, delegator, shipmentGatewayConfigId, resource);
    Element zipCodeElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "ZipCode", requestDocument);
    zipCodeElement.setAttribute("ID", "0");
    // trim leading/trailing spaces
    String zipCode = ((String) context.get("zip5")).trim();
    // only the first 5 digits are used, the rest are ignored
    UtilXml.addChildElementValue(zipCodeElement, "Zip5", zipCode, requestDocument);
    Document responseDocument = null;
    try {
        responseDocument = sendUspsRequest("CityStateLookup", requestDocument, delegator, shipmentGatewayConfigId, resource, locale);
    } catch (UspsRequestException e) {
        Debug.logInfo(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsCityStateLookupSendingError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    Element respAddressElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "ZipCode");
    if (respAddressElement == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsCityStateLookupIncompleteResponse", locale));
    }
    Element respErrorElement = UtilXml.firstChildElement(respAddressElement, "Error");
    if (respErrorElement != null) {
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsCityStateLookupResponseError", UtilMisc.toMap("errorString", UtilXml.childElementValue(respErrorElement, "Description")), locale));
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    String city = UtilXml.childElementValue(respAddressElement, "City");
    if (UtilValidate.isEmpty(city)) {
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsCityStateLookupIncompleteCityElement", locale));
    }
    result.put("city", city);
    String state = UtilXml.childElementValue(respAddressElement, "State");
    if (UtilValidate.isEmpty(state)) {
        return ServiceUtil.returnFailure(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsCityStateLookupIncompleteStateElement", locale));
    }
    result.put("state", state);
    return result;
}
Also used : Locale(java.util.Locale) Delegator(org.apache.ofbiz.entity.Delegator) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 13 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class UspsRequestException method uspsDomesticRate.

/*

    Domestic Rate Calculator Samples: (API=Rate)

    Request:
    <RateRequest USERID="xxxxxx" PASSWORD="xxxxxxx">
        <Package ID="0">
            <Service>Priority</Service>
            <ZipOrigination>20770</ZipOrigination>
            <ZipDestination>09021</ZipDestination>
            <Pounds>5</Pounds>
            <Ounces>1</Ounces>
            <Container>None</Container>
            <Size>Regular</Size>
            <Machinable>False</Machinable>
        </Package>
    </RateRequest>

    Response:
    <RateResponse>
        <Package ID="0">
            <Service>Priority</Service>
            <ZipOrigination>20770</ZipOrigination>
            <ZipDestination>09021</ZipDestination>
            <Pounds>5</Pounds>
            <Ounces>1</Ounces>
            <Container>None</Container>
            <Size>REGULAR</Size>
            <Machinable>FALSE</Machinable>
            <Zone>3</Zone>
            <Postage>7.90</Postage>
            <RestrictionCodes>B-B1-C-D-U</RestrictionCodes>
            <RestrictionDescription>
            B. Form 2976-A is required for all mail weighing 16 ounces or more, with exceptions noted below.
            In addition, mailers must properly complete required customs documentation when mailing any potentially
            dutiable mail addressed to an APO or FPO regardless of weight. B1. Form 2976 or 2976-A is required.
            Articles are liable for customs duty and/or purchase tax unless they are bona fide gifts intended for
            use by military personnel or their dependents. C. Cigarettes and other tobacco products are prohibited.
            D. Coffee is prohibited. U. Parcels must weigh less than 16 ounces when addressed to Box R.
            </RestrictionDescription>
        </Package>
    </RateResponse>

    */
public static Map<String, Object> uspsDomesticRate(DispatchContext dctx, Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
    String resource = (String) context.get("configProps");
    Locale locale = (Locale) context.get("locale");
    Document requestDocument = createUspsRequestDocument("RateRequest", true, delegator, shipmentGatewayConfigId, resource);
    Element packageElement = UtilXml.addChildElement(requestDocument.getDocumentElement(), "Package", requestDocument);
    packageElement.setAttribute("ID", "0");
    UtilXml.addChildElementValue(packageElement, "Service", (String) context.get("service"), requestDocument);
    UtilXml.addChildElementValue(packageElement, "ZipOrigination", (String) context.get("originZip"), requestDocument);
    UtilXml.addChildElementValue(packageElement, "ZipDestination", (String) context.get("destinationZip"), requestDocument);
    UtilXml.addChildElementValue(packageElement, "Pounds", (String) context.get("pounds"), requestDocument);
    UtilXml.addChildElementValue(packageElement, "Ounces", (String) context.get("ounces"), requestDocument);
    String container = (String) context.get("container");
    if (UtilValidate.isEmpty(container)) {
        container = "None";
    }
    UtilXml.addChildElementValue(packageElement, "Container", container, requestDocument);
    String size = (String) context.get("size");
    if (UtilValidate.isEmpty(size)) {
        size = "Regular";
    }
    UtilXml.addChildElementValue(packageElement, "Size", size, requestDocument);
    String machinable = (String) context.get("machinable");
    if (UtilValidate.isEmpty(machinable)) {
        machinable = "False";
    }
    UtilXml.addChildElementValue(packageElement, "Machinable", machinable, requestDocument);
    Document responseDocument = null;
    try {
        responseDocument = sendUspsRequest("Rate", requestDocument, delegator, shipmentGatewayConfigId, resource, locale);
    } catch (UspsRequestException e) {
        Debug.logInfo(e, module);
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticSendingError", UtilMisc.toMap("errorString", e.getMessage()), locale));
    }
    Element respPackageElement = UtilXml.firstChildElement(responseDocument.getDocumentElement(), "Package");
    if (respPackageElement == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseIncompleteElementPackage", locale));
    }
    Element respErrorElement = UtilXml.firstChildElement(respPackageElement, "Error");
    if (respErrorElement != null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseError", UtilMisc.toMap("errorString", UtilXml.childElementValue(respErrorElement, "Description")), locale));
    }
    Map<String, Object> result = ServiceUtil.returnSuccess();
    String zone = UtilXml.childElementValue(respPackageElement, "Zone");
    if (UtilValidate.isEmpty(zone)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseIncompleteElementZone", locale));
    }
    result.put("zone", zone);
    String postage = UtilXml.childElementValue(respPackageElement, "Postage");
    if (UtilValidate.isEmpty(postage)) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError, "FacilityShipmentUspsRateDomesticResponseIncompleteElementPostage", locale));
    }
    result.put("postage", postage);
    String restrictionCodes = UtilXml.childElementValue(respPackageElement, "RestrictionCodes");
    if (UtilValidate.isNotEmpty(restrictionCodes)) {
        result.put("restrictionCodes", restrictionCodes);
    }
    String restrictionDesc = UtilXml.childElementValue(respPackageElement, "RestrictionDescription");
    if (UtilValidate.isNotEmpty(restrictionCodes)) {
        result.put("restrictionDesc", restrictionDesc);
    }
    return result;
}
Also used : Locale(java.util.Locale) Delegator(org.apache.ofbiz.entity.Delegator) Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 14 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class VerifyPickSessionRow method issueItemToShipment.

protected void issueItemToShipment(String shipmentId, String picklistBinId, GenericValue userLogin, BigDecimal quantity, LocalDispatcher dispatcher, Locale locale) throws GeneralException {
    if (quantity == null) {
        quantity = this.getReadyToVerifyQty();
    }
    Map<String, Object> issueOrderItemMap = new HashMap<String, Object>();
    issueOrderItemMap.put("shipmentId", shipmentId);
    issueOrderItemMap.put("orderId", this.getOrderId());
    issueOrderItemMap.put("orderItemSeqId", this.getOrderItemSeqId());
    issueOrderItemMap.put("shipGroupSeqId", this.getShipGroupSeqId());
    issueOrderItemMap.put("inventoryItemId", this.getInventoryItemId());
    issueOrderItemMap.put("quantity", quantity);
    issueOrderItemMap.put("userLogin", userLogin);
    Map<String, Object> issueOrderItemResp = dispatcher.runSync("issueOrderItemShipGrpInvResToShipment", issueOrderItemMap);
    if (ServiceUtil.isError(issueOrderItemResp)) {
        throw new GeneralException(ServiceUtil.getErrorMessage(issueOrderItemResp));
    }
    String shipmentItemSeqId = (String) issueOrderItemResp.get("shipmentItemSeqId");
    if (shipmentItemSeqId == null) {
        throw new GeneralException(UtilProperties.getMessage("ProductErrorUiLabels", "ProductErrorIssueItemDidNotReturnAValidShipmentItemSeqId", locale));
    } else {
        this.setShipmentItemSeqId(shipmentItemSeqId);
    }
    if (picklistBinId != null) {
        // find the pick list item
        Delegator delegator = dispatcher.getDelegator();
        Map<String, Object> picklistItemMap = new HashMap<String, Object>();
        picklistItemMap.put("picklistBinId", picklistBinId);
        picklistItemMap.put("orderId", this.getOrderId());
        picklistItemMap.put("orderItemSeqId", this.getOrderItemSeqId());
        picklistItemMap.put("shipGroupSeqId", this.getShipGroupSeqId());
        picklistItemMap.put("inventoryItemId", this.getInventoryItemId());
        GenericValue picklistItem = EntityQuery.use(delegator).from("PicklistItem").where(picklistItemMap).cache(true).queryOne();
        if (UtilValidate.isNotEmpty(picklistItem)) {
            BigDecimal itemQty = picklistItem.getBigDecimal("quantity");
            if (itemQty.compareTo(quantity) == 0) {
                // set to complete
                picklistItemMap.put("itemStatusId", "PICKITEM_COMPLETED");
            } else {
                picklistItemMap.put("itemStatusId", "PICKITEM_CANCELLED");
            }
            picklistItemMap.put("userLogin", userLogin);
            Map<String, Object> picklistItemResp = dispatcher.runSync("updatePicklistItem", picklistItemMap);
            if (ServiceUtil.isError(picklistItemResp)) {
                throw new GeneralException(ServiceUtil.getErrorMessage(picklistItemResp));
            }
        }
    }
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal)

Example 15 with Delegator

use of org.apache.ofbiz.entity.Delegator in project ofbiz-framework by apache.

the class WeightPackageSession method upsShipmentConfirm.

protected BigDecimal upsShipmentConfirm() throws GeneralException {
    Delegator delegator = this.getDelegator();
    BigDecimal actualCost = BigDecimal.ZERO;
    List<GenericValue> shipmentRouteSegments = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId).queryList();
    if (UtilValidate.isNotEmpty(shipmentRouteSegments)) {
        for (GenericValue shipmentRouteSegment : shipmentRouteSegments) {
            Map<String, Object> shipmentRouteSegmentMap = new HashMap<String, Object>();
            shipmentRouteSegmentMap.put("shipmentId", shipmentId);
            shipmentRouteSegmentMap.put("shipmentRouteSegmentId", shipmentRouteSegment.getString("shipmentRouteSegmentId"));
            shipmentRouteSegmentMap.put("userLogin", userLogin);
            Map<String, Object> shipmentRouteSegmentResult = this.getDispatcher().runSync("upsShipmentConfirm", shipmentRouteSegmentMap);
            if (ServiceUtil.isError(shipmentRouteSegmentResult)) {
                throw new GeneralException(ServiceUtil.getErrorMessage(shipmentRouteSegmentResult));
            }
            GenericValue shipRouteSeg = EntityQuery.use(delegator).from("ShipmentRouteSegment").where("shipmentId", shipmentId, "shipmentRouteSegmentId", shipmentRouteSegment.getString("shipmentRouteSegmentId")).queryOne();
            actualCost = actualCost.add(shipRouteSeg.getBigDecimal("actualCost"));
        }
    }
    return actualCost;
}
Also used : GenericValue(org.apache.ofbiz.entity.GenericValue) GeneralException(org.apache.ofbiz.base.util.GeneralException) Delegator(org.apache.ofbiz.entity.Delegator) HashMap(java.util.HashMap) BigDecimal(java.math.BigDecimal)

Aggregations

Delegator (org.apache.ofbiz.entity.Delegator)869 GenericValue (org.apache.ofbiz.entity.GenericValue)721 GenericEntityException (org.apache.ofbiz.entity.GenericEntityException)611 Locale (java.util.Locale)485 HashMap (java.util.HashMap)328 LocalDispatcher (org.apache.ofbiz.service.LocalDispatcher)324 GenericServiceException (org.apache.ofbiz.service.GenericServiceException)278 BigDecimal (java.math.BigDecimal)205 LinkedList (java.util.LinkedList)166 Timestamp (java.sql.Timestamp)163 GeneralException (org.apache.ofbiz.base.util.GeneralException)130 IOException (java.io.IOException)117 Map (java.util.Map)113 EntityCondition (org.apache.ofbiz.entity.condition.EntityCondition)61 Security (org.apache.ofbiz.security.Security)60 HttpSession (javax.servlet.http.HttpSession)59 Properties (java.util.Properties)37 UtilProperties (org.apache.ofbiz.base.util.UtilProperties)37 EntityUtilProperties (org.apache.ofbiz.entity.util.EntityUtilProperties)35 EntityListIterator (org.apache.ofbiz.entity.util.EntityListIterator)33