📄 dbprofilemanager.java
字号:
/**
* Copyright (C) 2001 Yasna.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* Yasna.com (http://www.yasna.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Yazd" and "Yasna.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact yazd@yasna.com.
*
* 5. Products derived from this software may not be called "Yazd",
* nor may "Yazd" appear in their name, without prior written
* permission of Yasna.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL YASNA.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of Yasna.com. For more information
* on Yasna.com, please see <http://www.yasna.com>.
*/
/**
* Copyright (C) 2000 CoolServlets.com. All rights reserved.
*
* ===================================================================
* The Apache Software License, Version 1.1
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution,
* if any, must include the following acknowledgment:
* "This product includes software developed by
* CoolServlets.com (http://www.coolservlets.com)."
* Alternately, this acknowledgment may appear in the software itself,
* if and wherever such third-party acknowledgments normally appear.
*
* 4. The names "Jive" and "CoolServlets.com" must not be used to
* endorse or promote products derived from this software without
* prior written permission. For written permission, please
* contact webmaster@coolservlets.com.
*
* 5. Products derived from this software may not be called "Jive",
* nor may "Jive" appear in their name, without prior written
* permission of CoolServlets.com.
*
* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COOLSERVLETS.COM OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
* ====================================================================
*
* This software consists of voluntary contributions made by many
* individuals on behalf of CoolServlets.com. For more information
* on CoolServlets.com, please see <http://www.coolservlets.com>.
*/
package com.Yasna.forum.database;
import java.util.*;
import java.sql.*;
import com.Yasna.forum.*;
import com.Yasna.util.*;
/**
* Database implementation of the ProfileManager interface.
*/
public class DbProfileManager implements ProfileManager {
/** DATABASE QUERIES **/
private static final String USER_GROUPS =
"SELECT groupID from yazdGroupUser WHERE userID=?";
private static final String USER_MESSAGE_COUNT =
"SELECT count(*) FROM yazdMessage,yazdForum,yazdThread WHERE " +
"yazdMessage.userID=? AND yazdForum.forumID=? AND " +
"yazdThread.forumID=yazdForum.forumID AND " +
"yazdMessage.threadID=yazdThread.threadID";
private static final String USER_COUNT = "SELECT count(*) FROM yazdUser";
private static final String ALL_USER_MESSAGES =
"SELECT messageID FROM yazdMessage WHERE userID=?";
private static final String DELETE_USER_MESSAGES =
"UPDATE yazdMessage set userID=-1 WHERE userID=?";
private static final String DELETE_USER_PERMS =
"DELETE FROM yazdUserPerm WHERE userID=?";
private static final String DELETE_USER_GROUPS =
"DELETE FROM yazdGroupUser WHERE userID=?";
private static final String DELETE_USER_PROPS =
"DELETE FROM yazdUserProp WHERE userID=?";
private static final String DELETE_USER =
"DELETE FROM yazdUser WHERE userID=?";
private static final String GROUP_COUNT = "SELECT count(*) FROM yazdGroup";
private static final String DELETE_GROUP_USERS =
"DELETE FROM yazdGroupUser WHERE groupID=?";
private static final String DELETE_GROUP =
"DELETE FROM yazdGroup WHERE groupID=?";
private User anonymousUser = null;
private User specialUser = null;
private DbForumFactory factory;
/**
* Creates a new ProfileManager.
*/
public DbProfileManager(DbForumFactory factory) {
this.factory = factory;
try {
anonymousUser = getUser(-1);
specialUser = getUser(0);
}
catch (UserNotFoundException unfe) { }
}
//FROM THE PROFILEMANAGER INTERFACE//
public User createUser(String username, String password, String email)
throws UserAlreadyExistsException
{
User newUser = null;
try {
User existingUser = getUser(username);
//The user already exists since now exception, so:
throw new UserAlreadyExistsException();
}
catch (UserNotFoundException unfe) {
//The user doesn't already exist so we can create a new user
newUser = new DbUser(username, password, email);
}
return newUser;
}
public User getUser(int userID) throws UserNotFoundException {
DbCacheManager cacheManager = factory.getCacheManager();
//If cache is not enabled, do a new lookup of object
if (!cacheManager.isCacheEnabled()) {
return new DbUser(userID);
}
//Cache is enabled.
Integer userIDInteger = new Integer(userID);
DbUser user = (DbUser)cacheManager.get(
DbCacheManager.USER_CACHE,
userIDInteger
);
if(user == null) {
user = new DbUser(userID);
cacheManager.add(DbCacheManager.USER_CACHE, userIDInteger, user);
}
return user;
}
public User getUser(String username) throws UserNotFoundException {
DbCacheManager cacheManager = factory.getCacheManager();
//If cache is not enabled, do a new lookup of object
if (!cacheManager.isCacheEnabled()) {
User user = new DbUser(username);
return getUser(user.getID());
}
//Cache is enabled.
CacheableInteger userIDInteger = (CacheableInteger)cacheManager.get(
DbCacheManager.USER_ID_CACHE,
username
);
//if id wan't found in cache, load it up and put it there.
if (userIDInteger == null) {
User user = new DbUser(username);
userIDInteger = new CacheableInteger(new Integer(user.getID()));
cacheManager.add(DbCacheManager.USER_ID_CACHE, username, userIDInteger);
}
return getUser(userIDInteger.getInteger().intValue());
}
public User getAnonymousUser() {
return anonymousUser;
}
public User getSpecialUser() {
return specialUser;
}
public void deleteUser(User user) throws UnauthorizedException {
int userID = user.getID();
int [] messages;
//Get array of all user's messages in the system so that
//we can expire them from cache.
ArrayList tempMessages = new ArrayList();
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(ALL_USER_MESSAGES);
pstmt.setInt(1, user.getID());
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
tempMessages.add(new Integer(rs.getInt("messageID")));
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbProfileManager:deleteUser()-" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
//Now copy into an array.
messages = new int[tempMessages.size()];
for (int i=0; i<messages.length; i++) {
messages[i] = ((Integer)tempMessages.get(i)).intValue();
}
con = null;
pstmt = null;
try {
con = DbConnectionManager.getConnection();
//mark all message by user as anonymous
pstmt = con.prepareStatement(DELETE_USER_MESSAGES);
pstmt.setInt(1,userID);
pstmt.execute();
pstmt.close();
//remove all permissions given to user
pstmt = con.prepareStatement(DELETE_USER_PERMS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -