Go to most recent revision | Details | Last modification | View Log | RSS feed
Rev | Author | Line No. | Line |
---|---|---|---|
34 | PointedEar | 1 | package ch.ffhs.webE.dao; |
2 | |||
3 | import java.util.ArrayList; |
||
4 | import java.util.List; |
||
5 | |||
6 | import org.hibernate.Session; |
||
7 | import org.hibernate.Transaction; |
||
8 | |||
9 | import ch.ffhs.webE.domain.Relationship; |
||
10 | |||
11 | import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget; |
||
12 | import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget; |
||
13 | |||
14 | /** |
||
15 | * Implements the Database Access Object for terms |
||
16 | * |
||
17 | * @author Thomas Lahn |
||
18 | */ |
||
19 | public class RelationshipDAOImpl |
||
20 | { |
||
21 | /** |
||
22 | * Database session |
||
23 | */ |
||
24 | @SessionTarget |
||
25 | Session session; |
||
26 | |||
27 | /** |
||
28 | * Database transaction |
||
29 | */ |
||
30 | @TransactionTarget |
||
31 | Transaction transaction; |
||
32 | |||
33 | /** |
||
34 | * Creates a list of all relationships |
||
35 | * |
||
36 | * @return an ArrayList with all the relationshops - in case of a problem, an |
||
37 | * empty list is returned |
||
38 | */ |
||
39 | @SuppressWarnings("unchecked") |
||
40 | public List<Relationship> listRelationships() |
||
41 | { |
||
42 | List<Relationship> relationship = null; |
||
43 | try |
||
44 | { |
||
45 | relationship = this.session.createQuery("from Relationship").list(); //$NON-NLS-1$ |
||
46 | } |
||
47 | catch (Exception e) |
||
48 | { |
||
49 | e.printStackTrace(); |
||
50 | } |
||
51 | |||
52 | /* |
||
53 | * If no relationship was checked, return an empty list to mitigate null |
||
54 | * pointer exceptions |
||
55 | */ |
||
56 | if (relationship == null) |
||
57 | { |
||
58 | relationship = new ArrayList<Relationship>(); |
||
59 | } |
||
60 | |||
61 | return relationship; |
||
62 | } |
||
63 | |||
64 | /** |
||
65 | * Executes the query to save the relationship |
||
66 | * |
||
67 | * @param relationship |
||
68 | * Domain object to be saved |
||
69 | * @return <code>true</code> if successful, <code>false</code> otherwise |
||
70 | */ |
||
71 | public boolean saveOrUpdate(Relationship relationship) |
||
72 | { |
||
73 | try |
||
74 | { |
||
75 | relationship.setObjectId(relationship.getObjectId()); |
||
76 | this.session.saveOrUpdate(relationship); |
||
77 | return true; |
||
78 | } |
||
79 | catch (Exception e) |
||
80 | { |
||
81 | this.transaction.rollback(); |
||
82 | e.printStackTrace(); |
||
83 | return false; |
||
84 | } |
||
85 | } |
||
86 | |||
87 | /** |
||
88 | * Delete a relationship |
||
89 | * |
||
90 | * @param id |
||
91 | * Relationship ID |
||
92 | */ |
||
93 | public void deleteRelationship(int id) |
||
94 | { |
||
95 | try |
||
96 | { |
||
97 | Relationship relationship = (Relationship) this.session.get( |
||
98 | Relationship.class, id); |
||
99 | this.session.delete(relationship); |
||
100 | } |
||
101 | catch (Exception e) |
||
102 | { |
||
103 | this.transaction.rollback(); |
||
104 | e.printStackTrace(); |
||
105 | } |
||
106 | } |
||
107 | |||
108 | /** |
||
109 | * Retrieves a relationship by ID |
||
110 | * |
||
111 | * @param id |
||
112 | * Term ID |
||
113 | * @return The relationship with this <var>id</var> |
||
114 | */ |
||
115 | public Relationship getRelationshipById(int id) |
||
116 | { |
||
117 | Relationship relationship = null; |
||
118 | |||
119 | try |
||
120 | { |
||
121 | relationship = (Relationship) this.session.get(Relationship.class, id); |
||
122 | } |
||
123 | catch (Exception e) |
||
124 | { |
||
125 | e.printStackTrace(); |
||
126 | } |
||
127 | |||
128 | return relationship; |
||
129 | } |
||
130 | } |