Class PromotionResult

All Implemented Interfaces:
Serializable, Comparable
Direct Known Subclasses:
GeneratedCachedPromotionResult

public class PromotionResult extends GeneratedPromotionResult
PromotionResult.

The result of evaluating a promotion against a cart or order is a set of promotion results. Each promotion my produce zero or more results to express the results of its evaluation. A PromotionResult is associated with a single AbstractPromotion and is either fired (getFired()) or potentially could fire ( getCouldFire()).

A result that has fired as met all the requirements of the promotion. A result that potentially could fire has not met all the requirements of the promotion. When the result could fire the promotion also assigns a certainty ( GeneratedPromotionResult.getCertainty()) value to the result to indicate how close the result is to firing. This is a value in the range 0 to 1, where 1 indicates that the promotion has fired. This value can be used to rank potential promotion results.

A promotion result holds a number of PromotionOrderEntryConsumed instances to represent the entries in the order that have been consumed by the promotion in generating this result. If the promotion has fired then these consumed entries are not available to other promotions. If the promotion has not fired then these consumed entries are just an indication of the entries that will be consumed when the promotion can fire.

If the promotion has fired the promotion result also holds a number of AbstractPromotionAction instances to represent the actions that the promotion takes. These actions are either applied or not (isApplied()). When the promotions are evaluated by PromotionsManager.updatePromotions(SessionContext, Collection, de.hybris.platform.jalo.order.AbstractOrder, boolean, de.hybris.platform.promotions.jalo.PromotionsManager.AutoApplyMode, de.hybris.platform.promotions.jalo.PromotionsManager.AutoApplyMode, java.util.Date) a firing promotion result may be automatically applied depending on the parameters passed to the method. The actions of a specific promotion result can be applied by calling the apply() method.

The actions of a specific promotion result can be reversed by calling the undo() method.

