package ch.ffhs.webE.action; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import org.apache.struts2.StrutsStatics; import ch.ffhs.webE.dao.UserDAO; import ch.ffhs.webE.domain.User; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class UserAction extends ActionSupport implements ModelDriven { private static final long serialVersionUID = 1L; private User user = new User(); private List userList = new ArrayList(); private final UserDAO userDAO = new UserDAO(); public boolean edit = false; public boolean added = false; public User savedUser; @Override public User getModel() { return this.user; } /** * DB query for userList * * @return SUCCESS */ public String list() { this.userList = this.userDAO.getList(); return Action.SUCCESS; } /** * Executes the DB query to save the user * * @return */ public String save() { this.userDAO.saveOrUpdate(this.user); this.savedUser = this.user; this.user = null; this.list(); return Action.SUCCESS; } public String edit() { int id = this.getIdParameter(); String result = Action.ERROR; if (id > 0) { this.user = this.userDAO.getById(id); this.edit = true; result = Action.SUCCESS; } this.list(); return result; } /** * Gets the ID Parameter for update / delete requests * * @return int from the ID request. If not set or wrong, it gives back -1 */ private int getIdParameter() { HttpServletRequest request = (HttpServletRequest) ActionContext .getContext().get(StrutsStatics.HTTP_REQUEST); int id = -1; try { id = Integer.parseInt(request.getParameter("id")); } catch (Exception e) { // TODO: Logging - wrong parameter set } return id; } /** * deletes a user, gets the ID from the "id" parameter that was submitted with * the HTTP request * * @return String - either SUCCESS or ERROR constant */ public String delete() { int id = this.getIdParameter(); // Check for malicious ID values if (id > 0) { this.userDAO.delete(id); return Action.SUCCESS; } else { return Action.ERROR; } } /* * Standard getters and setters */ public User getUser() { return this.user; } public void setUser(User user) { this.user = user; } public List getUserList() { return this.userList; } public void setUserList(List userList) { this.userList = userList; } }