Package org.wso2.carbon.billing.core.jdbc

Examples of org.wso2.carbon.billing.core.jdbc.DataAccessObject


        }
    }

    private void addInvoiceSubscriptionItem(Item item,
                                            int invoiceSubscriptionId) throws BillingException {
        DataAccessObject dataAccessObject = BillingManager.getInstance().getDataAccessObject();
        dataAccessObject.addInvoiceSubscriptionItem(item, invoiceSubscriptionId);
        // and iterate through all the item children
        if (item.getChildren() != null) {
            for (Item subItem : item.getChildren()) {
                addInvoiceSubscriptionItem(subItem, invoiceSubscriptionId);
            }
View Full Code Here


        handlerContext.setSubscriptions(subscriptions);
    }

    private List<Subscription> getFilteredActiveSubscriptions(String filter,
                                                Customer customer) throws BillingException {
        DataAccessObject dataAccessObject = BillingManager.getInstance().getDataAccessObject();
        List<Subscription> subscriptions = null;
        boolean succeeded = false;
        try {
            dataAccessObject.beginTransaction();
            if (customer == null) {
                subscriptions = dataAccessObject.getFilteredActiveSubscriptions(filter);
            } else {
                subscriptions = dataAccessObject.getFilteredActiveSubscriptionsForCustomer(filter, customer);
            }

            // iterate through all the subscriptions and assign correct customer and item
            for (Subscription subscription : subscriptions) {
                Customer dummyCustomer = subscription.getCustomer();
                Customer correctCustomer = getCustomer(dummyCustomer.getId());
                subscription.setCustomer(correctCustomer);

                Item dummyItem = subscription.getItem();
                Item correctItem = getItem(dummyItem.getId());
                subscription.setItem(correctItem);
            }
            succeeded = true;
        } finally {
            if (succeeded) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscriptions;
    }
View Full Code Here

        }
        return subscriptions;
    }

    private Item getItem(int itemId) throws BillingException {
        DataAccessObject dataAccessObject = BillingManager.getInstance().getDataAccessObject();
        Item item = null;
        boolean succeeded = false;
        try {
            dataAccessObject.beginTransaction();
            item = dataAccessObject.getItem(itemId);
            succeeded = true;
        } finally {
            if (succeeded) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return item;
    }
View Full Code Here

    public void execute(BillingEngineContext handlerContext) throws BillingException {
        buildSubscriptionTree(handlerContext);
    }

    private void buildSubscriptionTree(BillingEngineContext handlerContext) throws BillingException {
        DataAccessObject dataAccessObject = BillingManager.getInstance().getDataAccessObject();

        // get the subscription from handler context
        List<Subscription> subscriptions = handlerContext.getSubscriptions();

        Map<Customer, List<Subscription>> customersSubscriptions =
                new HashMap<Customer, List<Subscription>>();
        for (Subscription subscription : subscriptions) {
            Customer customer = subscription.getCustomer();
            List<Subscription> customerSubscriptions = customersSubscriptions.get(customer);
            if (customerSubscriptions == null) {
                customerSubscriptions = new ArrayList<Subscription>();
            }
            customerSubscriptions.add(subscription);
            customersSubscriptions.put(customer, customerSubscriptions);
        }

        // so iterating all the customers
        for (Map.Entry<Customer, List<Subscription>> entry : customersSubscriptions.entrySet()) {
            Customer customer = entry.getKey();
            List<Subscription> customerSubscriptions = entry.getValue();

            // create an empty invoice
            Invoice invoice = new Invoice();

            // get the last invoice for the customer
            Invoice lastInvoice = dataAccessObject.getLastInvoice(customer);

            if (lastInvoice != null) {
                invoice.setBoughtForward(lastInvoice.getCarriedForward());
                long lastInvoiceEnd = lastInvoice.getEndDate().getTime();
                long currentInvoiceStart = lastInvoiceEnd + 1;
                invoice.setStartDate(new Date(currentInvoiceStart));
            } else {
                invoice.setBoughtForward(new Cash("$0"));
                // the earliest of the subscriptions
                long earliestSubscriptionStart = -1;
                for (Subscription subscription : customerSubscriptions) {
                    long subscriptionStartDate = subscription.getActiveSince().getTime();
                    if (earliestSubscriptionStart == -1 ||
                        subscriptionStartDate < earliestSubscriptionStart) {
                        earliestSubscriptionStart = subscriptionStartDate;
                    }
                }
                invoice.setStartDate(new Date(earliestSubscriptionStart));
            }
           
            Date currentDate = new Date();
            SchedulerContext schedulerContext = handlerContext.getSchedulerContext();
            if (schedulerContext != null && schedulerContext.getCurrentDurationEnd() != -1) {
                invoice.setEndDate(new Date(schedulerContext.getCurrentDurationEnd()));
            } else {
                invoice.setEndDate(currentDate);
            }
            // this is the date the billing is initialized, this can be probably overwritten
            invoice.setDate(currentDate);

            // and we will count for the subscriptions for the invoice
            invoice.setSubscriptions(customerSubscriptions);

            // and then we will count on the un-billed purchases of all the subscriptions of the
            // customer
            Map<Integer, Payment> purchaseOrders = new HashMap<Integer, Payment>();
            for (Subscription subscription : customerSubscriptions) {
                dataAccessObject.fillUnbilledPayments(subscription, purchaseOrders, invoice );
            }
            for (Payment payment : purchaseOrders.values()) {
                invoice.addPayment(payment);
            }
            customer.setActiveInvoice(invoice);
View Full Code Here

public class TestUtils {

    private static final String RULE_COMPONENT_CONF = "rule-component.conf";

    public static void deleteAllTables() throws BillingException {
        DataAccessObject dataAccessObject = BillingManager.getInstance().getDataAccessObject();
        boolean succeeded = false;
        try {
            dataAccessObject.beginTransaction();
            /*dataAccessObject.deleteInvoiceSubscriptionItems();
            dataAccessObject.deleteInvoiceSubscriptions();
            dataAccessObject.deletePaymentSubscriptions();
            dataAccessObject.deletePayments();
            dataAccessObject.deleteInvoice();
            dataAccessObject.deleteSubscribers();
            dataAccessObject.deleteItems();
            dataAccessObject.deleteCustomers();
            */
            succeeded = true;
        } finally {
            if (succeeded) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
    }
View Full Code Here

    private Map<Integer, MultitenancyPackage> multitenancyPackagesMap =
            new HashMap<Integer, MultitenancyPackage>();

    public void init(Map<String, String> handlerConfig) throws BillingException {
        DataAccessObject dataAccessObject = Util.getBillingManager().getDataAccessObject();
        MultitenancyBillingInfo billingInfo = Util.getMultitenancyBillingInfo();

        // here we are initializing the packages
        List<MultitenancyPackage> multitenancyPackages = billingInfo.getMultitenancyPackages();
        boolean succeeded = false;
        try {
            dataAccessObject.beginTransaction();
            for (MultitenancyPackage multitenancyPackage : multitenancyPackages) {
                // check the package existence in the database; If not available, insert it
                int itemId = dataAccessObject.getItemIdWithName(multitenancyPackage.getName());
                if (itemId == DataAccessObject.INVALID) {
                    itemId = dataAccessObject.addItem(multitenancyPackage);
                }
                multitenancyPackage.setId(itemId);
                multitenancyPackagesMap.put(itemId, multitenancyPackage);
                // and add all the sub items too
                for (Item subItem : multitenancyPackage.getChildren()) {
                    int subItemId = dataAccessObject.getItemId(subItem.getName(), itemId);
                    if (subItemId == DataAccessObject.INVALID) {
                        subItemId = dataAccessObject.addItem(subItem);
                    }
                    subItem.setId(subItemId);
                }
            }
            succeeded = true;
        }catch (Exception e){
            log.error(e.getMessage());
            throw new BillingException(e.getMessage(), e);
        }finally {
            if (succeeded) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
    }
View Full Code Here

        }
    }

    private List<Subscription> getSubscriptions(String filter,
                                                Customer customer)throws BillingException {
        DataAccessObject dataAccessObject = Util.getBillingManager().getDataAccessObject();
        List<Subscription> subscriptions = null;
        boolean succeeded = false;
        Map<Integer, Customer> customersCash = new HashMap<Integer, Customer>();
        try {
            dataAccessObject.beginTransaction();
            if (customer == null) {
                subscriptions = dataAccessObject.getFilteredActiveSubscriptions(filter);
            } else {
                subscriptions = dataAccessObject.getFilteredActiveSubscriptionsForCustomer(filter, customer);
            }
            if(subscriptions!=null && subscriptions.size()>0){
                for (Subscription subscription : subscriptions) {
                    Customer dummyCustomer = subscription.getCustomer();
                    int customerId = dummyCustomer.getId();
                    Customer correctCustomer = customersCash.get(customerId);
                    if (correctCustomer == null) {
                        correctCustomer = getCustomer(customerId);
                        customersCash.put(customerId, correctCustomer);
                    }
                    subscription.setCustomer(correctCustomer);

                    Item dummyItem = subscription.getItem();
                    Item correctItem = getItem(dummyItem.getId());
                    subscription.setItem(correctItem);
                }
                //succeeded = true; //i think it is wrong to place this boolean here. If there are no subscriptions
                                    //this makes the transaction to rollback and hence failing to show the invoice
            }
            succeeded = true;
        }catch (Exception e){
            String msg = "Error occurred while getting subscription: " + filter;
            if(customer != null) {
                msg = msg + " for customer: " + customer.getName();
            }
            log.error(msg);
            throw new BillingException(msg, e);
        }finally {
            if (succeeded) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscriptions;
    }
View Full Code Here

    private static Log log = LogFactory.getLog(DataAccessManager.class);
    private DataAccessObject dataAccessObject = null;

    public DataAccessManager(DataSource dataSource){
        this.dataAccessObject = new DataAccessObject(dataSource);
    }
View Full Code Here

     * @return The end line number, or -1 if could not be determined
     */
    public int getEndLineNumber() {
      int ret=-1;
     
      ActivityInterface parent=getParent();
     
      if (parent != null) {
        int index=parent.getSubActivities().indexOf(this);
       
        if (index != -1) {
          if (index < (parent.getSubActivities().size()-1)) {
            ActivityInterface other=parent.getSubActivities().get(index+1);
           
            ret = other.getStartLineNumber()-1;
          } else {
            ret = parent.getEndLineNumber();
          }
        }
      }
View Full Code Here

     *
     * @param lineNumber The line number
     * @return The activity, or null if not found
     */
    public ActivityInterface getActivityAtLineNumber(int lineNumber) {
      ActivityInterface ret=null;
     
      int endline=getEndLineNumber();
     
      if (getStartLineNumber() <= lineNumber && (endline == -1 || endline >= lineNumber)) {
       
View Full Code Here

TOP

Related Classes of org.wso2.carbon.billing.core.jdbc.DataAccessObject

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.