implemented administration of relationship type (edit and delete are not yet working)
| /trunk/src/struts.xml |
|---|
| 14,8 → 14,6 |
| <!-- Admin environment --> |
| <package name="admin" namespace="/admin" extends="hibernate-default"> |
| <!-- User management --> |
| <action name="userAddForm"> |
| <result>/admin/userAddForm.jsp</result> |
| </action> |
| 31,25 → 29,6 |
| <action name="deleteUser" method="delete" class="ch.ffhs.webE.action.UserAction"> |
| <result name="success" type="redirect">/admin/userList</result> |
| </action> |
| <!-- Relationship Type management --> |
| <action name="relTypeList" method="list" class="ch.ffhs.webE.action.RelationshipTypeAction"> |
| <result name="success">/admin/relTypeList.jsp</result> |
| </action> |
| <action name="relTypeAddForm"> |
| <result>/admin/relTypeAddForm.jsp</result> |
| </action> |
| <action name="doRelTypeAdd" method="add" class="ch.ffhs.webE.action.RelationshipTypeAction"> |
| <result name="success" type="redirect">/admin/relTypeList</result> |
| </action> |
| <action name="deleteRelType" method="delete" class="ch.ffhs.webE.action.RelationshipTypeAction"> |
| <result name="success" type="redirect">/admin/relTypeList</result> |
| </action> |
| </package> |
| /trunk/src/ch/ffhs/webE/action/RelationshipTypeAction.java |
|---|
| File deleted |
| \ No newline at end of file |
| Property changes: |
| Deleted: svn:mime-type |
| ## -1 +0,0 ## |
| -text/plain |
| \ No newline at end of property |
| Index: ch/ffhs/webE/action/LoginAction.java |
| =================================================================== |
| --- ch/ffhs/webE/action/LoginAction.java (revision 26) |
| +++ ch/ffhs/webE/action/LoginAction.java (revision 23) |
| @@ -1,5 +1,6 @@ |
| package ch.ffhs.webE.action; |
| + |
| import java.util.Map; |
| import ch.ffhs.webE.dao.UserDAO; |
| @@ -10,8 +11,7 @@ |
| import com.opensymphony.xwork2.ActionSupport; |
| import com.opensymphony.xwork2.ModelDriven; |
| -public class LoginAction extends ActionSupport implements ModelDriven<User> |
| -{ |
| +public class LoginAction extends ActionSupport implements ModelDriven<User>{ |
| private static final long serialVersionUID = 1799753056277211344L; |
| private User user = new User(); |
| @@ -24,27 +24,21 @@ |
| // Session Object |
| Map<String, Object> session = ActionContext.getContext().getSession(); |
| - public LoginAction() |
| - { |
| + |
| + public LoginAction() { |
| } |
| - public String doLogin() |
| - { |
| + public String doLogin() { |
| // If password or user name are empty, the login fails. |
| - if ("".equals(getUserName()) || "".equals(getPw()) |
| - || getUserName() == null || getPw() == null) |
| - { |
| + if("".equals(getUserName()) || "".equals(getPw()) || getUserName() == null || getPw() == null) { |
| return ERROR; |
| } |
| String verifiedUser = verifyUser(getUserName(), getPw()); |
| - if (verifiedUser.equals("failed")) |
| - { |
| + if(verifiedUser.equals("failed")) { |
| return ERROR; |
| - } |
| - else |
| - { |
| + } else { |
| // Put user name, password into session |
| session.put("username", getUserName()); |
| @@ -53,8 +47,7 @@ |
| } |
| } |
| - public String doLogout() |
| - { |
| + public String doLogout() { |
| // TODO: Kill session content for logout |
| return SUCCESS; |
| } |
| @@ -61,15 +54,11 @@ |
| /** |
| * Verify user credentials |
| - * |
| - * @param String |
| - * username: User name |
| - * @param String |
| - * password: Password (hashed) |
| + * @param String username: User name |
| + * @param String password: Password (hashed) |
| * @return |
| */ |
| - public String verifyUser(String username, String password) |
| - { |
| + public String verifyUser(String username, String password) { |
| // DB Query |
| User u = userDAO.searchUsername(username); |
| @@ -82,39 +71,31 @@ |
| return ERROR; |
| // User credentials are fine, check for admin rights |
| - if (u.isAdmin()) |
| - { |
| + if(u.isAdmin()) { |
| return "admin"; |
| - } |
| - else |
| - { |
| + } else { |
| return "user"; |
| } |
| } |
| - public String getUserName() |
| - { |
| + public String getUserName() { |
| return userName; |
| } |
| - public void setUserName(String userName) |
| - { |
| + public void setUserName(String userName) { |
| this.userName = userName; |
| } |
| - public String getPw() |
| - { |
| + public String getPw() { |
| return pw; |
| } |
| - public void setPw(String pw) |
| - { |
| + public void setPw(String pw) { |
| this.pw = pw; |
| } |
| @Override |
| - public User getModel() |
| - { |
| + public User getModel() { |
| return user; |
| } |
| } |
| /trunk/src/ch/ffhs/webE/action/UserAction.java |
|---|
| 15,8 → 15,7 |
| import ch.ffhs.webE.domain.User; |
| public class UserAction extends ActionSupport implements ModelDriven<User> |
| { |
| public class UserAction extends ActionSupport implements ModelDriven<User> { |
| private static final long serialVersionUID = -6659925652584240539L; |
| 25,88 → 24,41 |
| private UserDAO userDAO = new UserDAOImpl(); |
| @Override |
| public User getModel() |
| { |
| public User getModel() { |
| return user; |
| } |
| /** |
| * Executes the DB query to save the user |
| * |
| * @return |
| */ |
| public String add() |
| { |
| public String add() { |
| userDAO.saveUser(user); |
| return SUCCESS; |
| } |
| /** |
| * DB query for userList |
| * |
| * @return SUCCESS |
| */ |
| public String list() |
| { |
| public String list() { |
| userList = userDAO.listUser(); |
| return SUCCESS; |
| } |
| /** |
| * 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() |
| { |
| HttpServletRequest request = (HttpServletRequest) ActionContext |
| .getContext().get(ServletActionContext.HTTP_REQUEST); |
| int id = 0; |
| try |
| { |
| id = Integer.parseInt(request.getParameter("id")); |
| } |
| catch (Exception e) |
| { |
| return ERROR; |
| } |
| // Check for malicious ID values |
| if (id > 0) |
| { |
| userDAO.deleteUser(id); |
| return SUCCESS; |
| } |
| else |
| { |
| return ERROR; |
| } |
| } |
| /* |
| * Standard getters and setters |
| */ |
| public User getUser() |
| { |
| public User getUser() { |
| return user; |
| } |
| public void setUser(User user) |
| { |
| public void setUser(User user) { |
| this.user = user; |
| } |
| public List<User> getUserList() |
| { |
| public List<User> getUserList() { |
| return userList; |
| } |
| public void setUserList(List<User> userList) |
| { |
| public void setUserList(List<User> userList) { |
| this.userList = userList; |
| } |
| public String delete() { |
| HttpServletRequest request = (HttpServletRequest) ActionContext |
| .getContext().get(ServletActionContext.HTTP_REQUEST); |
| userDAO.deleteUser(Integer.parseInt(request.getParameter("id"))); |
| return SUCCESS; |
| } |
| } |
| /trunk/src/ch/ffhs/webE/action/UserForm.java |
|---|
| 0,0 → 1,73 |
| package ch.ffhs.webE.action; |
| import javax.servlet.http.HttpServletRequest; |
| import org.apache.struts2.components.ActionError; |
| import org.apache.struts2.dispatcher.mapper.ActionMapping; |
| import com.opensymphony.xwork2.ActionSupport; |
| public class UserForm extends ActionSupport { |
| private static final long serialVersionUID = 2574972467250197244L; |
| private String username; |
| private String password; |
| private String firstname; |
| private String lastname; |
| private boolean admin; |
| public void reset(ActionMapping mapping, HttpServletRequest request) { |
| this.username = null; |
| this.password = null; |
| this.firstname = null; |
| this.lastname = null; |
| this.admin = false; |
| } |
| public void validate() { |
| } |
| // Getter and setters |
| public String getUsername() { |
| return username; |
| } |
| public void setUsername(String username) { |
| this.username = username; |
| } |
| public String getPassword() { |
| return password; |
| } |
| public void setPassword(String password) { |
| this.password = password; |
| } |
| public String getFirstname() { |
| return firstname; |
| } |
| public void setFirstname(String firstname) { |
| this.firstname = firstname; |
| } |
| public String getLastname() { |
| return lastname; |
| } |
| public void setLastname(String lastname) { |
| this.lastname = lastname; |
| } |
| public boolean isAdmin() { |
| return admin; |
| } |
| public void setAdmin(boolean admin) { |
| this.admin = admin; |
| } |
| } |
| Property changes: |
| Added: svn:mime-type |
| ## -0,0 +1 ## |
| +text/plain |
| \ No newline at end of property |
| Index: ch/ffhs/webE/dao/RelationshipTypeDAO.java |
| =================================================================== |
| --- ch/ffhs/webE/dao/RelationshipTypeDAO.java (revision 26) |
| +++ ch/ffhs/webE/dao/RelationshipTypeDAO.java (nonexistent) |
| @@ -1,15 +0,0 @@ |
| -package ch.ffhs.webE.dao; |
| - |
| -import java.util.List; |
| - |
| -import ch.ffhs.webE.domain.RelationshipType; |
| - |
| -public interface RelationshipTypeDAO { |
| - |
| - List<RelationshipType> listRelationshipTypes(); |
| - |
| - boolean saveRelationshipType(RelationshipType relType); |
| - |
| - boolean deleteRelationshipType(int relTypeID); |
| - |
| -} |
| /ch/ffhs/webE/dao/RelationshipTypeDAO.java |
|---|
| Property changes: |
| Deleted: svn:mime-type |
| ## -1 +0,0 ## |
| -text/plain |
| \ No newline at end of property |
| Index: ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java |
| =================================================================== |
| --- ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java (revision 26) |
| +++ ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java (nonexistent) |
| @@ -1,97 +0,0 @@ |
| -package ch.ffhs.webE.dao; |
| - |
| -import java.util.ArrayList; |
| -import java.util.List; |
| - |
| -import org.hibernate.Session; |
| -import org.hibernate.Transaction; |
| - |
| -import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget; |
| -import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget; |
| -import ch.ffhs.webE.domain.*; |
| - |
| -public class RelationshipTypeDAOImpl implements RelationshipTypeDAO |
| -{ |
| - |
| - @SessionTarget |
| - Session session; |
| - @TransactionTarget |
| - Transaction transaction; |
| - |
| - /** |
| - * Gets a list of all the relationshipTypes in the database. |
| - * |
| - * @return List of all the users. In case of a problem, an empty list is |
| - * returned. |
| - */ |
| - @SuppressWarnings("unchecked") |
| - @Override |
| - public List<RelationshipType> listRelationshipTypes() |
| - { |
| - |
| - List<RelationshipType> relType = null; |
| - |
| - try |
| - { |
| - relType = session.createQuery("from RelationshipType").list(); |
| - } |
| - catch (Exception e) |
| - { |
| - // TODO: Logging |
| - } |
| - |
| - if (relType == null) |
| - { |
| - relType = new ArrayList<RelationshipType>(); |
| - } |
| - |
| - return relType; |
| - } |
| - |
| - /** |
| - * used to save a relationship type |
| - * |
| - * @param RelationshipType |
| - * relType: A filled DAO |
| - * @return Boolean indicating success or error in saving the |
| - * relationshipType |
| - */ |
| - @Override |
| - public boolean saveRelationshipType(RelationshipType relType) |
| - { |
| - try |
| - { |
| - session.save(relType); |
| - return true; |
| - } |
| - catch (Exception e) |
| - { |
| - transaction.rollback(); |
| - return false; |
| - // TODO: Logging |
| - } |
| - } |
| - |
| - /** |
| - * Used to delete a relationship type. |
| - * |
| - * @param int RelationshipType ID |
| - * @return boolean indicating success or error in the query execution |
| - */ |
| - @Override |
| - public boolean deleteRelationshipType(int relTypeID) |
| - { |
| - try |
| - { |
| - User user = (User) session.get(RelationshipType.class, relTypeID); |
| - session.delete(user); |
| - return true; |
| - } |
| - catch (Exception e) |
| - { |
| - transaction.rollback(); |
| - // TODO: Logging |
| - return false; |
| - } |
| - } |
| -} |
| /ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java |
|---|
| Property changes: |
| Deleted: svn:mime-type |
| ## -1 +0,0 ## |
| -text/plain |
| \ No newline at end of property |
| Index: ch/ffhs/webE/dao/UserDAOImpl.java |
| =================================================================== |
| --- ch/ffhs/webE/dao/UserDAOImpl.java (revision 26) |
| +++ ch/ffhs/webE/dao/UserDAOImpl.java (revision 23) |
| @@ -1,6 +1,5 @@ |
| package ch.ffhs.webE.dao; |
| -import java.util.ArrayList; |
| import java.util.List; |
| import org.hibernate.Session; |
| @@ -10,8 +9,7 @@ |
| import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget; |
| import ch.ffhs.webE.domain.*; |
| -public class UserDAOImpl implements UserDAO |
| -{ |
| +public class UserDAOImpl implements UserDAO { |
| @SessionTarget |
| Session session; |
| @@ -18,50 +16,23 @@ |
| @TransactionTarget |
| Transaction transaction; |
| - /** |
| - * Creates a list of all the registered users |
| - * |
| - * @return an ArrayList with all the users - in case of a problem, an empty |
| - * list is returned |
| - */ |
| @SuppressWarnings("unchecked") |
| @Override |
| - public List<User> listUser() |
| - { |
| + public List<User> listUser() { |
| List<User> user = null; |
| - try |
| - { |
| + try { |
| user = session.createQuery("from User").list(); |
| - } |
| - catch (Exception e) |
| - { |
| + } catch (Exception e) { |
| e.printStackTrace(); |
| } |
| - |
| - //If no user was checked, return an empty list to mitigate null pointer exceptions |
| - if (user == null) |
| - { |
| - user = new ArrayList<User>(); |
| - } |
| return user; |
| } |
| - /** |
| - * Executes the query to save the user |
| - * |
| - * @param User |
| - * Domain object to be saved |
| - * @return void |
| - */ |
| @Override |
| - public void saveUser(User user) |
| - { |
| - try |
| - { |
| + public void saveUser(User user) { |
| + try { |
| session.save(user); |
| - } |
| - catch (Exception e) |
| - { |
| + } catch (Exception e) { |
| transaction.rollback(); |
| e.printStackTrace(); |
| } |
| @@ -69,19 +40,13 @@ |
| /** |
| * Used to delete a user. |
| - * |
| - * @param int userId |
| */ |
| @Override |
| - public void deleteUser(int userId) |
| - { |
| - try |
| - { |
| + public void deleteUser(int userId) { |
| + try { |
| User user = (User) session.get(User.class, userId); |
| session.delete(user); |
| - } |
| - catch (Exception e) |
| - { |
| + } catch (Exception e) { |
| transaction.rollback(); |
| e.printStackTrace(); |
| } |
| @@ -95,19 +60,14 @@ |
| * @return User: Returns a user object if something is found. If not, null |
| * is returned |
| */ |
| - public User searchUsername(String username) |
| - { |
| + public User searchUsername(String username) { |
| User user = null; |
| - //Exec query |
| - try |
| - { |
| + try { |
| user = (User) session |
| .createQuery("FROM User " + "WHERE username = :username") |
| .setParameter("username", username).uniqueResult(); |
| - } |
| - catch (Exception e) |
| - { |
| + } catch (Exception e) { |
| // TODO: Log error |
| } |
| return user; |