Subversion Repositories WebE

Rev

Rev 27 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 27 Rev 33
Line 3... Line 3...
3
import java.util.ArrayList;
3
import java.util.ArrayList;
4
import java.util.List;
4
import java.util.List;
5
5
6
import javax.servlet.http.HttpServletRequest;
6
import javax.servlet.http.HttpServletRequest;
7
7
8
import org.apache.struts2.ServletActionContext;
8
import org.apache.struts2.StrutsStatics;
9
9
-
 
10
import ch.ffhs.webE.dao.UserDAOImpl;
-
 
11
import ch.ffhs.webE.domain.User;
-
 
12
-
 
13
import com.opensymphony.xwork2.Action;
10
import com.opensymphony.xwork2.ActionContext;
14
import com.opensymphony.xwork2.ActionContext;
11
import com.opensymphony.xwork2.ActionSupport;
15
import com.opensymphony.xwork2.ActionSupport;
12
import com.opensymphony.xwork2.ModelDriven;
16
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
17
18
public class UserAction extends ActionSupport implements ModelDriven<User>
18
public class UserAction extends ActionSupport implements ModelDriven<User>
19
{
19
{
20
20
21
    private static final long serialVersionUID = -6659925652584240539L;
21
  private static final long serialVersionUID = 1L;
22
22
23
    private User user = new User();
23
  private User user = new User();
24
    private List<User> userList = new ArrayList<User>();
24
  private List<User> userList = new ArrayList<User>();
25
    private UserDAO userDAO = new UserDAOImpl();
25
  private final UserDAOImpl userDAO = new UserDAOImpl();
26
26
27
    @Override
27
  @Override
28
    public User getModel()
28
  public User getModel()
29
    {
29
  {
30
        return user;
30
    return this.user;
31
    }
31
  }
32
32
33
    /**
33
  /**
34
     * Executes the DB query to save the user
34
   * Executes the DB query to save the user
35
     *
35
   *
36
     * @return
36
   * @return
37
     */
37
   */
38
    public String addOrUpdate()
38
  public String addOrUpdate()
39
    {
39
  {
40
        userDAO.saveOrUpdateUser(user);
40
    this.userDAO.saveOrUpdateUser(this.user);
41
        return SUCCESS;
41
    return Action.SUCCESS;
42
    }
42
  }
43
43
44
    /**
44
  /**
45
     * DB query for userList
45
   * DB query for userList
46
     *
46
   *
47
     * @return SUCCESS
47
   * @return SUCCESS
48
     */
48
   */
49
    public String list()
49
  public String list()
50
    {
50
  {
51
        userList = userDAO.listUser();
51
    this.userList = this.userDAO.listUser();
52
        return SUCCESS;
52
    return Action.SUCCESS;
53
    }
53
  }
54
54
55
    public String edit()
55
  public String edit()
56
    {
56
  {
57
        int id = getIdParameter();
57
    int id = this.getIdParameter();
58
58
59
        if (id > 0)
59
    if (id > 0)
60
        {
60
    {
61
            user = userDAO.listUserById(id);
61
      this.user = this.userDAO.listUserById(id);
62
            return SUCCESS;
62
      return Action.SUCCESS;
63
        }
63
    }
64
        else
64
    else
65
        {
65
    {
66
            return ERROR;
66
      return Action.ERROR;
67
        }
67
    }
68
    }
68
  }
69
69
70
    /**
70
  /**
71
     * Gets the ID Parameter for update / delete requests
71
   * Gets the ID Parameter for update / delete requests
Line 73... Line 73...
73
     * @return int from the ID request. If not set or wrong, it gives back -1
73
   * @return int from the ID request. If not set or wrong, it gives back -1
74
     */
74
   */
75
    private int getIdParameter()
75
  private int getIdParameter()
76
    {
76
  {
77
        HttpServletRequest request = (HttpServletRequest) ActionContext
77
    HttpServletRequest request = (HttpServletRequest) ActionContext
78
                .getContext().get(ServletActionContext.HTTP_REQUEST);
78
        .getContext().get(StrutsStatics.HTTP_REQUEST);
79
79
80
        int id = -1;
80
    int id = -1;
81
        try
81
    try
82
        {
82
    {
83
            id = Integer.parseInt(request.getParameter("id"));
83
      id = Integer.parseInt(request.getParameter("id"));
Line 89... Line 89...
89
89
90
        return id;
90
    return id;
91
    }
91
  }
92
92
93
    /**
93
  /**
94
     * deletes a user, gets the ID from the "id" parameter that was submitted
94
   * deletes a user, gets the ID from the "id" parameter that was submitted with
95
     * with the HTTP request
95
   * the HTTP request
96
     *
96
   *
97
     * @return String - either SUCCESS or ERROR constant
97
   * @return String - either SUCCESS or ERROR constant
98
     */
98
   */
99
    public String delete()
99
  public String delete()
100
    {
100
  {
101
101
102
        int id = getIdParameter();
102
    int id = this.getIdParameter();
103
103
104
        // Check for malicious ID values
104
    // Check for malicious ID values
105
        if (id > 0)
105
    if (id > 0)
106
        {
106
    {
107
            userDAO.deleteUser(id);
107
      this.userDAO.deleteUser(id);
108
            return SUCCESS;
108
      return Action.SUCCESS;
109
        }
109
    }
110
        else
110
    else
111
        {
111
    {
112
            return ERROR;
112
      return Action.ERROR;
113
        }
113
    }
114
    }
114
  }
115
115
116
    /*
116
  /*
117
     * Standard getters and setters
117
   * Standard getters and setters
118
     */
118
   */
119
119
120
    public User getUser()
120
  public User getUser()
121
    {
121
  {
122
        return user;
122
    return this.user;
123
    }
123
  }
124
124
125
    public void setUser(User user)
125
  public void setUser(User user)
126
    {
126
  {
127
        this.user = user;
127
    this.user = user;
128
    }
128
  }
129
129
130
    public List<User> getUserList()
130
  public List<User> getUserList()
131
    {
131
  {
132
        return userList;
132
    return this.userList;
133
    }
133
  }
134
134
135
    public void setUserList(List<User> userList)
135
  public void setUserList(List<User> userList)
136
    {
136
  {
137
        this.userList = userList;
137
    this.userList = userList;