Package org.wso2.carbon.billing.core

Source Code of org.wso2.carbon.billing.core.DataAccessManager

/*
* 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;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.wso2.carbon.billing.core.dataobjects.Customer;
import org.wso2.carbon.billing.core.dataobjects.Subscription;
import org.wso2.carbon.billing.core.internal.Util;
import org.wso2.carbon.billing.core.jdbc.DataAccessObject;
import org.wso2.carbon.user.api.Tenant;
import org.wso2.carbon.user.api.TenantManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.List;


public class DataAccessManager {

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

    public DataAccessManager(DataSource dataSource){
        this.dataAccessObject = new DataAccessObject(dataSource);
    }

    public int addSubscription(Subscription subscription) throws BillingException {
        boolean successful = false;
        int subscriptionId = 0;
        try {
            dataAccessObject.beginTransaction();
            subscriptionId =
                    dataAccessObject.addSubscription(subscription,
                            subscription.getSubscriptionPlan());
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscriptionId;
    }

    public List<Customer> getCustomersWithName(String customerName) throws BillingException {
        TenantManager tenantManager = Util.getRealmService().getTenantManager();
        List<Customer> customers = new ArrayList<Customer>();
        try{
            int tenantId = tenantManager.getTenantId(customerName);
            Tenant tenant = tenantManager.getTenant(tenantId);
            if(tenant!=null){
                Customer customer = new Customer();
                customer.setId(tenant.getId());
                customer.setName(tenant.getDomain());
                customer.setStartedDate(tenant.getCreatedDate());
                customer.setEmail(tenant.getEmail());
                //customer.setAddress();
                customers.add(customer);
            }
        }catch(Exception e){
            String msg = "Failed to get customers for customers: " + customerName + ".";
            log.error(msg, e);
            throw new BillingException(msg, e);
        }

        return customers;
    }

    public Subscription getSubscription (int subscriptionId) throws BillingException {
        boolean successful = false;
        Subscription subscription = null;
        try {
            dataAccessObject.beginTransaction();
            subscription = dataAccessObject.getSubscription(subscriptionId);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscription;
    }

    public Subscription getActiveSubscriptionOfCustomer(int customerId) throws BillingException {
        boolean successful = false;
        Subscription subscription;
        try {
            dataAccessObject.beginTransaction();
            subscription =
                    dataAccessObject.getActiveSubscriptionOfCustomer(customerId);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscription;
    }

    public int getItemIdWithName (String name, int parentId) throws BillingException {
        boolean successful = false;
        int itemId;
        try {
            dataAccessObject.beginTransaction();
            itemId = dataAccessObject.getItemId(name, parentId);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return itemId;
    }

    public boolean changeSubscription (int customerId, String subscriptionPlan) throws BillingException {
        boolean successful = false;
        boolean changed = false;
        try {
            dataAccessObject.beginTransaction();
            changed = dataAccessObject.changeSubscription(customerId, subscriptionPlan);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return changed;
    }

    public List<Subscription> getInactiveSubscriptionsOfCustomer(int customerId) throws BillingException {
        boolean successful = false;
        List<Subscription> subscriptions;
        try {
            dataAccessObject.beginTransaction();
            subscriptions = dataAccessObject.getInactiveSubscriptionsOfCustomer(customerId);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return subscriptions;

    }

    public boolean activateSubscription(int subscriptionId) throws BillingException {
        boolean successful = false;
        boolean activated = false;
        try {
            dataAccessObject.beginTransaction();
            activated = dataAccessObject.activateSubscription(subscriptionId);
            successful = true;
        } finally {
            if (successful) {
                dataAccessObject.commitTransaction();
            } else {
                dataAccessObject.rollbackTransaction();
            }
        }
        return activated;   
    }

   
}
TOP

Related Classes of org.wso2.carbon.billing.core.DataAccessManager

TOP
Copyright © 2018 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.