Class SubscriptionUtils

java.lang.Object
com.broadleafcommerce.subscription.service.SubscriptionUtils

public class SubscriptionUtils extends Object
Utility methods related to subscription handling.
  • Field Details

  • Constructor Details

    • SubscriptionUtils

      public SubscriptionUtils()
  • Method Details

    • setupTenantOnlyRead

      public static void setupTenantOnlyRead(boolean isSandboxAware, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Prepare the context for optional sandbox handling for a read operation. This only facilitates tenant discrimination with optional sandbox support. To indicate application discrimination, use setupRead(Supplier, boolean, boolean, ContextInfo).
      Parameters:
      isSandboxAware - Whether sandboxing should be enabled for the scope of the operation
      context - The context to prepare with sandbox information
    • setupRead

      public static <T> T setupRead(Supplier<T> doWith, boolean isSandboxAware, boolean isApplicationAware, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Prepare the context for optional sandbox handling for a read operation.
      Parameters:
      doWith - The operation to perform within the possible scope of trackable behavior alterations. See TrackableBehaviorContext.doWith(java.lang.Runnable, com.broadleafcommerce.data.tracking.core.TrackableBehaviorOverride...).
      isSandboxAware - Whether sandboxing should be enabled for the scope of the operation
      isApplicationAware - Whether the operation should be aware of application context for discrimination
      context - The context to prepare with sandbox information
    • setupWrite

      public static void setupWrite(boolean isSandboxAware, com.broadleafcommerce.data.tracking.core.context.ContextInfo context)
      Prepare the context for optional sandbox handling for a write operation.
      Parameters:
      isSandboxAware - Whether sandboxing should be enabled for the scope of the operation
      context - The context to prepare with sandbox information
    • getBeginningOfNextDay

      public static Instant getBeginningOfNextDay(@NonNull @NonNull Instant startDate, @Nullable String timezone)
      Gets the beginning of the next day from the given Instant.
      Parameters:
      startDate - the start date
      Returns:
      the beginning of the next day
    • getBeginningOfNextDay

      public static Date getBeginningOfNextDay(@NonNull @NonNull Date startDate, @Nullable String timezone)
      Gets the beginning of the next day from the given Instant.
      Parameters:
      startDate - the start date
      Returns:
      the beginning of the next day
    • getNumberOfPeriodsLeftInTerm

      public static Optional<Long> getNumberOfPeriodsLeftInTerm(String periodType, int periodFrequency, @Nullable Instant termStartDate, @Nullable Instant termEndDate, @Nullable String customerTimezone)
      Determines the number of billing periods left after the current that are outstanding. It is assumed that the term length is a multiple of the period length in months.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      termStartDate - The start of the subscription's term.
      termEndDate - The end of the subscription's term.
      customerTimezone - The customer's timezone ID
      Returns:
      The number billings left after the current that are unbilled.
    • getZoneId

      public static ZoneId getZoneId(@Nullable String customerTimezone)
      Returns the ZoneId for the given customer timezone string. If the timezone is null, it defaults to ZoneOffset.UTC.
      Parameters:
      customerTimezone - the customer's timezone ID
      Returns:
      the resolved ZoneId
      Throws:
      IllegalArgumentException - if the provided timezone ID is invalid
    • getUnitsInPeriodStartingOn

      protected static long getUnitsInPeriodStartingOn(String periodType, int periodFrequency, @NonNull @NonNull ChronoUnit units, @Nullable Instant periodStartDate, @Nullable String customerTimezone)
      Determines the number of the specified ChronoUnits in a billing period.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      units - The ChronoUnits for which to return the number per period.
      periodStartDate - The start date of the period for which the number of days is needed.
      customerTimezone - The customer's timezone ID
      Returns:
      The number of days in a billing period.
      See Also:
    • getUnitsInCustomPeriodType

      protected static int getUnitsInCustomPeriodType(String periodType, int periodFrequency, @NonNull @NonNull ChronoUnit units, @Nullable Instant periodStartDate, @Nullable String customerTimezone)
      Hook point for handling custom DefaultSubscriptionPeriodType values.
      Parameters:
      periodType - The type of billing period, e.g., DefaultSubscriptionPeriodType.
      periodFrequency - The frequency for billing, e.g., 3 for every 3 months.
      units - The ChronoUnits for which to return the number per period.
      periodStartDate - The start date of the period for which the number of days is needed.
      customerTimezone - The customer's timezone ID
      Returns:
      The number of days in a billing period.
      See Also:
    • getMonthsUntilEndOfTerm

      protected static long getMonthsUntilEndOfTerm(@NonNull @NonNull Instant termStartDate, @NonNull @NonNull Instant termEndDate, @Nullable String customerTimezone)
      Determines the number of months in a subscription's terms.
      Parameters:
      termStartDate - The start of the subscription's term.
      termEndDate - The end of the subscription's term.
      customerTimezone - The customer's timezone ID
      Returns:
      The number of months in a subscription's terms.
    • getCurrentPeriod

      protected static long getCurrentPeriod(@NonNull @NonNull Instant termStartDate, long monthsInPeriod, @Nullable String customerTimezone)
      Determines which billing period the subscription is currently in given the Subscription.getStartOfTermDate(). This will determine the number of months between now and the term's start and the number of months in a period to make the determination.
      Parameters:
      termStartDate - Date the subscription's term began.
      monthsInPeriod - Number of days in a period.
      customerTimezone - The customer's timezone ID
      Returns:
      The billing period the subscription is currently in