Subversion Repositories WebE

Rev

Rev 34 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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