net.sf.opensmus
Class MUSSQLConnection

java.lang.Object
  extended by net.sf.opensmus.MUSSQLConnection
All Implemented Interfaces:
ServerUserDatabase, SQLGateway

public class MUSSQLConnection
extends java.lang.Object
implements ServerUserDatabase, SQLGateway


Field Summary
 boolean m_enabled
           
 
Fields inherited from interface net.sf.opensmus.ServerUserDatabase
AUTHENTICATION_NONE, AUTHENTICATION_OPTIONAL, AUTHENTICATION_REQUIRED
 
Constructor Summary
MUSSQLConnection(MUSServer srv, boolean enableFlag)
           
 
Method Summary
 int checkLogin(MUSUser oneUser, java.lang.String username, java.lang.String password)
          Checks if a user/pass combination is valid and sets the userlevel
 void checkPoint()
           
 boolean connect(java.lang.String sqldriver, java.lang.String sqlurl, java.lang.String sqluser, java.lang.String sqlpass)
          Connects to the default SQL database.
 void createDefaultDB()
           
 boolean createUser(java.lang.String usernamein, java.lang.String password, java.lang.String userlevel)
          Creates a user record in the user database
 boolean deleteDBUser(int userid)
          Deletes the user with the specified user id from the database.
 void deliver(ServerUser user, MUSMovie mov, java.lang.String[] args, MUSMessage msg, MUSMessage reply)
           
 void disconnect()
          Disconnects from the SQL database Usually the connection is kept open for the entire duration of the OpenSMUS session
 void ensureDBPresence()
           
 LValue executeQuery(java.lang.String sqlquery, LList params)
          Executes an SQL query call
Use question marks in the query as placeholders for values contained in the params list.
 boolean executeUpdate(java.lang.String sqlquery, LList params)
          Executes an SQL update call
Use question marks in the query as placeholders for values contained in the params list.
 java.sql.Connection getConnection()
          Returns the default SQL database connection as a java.sql.Connection object.
 int getDBUser(java.lang.String usernamein)
          Returns the integer id for the user in the database.
 int getDBUserLevel(int userid)
          Retrieves the user access level
 java.lang.String getDBUserPassword(int userid)
          Retrieves the user password
 boolean isConnected()
          Checks if the SQL database connection is alive
 boolean isEnabled()
          Checks if the user database is enabled.
 void killDBConnection()
           
 void processDBConfigCommands()
           
 void updateUserLastLoginTime(int userid)
          Updates the last login time for this user in the database to the current time.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

m_enabled

public boolean m_enabled
Constructor Detail

MUSSQLConnection

public MUSSQLConnection(MUSServer srv,
                        boolean enableFlag)
Method Detail

isEnabled

public boolean isEnabled()
Description copied from interface: ServerUserDatabase
Checks if the user database is enabled.
Databases are enabled by default, but can be disabled by using the OpenSMUS.cfg file.

Specified by:
isEnabled in interface ServerUserDatabase

connect

public boolean connect(java.lang.String sqldriver,
                       java.lang.String sqlurl,
                       java.lang.String sqluser,
                       java.lang.String sqlpass)
Description copied from interface: SQLGateway
Connects to the default SQL database.
This method is usually not necessary since OpenSMUS establishes the connection automatically using the following directives from OpenSMUS.cfg:
SQLDatabaseDriver
SQLDatabaseURL
SQLDatabaseUsername
SQLDatabasePassword

Specified by:
connect in interface SQLGateway
Parameters:
sqldriver - JDBC database driver
sqlurl - JDBC database URL
sqluser - Username for connection
sqlpass - Password for connection

isConnected

public boolean isConnected()
Description copied from interface: SQLGateway
Checks if the SQL database connection is alive

Specified by:
isConnected in interface SQLGateway

checkPoint

public void checkPoint()

disconnect

public void disconnect()
Description copied from interface: SQLGateway
Disconnects from the SQL database Usually the connection is kept open for the entire duration of the OpenSMUS session

Specified by:
disconnect in interface SQLGateway

getConnection

public java.sql.Connection getConnection()
Description copied from interface: SQLGateway
Returns the default SQL database connection as a java.sql.Connection object.
This is useful if the script author wants to call JDBC methods directly.

Specified by:
getConnection in interface SQLGateway

killDBConnection

public void killDBConnection()

processDBConfigCommands

public void processDBConfigCommands()

ensureDBPresence

public void ensureDBPresence()
                      throws java.sql.SQLException