See Also:
  • Constructor Details

    • PromotionResult

      public PromotionResult()
  • Method Details

    • remove

      public void remove(SessionContext ctx) throws ConsistencyCheckException
      Removes the item. When this item is removed the associated consumed order entries and actions will also be removed.
      Overrides:
      remove in class Item
      Parameters:
      ctx - A SessionContext object
      Throws:
      ConsistencyCheckException - if this item could not be removed for some reason
    • isApplied

      public final boolean isApplied()
      Returns true if the promotion fired and all of its actions have been applied to the order.
      Returns:
      Whether the promotion has been applied
    • isApplied

      public boolean isApplied(SessionContext ctx)
      Returns true if the promotion fired and all of its actions have been applied to the order. This method just checks that all the actions have been asked to apply themselves to the order.
      Parameters:
      ctx - The hybris session context
      Returns:
      Whether the promotion has been applied
    • isAppliedToOrder

      public boolean isAppliedToOrder(SessionContext ctx)
      Returns true if the promotion fired and all of its actions have been applied to the order. This method checks that all the actions are still applied to the order.
      Parameters:
      ctx - The hybris session context
      Returns:
      Whether the promotion has been applied
    • getFired

      public final boolean getFired()
      Returns true if the promotion fired and has produced a result.
      Returns:
      Whether the promotion fired
    • getFired

      public boolean getFired(SessionContext ctx)
      Returns true if the promotion fired and has produced a result.

      When the certainty is 1 the promotion has fired.

      Parameters:
      ctx - The hybris session context
      Returns:
      Whether the promotion fired
    • getCouldFire

      public final boolean getCouldFire()
      Is this a potential result. Returns true if the promotion believes it has a chance of firing, for instance if it requires 3 qualifying products but can only find 1.
      Returns:
      Whether the promotion could fire
    • getCouldFire

      public boolean getCouldFire(SessionContext ctx)
      Is this a potential result. Returns true if the promotion believes it has a chance of firing, for instance if it requires 3 qualifying products but can only find 1.

      When the certainty is less than 1 the promotion has not fired.

      Parameters:
      ctx - The hybris session context
      Returns:
      Whether the promotion could fire
    • getDescription

      public final String getDescription()
      Gets the description of this promotion result. This method uses the default locale (Locale.getDefault())
      Returns:
      A description of the promotion result
      See Also:
    • getDescription

      public final String getDescription(Locale locale)
      Gets the description of this promotion result. Gets the description for this promotion result. This description is based on the state of the result, the promotion that generated the result and the user supplied formatting strings.

      The Locale specified is used to format any numbers, dates or currencies for display to the user. It is important that this locale best represents the formatting options appropriate for display to the user. The default currency for the locale is ignored. The currency is always explicitly taken from the GeneratedAbstractOrder.getCurrency(). The currency is then formatted appropriately in the locale specified. For example, this does mean that values in the EURO currency will be formatted differently depending on the locale specified as each locale can specify currency specific formatting.

      The currency formatting is part of the Java VM configuration to support multiple locales. If the formatting is incorrect check your VM configuration for the locale and currency combination.

      Parameters:
      locale - The locale to use to format the messages. This locale must support currency formatting, i.e. this should be a region specific local. e.g de_DE, en_US, en_GB
      Returns:
      A description of the promotion result
    • getDescription

      public String getDescription(SessionContext ctx, Locale locale)
      Gets the description of this promotion result. Gets the description for this promotion result. This description is based on the state of the result, the promotion that generated the result and the user supplied formatting strings.

      The Locale specified is used to format any numbers, dates or currencies for display to the user. It is important that this locale best represents the formatting options appropriate for display to the user. The default currency for the locale is ignored. The currency is always explicitly taken from the GeneratedAbstractOrder.getCurrency(). The currency is then formatted appropriately in the locale specified. For example, this does mean that values in the EURO currency will be formatted differently depending on the locale specified as each locale can specify currency specific formatting.

      The currency formatting is part of the Java VM configuration to support multiple locales. If the formatting is incorrect check your VM configuration for the locale and currency combination.

      Parameters:
      ctx - The hybris session context
      locale - The locale to use to format the messages. This locale must support currency formatting, i.e. this must be a region specific local. e.g de_DE, en_US, en_GB
      Returns:
      A description of the promotion result
    • apply

      public final boolean apply()
      Applies all of the actions that this promotion generated to the order.
      Returns:
      true if calculateTotals() should be called to update the order totals.
    • apply

      public boolean apply(SessionContext ctx)
      Applies all of the actions that this promotion generated to the order.
      Parameters:
      ctx - The hybris session context
      Returns:
      true if calculateTotals() should be called to update the order totals.
    • undo

      public final boolean undo()
      Undoes all of the changes that this promotion made to the order.
      Returns:
      true if calculateTotals() should be called to update the order totals.
    • undo

      public boolean undo(SessionContext ctx)
      Undoes all of the changes that this promotion made to the order.
      Parameters:
      ctx - The hybris session context
      Returns:
      true if calculateTotals() should be called to update the order totals.
    • getConsumedCount

      public final long getConsumedCount(boolean includeCouldFirePromotions)
      Gets the total number of items consumed by this promotion.
      Parameters:
      includeCouldFirePromotions - include could fire promotions
      Returns:
      The total number of items consumed
    • getConsumedCount

      public long getConsumedCount(SessionContext ctx, boolean includeCouldFirePromotions)
      Gets the total number of items consumed by this promotion.
      Parameters:
      ctx - The hybris session context
      includeCouldFirePromotions - include could fire promotions
      Returns:
      The total number of items consumed
    • getTotalDiscount

      public final double getTotalDiscount()
      Gets the total value of all discounts in this result. This result will be the same regardless of the applied state of this result, i.e. if not applied this is the discount value that would be applied, if it is applied then it is the value of the discount.
      Returns:
      The double value for the total discount value
    • getTotalDiscount

      public double getTotalDiscount(SessionContext ctx)
      Gets the total value of all discounts in this result. This result will be the same regardless of the applied state of this result, i.e. if not applied this is the discount value that would be applied, if it is applied then it is the value of the discount.
      Parameters:
      ctx - The session context
      Returns:
      The double value for the total discount value
    • isValid

      protected boolean isValid(SessionContext ctx)
      Is this promotion result valid. This method checks to ensure that the promotion exists.
      Parameters:
      ctx - The hybris context
      Returns:
      true if the promotion result is valid
    • addAction

      public void addAction(SessionContext ctx, AbstractPromotionAction action)
      Adds an action to the promotion results.
      Parameters:
      ctx - The hybris context
      action - The action to add
    • addConsumedEntry

      public void addConsumedEntry(SessionContext ctx, PromotionOrderEntryConsumed poec)
      Adds a consumed entry to the current result.
      Parameters:
      ctx - the hybris context
      poec - the entry to add
    • removeConsumedEntry

      public void removeConsumedEntry(SessionContext ctx, PromotionOrderEntryConsumed poec)
      Removes a consumed entry from the current result.
      Parameters:
      ctx - the hybris context
      poec - the entry to remove
    • transferToOrder

      public PromotionResult transferToOrder(SessionContext ctx, Order target)
      Creates a deep clone of this promotion result and attach it to the specified order.
      Parameters:
      ctx - The hybris context
      target - the target order to transfer to
      Returns:
      the cloned promotion result
    • deepCloneAllActions

      protected Collection<AbstractPromotionAction> deepCloneAllActions(SessionContext ctx)
      Clones all the actions attached to this promotion result.
      Parameters:
      ctx - the hybris context
      Returns:
      the cloned actions
    • deepCloneConsumedEntriesAndAttachToOrder

      protected Collection<PromotionOrderEntryConsumed> deepCloneConsumedEntriesAndAttachToOrder(SessionContext ctx, Order target)
      Clones of the consumed order entries attached to this promotion result and attach them to order entries in the specified order.
      Parameters:
      ctx - the hybris context
      target - the target order to attach the consumed order entries to
      Returns:
      the cloned order entries
    • deepCloneConsumedEntryAndAttachToOrder

      protected static PromotionOrderEntryConsumed deepCloneConsumedEntryAndAttachToOrder(SessionContext ctx, PromotionOrderEntryConsumed source, List<AbstractOrderEntry> allTargetEntries)
      Clones a consumed order entry and attach it to an order entry from the list specified.
      Parameters:
      ctx - the hybris context
      source - the consumed entry to clone
      allTargetEntries - the list of order entries in the target order
      Returns:
      the cloned consumed order entry
    • findOrderEntryWithEntryNumber

      protected static AbstractOrderEntry findOrderEntryWithEntryNumber(List<AbstractOrderEntry> allTargetEntries, int entryNumber)
      Finds the order entry with the specified entry number.
      Parameters:
      allTargetEntries - the list of order entries
      entryNumber - the entry number to search for
      Returns:
      the order entry with the entry number specified or null of not found.
    • getConsumedEntries

      public Collection getConsumedEntries(SessionContext ctx)
      Gets the collection of PromotionOrderEntryConsumed consumed.
      Specified by:
      getConsumedEntries in class GeneratedPromotionResult
      Parameters:
      ctx - The hybris session context
      Returns:
      A collection of PromotionOrderEntryConsumed consumed by this result.
    • setConsumedEntries

      public void setConsumedEntries(SessionContext ctx, Collection promotionOrderEntryConsumeds)
      Sets the collection of PromotionOrderEntryConsumed consumed.

      The PromotionOrderEntryConsumed instances associated with this promotion result are owned (and part of) this result. They cannot belong to another instance, therefore when setting the collection any PromotionOrderEntryConsumed instances previously associated with this result, that are no longer associated are deleted from the database.

      Specified by:
      setConsumedEntries in class GeneratedPromotionResult
      Parameters:
      ctx - The hybris session context
      promotionOrderEntryConsumeds - the collection of consumed entries
    • getActions

      public Collection getActions(SessionContext ctx)
      Gets the collection of AbstractPromotionAction instances.
      Specified by:
      getActions in class GeneratedPromotionResult
      Parameters:
      ctx - The hybris session context
      Returns:
      A collection of AbstractPromotionAction instances attached to this result.
    • setActions

      public void setActions(SessionContext ctx, Collection actions)
      Sets the collection of AbstractPromotionAction instance.

      The AbstractPromotionAction instances associated with this promotion result are owned (and part of) this result. They cannot belong to another instance, therefore when setting the collection any AbstractPromotionAction instances previously associated with this result, that are no longer associated are deleted from the database.

      Specified by:
      setActions in class GeneratedPromotionResult
      Parameters:
      ctx - The hybris session context
      actions - the collection of actions
    • getDataUnigueKey

      protected String getDataUnigueKey(SessionContext ctx)
      Generates a string identifier that can be used to establish if 2 PromotionResults are the same. The identifier should be based on the data for the PromotionResult, e.g. the promotion that created it, the number and type of products consumed, the actions created.
      Parameters:
      ctx - The hybris context
      Returns:
      a string that identifies this promotion result