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