/** * 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; }
/** * Subscribes a customer to all plans held by the given "plan subscription" item, adding all plan * item prices to a customer price map. * * @param userId user id of the customer to subscribe * @param itemId item representing the subscription to a plan * @return list of saved customer price entries, empty if no prices applied to customer. */ public static List<CustomerPriceDTO> subscribe(Integer userId, Integer itemId) { LOG.debug("Subscribing customer %s to plan subscription item %s", userId, itemId); List<CustomerPriceDTO> saved = new ArrayList<CustomerPriceDTO>(); CustomerPriceBL customerPriceBl = new CustomerPriceBL(userId); for (PlanDTO plan : new PlanBL().getPlansBySubscriptionItem(itemId)) saved.addAll(customerPriceBl.addPrices(plan.getPlanItems())); return saved; }
/** * 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!"); } }
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!"); } }
/** * Validates all pricing models within the plan to ensure that they have the correct attributes. * * @param plan plan to validate * @throws SessionInternalError if attributes are missing or of an incorrect type */ public static void validateAttributes(PlanDTO plan) throws SessionInternalError { List<String> errors = new ArrayList<String>(); for (PlanItemDTO planItem : plan.getPlanItems()) { for (PriceModelDTO model : planItem.getModels().values()) { for (PriceModelDTO next = model; next != null; next = next.getNext()) { try { AttributeUtils.validateAttributes(next.getAttributes(), next.getStrategy()); } catch (SessionInternalError e) { errors.addAll(Arrays.asList(e.getErrorMessages())); } } } } if (!errors.isEmpty()) { throw new SessionInternalError( "Plan pricing attributes failed validation.", errors.toArray(new String[errors.size()])); } }
private void updatePlan(PlanDTO plan, String action, String sessionId) throws PluggableTaskException { ItemDTO item = plan.getItem(); int itemId = item.getId(); String itemDescription = item.getDescription(item.getEntity().getLanguageId()); String xml = "<updateplans>" + " <plan action='" + action + "'>" + " <code>" + itemId + "</code>" + " <description>" + itemDescription + "</description>" + " </plan>" + "</updateplans>"; String json = "{" + " \"session\": \"" + sessionId + "\", " + " \"xmldata\": \"" + xml + "\" " + "}"; Map<String, Object> result = makeCall(json, "updatePlan"); String error = null; if (result.get("status") != null) { if (!result.get("status").toString().equals("OK")) { error = result.get("error").toString(); } } else { error = result.get("description").toString(); } if (error != null) { error = "Error calling SugarCRM: " + error; LOG.error(error); throw new PluggableTaskException(error); } }
/** * 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()); }
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!"); } }
/** * Convert a given PlanDTO into a PlanWS web-service object * * @param dto dto to convert * @return converted web-service object */ public static PlanWS getWS(PlanDTO dto) { if (dto != null) { return new PlanWS(dto, PlanItemBL.getWS(dto.getPlanItems())); } return null; }