Subversion Repositories WebE

Rev

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

Rev 33 Rev 34
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.Term;
9
import ch.ffhs.webE.domain.Term;
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 TermDAOImpl implements TermDAO
19
public class TermDAOImpl implements TermDAO
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 terms
34
   * Returns a list of all terms
35
   *
35
   *
36
   * @return an ArrayList with all the users - in case of a problem, an empty
36
   * @return an ArrayList with all the terms - in case of a problem, an empty
37
   *         list is returned
37
   *         list is returned
38
   */
38
   */
39
  @SuppressWarnings("unchecked")
39
  @SuppressWarnings("unchecked")
40
  public List<Term> listTerm()
40
  public List<Term> getTerms()
41
  {
41
  {
42
    List<Term> term = null;
42
    List<Term> term = null;
43
    try
43
    try
44
    {
44
    {
45
      term = this.session.createQuery("from Term").list(); //$NON-NLS-1$
45
      term = this.session.createQuery("from Term").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 term was checked, return an empty list to mitigate null pointer
53
     * If no term was checked, return an empty list to mitigate null pointer
54
     * exceptions
54
     * exceptions
55
     */
55
     */
56
    if (term == null)
56
    if (term == null)
57
    {
57
    {
58
      term = new ArrayList<Term>();
58
      term = new ArrayList<Term>();
59
    }
59
    }
60
60
61
    return term;
61
    return term;
62
  }
62
  }
63
63
64
  /*
64
  /*
65
   * (non-Javadoc)
65
   * (non-Javadoc)
66
   *
66
   *
67
   * @see ch.ffhs.webE.dao.TermDAO#saveOrUpdate(ch.ffhs.webE.domain.Term)
67
   * @see ch.ffhs.webE.dao.TermDAO#saveOrUpdate(ch.ffhs.webE.domain.Term)
68
   */
68
   */
69
  public boolean saveOrUpdate(Term term)
69
  public boolean saveOrUpdate(Term term)
70
  {
70
  {
71
    try
71
    try
72
    {
72
    {
73
      term.setObjectId(term.getObjectId());
73
      term.setObjectId(term.getObjectId());
74
      this.session.saveOrUpdate(term);
74
      this.session.saveOrUpdate(term);
75
      return true;
75
      return true;
76
    }
76
    }
77
    catch (Exception e)
77
    catch (Exception e)
78
    {
78
    {
79
      this.transaction.rollback();
79
      this.transaction.rollback();
80
      e.printStackTrace();
80
      e.printStackTrace();
81
      return false;
81
      return false;
82
    }
82
    }
83
  }
83
  }
84
84
85
  /*
85
  /*
86
   * (non-Javadoc)
86
   * (non-Javadoc)
87
   *
87
   *
88
   * @see ch.ffhs.webE.dao.TermDAO#deleteTerm(int)
88
   * @see ch.ffhs.webE.dao.TermDAO#deleteTerm(int)
89
   */
89
   */
90
  public void deleteTerm(int termId)
90
  public void deleteTerm(int termId)
91
  {
91
  {
92
    try
92
    try
93
    {
93
    {
94
      Term term = (Term) this.session.get(Term.class, termId);
94
      Term term = (Term) this.session.get(Term.class, termId);
95
      this.session.delete(term);
95
      this.session.delete(term);
96
    }
96
    }
97
    catch (Exception e)
97
    catch (Exception e)
98
    {
98
    {
99
      this.transaction.rollback();
99
      this.transaction.rollback();
100
      e.printStackTrace();
100
      e.printStackTrace();
101
    }
101
    }
102
  }
102
  }
103
103
104
  /*
104
  /*
105
   * (non-Javadoc)
105
   * (non-Javadoc)
106
   *
106
   *
107
   * @see ch.ffhs.webE.dao.TermDAO#getTermById(int)
107
   * @see ch.ffhs.webE.dao.TermDAO#getTermById(int)
108
   */
108
   */
109
  public Term getTermById(int termId)
109
  public Term getTermById(int termId)
110
  {
110
  {
111
    Term term = null;
111
    Term term = null;
112
112
113
    try
113
    try
114
    {
114
    {
115
      term = (Term) this.session.get(Term.class, termId);
115
      term = (Term) this.session.get(Term.class, termId);
116
    }
116
    }
117
    catch (Exception e)
117
    catch (Exception e)
118
    {
118
    {
119
      e.printStackTrace();
119
      e.printStackTrace();
120
    }
120
    }
121
121
122
    return term;
122
    return term;
123
  }
123
  }
124
}
124
}