Subversion Repositories WebE

Rev

Rev 26 | Rev 33 | 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
     */
27 moos 38
    public String addOrUpdate()
26 moos 39
    {
27 moos 40
        userDAO.saveOrUpdateUser(user);
26 moos 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
 
27 moos 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
 
26 moos 70
    /**
27 moos 71
     * Gets the ID Parameter for update / delete requests
26 moos 72
     *
27 moos 73
     * @return int from the ID request. If not set or wrong, it gives back -1
26 moos 74
     */
27 moos 75
    private int getIdParameter()
26 moos 76
    {
77
        HttpServletRequest request = (HttpServletRequest) ActionContext
78
                .getContext().get(ServletActionContext.HTTP_REQUEST);
18 moos 79
 
27 moos 80
        int id = -1;
26 moos 81
        try
82
        {
83
            id = Integer.parseInt(request.getParameter("id"));
84
        }
85
        catch (Exception e)
86
        {
27 moos 87
            // TODO: Logging - wrong parameter set
26 moos 88
        }
18 moos 89
 
27 moos 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
 
26 moos 104
        // Check for malicious ID values
105
        if (id > 0)
106
        {
107
            userDAO.deleteUser(id);
108
            return SUCCESS;
109
        }
110
        else
111
        {
112
            return ERROR;
113
        }
114
    }
18 moos 115
 
26 moos 116
    /*
117
     * Standard getters and setters
118
     */
18 moos 119
 
26 moos 120
    public User getUser()
121
    {
122
        return user;
123
    }
22 moos 124
 
26 moos 125
    public void setUser(User user)
126
    {
127
        this.user = user;
128
    }
129
 
130
    public List<User> getUserList()
131
    {
132
        return userList;
133
    }
134
 
135
    public void setUserList(List<User> userList)
136
    {
137
        this.userList = userList;
138
    }
18 moos 139
}