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");
} // 用户名或者密码错
// 确保�
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
JSF Web 应用开发实战源码 (1129个子文件)
stylesheet.css 874B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
stylesheet.css 806B
forum6.dbschema 179KB
JPAExample.dbschema 86KB
tree.GIF 2KB
locked.gif 245B
admin.gif 128B
online.gif 78B
Business.java 45KB
ManageUser.java 21KB
Index.java 20KB
ReadSubject.java 15KB
MyForum.java 14KB
Page1.java 11KB
Page1.java 10KB
Reply.java 10KB
ResetPassword.java 10KB
SessionBean1.java 10KB
Page1.java 10KB
Register.java 9KB
Page1.java 9KB
Page1.java 9KB
ChangePassword.java 9KB
NewPost.java 9KB
Login.java 9KB
DeletePostConfirm.java 9KB
EditPost.java 9KB
Page1.java 9KB
Page1.java 9KB
Page1.java 9KB
DeleteUserConfirm.java 9KB
Page1.java 8KB
Page1.java 8KB
Page1.java 8KB
Page1.java 8KB
Page1.java 8KB
RegisterSuccess.java 8KB
Page2.java 8KB
Page1.java 8KB
Page1.java 8KB
Page1.java 8KB
Page1.java 8KB
NewPostSuccess.java 8KB
Page1.java 8KB
Login.java 7KB
Page1.java 7KB
Page1.java 7KB
LoginSuccess.java 7KB
Page1.java 7KB
LogoutSuccess.java 7KB
共 1129 条
- 1
- 2
- 3
- 4
- 5
- 6
- 12
资源评论
- jack83232020-08-17还可以吧,就是版本有点老了
sun369shine
- 粉丝: 0
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功