}
}
public static void addSubscriptionInfo(SubscriptionInfoBean subscriptionInfoBean,
UserRegistry userRegistry) throws Exception {
BillingEngine billingEngine = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID);
Customer customer = getCurrentCustomer(userRegistry);
// if customer doesn't exist, we are making a one
if (customer == null) {
int currentTenantId = userRegistry.getTenantId();
TenantManager tenantManger = getRealmService().getTenantManager();
Tenant currentTenant = (Tenant) tenantManger.getTenant(currentTenantId);
if (currentTenant == null || currentTenant.getDomain() == null) {
String msg = "Error in getting the customer information.";
throw new Exception(msg);
}
customer = new Customer();
customer.setName(currentTenant.getDomain());
customer.setEmail(currentTenant.getEmail());
customer.setStartedDate(new Date());
billingEngine.addCustomer(customer);
}
String itemName = subscriptionInfoBean.getPackageName();
if (itemName.toLowerCase().contains("free")) {
return; //nothing to upgrade in a free package
}
List<Item> items = billingManager.getBillingEngine(StratosConstants.MULTITENANCY_SCHEDULED_TASK_ID).
getItemsWithName(itemName);
if (items == null || items.size() == 0) {
String msg = "Invalid item name: " + itemName + ".";
throw new Exception(msg);
}
Item item = items.get(0);
// adding the subscription
Subscription subscription = new Subscription();
subscription.setItem(item);
subscription.setCustomer(customer);
subscription.setActive(true);
subscription.setActiveSince(subscriptionInfoBean.getActiveSince());
subscription.setActiveUntil(subscriptionInfoBean.getActiveUntil());
billingEngine.addSubscription(subscription);
}