📄 subscribermanagementsystembean.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]
*/
/*
* $(@)SubscriberManagementSystemBean.java $Revision: 1.2 $ $Date: 2006/07/24 21:58:58 $
*
* Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
*/
/**
*
* @version: 1.0
* @date: Sept 19, 2002
*
*/
package com.sun.sjc.idtv.vod.server.subscriber;
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>SubscriberManagementSystem</code> interface, <code>SubscriberManagementSystem</code>
* defines all possible business methods for the bean.
*
* @see SubscriberManagementSystem
* @see SubscriberManagementSystemHome
*/
public class SubscriberManagementSystemBean implements SessionBean {
private javax.ejb.SessionContext m_ctx = null;
public static final int QUERYTIMEOUT = 60*5;
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 SubscriberManagementSystem() {
}
/**
* Fetch the list of subscribers for a given STB IP address.
* @param ip IP address of the STB.
* @return array of subscribers where master is first, or null
* @exception RemoteException
*/
public Subscriber[] getSubscriberList(String ip) throws java.rmi.RemoteException, SQLException {
// get db connection from pool
Connection conn = dataSource.getConnection();
System.out.println("*** GETTING SUBSCRIBER LIST FOR IP " + ip);
// get subscriber list
PreparedStatement stmt = conn.prepareStatement("SELECT mastersubscribers.masterid, mastersubscribers.login, mastersubscribers.firstname, mastersubscribers.lastname FROM mastersubscribers, subscrstb WHERE (subscrstb.ipaddress = ?) AND (mastersubscribers.masterid = subscrstb.subscrid)");
stmt.setQueryTimeout(QUERYTIMEOUT);
stmt.setString(1, ip);
ResultSet rs = stmt.executeQuery();
Subscriber master = null;
if (rs.next()) {
master = new Subscriber();
master.ismaster = true;
master.masterid = rs.getLong(1);
master.login = rs.getString(2);
master.firstname = rs.getString(3);
master.lastname = rs.getString(4);
}
rs.close();
stmt.close();
if (master != null) {
stmt = conn.prepareStatement("SELECT subsubscribers.id, subsubscribers.login, subsubscribers.firstname, subsubscribers.pin FROM subsubscribers WHERE subsubscribers.masterid = ? ORDER BY subsubscribers.firstname");
stmt.setQueryTimeout(QUERYTIMEOUT);
stmt.setLong(1, master.masterid);
rs = stmt.executeQuery();
Vector v = new Vector();
while (rs.next()) {
Subscriber sub = new Subscriber();
sub.masterid = master.masterid;
sub.id = rs.getLong(1);
sub.login = rs.getString(2);
sub.firstname = rs.getString(3);
sub.lastname = rs.getString(4);
v.addElement(sub);
}
rs.close();
stmt.close();
Subscriber[] subs = new Subscriber[v.size() + 1];
subs[0] = master;
for (int i=0; i<v.size(); i++) {
subs[i+1] = (Subscriber) v.elementAt(i);
}
if (v.size() == 0) System.out.println("*** NO SUBSCR FOR THIS IP");
for (int i=0; i<v.size(); i++) {
System.out.println("*** FOUND: " + ((Subscriber) v.elementAt(i)).login);
}
conn.close();
return subs;
} else {
conn.close();
return null;
}
}
/**
* Authenticate a subscriber.
* @param subscriber the subscriber.
* @return the subscriber's detailed info
* @exception RemoteException
*/
public Subscriber authenticate(Subscriber subscriber) throws java.rmi.RemoteException, SQLException {
String password = null;
// get db connection from pool
Connection conn = dataSource.getConnection();
// get subscriberSELECT moviegenres.id, moviegenres.genre, COUNT(moviecatalog.genre) FROM moviegenres, moviecatalog WHERE (moviegenres.id = moviecatalog.genre) GROUP BY moviecatalog.genre, moviegenres.id, moviegenres.genre
if (!subscriber.ismaster) {
PreparedStatement stmt = conn.prepareStatement("SELECT pin FROM subsubscribers WHERE (subsubscribers.id = ?)");
stmt.setQueryTimeout(QUERYTIMEOUT);
stmt.setLong(1, subscriber.id);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
password = rs.getString(1);
}
rs.close();
stmt.close();
} else {
PreparedStatement stmt = conn.prepareStatement("SELECT pin FROM mastersubscribers WHERE (mastersubscribers.masterid = ?)");
stmt.setQueryTimeout(QUERYTIMEOUT);
stmt.setLong(1, subscriber.masterid);
ResultSet rs = stmt.executeQuery();
if (rs.next()) {
password = rs.getString(1);
}
rs.close();
stmt.close();
}
// check password
if (subscriber.pin.equals(password)) {
// get rentals
PreparedStatement stmt = conn.prepareStatement("SELECT DISTINCT movieid, bookmark, shorttitle FROM subscrentals, moviecatalog WHERE (subscrentals.subscrid = ?) AND (subscrentals.movieid = moviecatalog.id) AND (subscrentals.starttimest >= ?) ORDER BY moviecatalog.shorttitle"); // AND (subscrentals.starttimest >= ?)
Timestamp yesterday = new Timestamp(System.currentTimeMillis() - (24*60*60*1000));
stmt.setQueryTimeout(QUERYTIMEOUT);
if (subscriber.ismaster) {
stmt.setLong(1, subscriber.masterid);
} else {
stmt.setLong(1, subscriber.id);
}
stmt.setTimestamp(2, yesterday);
ResultSet rs = stmt.executeQuery();
Vector v = new Vector();
while (rs.next()) {
Rental r = new Rental();
r.id = rs.getLong(1);
r.bookmark = rs.getString(2);
r.shorttitle = rs.getString(3);
v.addElement(r);
}
rs.close();
stmt.close();
subscriber.rentals = new Rental[v.size()];
for (int i=0; i<v.size(); i++) {
subscriber.rentals[i] = (Rental) v.elementAt(i);
}
// get categories
stmt = conn.prepareStatement("SELECT genreid, genre, assetcount FROM subscrgenres, moviegenres WHERE (subscrgenres.subscrid = ?) AND (subscrgenres.genreid = moviegenres.id)");
stmt.setQueryTimeout(QUERYTIMEOUT);
if (subscriber.ismaster) {
stmt.setLong(1, subscriber.masterid);
} else {
stmt.setLong(1, subscriber.id);
}
rs = stmt.executeQuery();
v = new Vector();
while (rs.next()) {
MovieCategory r = new MovieCategory();
r.genreid = rs.getInt(1);
r.genrename = rs.getString(2);
r.count = rs.getInt(3);
v.addElement(r);
}
rs.close();
stmt.close();
subscriber.categories = new MovieCategory[v.size()];
for (int i=0; i<v.size(); i++) {
subscriber.categories[i] = (MovieCategory) v.elementAt(i);
}
// get account balance
stmt = conn.prepareStatement("SELECT subscracct.balance FROM subscracct WHERE (subscracct.subscrid = ?)");
stmt.setQueryTimeout(QUERYTIMEOUT);
stmt.setLong(1, subscriber.masterid);
rs = stmt.executeQuery();
float balance = 0;
if (rs.next()) {
balance = rs.getFloat(1);
}
rs.close();
stmt.close();
subscriber.balance = balance;
conn.close();
return subscriber;
} else {
conn.close();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -