Rev 27 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
| Rev 27 | Rev 37 | ||
|---|---|---|---|
| Line 1... | Line 1... | ||
| 1 | package ch.ffhs.webE.dao; |
1 | package ch.ffhs.webE.dao; |
| 2 | 2 | ||
| - | 3 | import java.util.ArrayList; |
|
| 3 | import java.util.List; |
4 | import java.util.List; |
| 4 | 5 | ||
| 5 | import ch.ffhs.webE.domain.User; |
6 | import org.hibernate.Session; |
| 6 | - | ||
| 7 | public interface UserDAO |
7 | import org.hibernate.Transaction; |
| 8 | {
|
- | |
| 9 | 8 | ||
| 10 | List<User> listUser(); |
9 | import ch.ffhs.webE.domain.User; |
| 11 | - | ||
| 12 | User searchUsername(String username); |
- | |
| 13 | 10 | ||
| 14 | void deleteUser(int userId); |
11 | import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget; |
| - | 12 | import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget; |
|
| 15 | 13 | ||
| 16 | User listUserById(int userId); |
14 | public class UserDAO |
| - | 15 | {
|
|
| 17 | 16 | ||
| - | 17 | @SessionTarget |
|
| - | 18 | Session session;
|
|
| - | 19 | @TransactionTarget |
|
| - | 20 | Transaction transaction;
|
|
| - | 21 | ||
| - | 22 | /**
|
|
| - | 23 | * Creates a list of all the registered users
|
|
| - | 24 | *
|
|
| - | 25 | * @return an ArrayList with all the users - in case of a problem, an empty
|
|
| - | 26 | * list is returned
|
|
| - | 27 | */
|
|
| - | 28 | @SuppressWarnings("unchecked") |
|
| - | 29 | public List<User> getList() |
|
| - | 30 | {
|
|
| - | 31 | List<User> user = null; |
|
| - | 32 | try
|
|
| - | 33 | {
|
|
| - | 34 | user = this.session.createQuery("from User").list(); |
|
| - | 35 | }
|
|
| - | 36 | catch (Exception e) |
|
| - | 37 | {
|
|
| - | 38 | e.printStackTrace(); |
|
| - | 39 | }
|
|
| - | 40 | ||
| - | 41 | // If no user was checked, return an empty list to mitigate null pointer
|
|
| - | 42 | // exceptions
|
|
| - | 43 | if (user == null) |
|
| - | 44 | {
|
|
| - | 45 | user = new ArrayList<User>(); |
|
| - | 46 | }
|
|
| - | 47 | return user; |
|
| - | 48 | }
|
|
| - | 49 | ||
| - | 50 | /**
|
|
| - | 51 | * Executes the query to save the user
|
|
| - | 52 | *
|
|
| - | 53 | * @param user
|
|
| - | 54 | * Domain object to be saved
|
|
| - | 55 | * @return void
|
|
| - | 56 | */
|
|
| 18 | void saveOrUpdateUser(User user); |
57 | public void saveOrUpdate(User user) |
| - | 58 | {
|
|
| - | 59 | try
|
|
| - | 60 | {
|
|
| - | 61 | this.session.saveOrUpdate(user); |
|
| - | 62 | }
|
|
| - | 63 | catch (Exception e) |
|
| - | 64 | {
|
|
| - | 65 | this.transaction.rollback(); |
|
| - | 66 | e.printStackTrace(); |
|
| - | 67 | }
|
|
| - | 68 | }
|
|
| - | 69 | ||
| - | 70 | /**
|
|
| - | 71 | * Used to delete a user.
|
|
| - | 72 | *
|
|
| - | 73 | * @param userId
|
|
| - | 74 | */
|
|
| - | 75 | public void delete(int userId) |
|
| - | 76 | {
|
|
| - | 77 | try
|
|
| - | 78 | {
|
|
| - | 79 | User user = (User) this.session.get(User.class, userId); |
|
| - | 80 | this.session.delete(user); |
|
| - | 81 | }
|
|
| - | 82 | catch (Exception e) |
|
| - | 83 | {
|
|
| - | 84 | this.transaction.rollback(); |
|
| - | 85 | e.printStackTrace(); |
|
| - | 86 | }
|
|
| - | 87 | }
|
|
| - | 88 | ||
| - | 89 | /**
|
|
| - | 90 | * Returns a single user with this user name (used for login)
|
|
| - | 91 | *
|
|
| - | 92 | * @param username
|
|
| - | 93 | * : String - entire user name
|
|
| - | 94 | * @return User: Returns a user object if something is found. If not, null is
|
|
| - | 95 | * returned
|
|
| - | 96 | */
|
|
| - | 97 | public User getByUsername(String username) |
|
| - | 98 | {
|
|
| - | 99 | User user = null; |
|
| - | 100 | ||
| - | 101 | // Exec query
|
|
| - | 102 | try
|
|
| - | 103 | {
|
|
| - | 104 | user = (User) this.session |
|
| - | 105 | .createQuery("FROM User " + "WHERE username = :username") |
|
| - | 106 | .setParameter("username", username).uniqueResult(); |
|
| - | 107 | }
|
|
| - | 108 | catch (Exception e) |
|
| - | 109 | {
|
|
| - | 110 | // TODO: Log error
|
|
| - | 111 | }
|
|
| - | 112 | return user; |
|
| - | 113 | }
|
|
| - | 114 | ||
| - | 115 | /**
|
|
| - | 116 | * Used to list a single user by Id.
|
|
| - | 117 | *
|
|
| - | 118 | * @param userId
|
|
| - | 119 | * @return
|
|
| - | 120 | */
|
|
| - | 121 | public User getById(int userId) |
|
| - | 122 | {
|
|
| - | 123 | User user = null; |
|
| - | 124 | try
|
|
| - | 125 | {
|
|
| - | 126 | user = (User) this.session.get(User.class, userId); |
|
| - | 127 | }
|
|
| - | 128 | catch (Exception e) |
|
| - | 129 | {
|
|
| - | 130 | e.printStackTrace(); |
|
| - | 131 | }
|
|
| - | 132 | return user; |
|
| - | 133 | }
|
|
| 19 | }
|
134 | }
|
| 20 | 135 | ||