package com.stuman.web.struts.action;import java.io.UnsupportedEncodingException;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;import org.apache.commons.beanutils.BeanUtilsBean;
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.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.validator.DynaValidatorForm;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;import com.stuman.dao.hibernate.HibernateUtil;
import com.stuman.domain.Admin;
import com.stuman.domain.Student;
import com.stuman.domain.Teacher;
import com.stuman.web.struts.form.LoginForm;public class LoginAction extends Action {
private BeanUtilsBean beanUtilsBean; public LoginAction() {
this.beanUtilsBean = new BeanUtilsBean();
}   /**
 * Method execute
 * 
 * @param mapping
 * @param form
 * @param request
 * @param response
 * @return ActionForward
 * @throws UnsupportedEncodingException
 */
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws UnsupportedEncodingException { // TODO Auto-generated method stub HttpSession session = request.getSession();
Session s = HibernateUtil.currentSession();
LoginForm loginForm = new LoginForm();
DynaValidatorForm dform = (DynaValidatorForm) form;
try {
this.getBeanUtilsBean().copyProperties(loginForm, dform);
} catch (IllegalAccessException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} catch (InvocationTargetException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
} String sort = loginForm.getSort();
String username = loginForm.getUsername();

String password = loginForm.getPassword();
int loginSort = Integer.parseInt(sort);
String[] userlist = new String[2];
userlist[0] = username;
userlist[1] = password;
List list = new ArrayList();
try {

HibernateUtil.beginTransaction();
String str = new String();
switch (loginSort) {
case 1:
str = " from Student as stu where stu.name=:stuName and stu.password=:stuPassword";
Query query = s.createQuery(str);
 System.out.println(username + " " + password);
query.setString("stuName", username);
query.setString("stuPassword", password);
list = query.list(); if (list.size() > 0) {
request.getSession().setAttribute("stuid",
((Student) list.get(0)).getId());
request.getSession().setAttribute("deptment",
((Student) list.get(0)).getDepartment());
s.close();
return mapping.findForward("studentLoginsuccess");
} else

break;
case 2:
str = " from Teacher tea where tea.name =? and tea.password ='"
+ password + "'"; Query query2 = s.createQuery(str);
// System.out.println(username + " " + password);
query2.setString(0, username);
list = query2.list();
if (list.size() > 0) {
request.getSession().setAttribute("teaid",
((Teacher) list.get(0)).getId());
request.getSession().setAttribute("teacherId",
((Teacher) list.get(0)).getId());
s.close();
return mapping.findForward("teacherLoginsuccess");
} else
break;
case 3:
str = " from Admin admin where admin.name = ? and admin.password ='"
+ password + "'";
// System.out.println(username + " " + password);
Query query3 = s.createQuery(str);
query3.setString(0, username);
list = query3.list();
if (list.size() > 0) {
request.getSession().setAttribute("id",
((Admin) list.get(0)).getId());
s.close();
return mapping.findForward("adminLoginsuccess");
} else
break;
default:
break;
}
} catch (HibernateException e) {
e.printStackTrace();
} finally {
s.close();
}
ActionMessages errors = new ActionMessages();
errors.add("login error", new ActionMessage("login.error"));
saveErrors(request, errors);
return mapping.getInputForward(); } public BeanUtilsBean getBeanUtilsBean() {
return beanUtilsBean;
}
}