Rev 22 | Rev 27 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
20 | moos | 1 | package ch.ffhs.webE.action; |
18 | moos | 2 | |
3 | import java.util.ArrayList; |
||
4 | import java.util.List; |
||
5 | |||
22 | moos | 6 | import javax.servlet.http.HttpServletRequest; |
7 | |||
8 | import org.apache.struts2.ServletActionContext; |
||
9 | |||
10 | import com.opensymphony.xwork2.ActionContext; |
||
18 | moos | 11 | import com.opensymphony.xwork2.ActionSupport; |
12 | import com.opensymphony.xwork2.ModelDriven; |
||
13 | import ch.ffhs.webE.dao.UserDAO; |
||
14 | import ch.ffhs.webE.dao.UserDAOImpl; |
||
15 | |||
16 | import ch.ffhs.webE.domain.User; |
||
17 | |||
26 | moos | 18 | public class UserAction extends ActionSupport implements ModelDriven<User> |
19 | { |
||
18 | moos | 20 | |
26 | moos | 21 | private static final long serialVersionUID = -6659925652584240539L; |
18 | moos | 22 | |
26 | moos | 23 | private User user = new User(); |
24 | private List<User> userList = new ArrayList<User>(); |
||
25 | private UserDAO userDAO = new UserDAOImpl(); |
||
22 | moos | 26 | |
26 | moos | 27 | @Override |
28 | public User getModel() |
||
29 | { |
||
30 | return user; |
||
31 | } |
||
22 | moos | 32 | |
26 | moos | 33 | /** |
34 | * Executes the DB query to save the user |
||
35 | * |
||
36 | * @return |
||
37 | */ |
||
38 | public String add() |
||
39 | { |
||
40 | userDAO.saveUser(user); |
||
41 | return SUCCESS; |
||
42 | } |
||
22 | moos | 43 | |
26 | moos | 44 | /** |
45 | * DB query for userList |
||
46 | * |
||
47 | * @return SUCCESS |
||
48 | */ |
||
49 | public String list() |
||
50 | { |
||
51 | userList = userDAO.listUser(); |
||
52 | return SUCCESS; |
||
53 | } |
||
22 | moos | 54 | |
26 | moos | 55 | /** |
56 | * deletes a user, gets the ID from the "id" parameter that was submitted |
||
57 | * with the HTTP request |
||
58 | * |
||
59 | * @return String - either SUCCESS or ERROR constant |
||
60 | */ |
||
61 | public String delete() |
||
62 | { |
||
63 | HttpServletRequest request = (HttpServletRequest) ActionContext |
||
64 | .getContext().get(ServletActionContext.HTTP_REQUEST); |
||
18 | moos | 65 | |
26 | moos | 66 | int id = 0; |
67 | |||
68 | try |
||
69 | { |
||
70 | id = Integer.parseInt(request.getParameter("id")); |
||
71 | } |
||
72 | catch (Exception e) |
||
73 | { |
||
74 | return ERROR; |
||
75 | } |
||
18 | moos | 76 | |
26 | moos | 77 | // Check for malicious ID values |
78 | if (id > 0) |
||
79 | { |
||
80 | userDAO.deleteUser(id); |
||
81 | return SUCCESS; |
||
82 | } |
||
83 | else |
||
84 | { |
||
85 | return ERROR; |
||
86 | } |
||
87 | } |
||
18 | moos | 88 | |
26 | moos | 89 | /* |
90 | * Standard getters and setters |
||
91 | */ |
||
18 | moos | 92 | |
26 | moos | 93 | public User getUser() |
94 | { |
||
95 | return user; |
||
96 | } |
||
22 | moos | 97 | |
26 | moos | 98 | public void setUser(User user) |
99 | { |
||
100 | this.user = user; |
||
101 | } |
||
102 | |||
103 | public List<User> getUserList() |
||
104 | { |
||
105 | return userList; |
||
106 | } |
||
107 | |||
108 | public void setUserList(List<User> userList) |
||
109 | { |
||
110 | this.userList = userList; |
||
111 | } |
||
18 | moos | 112 | } |