/*
 * Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package cn.jack.struts;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

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 cn.jack.hibernate.Manager;
import cn.jack.service.LoginsDao;
import cn.jack.tool.IConstants;
import cn.jack.tool.MD5;


/** 
 * MyEclipse Struts
 * Creation date: 05-14-2007
 * 
 * XDoclet definition:
 * @struts.action path="/logins" name="loginsForm" input="/WEB-INF/JSP/logins.jsp" scope="request"
 */
public class LoginsAction extends Action {
	private LoginsDao ldao;
	MD5 md5 = new MD5();
	/*
	 * Generated Methods
	 */

	/** 
	 * Method execute
	 * @param mapping
	 * @param form
	 * @param request
	 * @param response
	 * @return ActionForward
	 * @throws IOException 
	 * @throws ServletException 
	 */
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String action=request.getParameter("action");
		if(action.equals("login")){return login(mapping,form,request,response);}
		if(action.equals("adduser")){return adduser(mapping,form,request,response);}
		if(action.equals("loginoff")){return loginoff(mapping,form,request,response);}
		if(action.equals("editpass")){return editpass(mapping,form,request,response);}
		if(action.equals("editpa")){return editpa(mapping,form,request,response);}
		
		
		return null;
	}
	public ActionForward login(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginsForm lf = (LoginsForm) form;// TODO Auto-generated method stub
		Manager mr =new Manager();
		HttpSession session =request.getSession();
		mr.setUsername(lf.getUsername().trim());
		mr.setUserpass(md5.getMD5ofStr(lf.getUserpass().trim()));
		if(ldao.login(mr)){
			session.setAttribute("manager", IConstants.SESSION_KEY);
			session.setAttribute("users", lf.getUsername());
			return mapping.findForward(IConstants.MANAGER_KEY);
		}
		request.getRequestDispatcher("/login.do").forward(request, response);
		return null;
	}
	public ActionForward adduser(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginsForm lf = (LoginsForm) form;// TODO Auto-generated method stub
		Manager mr = new Manager();
		if(lf.getUsername().trim().equals("") && lf.getUserpass().trim().equals("")){
			request.getRequestDispatcher("/adduser.do").forward(request, response);
			return null;
		}
		if(!lf.getUserpasa().trim().equals(lf.getUserpass().trim())){
			request.getRequestDispatcher("/adduser.do").forward(request, response);
			return null;
		}
		String username=lf.getUsername();
		if(ldao.exite(username)){
			request.getRequestDispatcher("/adduser.do").forward(request, response);
			return null;
		}
		mr.setUsername(lf.getUsername().trim());
		mr.setUserpass(md5.getMD5ofStr(lf.getUserpass().trim()));
		ldao.adduser(mr);
		return mapping.findForward(IConstants.MANAGER_KEY);
	}
	public ActionForward loginoff(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session =request.getSession();
		if(session!=null){
			session.invalidate();
		}
		return mapping.findForward(IConstants.INDEX_KEY);
	}
	public ActionForward editpa(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginsForm lf = (LoginsForm) form;// TODO Auto-generated method stub		
		HttpSession session =request.getSession();
		if(session.getAttribute("manager")==null &&session.getAttribute("users")==null){
			request.getRequestDispatcher("/login.do").forward(request, response);
			return null;
		}
		String username=(String) session.getAttribute("users");
		List list = ldao.userid(username);
		request.setAttribute("users", list);
		return mapping.findForward("editpa");
	}
	public ActionForward editpass(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		LoginsForm lf = (LoginsForm) form;// TODO Auto-generated method stub		
		if(lf.getUserpass().trim().equals("") && lf.getUserpasa().trim().equals("")){
			request.getRequestDispatcher("/editpass.do").forward(request, response);
			return null;
		}
		if(!lf.getUserpasa().trim().equals(lf.getUserpass().trim())){
			request.getRequestDispatcher("/editpass.do").forward(request, response);
			return null;
		}
		HttpSession session =request.getSession();
		if(session.getAttribute("manager")==null ||! session.getAttribute("manager").equals(IConstants.SESSION_KEY)){
			request.getRequestDispatcher("/login.do").forward(request, response);
			return null;
		}
		String userpass=md5.getMD5ofStr(lf.getUserpass());
		String id=lf.getId();
		ldao.editpass(userpass,id);
		return mapping.findForward(IConstants.INDEX_KEY);
	}

	public LoginsDao getLdao() {
		return ldao;
	}

	public void setLdao(LoginsDao ldao) {
		this.ldao = ldao;
	}
}