Subversion Repositories WebE

Rev

Rev 27 | Go to most recent revision | Show entire file | Ignore 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
-
 
23
    private User user = new User();
-
 
24
    private List<User> userList = new ArrayList<User>();
-
 
25
    private UserDAO userDAO = new UserDAOImpl();
-
 
26
-
 
27
    @Override
-
 
28
    public User getModel()
-
 
29
    {
-
 
30
        return user;
-
 
31
    }
-
 
32
-
 
33
    /**
-
 
34
     * Executes the DB query to save the user
-
 
35
     *
-
 
36
     * @return
-
 
37
     */
-
 
38
    public String addOrUpdate()
-
 
39
    {
-
 
40
        userDAO.saveOrUpdateUser(user);
-
 
41
        return SUCCESS;
-
 
42
    }
-
 
43
-
 
44
    /**
-
 
45
     * DB query for userList
-
 
46
     *
-
 
47
     * @return SUCCESS
-
 
48
     */
-
 
49
    public String list()
-
 
50
    {
-
 
51
        userList = userDAO.listUser();
-
 
52
        return SUCCESS;
-
 
53
    }
-
 
54
-
 
55
    public String edit()
-
 
56
    {
-
 
57
        int id = getIdParameter();
-
 
58
-
 
59
        if (id > 0)
-
 
60
        {
-
 
61
            user = userDAO.listUserById(id);
-
 
62
            return SUCCESS;
-
 
63
        }
-
 
64
        else
-
 
65
        {
-
 
66
            return ERROR;
-
 
67
        }
-
 
68
    }
-
 
69
-
 
70
    /**
-
 
71
     * Gets the ID Parameter for update / delete requests
-
 
72
     *
-
 
73
     * @return int from the ID request. If not set or wrong, it gives back -1
-
 
74
     */
-
 
75
    private int getIdParameter()
-
 
76
    {
-
 
77
        HttpServletRequest request = (HttpServletRequest) ActionContext
-
 
78
                .getContext().get(ServletActionContext.HTTP_REQUEST);
-
 
79
-
 
80
        int id = -1;
-
 
81
        try
-
 
82
        {
-
 
83
            id = Integer.parseInt(request.getParameter("id"));
-
 
84
        }
-
 
85
        catch (Exception e)
-
 
86
        {
-
 
87
            // TODO: Logging - wrong parameter set
-
 
88
        }
-
 
89
-
 
90
        return id;
-
 
91
    }
-
 
92
-
 
93
    /**
-
 
94
     * deletes a user, gets the ID from the "id" parameter that was submitted
-
 
95
     * with the HTTP request
-
 
96
     *
-
 
97
     * @return String - either SUCCESS or ERROR constant
-
 
98
     */
-
 
99
    public String delete()
-
 
100
    {
-
 
101
22
-
 
23
  private User user = new User();
-
 
24
  private List<User> userList = new ArrayList<User>();
-
 
25
  private final UserDAOImpl userDAO = new UserDAOImpl();
-
 
26
-
 
27
  @Override
-
 
28
  public User getModel()
-
 
29
  {
-
 
30
    return this.user;
-
 
31
  }
-
 
32
-
 
33
  /**
-
 
34
   * Executes the DB query to save the user
-
 
35
   *
-
 
36
   * @return
-
 
37
   */
-
 
38
  public String addOrUpdate()
-
 
39
  {
-
 
40
    this.userDAO.saveOrUpdateUser(this.user);
-
 
41
    return Action.SUCCESS;
-
 
42
  }
-
 
43
-
 
44
  /**
-
 
45
   * DB query for userList
-
 
46
   *
-
 
47
   * @return SUCCESS
-
 
48
   */
-
 
49
  public String list()
-
 
50
  {
-
 
51
    this.userList = this.userDAO.listUser();
-
 
52
    return Action.SUCCESS;
-
 
53
  }
-
 
54
-
 
55
  public String edit()
-
 
56
  {
-
 
57
    int id = this.getIdParameter();
-
 
58
-
 
59
    if (id > 0)
-
 
60
    {
-
 
61
      this.user = this.userDAO.listUserById(id);
-
 
62
      return Action.SUCCESS;
-
 
63
    }
-
 
64
    else
-
 
65
    {
-
 
66
      return Action.ERROR;
-
 
67
    }
-
 
68
  }
-
 
69
-
 
70
  /**
-
 
71
   * Gets the ID Parameter for update / delete requests
-
 
72
   *
-
 
73
   * @return int from the ID request. If not set or wrong, it gives back -1
-
 
74
   */
-
 
75
  private int getIdParameter()
-
 
76
  {
-
 
77
    HttpServletRequest request = (HttpServletRequest) ActionContext
-
 
78
        .getContext().get(StrutsStatics.HTTP_REQUEST);
-
 
79
-
 
80
    int id = -1;
-
 
81
    try
-
 
82
    {
-
 
83
      id = Integer.parseInt(request.getParameter("id"));
-
 
84
    }
-
 
85
    catch (Exception e)
-
 
86
    {
-
 
87
      // TODO: Logging - wrong parameter set
-
 
88
    }
-
 
89
-
 
90
    return id;
-
 
91
  }
-
 
92
-
 
93
  /**
-
 
94
   * deletes a user, gets the ID from the "id" parameter that was submitted with
-
 
95
   * the HTTP request
-
 
96
   *
-
 
97
   * @return String - either SUCCESS or ERROR constant
-
 
98
   */
-
 
99
  public String delete()
-
 
100
  {
-
 
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;
138
    }
138
  }
139
}
139
}