Example usage for java.math BigDecimal ROUND_CEILING

List of usage examples for java.math BigDecimal ROUND_CEILING

Introduction

In this page you can find the example usage for java.math BigDecimal ROUND_CEILING.

Prototype

int ROUND_CEILING

To view the source code for java.math BigDecimal ROUND_CEILING.

Click Source Link

Document

Rounding mode to round towards positive infinity.

Usage

From source file:org.apache.ofbiz.shipment.thirdparty.usps.UspsServices.java

public static Map<String, Object> uspsPriorityMailInternationalLabel(DispatchContext dctx,
        Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
    String resource = (String) context.get("configProps");
    GenericValue shipmentRouteSegment = (GenericValue) context.get("shipmentRouteSegment");
    Locale locale = (Locale) context.get("locale");

    // Start the document
    Document requestDocument;/*from w  ww  . jav  a  2s.com*/
    boolean certify = false;
    String test = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "test", resource,
            "shipment.usps.test");
    if (!"Y".equalsIgnoreCase(test)) {
        requestDocument = createUspsRequestDocument("PriorityMailIntlRequest", false, delegator,
                shipmentGatewayConfigId, resource);
    } else {
        requestDocument = createUspsRequestDocument("PriorityMailIntlCertifyRequest", false, delegator,
                shipmentGatewayConfigId, resource);
        certify = true;
    }
    Element rootElement = requestDocument.getDocumentElement();

    // Retrieve from/to address and package details
    GenericValue originAddress = null;
    GenericValue originTelecomNumber = null;
    GenericValue destinationAddress = null;
    GenericValue destinationProvince = null;
    GenericValue destinationCountry = null;
    GenericValue destinationTelecomNumber = null;
    List<GenericValue> shipmentPackageRouteSegs = null;
    try {
        originAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress", false);
        originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber", false);
        destinationAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress", false);
        if (destinationAddress != null) {
            destinationProvince = destinationAddress.getRelatedOne("StateProvinceGeo", false);
            destinationCountry = destinationAddress.getRelatedOne("CountryGeo", false);
        }
        destinationTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber", false);
        shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, null,
                false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (originAddress == null || originTelecomNumber == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                "FacilityShipmentUspsPriorityMailLabelOriginAddressMissing", locale));
    }

    // Origin Info
    // USPS wants a separate first name and last, best we can do is split the string on the white space, if that doesn't work then default to putting the attnName in both fields
    String fromAttnName = originAddress.getString("attnName");
    String fromFirstName = StringUtils.defaultIfEmpty(StringUtils.substringBefore(fromAttnName, " "),
            fromAttnName);
    String fromLastName = StringUtils.defaultIfEmpty(StringUtils.substringAfter(fromAttnName, " "),
            fromAttnName);
    UtilXml.addChildElementValue(rootElement, "FromFirstName", fromFirstName, requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromLastName", fromLastName, requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromFirm", originAddress.getString("toName"), requestDocument);
    // The following 2 assignments are not typos - USPS address1 = OFBiz address2, USPS address2 = OFBiz address1
    UtilXml.addChildElementValue(rootElement, "FromAddress1", originAddress.getString("address2"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromAddress2", originAddress.getString("address1"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromCity", originAddress.getString("city"), requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromState", originAddress.getString("stateProvinceGeoId"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromZip5", originAddress.getString("postalCode"),
            requestDocument);
    // USPS expects a phone number consisting of area code + contact number as a single numeric string
    String fromPhoneNumber = originTelecomNumber.getString("areaCode")
            + originTelecomNumber.getString("contactNumber");
    fromPhoneNumber = StringUtil.removeNonNumeric(fromPhoneNumber);
    UtilXml.addChildElementValue(rootElement, "FromPhone", fromPhoneNumber, requestDocument);

    // Destination Info
    UtilXml.addChildElementValue(rootElement, "ToName", destinationAddress.getString("attnName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToFirm", destinationAddress.getString("toName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToAddress1", destinationAddress.getString("address1"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToAddress2", destinationAddress.getString("address2"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToCity", destinationAddress.getString("city"), requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToProvince", destinationProvince.getString("geoName"),
            requestDocument);
    // TODO: Test these country names, I think we're going to need to maintain a list of USPS names
    UtilXml.addChildElementValue(rootElement, "ToCountry", destinationCountry.getString("geoName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToPostalCode", destinationAddress.getString("postalCode"),
            requestDocument);
    // TODO: Figure out how to answer this question accurately
    UtilXml.addChildElementValue(rootElement, "ToPOBoxFlag", "N", requestDocument);
    String toPhoneNumber = destinationTelecomNumber.getString("countryCode")
            + destinationTelecomNumber.getString("areaCode")
            + destinationTelecomNumber.getString("contactNumber");
    UtilXml.addChildElementValue(rootElement, "ToPhone", toPhoneNumber, requestDocument);
    UtilXml.addChildElementValue(rootElement, "NonDeliveryOption", "RETURN", requestDocument);

    for (GenericValue shipmentPackageRouteSeg : shipmentPackageRouteSegs) {
        Document packageDocument = (Document) requestDocument.cloneNode(true);
        // This is our reference and can be whatever we want.  For lack of a better alternative we'll use shipmentId:shipmentPackageSeqId:shipmentRouteSegmentId
        String fromCustomsReference = shipmentRouteSegment.getString("shipmentId") + ":"
                + shipmentRouteSegment.getString("shipmentRouteSegmentId");
        fromCustomsReference = StringUtils.join(UtilMisc.toList(shipmentRouteSegment.get("shipmentId"),
                shipmentPackageRouteSeg.get("shipmentPackageSeqId"),
                shipmentRouteSegment.get("shipmentRouteSegementId")), ':');
        UtilXml.addChildElementValue(rootElement, "FromCustomsReference", fromCustomsReference,
                packageDocument);
        // Determine the container type for this package
        String container = "VARIABLE";
        String packageTypeCode = null;
        GenericValue shipmentPackage = null;
        List<GenericValue> shipmentPackageContents = null;
        try {
            shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage", false);
            shipmentPackageContents = shipmentPackage.getRelated("ShipmentPackageContent", null, null, false);
            GenericValue shipmentBoxType = shipmentPackage.getRelatedOne("ShipmentBoxType", false);
            if (shipmentBoxType != null) {
                GenericValue carrierShipmentBoxType = EntityUtil.getFirst(shipmentBoxType
                        .getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "USPS"), null, false));
                if (carrierShipmentBoxType != null) {
                    packageTypeCode = carrierShipmentBoxType.getString("packageTypeCode");
                    // Supported type codes
                    List<String> supportedPackageTypeCodes = UtilMisc.toList("LGFLATRATEBOX", "SMFLATRATEBOX",
                            "FLATRATEBOX", "MDFLATRATEBOX", "FLATRATEENV");
                    if (supportedPackageTypeCodes.contains(packageTypeCode)) {
                        container = packageTypeCode;
                    }
                }
            }
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        UtilXml.addChildElementValue(rootElement, "Container", container, packageDocument);
        // According to the docs sending an empty postage tag will cause the postage to be calculated
        UtilXml.addChildElementValue(rootElement, "Postage", "", packageDocument);

        BigDecimal packageWeight = shipmentPackage.getBigDecimal("weight");
        String weightUomId = shipmentPackage.getString("weightUomId");
        BigDecimal packageWeightPounds = UomWorker.convertUom(packageWeight, weightUomId, "WT_lb", dispatcher);
        Integer[] packagePoundsOunces = convertPoundsToPoundsOunces(packageWeightPounds);
        UtilXml.addChildElementValue(rootElement, "GrossPounds", packagePoundsOunces[0].toString(),
                packageDocument);
        UtilXml.addChildElementValue(rootElement, "GrossOunces", packagePoundsOunces[1].toString(),
                packageDocument);

        UtilXml.addChildElementValue(rootElement, "ContentType", "MERCHANDISE", packageDocument);
        UtilXml.addChildElementValue(rootElement, "Agreement", "N", packageDocument);
        UtilXml.addChildElementValue(rootElement, "ImageType", "PDF", packageDocument);
        // TODO: Try the different layouts
        UtilXml.addChildElementValue(rootElement, "ImageType", "ALLINONEFILE", packageDocument);
        UtilXml.addChildElementValue(rootElement, "CustomerRefNo", fromCustomsReference, packageDocument);

        // Add the shipping contents
        Element shippingContents = UtilXml.addChildElement(rootElement, "ShippingContents", packageDocument);
        for (GenericValue shipmentPackageContent : shipmentPackageContents) {
            Element itemDetail = UtilXml.addChildElement(shippingContents, "ItemDetail", packageDocument);
            GenericValue product = null;
            GenericValue originGeo = null;
            try {
                GenericValue shipmentItem = shipmentPackageContent.getRelatedOne("ShipmentItem", false);
                product = shipmentItem.getRelatedOne("Product", false);
                originGeo = product.getRelatedOne("OriginGeo", false);
            } catch (GenericEntityException e) {
                Debug.logInfo(e, module);
            }

            UtilXml.addChildElementValue(itemDetail, "Description", product.getString("productName"),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "Quantity", shipmentPackageContent
                    .getBigDecimal("quantity").setScale(0, BigDecimal.ROUND_CEILING).toPlainString(),
                    packageDocument);
            String packageContentValue = ShipmentWorker.getShipmentPackageContentValue(shipmentPackageContent)
                    .setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
            UtilXml.addChildElementValue(itemDetail, "Value", packageContentValue, packageDocument);
            BigDecimal productWeight = ProductWorker.getProductWeight(product, "WT_lbs", delegator, dispatcher);
            Integer[] productPoundsOunces = convertPoundsToPoundsOunces(productWeight);
            UtilXml.addChildElementValue(itemDetail, "NetPounds", productPoundsOunces[0].toString(),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "NetOunces", productPoundsOunces[1].toString(),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "HSTariffNumber", "", packageDocument);
            UtilXml.addChildElementValue(itemDetail, "CountryOfOrigin", originGeo.getString("geoName"),
                    packageDocument);
        }

        // Send the request
        Document responseDocument = null;
        String api = certify ? "PriorityMailIntlCertify" : "PriorityMailIntl";
        try {
            responseDocument = sendUspsRequest(api, requestDocument, delegator, shipmentGatewayConfigId,
                    resource, locale);
        } catch (UspsRequestException e) {
            Debug.logInfo(e, module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelSendingError",
                    UtilMisc.toMap("errorString", e.getMessage()), locale));
        }
        Element responseElement = responseDocument.getDocumentElement();

        // TODO: No mention of error returns in the docs

        String labelImageString = UtilXml.childElementValue(responseElement, "LabelImage");
        if (UtilValidate.isEmpty(labelImageString)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelResponseIncompleteElementLabelImage", locale));
        }
        shipmentPackageRouteSeg.setBytes("labelImage", Base64.base64Decode(labelImageString.getBytes()));
        String trackingCode = UtilXml.childElementValue(responseElement, "BarcodeNumber");
        if (UtilValidate.isEmpty(trackingCode)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelResponseIncompleteElementBarcodeNumber", locale));
        }
        shipmentPackageRouteSeg.set("trackingCode", trackingCode);
        try {
            shipmentPackageRouteSeg.store();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }

    }
    return ServiceUtil.returnSuccess();
}

From source file:org.ofbiz.shipment.thirdparty.usps.UspsServices.java

public static Map<String, Object> uspsPriorityMailInternationalLabel(DispatchContext dctx,
        Map<String, ? extends Object> context) {
    Delegator delegator = dctx.getDelegator();
    LocalDispatcher dispatcher = dctx.getDispatcher();
    String shipmentGatewayConfigId = (String) context.get("shipmentGatewayConfigId");
    String resource = (String) context.get("configProps");
    GenericValue shipmentRouteSegment = (GenericValue) context.get("shipmentRouteSegment");
    Locale locale = (Locale) context.get("locale");

    // Start the document
    Document requestDocument;/*w  w  w  .  j  a  v a 2 s .c o  m*/
    boolean certify = false;
    String test = getShipmentGatewayConfigValue(delegator, shipmentGatewayConfigId, "test", resource,
            "shipment.usps.test");
    if (!"Y".equalsIgnoreCase(test)) {
        requestDocument = createUspsRequestDocument("PriorityMailIntlRequest", false, delegator,
                shipmentGatewayConfigId, resource);
    } else {
        requestDocument = createUspsRequestDocument("PriorityMailIntlCertifyRequest", false, delegator,
                shipmentGatewayConfigId, resource);
        certify = true;
    }
    Element rootElement = requestDocument.getDocumentElement();

    // Retrieve from/to address and package details
    GenericValue originAddress = null;
    GenericValue originTelecomNumber = null;
    GenericValue destinationAddress = null;
    GenericValue destinationProvince = null;
    GenericValue destinationCountry = null;
    GenericValue destinationTelecomNumber = null;
    List<GenericValue> shipmentPackageRouteSegs = null;
    try {
        originAddress = shipmentRouteSegment.getRelatedOne("OriginPostalAddress", false);
        originTelecomNumber = shipmentRouteSegment.getRelatedOne("OriginTelecomNumber", false);
        destinationAddress = shipmentRouteSegment.getRelatedOne("DestPostalAddress", false);
        if (destinationAddress != null) {
            destinationProvince = destinationAddress.getRelatedOne("StateProvinceGeo", false);
            destinationCountry = destinationAddress.getRelatedOne("CountryGeo", false);
        }
        destinationTelecomNumber = shipmentRouteSegment.getRelatedOne("DestTelecomNumber", false);
        shipmentPackageRouteSegs = shipmentRouteSegment.getRelated("ShipmentPackageRouteSeg", null, null,
                false);
    } catch (GenericEntityException e) {
        Debug.logError(e, module);
    }
    if (originAddress == null || originTelecomNumber == null) {
        return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                "FacilityShipmentUspsPriorityMailLabelOriginAddressMissing", locale));
    }

    // Origin Info
    // USPS wants a separate first name and last, best we can do is split the string on the white space, if that doesn't work then default to putting the attnName in both fields
    String fromAttnName = originAddress.getString("attnName");
    String fromFirstName = StringUtils.defaultIfEmpty(StringUtils.substringBefore(fromAttnName, " "),
            fromAttnName);
    String fromLastName = StringUtils.defaultIfEmpty(StringUtils.substringAfter(fromAttnName, " "),
            fromAttnName);
    UtilXml.addChildElementValue(rootElement, "FromFirstName", fromFirstName, requestDocument);
    // UtilXml.addChildElementValue(rootElement, "FromMiddleInitial", "", requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromLastName", fromLastName, requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromFirm", originAddress.getString("toName"), requestDocument);
    // The following 2 assignments are not typos - USPS address1 = OFBiz address2, USPS address2 = OFBiz address1
    UtilXml.addChildElementValue(rootElement, "FromAddress1", originAddress.getString("address2"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromAddress2", originAddress.getString("address1"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromCity", originAddress.getString("city"), requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromState", originAddress.getString("stateProvinceGeoId"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "FromZip5", originAddress.getString("postalCode"),
            requestDocument);
    // USPS expects a phone number consisting of area code + contact number as a single numeric string
    String fromPhoneNumber = originTelecomNumber.getString("areaCode")
            + originTelecomNumber.getString("contactNumber");
    fromPhoneNumber = StringUtil.removeNonNumeric(fromPhoneNumber);
    UtilXml.addChildElementValue(rootElement, "FromPhone", fromPhoneNumber, requestDocument);

    // Destination Info
    UtilXml.addChildElementValue(rootElement, "ToName", destinationAddress.getString("attnName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToFirm", destinationAddress.getString("toName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToAddress1", destinationAddress.getString("address1"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToAddress2", destinationAddress.getString("address2"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToCity", destinationAddress.getString("city"), requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToProvince", destinationProvince.getString("geoName"),
            requestDocument);
    // TODO: Test these country names, I think we're going to need to maintain a list of USPS names
    UtilXml.addChildElementValue(rootElement, "ToCountry", destinationCountry.getString("geoName"),
            requestDocument);
    UtilXml.addChildElementValue(rootElement, "ToPostalCode", destinationAddress.getString("postalCode"),
            requestDocument);
    // TODO: Figure out how to answer this question accurately
    UtilXml.addChildElementValue(rootElement, "ToPOBoxFlag", "N", requestDocument);
    String toPhoneNumber = destinationTelecomNumber.getString("countryCode")
            + destinationTelecomNumber.getString("areaCode")
            + destinationTelecomNumber.getString("contactNumber");
    UtilXml.addChildElementValue(rootElement, "ToPhone", toPhoneNumber, requestDocument);
    UtilXml.addChildElementValue(rootElement, "NonDeliveryOption", "RETURN", requestDocument);

    for (GenericValue shipmentPackageRouteSeg : shipmentPackageRouteSegs) {
        Document packageDocument = (Document) requestDocument.cloneNode(true);
        //Element packageRootElement = requestDocument.getDocumentElement();
        // This is our reference and can be whatever we want.  For lack of a better alternative we'll use shipmentId:shipmentPackageSeqId:shipmentRouteSegmentId
        String fromCustomsReference = shipmentRouteSegment.getString("shipmentId") + ":"
                + shipmentRouteSegment.getString("shipmentRouteSegmentId");
        fromCustomsReference = StringUtils.join(UtilMisc.toList(shipmentRouteSegment.get("shipmentId"),
                shipmentPackageRouteSeg.get("shipmentPackageSeqId"),
                shipmentRouteSegment.get("shipmentRouteSegementId")), ':');
        UtilXml.addChildElementValue(rootElement, "FromCustomsReference", fromCustomsReference,
                packageDocument);
        // Determine the container type for this package
        String container = "VARIABLE";
        String packageTypeCode = null;
        GenericValue shipmentPackage = null;
        List<GenericValue> shipmentPackageContents = null;
        try {
            shipmentPackage = shipmentPackageRouteSeg.getRelatedOne("ShipmentPackage", false);
            shipmentPackageContents = shipmentPackage.getRelated("ShipmentPackageContent", null, null, false);
            GenericValue shipmentBoxType = shipmentPackage.getRelatedOne("ShipmentBoxType", false);
            if (shipmentBoxType != null) {
                GenericValue carrierShipmentBoxType = EntityUtil.getFirst(shipmentBoxType
                        .getRelated("CarrierShipmentBoxType", UtilMisc.toMap("partyId", "USPS"), null, false));
                if (carrierShipmentBoxType != null) {
                    packageTypeCode = carrierShipmentBoxType.getString("packageTypeCode");
                    // Supported type codes
                    List<String> supportedPackageTypeCodes = UtilMisc.toList("LGFLATRATEBOX", "SMFLATRATEBOX",
                            "FLATRATEBOX", "MDFLATRATEBOX", "FLATRATEENV");
                    if (supportedPackageTypeCodes.contains(packageTypeCode)) {
                        container = packageTypeCode;
                    }
                }
            }
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }
        UtilXml.addChildElementValue(rootElement, "Container", container, packageDocument);
        /* TODO:
        UtilXml.addChildElementValue(rootElement, "Insured", "", packageDocument);
        UtilXml.addChildElementValue(rootElement, "InsuredNumber", "", packageDocument);
        UtilXml.addChildElementValue(rootElement, "InsuredAmount", "", packageDocument);
        */
        // According to the docs sending an empty postage tag will cause the postage to be calculated
        UtilXml.addChildElementValue(rootElement, "Postage", "", packageDocument);

        BigDecimal packageWeight = shipmentPackage.getBigDecimal("weight");
        String weightUomId = shipmentPackage.getString("weightUomId");
        BigDecimal packageWeightPounds = UomWorker.convertUom(packageWeight, weightUomId, "WT_lb", dispatcher);
        Integer[] packagePoundsOunces = convertPoundsToPoundsOunces(packageWeightPounds);
        UtilXml.addChildElementValue(rootElement, "GrossPounds", packagePoundsOunces[0].toString(),
                packageDocument);
        UtilXml.addChildElementValue(rootElement, "GrossOunces", packagePoundsOunces[1].toString(),
                packageDocument);

        UtilXml.addChildElementValue(rootElement, "ContentType", "MERCHANDISE", packageDocument);
        UtilXml.addChildElementValue(rootElement, "Agreement", "N", packageDocument);
        UtilXml.addChildElementValue(rootElement, "ImageType", "PDF", packageDocument);
        // TODO: Try the different layouts
        UtilXml.addChildElementValue(rootElement, "ImageType", "ALLINONEFILE", packageDocument);
        UtilXml.addChildElementValue(rootElement, "CustomerRefNo", fromCustomsReference, packageDocument);

        // Add the shipping contents
        Element shippingContents = UtilXml.addChildElement(rootElement, "ShippingContents", packageDocument);
        for (GenericValue shipmentPackageContent : shipmentPackageContents) {
            Element itemDetail = UtilXml.addChildElement(shippingContents, "ItemDetail", packageDocument);
            GenericValue product = null;
            GenericValue originGeo = null;
            try {
                GenericValue shipmentItem = shipmentPackageContent.getRelatedOne("ShipmentItem", false);
                product = shipmentItem.getRelatedOne("Product", false);
                originGeo = product.getRelatedOne("OriginGeo", false);
            } catch (GenericEntityException e) {
                Debug.logInfo(e, module);
            }

            UtilXml.addChildElementValue(itemDetail, "Description", product.getString("productName"),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "Quantity", shipmentPackageContent
                    .getBigDecimal("quantity").setScale(0, BigDecimal.ROUND_CEILING).toPlainString(),
                    packageDocument);
            String packageContentValue = ShipmentWorker.getShipmentPackageContentValue(shipmentPackageContent)
                    .setScale(2, BigDecimal.ROUND_HALF_UP).toPlainString();
            UtilXml.addChildElementValue(itemDetail, "Value", packageContentValue, packageDocument);
            BigDecimal productWeight = ProductWorker.getProductWeight(product, "WT_lbs", delegator, dispatcher);
            Integer[] productPoundsOunces = convertPoundsToPoundsOunces(productWeight);
            UtilXml.addChildElementValue(itemDetail, "NetPounds", productPoundsOunces[0].toString(),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "NetOunces", productPoundsOunces[1].toString(),
                    packageDocument);
            UtilXml.addChildElementValue(itemDetail, "HSTariffNumber", "", packageDocument);
            UtilXml.addChildElementValue(itemDetail, "CountryOfOrigin", originGeo.getString("geoName"),
                    packageDocument);
        }

        // Send the request
        Document responseDocument = null;
        String api = certify ? "PriorityMailIntlCertify" : "PriorityMailIntl";
        try {
            responseDocument = sendUspsRequest(api, requestDocument, delegator, shipmentGatewayConfigId,
                    resource, locale);
        } catch (UspsRequestException e) {
            Debug.logInfo(e, module);
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelSendingError",
                    UtilMisc.toMap("errorString", e.getMessage()), locale));
        }
        Element responseElement = responseDocument.getDocumentElement();

        // TODO: No mention of error returns in the docs

        String labelImageString = UtilXml.childElementValue(responseElement, "LabelImage");
        if (UtilValidate.isEmpty(labelImageString)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelResponseIncompleteElementLabelImage", locale));
        }
        shipmentPackageRouteSeg.setBytes("labelImage", Base64.base64Decode(labelImageString.getBytes()));
        String trackingCode = UtilXml.childElementValue(responseElement, "BarcodeNumber");
        if (UtilValidate.isEmpty(trackingCode)) {
            return ServiceUtil.returnError(UtilProperties.getMessage(resourceError,
                    "FacilityShipmentUspsPriorityMailLabelResponseIncompleteElementBarcodeNumber", locale));
        }
        shipmentPackageRouteSeg.set("trackingCode", trackingCode);
        try {
            shipmentPackageRouteSeg.store();
        } catch (GenericEntityException e) {
            Debug.logError(e, module);
        }

    }
    return ServiceUtil.returnSuccess();
}

From source file:org.pentaho.di.core.ConstTest.java

@Test
public void testRound_Long() {
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_UP));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_DOWN));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_CEILING));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_FLOOR));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_HALF_UP));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(1L, Const.round(1L, 0, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(1L, Const.round(1L, 0, Const.ROUND_HALF_CEILING));

    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_UP));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_DOWN));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_CEILING));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_FLOOR));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_HALF_UP));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(2L, Const.round(2L, 0, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(2L, Const.round(2L, 0, Const.ROUND_HALF_CEILING));

    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_UP));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_DOWN));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_CEILING));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_FLOOR));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_HALF_UP));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-1L, Const.round(-1L, 0, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-1L, Const.round(-1L, 0, Const.ROUND_HALF_CEILING));

    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_UP));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_DOWN));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_CEILING));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_FLOOR));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_HALF_UP));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-2L, Const.round(-2L, 0, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-2L, Const.round(-2L, 0, Const.ROUND_HALF_CEILING));

    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_UP));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(100L, Const.round(100L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(100L, Const.round(100L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(200L, Const.round(120L, -2, BigDecimal.ROUND_UP));
    assertEquals(100L, Const.round(120L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(200L, Const.round(120L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(100L, Const.round(120L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(100L, Const.round(120L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(100L, Const.round(120L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(100L, Const.round(120L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(100L, Const.round(120L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(200L, Const.round(150L, -2, BigDecimal.ROUND_UP));
    assertEquals(100L, Const.round(150L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(200L, Const.round(150L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(100L, Const.round(150L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(200L, Const.round(150L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(100L, Const.round(150L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(200L, Const.round(150L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(200L, Const.round(150L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(200L, Const.round(170L, -2, BigDecimal.ROUND_UP));
    assertEquals(100L, Const.round(170L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(200L, Const.round(170L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(100L, Const.round(170L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(200L, Const.round(170L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(200L, Const.round(170L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(200L, Const.round(170L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(200L, Const.round(170L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_UP));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(200L, Const.round(200L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(200L, Const.round(200L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(300L, Const.round(220L, -2, BigDecimal.ROUND_UP));
    assertEquals(200L, Const.round(220L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(300L, Const.round(220L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(200L, Const.round(220L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(200L, Const.round(220L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(200L, Const.round(220L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(200L, Const.round(220L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(200L, Const.round(220L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(300L, Const.round(250L, -2, BigDecimal.ROUND_UP));
    assertEquals(200L, Const.round(250L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(300L, Const.round(250L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(200L, Const.round(250L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(300L, Const.round(250L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(200L, Const.round(250L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(200L, Const.round(250L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(300L, Const.round(250L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(300L, Const.round(270L, -2, BigDecimal.ROUND_UP));
    assertEquals(200L, Const.round(270L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(300L, Const.round(270L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(200L, Const.round(270L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(300L, Const.round(270L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(300L, Const.round(270L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(300L, Const.round(270L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(300L, Const.round(270L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_UP));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-100L, Const.round(-100L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-100L, Const.round(-100L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-200L, Const.round(-120L, -2, BigDecimal.ROUND_UP));
    assertEquals(-100L, Const.round(-120L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-100L, Const.round(-120L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-200L, Const.round(-120L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-100L, Const.round(-120L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-100L, Const.round(-120L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-100L, Const.round(-120L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-100L, Const.round(-120L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-200L, Const.round(-150L, -2, BigDecimal.ROUND_UP));
    assertEquals(-100L, Const.round(-150L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-100L, Const.round(-150L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-200L, Const.round(-150L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-200L, Const.round(-150L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-100L, Const.round(-150L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-200L, Const.round(-150L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-100L, Const.round(-150L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-200L, Const.round(-170L, -2, BigDecimal.ROUND_UP));
    assertEquals(-100L, Const.round(-170L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-100L, Const.round(-170L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-200L, Const.round(-170L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-200L, Const.round(-170L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-200L, Const.round(-170L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-200L, Const.round(-170L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-200L, Const.round(-170L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_UP));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-200L, Const.round(-200L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-200L, Const.round(-200L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-300L, Const.round(-220L, -2, BigDecimal.ROUND_UP));
    assertEquals(-200L, Const.round(-220L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-200L, Const.round(-220L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-300L, Const.round(-220L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-200L, Const.round(-220L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-200L, Const.round(-220L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-200L, Const.round(-220L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-200L, Const.round(-220L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-300L, Const.round(-250L, -2, BigDecimal.ROUND_UP));
    assertEquals(-200L, Const.round(-250L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-200L, Const.round(-250L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-300L, Const.round(-250L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-300L, Const.round(-250L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-200L, Const.round(-250L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-200L, Const.round(-250L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-200L, Const.round(-250L, -2, Const.ROUND_HALF_CEILING));

    assertEquals(-300L, Const.round(-270L, -2, BigDecimal.ROUND_UP));
    assertEquals(-200L, Const.round(-270L, -2, BigDecimal.ROUND_DOWN));
    assertEquals(-200L, Const.round(-270L, -2, BigDecimal.ROUND_CEILING));
    assertEquals(-300L, Const.round(-270L, -2, BigDecimal.ROUND_FLOOR));
    assertEquals(-300L, Const.round(-270L, -2, BigDecimal.ROUND_HALF_UP));
    assertEquals(-300L, Const.round(-270L, -2, BigDecimal.ROUND_HALF_DOWN));
    assertEquals(-300L, Const.round(-270L, -2, BigDecimal.ROUND_HALF_EVEN));
    assertEquals(-300L, Const.round(-270L, -2, Const.ROUND_HALF_CEILING));
}

From source file:org.apache.ofbiz.shipment.thirdparty.usps.UspsServices.java

public static Integer[] convertPoundsToPoundsOunces(BigDecimal decimalPounds) {
    if (decimalPounds == null)
        return null;
    Integer[] poundsOunces = new Integer[2];
    poundsOunces[0] = Integer.valueOf(decimalPounds.setScale(0, BigDecimal.ROUND_FLOOR).toPlainString());
    // (weight % 1) * 16 rounded up to nearest whole number
    poundsOunces[1] = Integer.valueOf(decimalPounds.remainder(BigDecimal.ONE).multiply(new BigDecimal("16"))
            .setScale(0, BigDecimal.ROUND_CEILING).toPlainString());
    return poundsOunces;
}

From source file:org.kuali.ole.select.document.service.impl.OleInvoiceServiceImpl.java

/**
 * This method calculates the Amount and the Percent in Accounting Line if the Invoiced List Price changed
 *///w w  w  . j av  a 2s .c o  m
public void calculateAccount(PurApItem purapItem) {
    purapItem.setExtendedPrice(purapItem.calculateExtendedPrice());
    List<PurApAccountingLine> purApAccountingLines = purapItem.getSourceAccountingLines();
    BigDecimal totalPercent = BigDecimal.ZERO;
    BigDecimal totalAmt = BigDecimal.ZERO;
    for (PurApAccountingLine account : purApAccountingLines) {
        if (purapItem.getTotalAmount() != null && !purapItem.getTotalAmount().equals(KualiDecimal.ZERO)) {
            if (account.getAccountLinePercent() != null
                    && (account.getAmount() == null || account.getAmount().equals(KualiDecimal.ZERO))) {
                BigDecimal percent = account.getAccountLinePercent().divide(new BigDecimal(100));
                account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
            } else if (account.getAmount() != null && account.getAmount().isNonZero()
                    && account.getAccountLinePercent() == null) {
                KualiDecimal dollar = account.getAmount().multiply(new KualiDecimal(100));
                BigDecimal dollarToPercent = dollar.bigDecimalValue()
                        .divide((purapItem.getTotalAmount().bigDecimalValue()), 0, RoundingMode.FLOOR);
                account.setAccountLinePercent(dollarToPercent);
            } else if (account.getAmount() != null && account.getAmount().isZero()
                    && account.getAccountLinePercent() == null) {
                account.setAccountLinePercent(new BigDecimal(0));
            } else if ((account.getAmount() != null && account.getAccountLinePercent() != null)
                    || (account.getAmount() != null && account.getAccountLinePercent().intValue() == 100)) {
                BigDecimal percent = account.getAccountLinePercent().divide(new BigDecimal(100));
                account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
            }
            totalPercent = totalPercent.add(account.getAccountLinePercent());
            totalAmt = totalAmt.add(account.getAmount().bigDecimalValue());
        } else {
            account.setAmount(KualiDecimal.ZERO);
        }
    }
    // If Total Percent or Total Amount mis matches,percentage is divided across accounting lines.
    if (totalPercent.intValue() != 100 || (purapItem.getTotalAmount() != null
            && totalAmt.compareTo(purapItem.getTotalAmount().bigDecimalValue()) != 0)) {
        for (PurApAccountingLine account : purApAccountingLines) {
            if (purapItem.getTotalAmount() != null && !purapItem.getTotalAmount().equals(KualiDecimal.ZERO)) {
                BigDecimal percent = BigDecimal.ONE.divide(new BigDecimal(purApAccountingLines.size()),
                        BigDecimal.ROUND_CEILING, BigDecimal.ROUND_HALF_UP);
                account.setAmount((purapItem.getTotalAmount().multiply(new KualiDecimal(percent))));
            } else {
                account.setAmount(KualiDecimal.ZERO);
            }
        }
    }

}