Subversion Repositories WebE

Rev

Rev 33 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 33 Rev 37
1
package ch.ffhs.webE.action;
1
package ch.ffhs.webE.action;
2
2
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.StrutsStatics;
8
import org.apache.struts2.StrutsStatics;
9
9
10
import ch.ffhs.webE.dao.UserDAOImpl;
10
import ch.ffhs.webE.dao.UserDAO;
11
import ch.ffhs.webE.domain.User;
11
import ch.ffhs.webE.domain.User;
12
12
13
import com.opensymphony.xwork2.Action;
13
import com.opensymphony.xwork2.Action;
14
import com.opensymphony.xwork2.ActionContext;
14
import com.opensymphony.xwork2.ActionContext;
15
import com.opensymphony.xwork2.ActionSupport;
15
import com.opensymphony.xwork2.ActionSupport;
16
import com.opensymphony.xwork2.ModelDriven;
16
import com.opensymphony.xwork2.ModelDriven;
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 = 1L;
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 final UserDAOImpl userDAO = new UserDAOImpl();
25
  private final UserDAO userDAO = new UserDAO();
-
 
26
-
 
27
  public boolean edit = false;
-
 
28
  public boolean added = false;
-
 
29
  public User savedUser;
26
30
27
  @Override
31
  @Override
28
  public User getModel()
32
  public User getModel()
29
  {
33
  {
30
    return this.user;
34
    return this.user;
31
  }
35
  }
32
36
33
  /**
37
  /**
34
   * Executes the DB query to save the user
38
   * DB query for userList
35
   *
39
   *
36
   * @return
40
   * @return SUCCESS
37
   */
41
   */
38
  public String addOrUpdate()
42
  public String list()
39
  {
43
  {
40
    this.userDAO.saveOrUpdateUser(this.user);
44
    this.userList = this.userDAO.getList();
41
    return Action.SUCCESS;
45
    return Action.SUCCESS;
42
  }
46
  }
43
47
44
  /**
48
  /**
45
   * DB query for userList
49
   * Executes the DB query to save the user
46
   *
50
   *
47
   * @return SUCCESS
51
   * @return
48
   */
52
   */
49
  public String list()
53
  public String save()
50
  {
54
  {
51
    this.userList = this.userDAO.listUser();
55
    this.userDAO.saveOrUpdate(this.user);
-
 
56
    this.savedUser = this.user;
-
 
57
    this.user = null;
-
 
58
-
 
59
    this.list();
-
 
60
52
    return Action.SUCCESS;
61
    return Action.SUCCESS;
53
  }
62
  }
54
63
55
  public String edit()
64
  public String edit()
56
  {
65
  {
57
    int id = this.getIdParameter();
66
    int id = this.getIdParameter();
58
67
-
 
68
    String result = Action.ERROR;
59
    if (id > 0)
69
    if (id > 0)
60
    {
70
    {
61
      this.user = this.userDAO.listUserById(id);
71
      this.user = this.userDAO.getById(id);
62
      return Action.SUCCESS;
72
      this.edit = true;
63
    }
-
 
64
    else
-
 
65
    {
-
 
66
      return Action.ERROR;
73
      result = Action.SUCCESS;
67
    }
74
    }
-
 
75
-
 
76
    this.list();
-
 
77
-
 
78
    return result;
68
  }
79
  }
69
80
70
  /**
81
  /**
71
   * Gets the ID Parameter for update / delete requests
82
   * Gets the ID Parameter for update / delete requests
72
   *
83
   *
73
   * @return int from the ID request. If not set or wrong, it gives back -1
84
   * @return int from the ID request. If not set or wrong, it gives back -1
74
   */
85
   */
75
  private int getIdParameter()
86
  private int getIdParameter()
76
  {
87
  {
77
    HttpServletRequest request = (HttpServletRequest) ActionContext
88
    HttpServletRequest request = (HttpServletRequest) ActionContext
78
        .getContext().get(StrutsStatics.HTTP_REQUEST);
89
        .getContext().get(StrutsStatics.HTTP_REQUEST);
79
90
80
    int id = -1;
91
    int id = -1;
81
    try
92
    try
82
    {
93
    {
83
      id = Integer.parseInt(request.getParameter("id"));
94
      id = Integer.parseInt(request.getParameter("id"));
84
    }
95
    }
85
    catch (Exception e)
96
    catch (Exception e)
86
    {
97
    {
87
      // TODO: Logging - wrong parameter set
98
      // TODO: Logging - wrong parameter set
88
    }
99
    }
89
100
90
    return id;
101
    return id;
91
  }
102
  }
92
103
93
  /**
104
  /**
94
   * deletes a user, gets the ID from the "id" parameter that was submitted with
105
   * deletes a user, gets the ID from the "id" parameter that was submitted with
95
   * the HTTP request
106
   * the HTTP request
96
   *
107
   *
97
   * @return String - either SUCCESS or ERROR constant
108
   * @return String - either SUCCESS or ERROR constant
98
   */
109
   */
99
  public String delete()
110
  public String delete()
100
  {
111
  {
101
112
102
    int id = this.getIdParameter();
113
    int id = this.getIdParameter();
103
114
104
    // Check for malicious ID values
115
    // Check for malicious ID values
105
    if (id > 0)
116
    if (id > 0)
106
    {
117
    {
107
      this.userDAO.deleteUser(id);
118
      this.userDAO.delete(id);
108
      return Action.SUCCESS;
119
      return Action.SUCCESS;
109
    }
120
    }
110
    else
121
    else
111
    {
122
    {
112
      return Action.ERROR;
123
      return Action.ERROR;
113
    }
124
    }
114
  }
125
  }
115
126
116
  /*
127
  /*
117
   * Standard getters and setters
128
   * Standard getters and setters
118
   */
129
   */
119
130
120
  public User getUser()
131
  public User getUser()
121
  {
132
  {
122
    return this.user;
133
    return this.user;
123
  }
134
  }
124
135
125
  public void setUser(User user)
136
  public void setUser(User user)
126
  {
137
  {
127
    this.user = user;
138
    this.user = user;
128
  }
139
  }
129
140
130
  public List<User> getUserList()
141
  public List<User> getUserList()
131
  {
142
  {
132
    return this.userList;
143
    return this.userList;
133
  }
144
  }
134
145
135
  public void setUserList(List<User> userList)
146
  public void setUserList(List<User> userList)
136
  {
147
  {
137
    this.userList = userList;
148
    this.userList = userList;
138
  }
149
  }
139
}
150
}
140
 
151