package com.wilson.struts;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.struts.action.ActionError;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.wilson.service.admin.AdminService;
import com.wilson.service.user.UserService;
import com.wilson.util.ObjectFactory;
import com.wilson.vo.DVD;
import com.wilson.vo.User;
public class AdminAction extends DispatchAction {
private UserService us = (UserService) ObjectFactory
.createObject(UserService.class);
private AdminService as = (AdminService) ObjectFactory
.createObject(AdminService.class);
private List<User> userlist = null;
private List<DVD> dvdlist = null;
@SuppressWarnings("deprecation")
public ActionForward adminLogin(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String adminname = request.getParameter("adminname");
String adminpassword = request.getParameter("adminpassword");
if (adminname.equals("admin") && adminpassword.equals("password")) {
return mapping.findForward("adminmain");
} else {
ActionErrors aes = new ActionErrors();
aes.add("adminname.adminpassword.error", new ActionError(
"adminname.adminpassword.error"));
saveErrors(request, aes);
return mapping.findForward("adminlogin");
}
}
@SuppressWarnings( { "unchecked", "deprecation" })
public ActionForward userList(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
userlist = us.showAll();
HttpSession session = request.getSession();
session.setAttribute("userlist", userlist);
return mapping.findForward("userlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("adminuserlist.notfound.error", new ActionError(
"adminuserlist.notfound.error"));
saveErrors(request, aes);
return mapping.findForward("adminmain");
}
}
// 删除用户
@SuppressWarnings( { "unchecked", "deprecation" })
public ActionForward deleteUser(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Integer userid = Integer.parseInt(request.getParameter("userid"));
try {
userlist = as.delUserById(userid);
HttpSession session = request.getSession();
session.setAttribute("userlist", userlist);
return mapping.findForward("userlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("adminuserdelete.error", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("userlist");
}
}
// 得到所有DVD
@SuppressWarnings( { "unchecked", "deprecation" })
public ActionForward showAll(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
String funcType = request.getParameter("funcType");
try {
dvdlist = as.getAllDvd();
HttpSession session = request.getSession();
session.setAttribute("dvdlist", dvdlist);
return mapping.findForward("admindvdlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("admindvdlist.null", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("admindvdlist");
}
}
// 删除DVD
@SuppressWarnings("deprecation")
public ActionForward deleteDVD(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Integer dvdid = Integer.parseInt(request.getParameter("dvdid"));
try {
dvdlist = as.delDvdById(dvdid);
HttpSession session = request.getSession();
session.setAttribute("dvdlist", dvdlist);
return mapping.findForward("admindvdlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("admindvd.delete.error", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("admindvdlist");
}
}
@SuppressWarnings( { "deprecation", "deprecation" })
public ActionForward add(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DvdForm df = (DvdForm) form;
try {
String dvdname = df.getDvdname();
String state = df.getState();
df.setDvdname(new String(dvdname.getBytes("ISO-8859-1"), "gbk"));
df.setState(new String(state.getBytes("ISO-8859-1"), "gbk"));
dvdlist = as.addDvd(df);
HttpSession session = request.getSession();
session.setAttribute("dvdlist", dvdlist);
return mapping.findForward("admindvdlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("admindvd.add.error", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("adminadddvd");
}
}
// 得到页面上的修改后信息,并且保存
public ActionForward edit(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DvdForm df = (DvdForm) form;
try {
String dvdname = df.getDvdname();
String state = df.getState();
df.setDvdname(new String(dvdname.getBytes("ISO-8859-1"), "gbk"));
df.setState(new String(state.getBytes("ISO-8859-1"), "gbk"));
dvdlist = as.editDvd(df);
HttpSession session = request.getSession();
session.setAttribute("dvdlist", dvdlist);
return mapping.findForward("admindvdlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("admindvd.edit.error", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("admindvdlist");
}
}
// 得到一个影碟信息
public ActionForward getOne(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
Integer dvdid = Integer.parseInt(request.getParameter("dvdid"));
String funcType = request.getParameter("functype");
try {
DVD dvd = as.getById(dvdid);
HttpSession session = request.getSession();
session.setAttribute("findDVD", dvd);
// 如果是查找影碟信息,则把查询信息存入会话中并且转向查询结果页面
if (funcType.equals("findone")) {
return mapping.findForward("adminfinddvd");
}
// 如果要在查询结果页面中对影碟信息进行编辑,则找到该影碟信息后转向编辑页面
if (funcType.equals("fromfind")) {
return mapping.findForward("admineditdvd");
}
// 如果在影碟列表页面中对影碟信息进行编辑,则找到该影碟信息后转向编辑页面
if (funcType.equals("fromlist")) {
return mapping.findForward("admineditdvd");
}
return mapping.findForward("admindvdlist");
} catch (Exception e) {
e.printStackTrace();
ActionErrors aes = new ActionErrors();
aes.add("admin.getone.error", new ActionError(e.getMessage()));
saveErrors(request, aes);
return mapping.findForward("admindvdlist");
}
}
}