Throws:
java.sql.SQLException

createDefaultDB

public void createDefaultDB()
                     throws java.sql.SQLException
Throws:
java.sql.SQLException

createUser

public boolean createUser(java.lang.String usernamein,
                          java.lang.String password,
                          java.lang.String userlevel)
Description copied from interface: ServerUserDatabase
Creates a user record in the user database

Specified by:
createUser in interface ServerUserDatabase
Parameters:
usernamein - Username string, will be converted to uppercase for storage
password - Password string
userlevel - User access level as a string. Usual values are between 20 and 100.
If the string specified can not be converted the default user level will be set.
Returns:
true if the user account is created successfully, false if an error occurs

updateUserLastLoginTime

public void updateUserLastLoginTime(int userid)
Description copied from interface: ServerUserDatabase
Updates the last login time for this user in the database to the current time.

Specified by:
updateUserLastLoginTime in interface ServerUserDatabase
Parameters:
userid - User id integer retrieved by the getDBUser method.

getDBUser

public int getDBUser(java.lang.String usernamein)
              throws DBException,
                     UserNotFoundException
Description copied from interface: ServerUserDatabase
Returns the integer id for the user in the database.
Other methods use the user id for speed.

Specified by:
getDBUser in interface ServerUserDatabase
Parameters:
usernamein - Username string
Throws:
DBException
UserNotFoundException

deleteDBUser

public boolean deleteDBUser(int userid)
Description copied from interface: ServerUserDatabase
Deletes the user with the specified user id from the database.

Specified by:
deleteDBUser in interface ServerUserDatabase
Parameters:
userid - User id integer retrieved by the getDBUser method.
Returns:
true if the user account is deleted successfully, false if an error occurs

getDBUserLevel

public int getDBUserLevel(int userid)
                   throws DBException
Description copied from interface: ServerUserDatabase
Retrieves the user access level

Specified by:
getDBUserLevel in interface ServerUserDatabase
Parameters:
userid - User id integer retrieved by the getDBUser method.
Throws:
DBException

getDBUserPassword

public java.lang.String getDBUserPassword(int userid)
                                   throws DBException
Description copied from interface: ServerUserDatabase
Retrieves the user password

Specified by:
getDBUserPassword in interface ServerUserDatabase
Parameters:
userid - User id integer retrieved by the getDBUser method.
Throws:
DBException

checkLogin

public int checkLogin(MUSUser oneUser,
                      java.lang.String username,
                      java.lang.String password)
Description copied from interface: ServerUserDatabase
Checks if a user/pass combination is valid and sets the userlevel

Specified by:
checkLogin in interface ServerUserDatabase
Parameters:
oneUser - The user object that will have the userlevel set
Returns:
0 if allowed to login, MUS errorcode if not.

executeUpdate

public boolean executeUpdate(java.lang.String sqlquery,
                             LList params)
Description copied from interface: SQLGateway
Executes an SQL update call
Use question marks in the query as placeholders for values contained in the params list.
OpenSMUS automatically maps the LList values to the appropriate SQL type.
Example:
LList params= new LList();
params.addElement(new LString("John"));
params.addElement(new LInteger(25));
sqlgateway.executeUpdate("UPDATE USERS SET NAME=? WHERE USERID=?",params);

Specified by:
executeUpdate in interface SQLGateway
Parameters:
sqlquery - Prepared SQL query string.
params - LList of parameters to the prepared statement.
Returns:
true if no SQL error occurs

executeQuery

public LValue executeQuery(java.lang.String sqlquery,
                           LList params)
Description copied from interface: SQLGateway
Executes an SQL query call
Use question marks in the query as placeholders for values contained in the params list.
OpenSMUS automatically maps the LList values to the appropriate SQL type.

Example:
LList params= new LList();
params.addElement(new LString("John"));
params.addElement(new LInteger(25));
LValue result = sqlgateway.executeQuery("SELECT LASTNAME, AGE FROM USERS WHERE FIRSTNAME=? AND AGE>?",params);

Specified by:
executeQuery in interface SQLGateway
Parameters:
sqlquery - Prepared SQL query string.
params - LList of parameters to the prepared statement.
Returns:
LList containing the query results as LValues.
Each row in the result is returned as an LList inside the main return list.
Sample output (Lingo formatted): [["Perkins",10],["Garcia",23]]

deliver

public void deliver(ServerUser user,
                    MUSMovie mov,
                    java.lang.String[] args,
                    MUSMessage msg,
                    MUSMessage reply)