Search in sources :

Example 1 with Decimal

use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.

the class DecimalFormatter method apply.

@Override
public void apply(Context ctx, Arguments args, Variables variables) throws CodeExecuteException {
    Variable var = variables.first();
    Decimal number = GeneralUtils.nodeToDecimal(var.node());
    if (number == null) {
        var.setMissing();
        return;
    }
    CLDR cldr = ctx.cldr();
    DecimalFormatOptions opts = (DecimalFormatOptions) args.getOpaque();
    String result = cldr.Numbers.formatDecimal(number, opts);
    var.set(result);
}
Also used : Variable(com.squarespace.template.Variable) Decimal(com.squarespace.cldrengine.api.Decimal) CLDR(com.squarespace.cldrengine.CLDR) DecimalFormatOptions(com.squarespace.cldrengine.api.DecimalFormatOptions)

Example 2 with Decimal

use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.

the class MessageFormats method decimal.

/**
 * Number / decimal message formatter.
 */
private String decimal(List<Object> args, List<String> options) {
    if (args.isEmpty()) {
        return "";
    }
    JsonNode node = (JsonNode) args.get(0);
    if (node == null) {
        return "";
    }
    Decimal value = this.converter.asDecimal(node);
    DecimalFormatOptions opts = OptionParsers.decimal(options);
    return cldr.Numbers.formatDecimal(value, opts);
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) JsonNode(com.fasterxml.jackson.databind.JsonNode) DecimalFormatOptions(com.squarespace.cldrengine.api.DecimalFormatOptions)

Example 3 with Decimal

use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.

the class CommerceUtils method getLowestPriceAmongVariants.

public static JsonNode getLowestPriceAmongVariants(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode first = variants.get(0);
            JsonNode moneyNode = isTruthy(first.path("onSale")) ? first.path("salePriceMoney") : first.path("priceMoney");
            Decimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode var = variants.get(i);
                JsonNode currentMoneyNode = isTruthy(var.path("onSale")) ? var.path("salePriceMoney") : var.path("priceMoney");
                Decimal current = getAmountFromMoneyNode(currentMoneyNode);
                if (current.compare(price) < 0) {
                    price = current;
                    moneyNode = currentMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 4 with Decimal

use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.

the class CommerceUtils method getSalePriceMoneyNode.

public static JsonNode getSalePriceMoneyNode(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            Decimal salePrice = null;
            JsonNode salePriceNode = null;
            for (int i = 0; i < variants.size(); i++) {
                JsonNode variant = variants.path(i);
                JsonNode priceMoney = variant.path("salePriceMoney");
                Decimal price = getAmountFromMoneyNode(priceMoney);
                if (isTruthy(variant.path("onSale")) && (salePriceNode == null || price.compare(salePrice) < 0)) {
                    salePrice = price;
                    salePriceNode = priceMoney;
                }
            }
            return (salePriceNode == null) ? DEFAULT_MONEY_NODE : salePriceNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("salePriceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        case GIFT_CARD:
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Example 5 with Decimal

use of com.squarespace.cldrengine.api.Decimal in project template-compiler by Squarespace.

the class CommerceUtils method getHighestPriceAmongVariants.

public static JsonNode getHighestPriceAmongVariants(JsonNode item) {
    ProductType type = getProductType(item);
    JsonNode structuredContent = item.path("structuredContent");
    switch(type) {
        case PHYSICAL:
        case SERVICE:
        case GIFT_CARD:
            JsonNode variants = structuredContent.path("variants");
            if (variants.size() == 0) {
                return DEFAULT_MONEY_NODE;
            }
            JsonNode moneyNode = variants.get(0).path("priceMoney");
            Decimal price = getAmountFromMoneyNode(moneyNode);
            for (int i = 1; i < variants.size(); i++) {
                JsonNode currMoneyNode = variants.get(i).path("priceMoney");
                Decimal curr = getAmountFromMoneyNode(currMoneyNode);
                if (curr.compare(price) > 0) {
                    price = curr;
                    moneyNode = currMoneyNode;
                }
            }
            return moneyNode;
        case DIGITAL:
            JsonNode digitalMoneyNode = structuredContent.path("priceMoney");
            return digitalMoneyNode.isMissingNode() ? DEFAULT_MONEY_NODE : digitalMoneyNode;
        default:
            return DEFAULT_MONEY_NODE;
    }
}
Also used : Decimal(com.squarespace.cldrengine.api.Decimal) ProductType(com.squarespace.template.plugins.platform.enums.ProductType) JsonNode(com.fasterxml.jackson.databind.JsonNode)

Aggregations

Decimal (com.squarespace.cldrengine.api.Decimal)8 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 CLDR (com.squarespace.cldrengine.CLDR)3 ProductType (com.squarespace.template.plugins.platform.enums.ProductType)3 CurrencyFormatOptions (com.squarespace.cldrengine.api.CurrencyFormatOptions)2 CurrencyType (com.squarespace.cldrengine.api.CurrencyType)2 DecimalFormatOptions (com.squarespace.cldrengine.api.DecimalFormatOptions)2 Variable (com.squarespace.template.Variable)2 Test (org.testng.annotations.Test)1