Subversion Repositories WebE

Compare Revisions

Last modification

Regard whitespace Rev 23 → 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,7 → 10,8
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;
16,23 → 18,50
@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();
}
40,13 → 69,19
 
/**
* 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();
}
60,14 → 95,19
* @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;
 
try {
//Exec query
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;
/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 23)
+++ 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 23)
+++ 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,7 +10,8 @@
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,21 +24,27 @@
//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());
@@ -47,7 +53,8 @@
}
}
- public String doLogout() {
+ public String doLogout()
+ {
//TODO: Kill session content for logout
return SUCCESS;
}
@@ -54,11 +61,15 @@
/**
* 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);
@@ -71,31 +82,39 @@
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,7 → 15,8
 
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;
 
24,41 → 25,88
private UserDAO userDAO = new UserDAOImpl();
 
@Override
public User getModel() {
public User getModel()
{
return user;
}
 
public String add() {
/**
* Executes the DB query to save the user
*
* @return
*/
public String add()
{
userDAO.saveUser(user);
return SUCCESS;
}
 
public String list() {
/**
* DB query for userList
*
* @return SUCCESS
*/
public String list()
{
userList = userDAO.listUser();
return SUCCESS;
}
 
public User getUser() {
/**
* 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()
{
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;
}
 
}