package com.easyjf.struts.action;
import java.util.ArrayList;
import java.util.Collection;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import com.easyjf.example.business.IUser;
import com.easyjf.example.business.IUserService;
import com.easyjf.struts.form.UserForm;
import com.easyjf.util.CommUtil;
import com.easyjf.web.tools.IPageList;
import com.easyjf.web.tools.ListQuery;
import com.easyjf.web.tools.PageList;
public class UserManageAction extends Action {
private IUserService userService;
public IUserService getUserService() {
return userService;
}
public void setUserService(IUserService userService)
{
this.userService=userService;
}
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception {
WebApplicationContext wac =WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServlet().getServletContext());
this.userService = (IUserService) wac.getBean("userService");
String command=request.getParameter("easyJWebCommand");
if(command==null || "".equals(command)||"list".equals(command)||"query".equals(command))
{
return doQuery(mapping,form,request,response);
}
else if("new".equals(command))
{
return mapping.findForward("edit");
}
else if("add".equals(command))
{
return doAdd(mapping,form,request,response);
}
else if("edit".equals(command))
{
return doEdit(mapping,form,request,response);
}
else if("update".equals(command))
{
return doUpdate(mapping,form,request,response);
}
else if("del".equals(command))
{
return doDel(mapping,form,request,response);
}
return super.execute(mapping,form,request,response);
}
public ActionForward doEdit(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
UserForm vo=(UserForm)form;
IUser user=null;
if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());
if(user==null)user=userService.newUser();
if(user!=null)
{
vo.setCid(user.getCid());
vo.setUserName(user.getUserName());
vo.setPassword(user.getPassword());
vo.setTel(user.getTel());
vo.setEmail(user.getEmail());
vo.setIntro(user.getIntro());
vo.setBirthday(user.getBirthday());
return mapping.findForward("edit");
}
else
{
request.setAttribute("msg","找不到数据!");
return doQuery(mapping,form,request,response);
}
}
public ActionForward doUpdate(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response)
{
IUser obj=(IUser)form2Po(form);
if(userService.update(obj))
{
request.setAttribute("msg","数据修改成功!");
return doQuery(mapping,form,request,response);
}
else
{
request.setAttribute("msg","数据修改失败");
return mapping.getInputForward();
}
}
public ActionForward doAdd(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
IUser obj=form2Po(form);
if(obj==null)
{
request.setAttribute("msg","无法创建要保存的对象,添加失败!");
return mapping.getInputForward();
}
if(userService.save(obj))
{
request.setAttribute("msg","数据添加成功!");
return doQuery(mapping,form,request,response);
}
else
{
request.setAttribute("msg","数据添加失败");
return mapping.getInputForward();
}
}
public ActionForward doQuery(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
int currentPage=CommUtil.null2Int(request.getParameter("page"));
int pageSize=CommUtil.null2Int(request.getParameter("pageSize"));
if(currentPage<1)currentPage=1;
if(pageSize<1)pageSize=10;
String scope="1=1";
Collection paras=new ArrayList();
String orderType=CommUtil.null2String(request.getParameter("orderType"));
String orderField=CommUtil.null2String(request.getParameter("orderField"));
String userName=CommUtil.null2String(request.getParameter("queryUserName"));
String tel=CommUtil.null2String(request.getParameter("queryTel"));
if(!userName.equals(""))
{
scope+=" and userName like ?";
paras.add("%"+userName+"%");
}
if(!tel.equals(""))
{
scope+=" and tel like ?";
paras.add("%"+tel+"%");
}
if(orderField.equals(""))//默认按用户名排序
{
orderField="userName";
orderType="desc";
}
if(!orderField.equals(""))
{
scope +=" order by "+orderField;
if(!orderType.equals(""))scope+=" "+orderType;
}
IPageList pList=new PageList(new ListQuery(userService.query(scope,paras)));
pList.doList(pageSize,currentPage,"","");
if(pList!=null){
request.setAttribute("list",pList.getResult());
request.setAttribute("pages",new Integer(pList.getPages()));
request.setAttribute("rows",new Integer(pList.getRowCount()));
request.setAttribute("page",new Integer(pList.getCurrentPage()));
request.setAttribute("gotoPageHTML",CommUtil.showPageHtml(pList.getCurrentPage(),pList.getPages()));
}
request.setAttribute("orderType",orderType);
request.setAttribute("orderField",orderField);
return mapping.findForward("list");
}
public ActionForward doDel(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
{
UserForm vo=(UserForm)form;
IUser user=null;
if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());
if(user!=null && userService.del(user))
{
request.setAttribute("msg","数据删除成功!");
}
else
{
request.setAttribute("msg","数据修改失败");
}
return doQuery(mapping,form,request,response);
}
public IUser form2Po(ActionForm form) {
UserForm vo=(UserForm)form;
IUser user=null;
if(vo.getCid()!=null && (!vo.getCid().equals("")))user=userService.read(vo.getCid());
if(user==null)user=userService.newUser();
user.setUserName(vo.getUserName());
user.setBirthday(vo.getBirthday());
user.setEmail(vo.getEmail());
user.setTel(vo.getTel());
user.setPassword(vo.getPassword());
user.setIntro(vo.getIntro());
return user;
}
}