Package org.wso2.carbon.billing.core.handlers

Source Code of org.wso2.carbon.billing.core.handlers.DefaultSubscriptionFeedingHandler

/*
* Copyright (c) 2008, WSO2 Inc. (http://www.wso2.org) All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
*      http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.wso2.carbon.billing.core.handlers;

import org.wso2.carbon.billing.core.BillingEngineContext;
import org.wso2.carbon.billing.core.BillingException;
import org.wso2.carbon.billing.core.BillingHandler;
import org.wso2.carbon.billing.core.BillingManager;
import org.wso2.carbon.billing.core.dataobjects.Customer;
import org.wso2.carbon.billing.core.dataobjects.Item;
import org.wso2.carbon.billing.core.dataobjects.Subscription;
import org.wso2.carbon.billing.core.jdbc.DataAccessObject;
import org.wso2.carbon.billing.core.utilities.CustomerUtils;

import java.util.List;
import java.util.Map;

public class DefaultSubscriptionFeedingHandler implements BillingHandler {
    public void init(Map<String, String> handlerConfig) throws BillingException {
        // nothing to initialize
    }

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

    private void feedSubscriptions(BillingEngineContext handlerContext) throws BillingException {
        // get the subscriptions right here..
        String filter = handlerContext.getTaskConfiguration().getSubscriptionFilter();
        Customer customer = handlerContext.getCustomer();
        List<Subscription> subscriptions = getFilteredActiveSubscriptions(filter, customer);
        // prepare the handler context
        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;
    }

    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;
    }

    private Customer getCustomer(int customerId) throws BillingException {
        Customer customer = new Customer();
        CustomerUtils.fillCustomerData(customerId, customer);
        return customer;
    }
}
TOP

Related Classes of org.wso2.carbon.billing.core.handlers.DefaultSubscriptionFeedingHandler

TOP
Copyright © 2015 www.massapi.com. 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.