Class Helper


  • public class Helper
    extends java.lang.Object
    Static helper methods for the promotions extension.
    Version:
    v1
    • Constructor Detail

      • Helper

        public Helper()
    • Method Detail

      • buildDateForYear

        public static java.util.Date buildDateForYear​(int year)
        Create a Date for the first day of the specified year.
        Parameters:
        year - the year
        Returns:
        a date that represents the first day of the year
      • formatCurrencyAmount

        public static java.lang.String formatCurrencyAmount​(SessionContext ctx,
                                                            java.util.Locale locale,
                                                            Currency currency,
                                                            double amount)
        Format an amount in a currency for a locale.
        Parameters:
        ctx - the hybris context
        locale - the java locale that the amount should be rendered in
        currency - the hybris currency for the amount
        amount - the value
        Returns:
        a formatted string
      • adjustDigits

        protected static java.text.DecimalFormat adjustDigits​(java.text.DecimalFormat format,
                                                              Currency currency)
        Adjusts DecimalFormat's fraction digits according to given Currency.
      • adjustSymbol

        protected static java.text.DecimalFormat adjustSymbol​(java.text.DecimalFormat format,
                                                              Currency currency)
        Adjusts DecimalFormat's symbol according to given Currency.
      • getSmallestCurrencyUnit

        protected static java.math.BigDecimal getSmallestCurrencyUnit​(SessionContext ctx,
                                                                      Currency currency)
        Get the smallest value that can be represented as a whole unit in the specified currency.
        Parameters:
        ctx - the hybris context
        currency - the hybris currency
        Returns:
        A BigDecimal that represents the smallest unit in the currency
      • roundCurrencyValue

        public static java.math.BigDecimal roundCurrencyValue​(SessionContext ctx,
                                                              Currency currency,
                                                              java.math.BigDecimal amount)
        Round the specified amount to the nearest whole unit in the specified currency.
        Parameters:
        ctx - the hybris context
        currency - the hybris currency
        amount - the amount
        Returns:
        A BigDecimal that represents the amount rounded to the precision of the currency
      • roundCurrencyValue

        public static java.math.BigDecimal roundCurrencyValue​(SessionContext ctx,
                                                              Currency currency,
                                                              double amount)
        Round the specified amount to the nearest whole unit in the specified currency.
        Parameters:
        ctx - the hybris context
        currency - the hybris currency
        amount - the amount
        Returns:
        A BigDecimal that represents the amount rounded to the precision of the currency
      • adjustUnitPrices

        public static void adjustUnitPrices​(SessionContext ctx,
                                            PromotionEvaluationContext promoContext,
                                            java.util.List<PromotionOrderEntryConsumed> consumedEntries,
                                            double targetTotal,
                                            double originalTotal)
        Adjust the unit prices of the PromotionOrderEntryConsumed items specified to match the target total

        Applies unit price adjustments to the PromotionOrderEntryConsumed items to take the total value of the items from the originalTotal to the targetTotal specified. If necessary this method will split PromotionOrderEntryConsumed items to allow it to match the targetTotal value exactly.

        Parameters:
        ctx - the hybris context
        promoContext - the promotion evaluation context
        consumedEntries - the list of consumed entries
        targetTotal - the target total value for the consumed entries
        originalTotal - the current total value for the consumed entries
      • calculateOrderEntryAdjustedTotal

        protected static java.math.BigDecimal calculateOrderEntryAdjustedTotal​(SessionContext ctx,
                                                                               Currency currency,
                                                                               java.util.List<PromotionOrderEntryConsumed> entries)
        Helper method to calculate the sum of the AdjustedEntryPrice for each of the PromotionOrderEntryConsumed items.
        Parameters:
        ctx - the hybris context
        currency - the hybris currency
        entries - the list of PromotionOrderEntryConsumed items to sum
        Returns:
        A BigDecimal that represents the rounded sum of the adjusted entry prices
      • getBaseProduct

        public static Product getBaseProduct​(SessionContext ctx,
                                             Product product)
        Return the base product for the product specified. Returns the current product if the product is not an instance of VariantProduct and the result of GeneratedVariantProduct.getBaseProduct() if it is.
        Parameters:
        ctx - The hybris context
        product - The product to get the base product for
        Returns:
        the base product for the product specified
      • getBaseProducts

        public static java.util.List<Product> getBaseProducts​(SessionContext ctx,
                                                              Product product)
        This is a recursive variant of the method above.
      • getBaseProducts

        protected static void getBaseProducts​(SessionContext ctx,
                                              Product product,
                                              java.util.List<Product> result)
        Internal recursive method to get all base products of a given product.

        Parameters:
        ctx -
        product -
        result -
      • findGlobalDiscountValue

        public static DiscountValue findGlobalDiscountValue​(SessionContext ctx,
                                                            AbstractOrder order,
                                                            java.lang.String discountValueCode)
        Find a global discount value with specified Code
        Parameters:
        ctx - the hybris context
        order - the order to lookup the discounts on
        discountValueCode - the code for the discount to find
        Returns:
        the discount value found
      • dumpOrder

        public static java.lang.String dumpOrder​(SessionContext ctx,
                                                 AbstractOrder order)
        Method to dump an order and its entries into a string.
        Parameters:
        ctx - the hybris context
        order - the order to dump
        Returns:
        the string dump of the order
      • join

        public static java.lang.String join​(java.util.Collection items)
        Join collection of objects together to form a delimited string
        Parameters:
        items - the collection of items to join together
        Returns:
        a joined string
      • join

        public static java.lang.String join​(java.util.Collection items,
                                            java.lang.String delimiter)
        Join collection of objects together to form a delimited string
        Parameters:
        items - the collection of items to join together
        delimiter - the delimiter to place between the items
        Returns:
        a joined string
      • getDateNowRoundedToMinute

        public static java.util.Date getDateNowRoundedToMinute()
        returns a Date of the current minute to assure that queries can be cached for up to 1 minute.
        Returns:
        the truncated Date