?? userhandler.java
字號:
/* */package com.sun.j2ee.workflow.control.actions;import java.util.HashMap;import java.util.ArrayList;import java.util.Locale;import java.util.Enumeration;import javax.servlet.http.HttpSession;import com.sun.j2ee.workflow.util.WebKeys;import com.sun.j2ee.workflow.util.JSPUtil;import javax.servlet.http.HttpServletRequest;import com.sun.j2ee.workflow.util.Debug;import com.sun.j2ee.workflow.control.exceptions.WorkflowActionException;import com.sun.j2ee.workflow.control.exceptions.WorkflowAppException;import com.sun.j2ee.workflow.control.exceptions.DuplicateNameException;import com.sun.j2ee.workflow.control.exceptions.SigninFailedException;import com.sun.j2ee.workflow.control.exceptions.MissingFormDataException;import com.sun.j2ee.workflow.user.model.UserModel;import com.sun.j2ee.workflow.user.dao.UserDAOImpl;import com.sun.j2ee.workflow.user.exceptions.UserDAOSysException;import com.sun.j2ee.workflow.user.exceptions.UserDAOAppException;import com.sun.j2ee.workflow.user.exceptions.UserDAODBUpdateException;import com.sun.j2ee.workflow.user.exceptions.UserDAOFinderException;import com.sun.j2ee.workflow.user.exceptions.UserDAODupKeyException;/** UserHandler * @author Jian (James) Cai */public class UserHandler extends ActionHandlerSupport { private static final int CREATE_USER = 1; private static final int UPDATE_USER = 2; private static final int REMOVE_USER = 3; private static final int UNKNOWN = 0; public void perform(HashMap userevent) throws WorkflowActionException, DuplicateNameException, MissingFormDataException{ Debug.println("UserHandler: perform()"); //session of the request are passed as partof the event HttpSession session = (HttpSession)userevent.get("session"); //Locale currentLocale = JSPUtil.getLocale(session); switch (this.mapactiontype((String)userevent.get("actiontype"))) { case CREATE_USER: { Debug.println("UserHandler: CREATE_USER event"); try { if(validateInfo(userevent)) { UserDAOImpl userDa = new UserDAOImpl(); UserModel userModel = getUserModel(userevent); userDa.create(userModel); } else { } } catch (UserDAOSysException use) { throw new WorkflowActionException(" Error Create User " + use); } catch (UserDAODupKeyException pde) { throw new DuplicateNameException(" Error Create User, Porject with same name exists" + pde); } catch (UserDAODBUpdateException pue) { throw new WorkflowActionException(" Error Create User" + pue); } catch (UserDAOAppException pue) { throw new WorkflowActionException(" Error Create User" + pue); } } break; case UPDATE_USER: { Debug.println("UserHandler: UPDATE_USER event"); try { if(validateInfo(userevent)) { UserDAOImpl userDa = new UserDAOImpl(); userDa.store(getUserModel(userevent)); } Debug.println("UserHandler: updating user " + (String)userevent.get("user_name")); } catch (UserDAOSysException use) { throw new WorkflowActionException(" Error Update User " + use); } catch (UserDAODupKeyException pde) { throw new WorkflowActionException(" Error Update User " + pde); } catch (UserDAODBUpdateException pue) { throw new WorkflowActionException(" Error Update User" + pue); } catch (UserDAOAppException pue) { throw new WorkflowActionException(" Error Create User" + pue); } } break; case REMOVE_USER: { Debug.println("UserHandler: REMOVE_USER event"); try { UserDAOImpl userDa = new UserDAOImpl(); userDa.remove((String)userevent.get("user_ID")); Debug.println("UserHandler: removing user " + (String)userevent.get("user_ID")); //should add methods to remove the rows in other table } catch (UserDAOSysException use) { throw new WorkflowActionException(" Error Remove User " + use); } catch (UserDAODBUpdateException pue) { throw new WorkflowActionException(" Error Remove User" + pue); } } break; default: Debug.print("Error: not implemented yet"); break; } } private int mapactiontype(String actiontype) { Debug.println("inside mapactiontype: "+ actiontype); if (actiontype.equals("CREATE_USER")) return this.CREATE_USER; else if (actiontype.equals("UPDATE_USER")) return this.UPDATE_USER; else if (actiontype.equals("REMOVE_USER")) return this.REMOVE_USER; else return this.UNKNOWN; } private UserModel getUserModel(HashMap pe){ UserModel pjm = new UserModel((String)pe.get("user_ID"), (String)pe.get("password"), (String)pe.get("f_name"), (String)pe.get("l_name"), (String)pe.get("location"), (String)pe.get("phone"), (String)pe.get("email"), (String)pe.get("title"), (String)pe.get("role")); return pjm; } private boolean validateInfo(HashMap userevent) throws MissingFormDataException{ ArrayList missingFields = null; Debug.println("validate use information: "); String userId = ((String)userevent.get("user_ID")).trim(); //if (currentLocale.equals(Locale.JAPAN)) userId = JSPUtil.convertJISEncoding(userId); if (userId.equals("")) { if (missingFields == null) { missingFields = new ArrayList(); } missingFields.add("User ID"); } String password = ((String)userevent.get("password")).trim(); //if (currentLocale.equals(Locale.JAPAN)) password = JSPUtil.convertJISEncoding(password); if (password.equals("")) { if (missingFields == null) { missingFields = new ArrayList(); } missingFields.add("Password"); } String email = ((String)userevent.get("email")).trim(); //if (currentLocale.equals(Locale.JAPAN)) email = JSPUtil.convertJISEncoding(email); if (email.equals("")) { if (missingFields == null) { missingFields = new ArrayList(); } missingFields.add("E-Mail Address"); } String title = ((String)userevent.get("title")).trim(); //if (currentLocale.equals(Locale.JAPAN)) email = JSPUtil.convertJISEncoding(email); if (email.equals("")) { if (missingFields == null) { missingFields = new ArrayList(); } missingFields.add("Title"); } String role = ((String)userevent.get("role")).trim(); //if (currentLocale.equals(Locale.JAPAN)) email = JSPUtil.convertJISEncoding(email); if (email.equals("")) { if (missingFields == null) { missingFields = new ArrayList(); } missingFields.add("Role"); } //capture the missing fields if (missingFields != null) { MissingFormDataException ex = new MissingFormDataException("Missing UserInformation", missingFields); request.setAttribute(WebKeys.MissingFormDataKey, ex); throw ex; } return true; } }
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -