⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 billingenginebean.java

📁 Sun公司Dream项目
💻 JAVA
字号:
/*
 * The contents of this file are subject to the terms
 * of the Common Development and Distribution License
 * (the "License").  You may not use this file except
 * in compliance with the License.
 *
 * You can obtain a copy of the license at
 * http://www.opensource.org/licenses/cddl1.php
 * See the License for the specific language governing
 * permissions and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL
 * HEADER in each file and include the License file at
 * http://www.opensource.org/licenses/cddl1.php.  If 
 * applicable, add the following below this CDDL HEADER, 
 * with the fields enclosed by brackets "[]" replaced 
 * with your own identifying information: 
 * Portions Copyright [yyyy]
 * [name of copyright owner]
 */ 

/*
 * $(@)BillingEngineBean.java $Revision: 1.1.1.1 $ $Date: 2006/03/15 13:12:10 $
 * 
 * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
 */
/**

 *

 * @version: 1.0

 * @date: Sept 19, 2002

 *

 */

package com.sun.sjc.idtv.vod.server.billing;



import java.sql.*;

import java.util.*;

import javax.sql.*;

import javax.ejb.*;

import javax.naming.*;

import com.sun.sjc.idtv.vod.shared.data.*;



/**

 * Implementation bean for the <code>BillingEngine</code> interface, <code>BillingEngine</code>

 * defines all possible business methods for the bean.

 *

 * @see BillingEngine

 * @see BillingEngineHome

 */

public class BillingEngineBean implements SessionBean {



    private  javax.ejb.SessionContext m_ctx = null; 

    public static final int QUERYTIMEOUT = 60;

    private DataSource dataSource;



    /**

     * Sets the session context. Required by EJB spec.

     * @param ctx A SessionContext object.

     */

    public void setSessionContext(javax.ejb.SessionContext ctx) { 

        m_ctx = ctx; 

	try {

	    InitialContext ic = new InitialContext();

	    //dataSource = (DataSource) ic.lookup("java:comp/env/jdbc/vod");

	    dataSource = (DataSource) ic.lookup("jdbc/sample");

	} catch (Exception ex) {

	    throw new EJBException("Unable to connect to database. " +

				   ex.getMessage());

	}

    }



    /**

     * Creates a bean. Required by EJB spec.

     * @exception throws CreateException.

     */

    public void ejbCreate() throws java.rmi.RemoteException, javax.ejb.CreateException { 

    } 



    /**

     * Removes the bean. Required by EJB spec.

     */

    public void ejbRemove() { 

    } 



    /**

     * Loads the state of the bean from secondary storage. Required by EJB spec.

     */

    public void ejbActivate() { 

    } 



    /**

     * Serializes the state of the bean to secondary storage. Required by EJB spec.

     */

    public void ejbPassivate() { 

    } 



    /**

     * Required by EJB spec.

     */

    public void BillingEngine() { 

    } 





    /**

     * Rent a movie.

     * @param subscriber the subscriber.

     * @param movie the movie.

     * @return true if the account balance allows the rental

     * @exception RemoteException

     */

    public boolean rentMovie(Subscriber subscriber, Movie movie) throws java.rmi.RemoteException, SQLException {



	// check if already rented

	boolean currentrental = isRental(subscriber, movie);

	float balance = 0;

	if (!currentrental) {

	    // check account balance

	    balance = getAccountBalance(subscriber);

	    if (balance < movie.stdprice) {

		return false;

	    }

	}



	// log

	insertCDR(subscriber, movie);



	// bill

	if (!currentrental) {

	    setAccountBalance(subscriber, balance - movie.stdprice);

	}

	return true;

    }



    private boolean isRental(Subscriber subscriber, Movie movie) throws java.rmi.RemoteException, SQLException {



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // check if this movie is currently rented

       PreparedStatement stmt = conn.prepareStatement("SELECT * FROM subscrentals WHERE subscrid = ? AND movieid = ? AND starttimest > ? AND elapsedviewtime > ?");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       if (subscriber.ismaster) {

	   stmt.setLong(1, subscriber.masterid);

       } else {

	   stmt.setLong(1, subscriber.id);

       }

       stmt.setLong(2, movie.id);

       Timestamp yesterday = new Timestamp(System.currentTimeMillis() - (24 * 60 * 60 * 1000)); // 24 hours watch window

       stmt.setTimestamp(3, yesterday);

       stmt.setLong(4, -1); // unlimited viewtime for now

       ResultSet rs = stmt.executeQuery();

       

       boolean isrental = rs.next();



       stmt.close();

       conn.close();

       return isrental;

    }



