Subversion Repositories WebE

Rev

Rev 34 | Details | Compare with Previous | Last modification | View Log | RSS feed

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