/**
   * Refreshes the customer plan item price mappings for all customers that have subscribed to this
   * plan. This method will remove all existing prices for the plan and insert the current list of
   * plan items into the customer price map.
   */
  public void refreshCustomerPrices() {
    if (plan != null) {
      LOG.debug("Refreshing customer prices for subscribers to plan %s", plan.getId());

      for (CustomerDTO customer : getCustomersByPlan(plan.getId())) {
        CustomerPriceBL bl = new CustomerPriceBL(customer);
        bl.removePrices(plan.getId());
        bl.addPrices(plan.getPlanItems());
      }
    } else {
      LOG.error("Cannot update customer prices, PlanDTO not found or not set!");
    }
  }
  /**
   * Un-subscribes a customer from all plans held by the given "plan subscription" item, removing
   * all plan item prices from the customer price map.
   *
   * @param userId user id of the customer to un-subscribe
   * @param itemId item representing the subscription to a plan
   */
  public static void unsubscribe(Integer userId, Integer itemId) {
    LOG.debug("Un-subscribing customer %s from plan subscription item %s", userId, itemId);

    CustomerPriceBL customerPriceBl = new CustomerPriceBL(userId);
    for (PlanDTO plan : new PlanBL().getPlansBySubscriptionItem(itemId))
      customerPriceBl.removePrices(plan.getId());
  }
 /**
  * Removes all customer prices for the plan's current set of plan items. This will remove prices
  * for subscribed customers AND orphaned prices where the customers order has been deleted in a
  * non-standard way (DB delete, non API usage).
  */
 public void purgeCustomerPrices() {
   if (plan != null) {
     LOG.debug("Removing ALL remaining customer prices for plan %s", plan.getId());
     new CustomerPriceBL().removeAllPrices(plan.getPlanItems());
   } else {
     LOG.error("Cannot purge customer prices, PlanDTO not found or not set!");
   }
 }
  /**
   * Returns true if the customer is subscribed to a plan held by the given "plan subscription"
   * item.
   *
   * @param userId user id of the customer to check
   * @param itemId plan subscription item id
   * @return true if customer is subscribed, false if not
   */
  public static boolean isSubscribed(Integer userId, Integer itemId) {
    // items can have multiple plans, but it's possible that a customer may only
    // be subscribed to 1 of the plans depending on where we are in the workflow
    for (PlanDTO plan : new PlanBL().getPlansBySubscriptionItem(itemId))
      if (new PlanDAS().isSubscribed(userId, plan.getId()))
        return true; // only return true if subscribed to one of the plans, otherwise keep checking.

    return false;
  }
  /**
   * Subscribes a customer to this plan, adding all plan item prices to the customer price map.
   *
   * @param userId user id of the customer to subscribe
   * @return list of saved customer price entries, empty if no prices applied to customer.
   */
  public List<CustomerPriceDTO> subscribe(Integer userId) {
    LOG.debug("Subscribing customer %s to plan %s", userId, plan.getId());

    List<CustomerPriceDTO> saved = new ArrayList<CustomerPriceDTO>();

    CustomerPriceBL customerPriceBl = new CustomerPriceBL(userId);
    saved.addAll(customerPriceBl.addPrices(plan.getPlanItems()));

    return saved;
  }
  public void update(PlanDTO dto) {
    if (plan != null) {

      // un-subscribe existing customers before updating
      List<CustomerDTO> subscribers = getCustomersByPlan(plan.getId());
      for (CustomerDTO customer : subscribers) {
        unsubscribe(customer.getBaseUser().getUserId());
      }

      // clean all remaining prices just-in-case there's an orphaned record
      if (plan.getPlanItems().size() > 0) {
        purgeCustomerPrices();
      }

      // do update
      validateAttributes(dto);

      plan.setDescription(dto.getDescription());
      plan.setItem(dto.getItem());
      plan.setPeriod(dto.getPeriod());

      plan.getPlanItems().clear();
      plan.getPlanItems().addAll(dto.getPlanItems());

      LOG.debug("Saving updates to plan %s", plan.getId());
      this.plan = planDas.save(plan);

      // re-subscribe customers after plan has been saved
      for (CustomerDTO customer : subscribers) {
        subscribe(customer.getBaseUser().getUserId());
      }

      // trigger internal event
      EventManager.process(new PlanUpdatedEvent(plan));

    } else {
      LOG.error("Cannot update, PlanDTO not found or not set!");
    }
  }
  public void addPrice(PlanItemDTO planItem) {
    if (plan != null) {
      PriceModelBL.validateAttributes(planItem.getModels().values());

      plan.addPlanItem(planItem);

      LOG.debug("Saving updates to plan %s", plan.getId());
      this.plan = planDas.save(plan);

      refreshCustomerPrices();

      // trigger internal event
      EventManager.process(new PlanUpdatedEvent(plan));

    } else {
      LOG.error("Cannot add price, PlanDTO not found or not set!");
    }
  }
 /**
  * Returns true if the customer is subscribed to this plan.
  *
  * @param userId user id of the customer to check
  * @return true if customer is subscribed, false if not
  */
 public boolean isSubscribed(Integer userId) {
   return planDas.isSubscribed(userId, plan.getId());
 }
 /**
  * Un-subscribes a customer from this plan, removing all plan item prices from the customer price
  * map.
  *
  * @param userId user id of the customer to un-subscribe
  */
 public void unsubscribe(Integer userId) {
   LOG.debug("Un-subscribing customer %s from plan %s", userId, plan.getId());
   new CustomerPriceBL(userId).removePrices(plan.getId());
 }