    private void insertCDR(Subscriber subscriber, Movie movie) throws SQLException {



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // insert a CDR

       PreparedStatement stmt = conn.prepareStatement("INSERT INTO billingcdr VALUES (?, ?, ?, ?)");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setLong(1, subscriber.masterid);

       stmt.setLong(2, movie.id);

       Timestamp now = new Timestamp(System.currentTimeMillis());

       stmt.setTimestamp(3, now);

       stmt.setFloat(4, movie.stdprice);

       stmt.executeUpdate();

       stmt.close();



       // rent it

       stmt = conn.prepareStatement("INSERT INTO subscrentals VALUES (?, ?, ?, ?, ?)");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       if (subscriber.ismaster) {

	   stmt.setLong(1, subscriber.masterid);

       } else {

	   stmt.setLong(1, subscriber.id);

       }

       stmt.setLong(2, movie.id);

       stmt.setTimestamp(3, now);

       stmt.setLong(4, 0);

       stmt.setString(5, "");

       stmt.executeUpdate();

       stmt.close();



       conn.close();

       return;

    }



    /**

     * Get the account balance for a given subscriber.

     * @param subscriber the subscriber.

     * @return account balance

     * @exception RemoteException

     */

    public float getAccountBalance(Subscriber subscriber) throws java.rmi.RemoteException, SQLException {



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // get account balance

       PreparedStatement stmt = conn.prepareStatement("SELECT subscracct.balance FROM subscracct WHERE (subscracct.subscrid = ?)");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setLong(1, subscriber.masterid);

       ResultSet rs = stmt.executeQuery();

       float balance = 0;

       if (rs.next()) {

	   balance = rs.getFloat(1);

       }

       rs.close();

       stmt.close();

       conn.close();

       return balance;

    }



    /**

     * Set the account balance for a given subscriber.

     * @param subscriber the subscriber.

     * @exception RemoteException

     */

    public void setAccountBalance(Subscriber subscriber, float balance) throws java.rmi.RemoteException, SQLException {



       // get db connection from pool

       Connection conn = dataSource.getConnection();



       // set account balance

       PreparedStatement stmt = conn.prepareStatement("UPDATE subscracct SET balance = ? WHERE (subscracct.subscrid = ?)");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setFloat(1, balance);

       stmt.setLong(2, subscriber.masterid);

       stmt.executeUpdate();

       stmt.close();

       conn.close();

       return;

    }



    /**

     * Get the account movement history for a given subscriber.

     * @param subscriber the subscriber.

     * @param start start date for the history.

     * @param end end date for the history.

     * @return movement history

     * @exception RemoteException

     */

    public String[] getHistory(Subscriber subscriber, java.util.Date start, java.util.Date end) throws java.rmi.RemoteException, SQLException {



	// get db connection from pool

       Connection conn = dataSource.getConnection();



       // get CDRs

       //PreparedStatement stmt = conn.prepareStatement("SELECT * FROM billingcdr WHERE (billingcdr.subscrid = ?) AND (billingcdr.purchasedate >= ?) AND (billingcdr.purchasedate < ?) ORDER BY billingcdr.purchasedate");

       PreparedStatement stmt = conn.prepareStatement("SELECT * FROM billingcdr WHERE (billingcdr.subscrid = ?) ORDER BY billingcdr.purchasedate");



       stmt.setQueryTimeout(QUERYTIMEOUT);

       stmt.setLong(1, subscriber.masterid);

       //stmt.setTimestamp(2, new Timestamp(start.getTime()));

       //stmt.setTimestamp(3, new Timestamp(end.getTime()));

       ResultSet rs = stmt.executeQuery();

       Vector v = new Vector();

       while (rs.next()) {

	   String cdr = "" + rs.getString(1) + " " + rs.getString(2) + " " + rs.getString(3);

	   v.addElement(cdr);

       }

       rs.close();

       stmt.close();

       conn.close();

       String[] cdrs = new String[v.size()];

       for (int i=0; i<v.size(); i++ ) {

	   cdrs[i] = (String) v.elementAt(i);

       }

       return cdrs;

    }

} 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -