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