package org.bubu.struts.action;
import java.util.Date;
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 org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.bubu.bean.Adminuser;
import org.bubu.bean.Ans;
import org.bubu.bean.Ques;
import org.bubu.dao.IAdminuserDAO;
import org.bubu.dao.IOrderDAO;
import org.bubu.dao.IPrivilegeDAO;
import org.bubu.dao.IQuesDAO;
import org.bubu.dao.IQuesanswDAO;
import org.bubu.dao.IRoleDAO;
import org.bubu.dao.IUserDAO;
import org.bubu.dao.IUseruserDAO;
import org.bubu.struts.form.AdminForm;
import org.bubu.util.MD5Code;
import org.bubu.util.StrUtil;
/**
* MyEclipse Struts
* Creation date: 04-24-2008
*
* XDoclet definition:
* @struts.action path="/admin" name="adminForm" input="/form/admin.jsp" parameter="method" scope="request" validate="true"
* @struts.action-forward name="error" path="/admin/error.jsp"
* @struts.action-forward name="success" path="/admin/frame.jsp" redirect="true"
*/
public class AdminAction extends DispatchAction {
private IAdminuserDAO adminuserDAO;
private IRoleDAO roleDAO;
private IPrivilegeDAO privilegeDAO;
private IOrderDAO orderDAO;
private IUserDAO userDAO;
private IUseruserDAO useruserDAO;
private IQuesDAO quesDAO;
private IQuesanswDAO quesanswDAO;
public IQuesanswDAO getQuesanswDAO() {
return quesanswDAO;
}
public void setQuesanswDAO(IQuesanswDAO quesanswDAO) {
this.quesanswDAO = quesanswDAO;
}
public IQuesDAO getQuesDAO() {
return quesDAO;
}
public void setQuesDAO(IQuesDAO quesDAO) {
this.quesDAO = quesDAO;
}
public IUseruserDAO getUseruserDAO() {
return useruserDAO;
}
public void setUseruserDAO(IUseruserDAO useruserDAO) {
this.useruserDAO = useruserDAO;
}
public IUserDAO getUserDAO() {
return userDAO;
}
public void setUserDAO(IUserDAO userDAO) {
this.userDAO = userDAO;
}
public IOrderDAO getOrderDAO() {
return orderDAO;
}
public void setOrderDAO(IOrderDAO orderDAO) {
this.orderDAO = orderDAO;
}
public IPrivilegeDAO getPrivilegeDAO() {
return privilegeDAO;
}
public void setPrivilegeDAO(IPrivilegeDAO privilegeDAO) {
this.privilegeDAO = privilegeDAO;
}
public IRoleDAO getRoleDAO() {
return roleDAO;
}
public void setRoleDAO(IRoleDAO roleDAO) {
this.roleDAO = roleDAO;
}
public IAdminuserDAO getAdminuserDAO() {
return adminuserDAO;
}
public void setAdminuserDAO(IAdminuserDAO adminuserDAO) {
this.adminuserDAO = adminuserDAO;
}
/**
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
public ActionForward login(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminForm adminForm = (AdminForm) form;
try {
boolean isok=adminuserDAO.isstatus(adminForm.getName());
if(isok==false){
ActionMessages msg=new ActionMessages();
msg.add("statuserro", new ActionMessage("page.admin.status.error"));
this.saveErrors(request, msg);
//System.out.println("000000000000000000");
return mapping.findForward("error");
}
MD5Code MD5=new MD5Code();
boolean is=adminuserDAO.querylogin(adminForm.getName(), MD5.getMD5ofStr(adminForm.getPwd()));
if(is==true){
String name=adminForm.getName();
request.getSession().setAttribute("isadminlogin", is);
request.getSession().setAttribute("adminname", name);
String enterlast=adminuserDAO.getenterlast(name);
request.getSession().setAttribute("enterlast", enterlast);
adminuserDAO.updateenterlast(name);
//System.out.println("1111111111111111111111");
return mapping.findForward("success");
}else{
ActionMessages msg=new ActionMessages();
msg.add("loginerror", new ActionMessage("page.admin.login.error"));
this.saveErrors(request, msg);
//System.out.println("2222222222222222222222");
return mapping.findForward("error");
}
} catch (Exception e) {
e.printStackTrace();
}
//System.out.println("333333333333333333333");
return mapping.findForward("error");
}
public ActionForward mainjsp(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
int isnotokcount=0;
int isokcount=0;
int subquescount=0;
int luiyancount=0;
try {
isnotokcount = orderDAO.queryokcount(0);
isokcount=orderDAO.queryokcount(1);
subquescount=quesanswDAO.querynoanswcount(0);
luiyancount = quesanswDAO.querynoanswcount(1);
} catch (Exception e) {
e.printStackTrace();
}
request.setAttribute("subquescount", subquescount);
request.setAttribute("isnotokcount", isnotokcount);
request.setAttribute("isokcount", isokcount);
request.setAttribute("luiyancount", luiyancount);
return mapping.findForward("main");
}
public ActionForward show(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
try {
List list=adminuserDAO.Queryall();
request.setAttribute("adminuserlist", list);
return mapping.findForward("adminusershow");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
//添加管理员
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminForm adminForm = (AdminForm) form;
try {
boolean is=adminuserDAO.isexist(adminForm.getName());
if(is==true){
ActionMessages msg=new ActionMessages();
msg.add("nameexist", new ActionMessage("page.admin.user.name.error"));
this.saveErrors(request, msg);
return mapping.findForward("error");
}
MD5Code MD5=new MD5Code();
Adminuser adminuser=new Adminuser();
adminuser.setName(adminForm.getName());
adminuser.setRealname(adminForm.getRealname());
adminuser.setDescription(adminForm.getDescription());
adminuser.setPwd(MD5.getMD5ofStr(adminForm.getPwd()));
adminuser.setStatus(1);
adminuser.setEnterCount(0);
adminuser.setAddtime(StrUtil.FormatDate(new Date(), "yyyy-MM-dd HH:mm:ss"));
adminuserDAO.insert(adminuser);
String url="admin.do?method=show";
request.setAttribute("urledit", url);
return mapping.findForward("updatesuccess");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ActionForward editinfo(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
String name=(String)request.getSession().getAttribute("adminname");
try {
Adminuser adminuser=adminuserDAO.getadminuser(name);
request.setAttribute("adminuser", adminuser);
return mapping.findForward("modifyinfo");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
AdminForm adminForm = (AdminForm) form;
String name=request.getParameter("name");
try {
Adminuser adminuser=adminuserDAO.getadminuser(name);
request.setAttribute("adminuser", adminuser);
//这里取出原来已有的权限项,以数组形式
String priv=adminuser.getPrivilege();
String[] select=StrUtil.splitStr(priv, ',');
List rolelist=roleDAO.queryall();
request.setAttribute("rolelist", rolelist);
List privilegelist=privilegeDAO.Queryall();
//这里取出共有的权限项,以数组形式
String[] source=privilegeDAO.privarray(privilegelist);
/*
for(int i=0;i<select.length;i++){