package forum6;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.TimeZone;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.NoResultException;
import javax.persistence.Persistence;
import javax.persistence.Query;
/**
*
* @author admin
*/
public class Business {
private static Business instance = null;
private Business() {
}
public static Business getInstance() {
if (null == instance) {
instance = new Business();
}
return instance;
}
//** Creates a new instance of Business */
public void close() {
instance = null;
}
/*
* 业务逻辑方法:用户注册
*/
public UserT register(
String userName,
String password,
String passwordConfirm,
String nickName)
throws ApplicationException {
// 参数检查
if (null == userName || null == nickName ||
null == password || null == passwordConfirm) {
throw new ApplicationException("InputRequired");
}
// 确保用户名合法
if (userName.length() < 1 || userName.length() > 10 ||
nickName.length() < 1 || nickName.length() > 10) {
throw new ApplicationException("NameInvalid");
}
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("forum6PU");
em = emf.createEntityManager();
// 检查用户名是否被占用
Query query = em.createNamedQuery("UserT.findByUserName");
query.setParameter("userName", userName);
try {
query.getSingleResult();
throw new ApplicationException(
"UserNameNotAvailable", userName);
} catch (NoResultException ex) {
}
// 检查昵称是否被占用
query = em.createNamedQuery("UserT.findByNickName");
query.setParameter("nickName", nickName);
try {
query.getSingleResult();
throw new ApplicationException(
"NickNameNotAvailable", nickName);
} catch (NoResultException ex) {
}
// 确保密码合法
if (password.length() < 1 || password.length() > 20 ||
passwordConfirm.length() < 1 || passwordConfirm.length() > 20) {
throw new ApplicationException("PasswordInvalid");
}
// 确保密码和密码确认相同
if (!password.equals(passwordConfirm)) {
throw new ApplicationException(
"PasswordNotConfirmed");
}
// 添加注册用户
UserT user = new UserT();
user.setUserName(userName);
user.setNickName(nickName);
user.setPassword(password);
user.setIsAdmin(0);
user.setIsLocked(0);
user.setIsOnline(0);
em.getTransaction().begin();
em.persist(user);
em.getTransaction().commit();
return user;
} finally {
// 回滚
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
/*
* 业务逻辑方法:封闭指定用户
*/
public void lock(UserT aOperator, UserT aUser)
throws ApplicationException {
if (null == aOperator) {
throw new ApplicationException("NullParameter", "operator");
}
if (null == aUser) {
throw new ApplicationException("NullParameter", "user");
}
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("forum6PU");
em = emf.createEntityManager();
// 确保操作者和用户存在
UserT operator = this.findUser(em, aOperator);
UserT user = this.findUser(em, aUser);
// 确保操作者在线
onlineVerify(operator);
// 权限验证,确保操作者是管理员
adminVerify(operator);
// 确保拟封闭用户未被封闭
unlockedVerify(user);
// 如果已封闭用户在线,强制其退出(即下线)
if (1 == user.getIsOnline()) {
try {
forceLogout(operator, user);
} catch (ApplicationException ex) {
throw new ApplicationException(
ex,
"ForceLogoutFailure",
operator.getNickName(),
user.getNickName()); // 强制用户退出时失败
}
}
// 封闭用户
em.getTransaction().begin();
user.setIsLocked(1);
em.getTransaction().commit();
} finally {
// 回滚
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
/*
* 业务逻辑方法:解封用户
*/
public void unlock(UserT aOperator, UserT aUser)
throws ApplicationException {
if (null == aOperator) {
throw new ApplicationException("NullParameter", "operator");
}
if (null == aUser) {
throw new ApplicationException("NullParameter", "user");
}
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("forum6PU");
em = emf.createEntityManager();
// 确保操作者和用户存在
UserT operator = this.findUser(em, aOperator);
UserT user = this.findUser(em, aUser);
// 确保操作者在线
onlineVerify(operator);
// 权限验证,确保操作者是管理员
adminVerify(operator);
// 确保用户处于封闭状态
lockedVerify(user);
// 解封用户
em.getTransaction().begin();
user.setIsLocked(0);
em.getTransaction().commit();
} finally {
// 回滚
if (em.getTransaction().isActive()) {
em.getTransaction().rollback();
}
em.close();
emf.close();
}
}
/*
* 业务逻辑方法:用户登录
*/
public UserT login(String userName, String password)
throws ApplicationException {
if (null == userName || null == password) {
throw new ApplicationException("InputRequired");
}
EntityManagerFactory emf = null;
EntityManager em = null;
try {
emf = Persistence.createEntityManagerFactory("forum6PU");
em = emf.createEntityManager();
// 确保用户名和密码正确
Query query = em.createQuery("select u from UserT u " +
"where u.userName = :userName and u.password = :password");
query.setParameter("userName", userName);
query.setParameter("password", password);
UserT user = null;
try {
user = (UserT) query.getSingleResult();
} catch (NoResultException ex) {
}
if (null == user) {
throw new ApplicationException(
"UserNamePasswordWrong");
} // 用户名或者密码错
// 确保