venice-dynamo-rewrite/src/venice-base/com/silverwrist/venice/community/CommunityOps_mysql.java
2003-06-02 01:37:40 +00:00

815 lines
24 KiB
Java

/*
* The contents of this file are subject to the Mozilla Public License Version 1.1
* (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.mozilla.org/MPL/>.
*
* Software distributed under the License is distributed on an "AS IS" basis, WITHOUT
* WARRANTY OF ANY KIND, either express or implied. See the License for the specific
* language governing rights and limitations under the License.
*
* The Original Code is the Venice Web Communities System.
*
* The Initial Developer of the Original Code is Eric J. Bowersox <erbo@silcom.com>,
* for Silverwrist Design Studios. Portions created by Eric J. Bowersox are
* Copyright (C) 2003 Eric J. Bowersox/Silverwrist Design Studios. All Rights Reserved.
*
* Contributor(s):
*/
package com.silverwrist.venice.community;
import java.sql.*;
import java.util.*;
import com.silverwrist.util.*;
import com.silverwrist.dynamo.except.*;
import com.silverwrist.dynamo.iface.*;
import com.silverwrist.dynamo.util.*;
import com.silverwrist.venice.CommunityVisibility;
class CommunityOps_mysql extends CommunityOps
{
/*--------------------------------------------------------------------------------
* Attributes
*--------------------------------------------------------------------------------
*/
private DBUtilities m_utils; // reference to utilities object
private PropertySerializer m_psz; // reference to property serializer
/*--------------------------------------------------------------------------------
* Constructor
*--------------------------------------------------------------------------------
*/
CommunityOps_mysql(DBConnectionPool pool)
{
super(pool);
m_utils = (DBUtilities)(pool.queryService(DBUtilities.class));
m_psz = (PropertySerializer)(pool.queryService(PropertySerializer.class));
} // end constructor
/*--------------------------------------------------------------------------------
* Internal operations
*--------------------------------------------------------------------------------
*/
private final java.util.Date touchCommunity(Connection conn, int cid, java.util.Date date) throws SQLException
{
PreparedStatement stmt = null;
try
{ // prepare and execute the update statement
stmt = conn.prepareStatement("UPDATE communities SET lastaccess = ?, lastupdate = ? WHERE cid = ?;");
m_utils.setDateTime(stmt,1,date);
m_utils.setDateTime(stmt,2,date);
stmt.setInt(3,cid);
stmt.executeUpdate();
return date;
} // end try
finally
{ // make sure and close out the statement
SQLUtils.shutdown(stmt);
} // end finally
} // end touchCommunity
private final java.util.Date touchCommunity(Connection conn, int cid) throws SQLException
{
return this.touchCommunity(conn,cid,new java.util.Date());
} // end touchCommunity
/*--------------------------------------------------------------------------------
* Overrides from class OpsBase
*--------------------------------------------------------------------------------
*/
public void dispose()
{
m_psz = null;
m_utils = null;
super.dispose();
} // end dispose
/*--------------------------------------------------------------------------------
* Abstract implementations from class CommunityOps
*--------------------------------------------------------------------------------
*/
java.util.Date setCategoryID(int cid, int catid) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
java.util.Date lastupd = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES communities WRITE;");
// prepare and execute the statement
stmt = conn.prepareStatement("UPDATE communities SET catid = ?, lastaccess = ?, lastupdate = ? WHERE cid = ?;");
stmt.setInt(1,catid);
lastupd = new java.util.Date();
m_utils.setDateTime(stmt,2,lastupd);
m_utils.setDateTime(stmt,3,lastupd);
stmt.setInt(4,cid);
stmt.executeUpdate();
return lastupd;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setCategoryID
Object getProperty(int cid, PropertyKey key) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
String rc_str = null;
try
{ // get a connection
conn = getConnection();
// look up the property
stmt = conn.prepareStatement("SELECT prop_value FROM commprops WHERE cid = ? AND nsid = ? AND prop_name = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,key.getNamespaceID());
stmt.setString(3,key.getName());
rs = stmt.executeQuery();
if (!(rs.next()))
return null; // property not found
rc_str = rs.getString(1);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
// Deserialize the property value.
Object rc = m_psz.deserializeProperty(rc_str);
if (rc!=null)
return rc;
// deserialization exception - throw it
DatabaseException de = new DatabaseException(CommunityOps_mysql.class,"CommunityMessages","property.deserialize");
de.setParameter(0,key.getName());
throw de;
} // end getProperty
DateObjectPair setProperty(int cid, PropertyKey key, Object value) throws DatabaseException
{
String serialized_value = m_psz.serializeProperty(value);
if (serialized_value==null)
{ // serialization exception - throw it
DatabaseException de = new DatabaseException(CommunityOps_mysql.class,"CommunityMessages","property.serialize");
de.setParameter(0,key.getName());
throw de;
} // end if
String old_value = null;
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
ResultSet rs = null;
java.util.Date update = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES commprops WRITE, communities WRITE;");
// look to see if the property value is already there
stmt = conn.prepareStatement("SELECT prop_value FROM commprops WHERE cid = ? AND nsid = ? AND prop_name = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,key.getNamespaceID());
stmt.setString(3,key.getName());
rs = stmt.executeQuery();
if (rs.next())
old_value = rs.getString(1);
SQLUtils.shutdown(rs);
rs = null;
SQLUtils.shutdown(stmt);
if (old_value!=null)
{ // prepare the statement to update the existing record
stmt = conn.prepareStatement("UPDATE commprops SET prop_value = ? WHERE cid = ? AND nsid = ? "
+ "AND prop_name = ?;");
stmt.setString(1,serialized_value);
stmt.setInt(2,cid);
stmt.setInt(3,key.getNamespaceID());
stmt.setString(4,key.getName());
} // end if
else
{ // prepare the statement to insert a new record
stmt = conn.prepareStatement("INSERT INTO commprops (cid, nsid, prop_name, prop_value) VALUES (?, ?, ?, ?);");
stmt.setInt(1,cid);
stmt.setInt(2,key.getNamespaceID());
stmt.setString(3,key.getName());
stmt.setString(4,serialized_value);
} // end else
stmt.executeUpdate(); // execute it!
// Touch the community entry.
update = touchCommunity(conn,cid);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(conn);
} // end finally
if (old_value==null)
return new DateObjectPair(null,update); // no previous value
// Deserialize the property value.
Object rc = m_psz.deserializeProperty(old_value);
if (rc!=null)
return new DateObjectPair(rc,update);
// deserialization exception - throw it
DatabaseException de = new DatabaseException(CommunityOps_mysql.class,"CommunityMessages","property.deserialize");
de.setParameter(0,key.getName());
throw de;
} // end setProperty
DateObjectPair removeProperty(int cid, PropertyKey key) throws DatabaseException
{
String old_value = null;
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
ResultSet rs = null;
java.util.Date update = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES commprops WRITE, communities WRITE;");
// look to see if the property value is already there
stmt = conn.prepareStatement("SELECT prop_value FROM commprops WHERE cid = ? AND nsid = ? AND prop_name = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,key.getNamespaceID());
stmt.setString(3,key.getName());
rs = stmt.executeQuery();
if (rs.next())
old_value = rs.getString(1);
else
return null; // no need to remove anything
SQLUtils.shutdown(rs);
rs = null;
SQLUtils.shutdown(stmt);
// delete the database row
stmt = conn.prepareStatement("DELETE FROM commprops WHERE cid = ? AND nsid = ? AND prop_name = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,key.getNamespaceID());
stmt.setString(3,key.getName());
stmt.executeUpdate();
// Touch the community entry.
update = touchCommunity(conn,cid);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(conn);
} // end finally
// Deserialize the property value.
Object rc = m_psz.deserializeProperty(old_value);
if (rc!=null)
return new DateObjectPair(rc,update);
// deserialization exception - throw it
DatabaseException de = new DatabaseException(CommunityOps_mysql.class,"DatabaseMessages","property.deserialize");
de.setParameter(0,key.getName());
throw de;
} // end removeProperty
int[] getPropertyNamespaceIDs(int cid) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
{ // get a connection
conn = getConnection();
// execute the query!
stmt = conn.prepareStatement("SELECT DISTINCT nsid FROM commprops WHERE cid = ?;");
stmt.setInt(1,cid);
rs = stmt.executeQuery();
// read out a list of the namespace IDs
ArrayList tmp = new ArrayList();
while (rs.next())
tmp.add(new Integer(rs.getInt(1)));
// create and return the array
int[] rc = new int[tmp.size()];
for (int i=0; i<tmp.size(); i++)
rc[i] = ((Integer)(tmp.get(i))).intValue();
tmp.clear();
return rc;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end getPropertyNamespaceIDs
Map getAllProperties(int cid, int namespace) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
{ // get a connection
conn = getConnection();
// execute the query!
stmt = conn.prepareStatement("SELECT prop_name, prop_value FROM commprops WHERE cid = ? AND nsid = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,namespace);
rs = stmt.executeQuery();
// prepare the return value
HashMap rc = new HashMap();
while (rs.next())
{ // copy data out, deserializing properties as we go
String key = rs.getString(1);
Object value = m_psz.deserializeProperty(rs.getString(2));
if (value==null)
{ // deserialization exception - throw it
DatabaseException de = new DatabaseException(CommunityOps_mysql.class,"CommunityMessages",
"property.deserialize");
de.setParameter(0,key);
throw de;
} // end if
rc.put(key,value);
} // end while
return rc;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end getAllProperties
java.util.Date setVisibility(int cid, CommunityVisibility visibility) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
java.util.Date lastupd = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES communities WRITE;");
// prepare and execute the statement
stmt = conn.prepareStatement("UPDATE communities SET hide_dir = ?, hide_search = ?, lastaccess = ?, "
+ "lastupdate = ? WHERE cid = ?;");
stmt.setInt(1,(CommunityVisibility.SEARCHDIR.equals(visibility) ? 1 : 0));
stmt.setInt(2,(CommunityVisibility.NONE.equals(visibility) ? 0 : 1));
lastupd = new java.util.Date();
m_utils.setDateTime(stmt,3,lastupd);
m_utils.setDateTime(stmt,4,lastupd);
stmt.setInt(5,cid);
stmt.executeUpdate();
return lastupd;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setVisibility
java.util.Date setName(int cid, String name) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
java.util.Date lastupd = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES communities WRITE;");
// prepare and execute the statement
stmt = conn.prepareStatement("UPDATE communities SET name = ?, lastaccess = ?, lastupdate = ? WHERE cid = ?;");
stmt.setString(1,name);
lastupd = new java.util.Date();
m_utils.setDateTime(stmt,2,lastupd);
m_utils.setDateTime(stmt,3,lastupd);
stmt.setInt(4,cid);
stmt.executeUpdate();
return lastupd;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setName
java.util.Date setAlias(int cid, String alias) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
java.util.Date lastupd = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES communities WRITE;");
// prepare and execute the statement
stmt = conn.prepareStatement("UPDATE communities SET alias = ?, lastaccess = ?, lastupdate = ? WHERE cid = ?;");
stmt.setString(1,alias);
lastupd = new java.util.Date();
m_utils.setDateTime(stmt,2,lastupd);
m_utils.setDateTime(stmt,3,lastupd);
stmt.setInt(4,cid);
stmt.executeUpdate();
return lastupd;
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setAlias
void setLastAccessDate(int cid, java.util.Date date) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES communities WRITE;");
// prepare and execute the statement
stmt = conn.prepareStatement("UPDATE communities SET lastaccess = ? WHERE cid = ?;");
m_utils.setDateTime(stmt,1,date);
stmt.setInt(2,cid);
stmt.executeUpdate();
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setLastAccessDate
void setLastUpdateDate(int cid, java.util.Date date) throws DatabaseException
{
Connection conn = null;
Statement stmt = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt = conn.createStatement();
stmt.executeUpdate("LOCK TABLES communities WRITE;");
// touch the community
touchCommunity(conn,cid,date);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
} // end setLastUpdateDate
java.util.Date grantAccess(int cid, int ugid, boolean is_group, boolean single_use, int auth_nsid,
String auth_name, String source_data, String auth_data) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
ResultSet rs = null;
java.util.Date update = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES commaccess WRITE, communities WRITE;");
// create the insert statement and execute it
stmt = conn.prepareStatement("INSERT INTO commaccess (cid, ugid, is_group, single_use, auth_nsid, auth_name, "
+ "source_data, auth_data) VALUES (?, ?, ?, ?, ?, ?, ?, ?);");
stmt.setInt(1,cid);
stmt.setInt(2,ugid);
stmt.setInt(3,(is_group ? 1 : 0));
stmt.setInt(4,(single_use ? 1 : 0));
if (auth_nsid==0)
stmt.setNull(5,Types.INTEGER);
else
stmt.setInt(5,auth_nsid);
stmt.setString(6,auth_name);
stmt.setString(7,source_data);
stmt.setString(8,auth_data);
stmt.executeUpdate();
// Touch the community entry.
return touchCommunity(conn,cid);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(conn);
} // end finally
} // end grantAccess
java.util.Date revokeAccess(int cid, int ugid, boolean is_group) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
Statement stmt2 = null;
ResultSet rs = null;
java.util.Date update = null;
try
{ // get a connection
conn = getConnection();
// lock the table
stmt2 = conn.createStatement();
stmt2.executeUpdate("LOCK TABLES commaccess WRITE, communities WRITE;");
// create the delete statement and execute it
stmt = conn.prepareStatement("DELETE FROM commaccess WHERE cid = ? AND ugid = ? AND is_group = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,ugid);
stmt.setInt(3,(is_group ? 1 : 0));
stmt.executeUpdate();
// Touch the community entry.
return touchCommunity(conn,cid);
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
MySQLUtils.unlockTables(conn);
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(stmt2);
SQLUtils.shutdown(conn);
} // end finally
} // end revokeAccess
Object getJoinRequirement(int cid, int uid) throws DatabaseException
{
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try
{ // get a connection
conn = getConnection();
// first, look for an entry in the access table matching the UID itself
stmt = conn.prepareStatement("SELECT auth_nsid, auth_name FROM commaccess WHERE cid = ? AND ugid = ? "
+ "AND is_group = 0;");
stmt.setInt(1,cid);
stmt.setInt(2,uid);
rs = stmt.executeQuery();
// If there's a PropertyKey, save it for a later return. If there's a "null/null," return it at once.
PropertyKey tmp = null;
if (rs.next())
{ // OK, found an entry, interpret the NSID and name
int nsid = rs.getInt(1);
boolean nsid_null = rs.wasNull();
String name = rs.getString(2);
boolean name_null = rs.wasNull();
if (nsid_null && name_null)
return Boolean.TRUE;
else
tmp = new PropertyKey(nsid,name);
} // end if
SQLUtils.shutdown(rs);
rs = null;
SQLUtils.shutdown(stmt);
// Now look for an entry matching any group ID we're a member of.
stmt = conn.prepareStatement("SELECT a.auth_nsid, a.auth_name FROM commaccess a, groupmembers g WHERE a.cid = ? "
+ "AND a.ugid = g.gid AND a.is_group = 1 AND g.uid = ?;");
stmt.setInt(1,cid);
stmt.setInt(2,uid);
rs = stmt.executeQuery();
// Determine a return value.
HashSet rc = new HashSet();
if (tmp!=null)
rc.add(tmp); // add the temporary return value from the first step
while (rs.next())
{ // scan all entries for a "null/null" and collect the rest into a Set
int nsid = rs.getInt(1);
boolean nsid_null = rs.wasNull();
String name = rs.getString(2);
boolean name_null = rs.wasNull();
if (nsid_null && name_null)
return Boolean.TRUE;
else
rc.add(new PropertyKey(nsid,name));
} // end while
if (rc.size()>0)
return rc; // if we found any values, return them
} // end try
catch (SQLException e)
{ // translate to a general DatabaseException
throw generalException(e);
} // end catch
finally
{ // shut everything down
SQLUtils.shutdown(rs);
SQLUtils.shutdown(stmt);
SQLUtils.shutdown(conn);
} // end finally
return null; // nothing else found...
} // end getJoinRequirement
} // end class CommunityOps_mysql