package pzy.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import pzy.biz.RoleBiz;
import pzy.biz.UserBiz;
import pzy.biz.UserFlag;
import pzy.entity.SysUser;
import pzy.form.UserForm;
public class UserAction extends BaseAction {
private UserBiz userBiz;
private RoleBiz roleBiz;
public void setUserBiz(UserBiz userBiz) {
this.userBiz = userBiz;
}
public void setRoleBiz(RoleBiz roleBiz) {
this.roleBiz = roleBiz;
}
/*
* 前往添加页
*/
public ActionForward toAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return mapping.findForward("add");
}
/*
* 添加用户
*/
public ActionForward doAdd(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
UserForm userForm = (UserForm) form;
SysUser user = userForm.getUser();
user.getUserRole().setRoleId(new Integer(3));
user.setUserFlag(UserFlag.Normal);
if (userBiz.addUser(user) == 0) {
super.clearErrors();
super.AddError("errors.user.name_already_exist", userForm.getUser().getUserName());
super.SaveErrors(request);
return mapping.findForward("add");
}
response.sendRedirect("user.do?method=toList");
return null;
}
/*
* 前往搜索页
*/
@SuppressWarnings("unchecked")
public ActionForward toList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
userBiz.getUserList(userForm.getUser(), userForm.getPageResult());
return mapping.findForward("list");
}
/*
* 前往编辑页
*/
public ActionForward toEdit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
String userId = request.getParameter("id");
SysUser user = userBiz.getUser(Integer.parseInt(userId));
userForm.setUser(user);
return mapping.findForward("edit");
}
/*
* 编辑用户(未完成)
*/
public ActionForward doEdit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
return null;
}
/*
* 删除用户
*/
public ActionForward doDelete(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
String userId = request.getParameter("id");
userBiz.deleteUser(Integer.parseInt(userId));
response.sendRedirect("user.do?method=toList");
return null;
}
/*
* 用户登录
*/
public ActionForward doLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
UserForm userForm = (UserForm) form;
SysUser user = userBiz.login(userForm.getUser());
if (user != null) {
request.getSession().setAttribute("User", user);
super.buildMenuString(request);
response.sendRedirect("main.jsp");
return null;
} else {
super.clearErrors();
super.AddError("errors.user.login_failure");
super.SaveErrors(request);
return mapping.findForward("index");
}
}
/*
* 注销
*/
public ActionForward doLogout(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
request.getSession().invalidate();
String str = "<script>window.parent.location='index.jsp'</script>";
response.getWriter().print(str);
return null;
}
/*
* 前往登录页
*/
public ActionForward toLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
return mapping.findForward("index");
}
/*
* 分配角色
*/
public ActionForward doAssignRole(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws IOException {
UserForm userForm = (UserForm) form;
userBiz.assignRole(userForm.getUser().getUserId(), userForm.getUser().getUserRole().getRoleId());
response.sendRedirect("user.do?method=toList");
return null;
}
/*
* 前往角色分配页
*/
@SuppressWarnings("unchecked")
public ActionForward toAssignRole(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
String userId = request.getParameter("id");
SysUser user = userBiz.getUser(Integer.parseInt(userId));
userForm.setUser(user);
List roles = roleBiz.getAllRoles();
request.setAttribute("Roles", roles);
return mapping.findForward("assign_role");
}
/*
* 前往用户信息详细页
*/
public ActionForward toDetail(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
UserForm userForm = (UserForm) form;
String userId = request.getParameter("id");
SysUser user = userBiz.getUser(Integer.parseInt(userId));
userForm.setUser(user);
return mapping.findForward("detail");
}
}