Subversion Repositories WebE

Rev

Rev 26 | Rev 33 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 26 Rev 27
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.ServletActionContext;
8
import org.apache.struts2.ServletActionContext;
9
9
10
import com.opensymphony.xwork2.ActionContext;
10
import com.opensymphony.xwork2.ActionContext;
11
import com.opensymphony.xwork2.ActionSupport;
11
import com.opensymphony.xwork2.ActionSupport;
12
import com.opensymphony.xwork2.ModelDriven;
12
import com.opensymphony.xwork2.ModelDriven;
13
import ch.ffhs.webE.dao.UserDAO;
13
import ch.ffhs.webE.dao.UserDAO;
14
import ch.ffhs.webE.dao.UserDAOImpl;
14
import ch.ffhs.webE.dao.UserDAOImpl;
15
15
16
import ch.ffhs.webE.domain.User;
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 = -6659925652584240539L;
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 UserDAO userDAO = new UserDAOImpl();
26
26
27
    @Override
27
    @Override
28
    public User getModel()
28
    public User getModel()
29
    {
29
    {
30
        return user;
30
        return 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 add()
38
    public String addOrUpdate()
39
    {
39
    {
40
        userDAO.saveUser(user);
40
        userDAO.saveOrUpdateUser(user);
41
        return SUCCESS;
41
        return 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
        userList = userDAO.listUser();
52
        return SUCCESS;
52
        return SUCCESS;
53
    }
53
    }
54
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
55
    /**
70
    /**
56
     * deletes a user, gets the ID from the "id" parameter that was submitted
71
     * Gets the ID Parameter for update / delete requests
57
     * with the HTTP request
-
 
58
     *
72
     *
59
     * @return String - either SUCCESS or ERROR constant
73
     * @return int from the ID request. If not set or wrong, it gives back -1
60
     */
74
     */
61
    public String delete()
75
    private int getIdParameter()
62
    {
76
    {
63
        HttpServletRequest request = (HttpServletRequest) ActionContext
77
        HttpServletRequest request = (HttpServletRequest) ActionContext
64
                .getContext().get(ServletActionContext.HTTP_REQUEST);
78
                .getContext().get(ServletActionContext.HTTP_REQUEST);
65
79
66
        int id = 0;
80
        int id = -1;
67
       
-
 
68
        try
81
        try
69
        {
82
        {
70
            id = Integer.parseInt(request.getParameter("id"));
83
            id = Integer.parseInt(request.getParameter("id"));
71
        }
84
        }
72
        catch (Exception e)
85
        catch (Exception e)
73
        {
86
        {
74
            return ERROR;
87
            // TODO: Logging - wrong parameter set
75
        }
88
        }
76
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
-
 
102
        int id = getIdParameter();
-
 
103
77
        // Check for malicious ID values
104
        // Check for malicious ID values
78
        if (id > 0)
105
        if (id > 0)
79
        {
106
        {
80
            userDAO.deleteUser(id);
107
            userDAO.deleteUser(id);
81
            return SUCCESS;
108
            return SUCCESS;
82
        }
109
        }
83
        else
110
        else
84
        {
111
        {
85
            return ERROR;
112
            return ERROR;
86
        }
113
        }
87
    }
114
    }
88
115
89
    /*
116
    /*
90
     * Standard getters and setters
117
     * Standard getters and setters
91
     */
118
     */
92
119
93
    public User getUser()
120
    public User getUser()
94
    {
121
    {
95
        return user;
122
        return user;
96
    }
123
    }
97
124
98
    public void setUser(User user)
125
    public void setUser(User user)
99
    {
126
    {
100
        this.user = user;
127
        this.user = user;
101
    }
128
    }
102
129
103
    public List<User> getUserList()
130
    public List<User> getUserList()
104
    {
131
    {
105
        return userList;
132
        return userList;
106
    }
133
    }
107
134
108
    public void setUserList(List<User> userList)
135
    public void setUserList(List<User> userList)
109
    {
136
    {
110
        this.userList = userList;
137
        this.userList = userList;
111
    }
138
    }
112
}
139
}
113
 
140