Subversion Repositories WebE

Compare Revisions

Last modification

Ignore whitespace Rev 25 → Rev 26

/trunk/src/struts.xml
14,6 → 14,8
<!-- Admin environment -->
<package name="admin" namespace="/admin" extends="hibernate-default">
<!-- User management -->
<action name="userAddForm">
<result>/admin/userAddForm.jsp</result>
</action>
29,6 → 31,25
<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/dao/UserDAOImpl.java
1,5 → 1,6
package ch.ffhs.webE.dao;
 
import java.util.ArrayList;
import java.util.List;
 
import org.hibernate.Session;
9,67 → 10,106
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;
@TransactionTarget
Transaction transaction;
@SessionTarget
Session session;
@TransactionTarget
Transaction transaction;
 
@SuppressWarnings("unchecked")
@Override
public List<User> listUser() {
List<User> user = null;
try {
user = session.createQuery("from User").list();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
/**
* 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()
{
List<User> user = null;
try
{
user = session.createQuery("from User").list();
}
catch (Exception e)
{
e.printStackTrace();
}
 
@Override
public void saveUser(User user) {
try {
session.save(user);
} catch (Exception e) {
transaction.rollback();
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;
}
 
/**
* Used to delete a user.
*/
@Override
public void deleteUser(int userId) {
try {
User user = (User) session.get(User.class, userId);
session.delete(user);
} catch (Exception e) {
transaction.rollback();
e.printStackTrace();
}
}
/**
* Executes the query to save the user
*
* @param User
* Domain object to be saved
* @return void
*/
@Override
public void saveUser(User user)
{
try
{
session.save(user);
}
catch (Exception e)
{
transaction.rollback();
e.printStackTrace();
}
}
 
/**
* Returns a single user with this user name (used for login)
*
* @param username
* : String - entire user name
* @return User: Returns a user object if something is found. If not, null
* is returned
*/
public User searchUsername(String username) {
User user = null;
/**
* Used to delete a user.
*
* @param int userId
*/
@Override
public void deleteUser(int userId)
{
try
{
User user = (User) session.get(User.class, userId);
session.delete(user);
}
catch (Exception e)
{
transaction.rollback();
e.printStackTrace();
}
}
 
try {
user = (User) session
.createQuery("FROM User " + "WHERE username = :username")
.setParameter("username", username).uniqueResult();
} catch (Exception e) {
// TODO: Log error
}
return user;
}
/**
* Returns a single user with this user name (used for login)
*
* @param username
* : String - entire user name
* @return User: Returns a user object if something is found. If not, null
* is returned
*/
public User searchUsername(String username)
{
User user = null;
 
//Exec query
try
{
user = (User) session
.createQuery("FROM User " + "WHERE username = :username")
.setParameter("username", username).uniqueResult();
}
catch (Exception e)
{
// TODO: Log error
}
return user;
}
}
/trunk/src/ch/ffhs/webE/dao/RelationshipTypeDAO.java
0,0 → 1,15
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);
 
}
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java
===================================================================
--- ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java (nonexistent)
+++ ch/ffhs/webE/dao/RelationshipTypeDAOImpl.java (revision 26)
@@ -0,0 +1,97 @@
+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:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: ch/ffhs/webE/action/UserForm.java
===================================================================
--- ch/ffhs/webE/action/UserForm.java (revision 25)
+++ ch/ffhs/webE/action/UserForm.java (nonexistent)
@@ -1,73 +0,0 @@
-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;
- }
-}
/ch/ffhs/webE/action/UserForm.java
Property changes:
Deleted: svn:mime-type
## -1 +0,0 ##
-text/plain
\ No newline at end of property
Index: ch/ffhs/webE/action/RelationshipTypeAction.java
===================================================================
--- ch/ffhs/webE/action/RelationshipTypeAction.java (nonexistent)
+++ ch/ffhs/webE/action/RelationshipTypeAction.java (revision 26)
@@ -0,0 +1,104 @@
+package ch.ffhs.webE.action;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.servlet.http.HttpServletRequest;
+
+import org.apache.struts2.ServletActionContext;
+
+import ch.ffhs.webE.dao.RelationshipTypeDAO;
+import ch.ffhs.webE.dao.RelationshipTypeDAOImpl;
+import ch.ffhs.webE.domain.RelationshipType;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionSupport;
+import com.opensymphony.xwork2.ModelDriven;
+
+public class RelationshipTypeAction extends ActionSupport implements
+ ModelDriven<RelationshipType>
+{
+
+ private static final long serialVersionUID = -3644691864156792139L;
+
+ private RelationshipType relType = new RelationshipType();
+ private List<RelationshipType> relTypeList = new ArrayList<RelationshipType>();
+ private RelationshipTypeDAO relTypeDAO = new RelationshipTypeDAOImpl();
+
+ @Override
+ public RelationshipType getModel()
+ {
+ return relType;
+ }
+
+ public String add()
+ {
+ relTypeDAO.saveRelationshipType(relType);
+ return SUCCESS;
+ }
+
+ public String list()
+ {
+ relTypeList = relTypeDAO.listRelationshipTypes();
+ return SUCCESS;
+ }
+
+ /**
+ * deletes a relationshipType, gets the ID from the id parameter that was
+ * submitted
+ *
+ * @return String - either success or error
+ */
+ public String delete()
+ {
+ HttpServletRequest request = (HttpServletRequest) ActionContext
+ .getContext().get(ServletActionContext.HTTP_REQUEST);
+
+ //Make sure the ID from the request parameter is valid
+ int id = 0;
+
+ try
+ {
+ id = Integer.parseInt(request.getParameter("id"));
+ }
+ catch (Exception e)
+ {
+ return ERROR;
+ }
+
+ // Check for malicious ID values
+ if (id > 0)
+ {
+ relTypeDAO.deleteRelationshipType(id);
+ return SUCCESS;
+ }
+ else
+ {
+ return ERROR;
+ }
+ }
+
+ /*
+ * Getters and setters
+ */
+
+ public RelationshipType getRelType()
+ {
+ return relType;
+ }
+
+ public void setRelType(RelationshipType relType)
+ {
+ this.relType = relType;
+ }
+
+ public List<RelationshipType> getRelTypeList()
+ {
+ return relTypeList;
+ }
+
+ public void setRelTypeList(List<RelationshipType> relTypeList)
+ {
+ this.relTypeList = relTypeList;
+ }
+}
\ No newline at end of file
/ch/ffhs/webE/action/RelationshipTypeAction.java
Property changes:
Added: svn:mime-type
## -0,0 +1 ##
+text/plain
\ No newline at end of property
Index: ch/ffhs/webE/action/LoginAction.java
===================================================================
--- ch/ffhs/webE/action/LoginAction.java (revision 25)
+++ ch/ffhs/webE/action/LoginAction.java (revision 26)
@@ -1,6 +1,5 @@
package ch.ffhs.webE.action;
-
import java.util.Map;
import ch.ffhs.webE.dao.UserDAO;
@@ -11,91 +10,111 @@
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
-public class LoginAction extends ActionSupport implements ModelDriven<User>{
-
- private static final long serialVersionUID = 1799753056277211344L;
- private User user = new User();
- private UserDAO userDAO = new UserDAOImpl();
-
- //Form fields
- private String userName ;
- private String pw;
-
- //Session Object
- Map<String, Object> session = ActionContext.getContext().getSession();
-
+public class LoginAction extends ActionSupport implements ModelDriven<User>
+{
- public LoginAction() {
- }
-
- public String doLogin() {
-
- //If password or user name are empty, the login fails.
- if("".equals(getUserName()) || "".equals(getPw()) || getUserName() == null || getPw() == null) {
- return ERROR;
- }
-
- String verifiedUser = verifyUser(getUserName(), getPw());
- if(verifiedUser.equals("failed")) {
- return ERROR;
- } else {
-
- //Put user name, password into session
- session.put("username", getUserName());
- session.put("pw", getPw());
- return verifiedUser;
- }
- }
-
- public String doLogout() {
- //TODO: Kill session content for logout
- return SUCCESS;
- }
-
- /**
- * Verify user credentials
- * @param String username: User name
- * @param String password: Password (hashed)
- * @return
- */
- public String verifyUser(String username, String password) {
- //DB Query
- User u = userDAO.searchUsername(username);
-
- //User does not exist
- if(u == null)
- return ERROR;
-
- //User password does not match
- if(!u.getPassword().equals(password))
- return ERROR;
-
- //User credentials are fine, check for admin rights
- if(u.isAdmin()) {
- return "admin";
- } else {
- return "user";
- }
- }
-
- public String getUserName() {
- return userName;
- }
+ private static final long serialVersionUID = 1799753056277211344L;
+ private User user = new User();
+ private UserDAO userDAO = new UserDAOImpl();
- public void setUserName(String userName) {
- this.userName = userName;
- }
-
- public String getPw() {
- return pw;
- }
+ // Form fields
+ private String userName;
+ private String pw;
- public void setPw(String pw) {
- this.pw = pw;
- }
+ // Session Object
+ Map<String, Object> session = ActionContext.getContext().getSession();
- @Override
- public User getModel() {
- return user;
- }
+ public LoginAction()
+ {
+ }
+
+ public String doLogin()
+ {
+
+ // If password or user name are empty, the login fails.
+ if ("".equals(getUserName()) || "".equals(getPw())
+ || getUserName() == null || getPw() == null)
+ {
+ return ERROR;
+ }
+
+ String verifiedUser = verifyUser(getUserName(), getPw());
+ if (verifiedUser.equals("failed"))
+ {
+ return ERROR;
+ }
+ else
+ {
+
+ // Put user name, password into session
+ session.put("username", getUserName());
+ session.put("pw", getPw());
+ return verifiedUser;
+ }
+ }
+
+ public String doLogout()
+ {
+ // TODO: Kill session content for logout
+ return SUCCESS;
+ }
+
+ /**
+ * Verify user credentials
+ *
+ * @param String
+ * username: User name
+ * @param String
+ * password: Password (hashed)
+ * @return
+ */
+ public String verifyUser(String username, String password)
+ {
+ // DB Query
+ User u = userDAO.searchUsername(username);
+
+ // User does not exist
+ if (u == null)
+ return ERROR;
+
+ // User password does not match
+ if (!u.getPassword().equals(password))
+ return ERROR;
+
+ // User credentials are fine, check for admin rights
+ if (u.isAdmin())
+ {
+ return "admin";
+ }
+ else
+ {
+ return "user";
+ }
+ }
+
+ public String getUserName()
+ {
+ return userName;
+ }
+
+ public void setUserName(String userName)
+ {
+ this.userName = userName;
+ }
+
+ public String getPw()
+ {
+ return pw;
+ }
+
+ public void setPw(String pw)
+ {
+ this.pw = pw;
+ }
+
+ @Override
+ public User getModel()
+ {
+ return user;
+ }
}
/trunk/src/ch/ffhs/webE/action/UserAction.java
15,50 → 15,98
 
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;
private static final long serialVersionUID = -6659925652584240539L;
 
private User user = new User();
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAOImpl();
private User user = new User();
private List<User> userList = new ArrayList<User>();
private UserDAO userDAO = new UserDAOImpl();
 
@Override
public User getModel() {
return user;
}
@Override
public User getModel()
{
return user;
}
 
public String add() {
userDAO.saveUser(user);
return SUCCESS;
}
/**
* Executes the DB query to save the user
*
* @return
*/
public String add()
{
userDAO.saveUser(user);
return SUCCESS;
}
 
public String list() {
userList = userDAO.listUser();
return SUCCESS;
}
/**
* DB query for userList
*
* @return SUCCESS
*/
public String list()
{
userList = userDAO.listUser();
return SUCCESS;
}
 
public User getUser() {
return user;
}
/**
* 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);
 
public void setUser(User user) {
this.user = user;
}
int id = 0;
try
{
id = Integer.parseInt(request.getParameter("id"));
}
catch (Exception e)
{
return ERROR;
}
 
public List<User> getUserList() {
return userList;
}
// Check for malicious ID values
if (id > 0)
{
userDAO.deleteUser(id);
return SUCCESS;
}
else
{
return ERROR;
}
}
 
public void setUserList(List<User> userList) {
this.userList = userList;
}
/*
* Standard getters and setters
*/
 
public String delete() {
HttpServletRequest request = (HttpServletRequest) ActionContext
.getContext().get(ServletActionContext.HTTP_REQUEST);
userDAO.deleteUser(Integer.parseInt(request.getParameter("id")));
return SUCCESS;
}
public User getUser()
{
return user;
}
 
public void setUser(User user)
{
this.user = user;
}
 
public List<User> getUserList()
{
return userList;
}
 
public void setUserList(List<User> userList)
{
this.userList = userList;
}
}