Subversion Repositories WebE

Rev

Rev 33 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
31 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.Term;
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 TermDAOImpl implements TermDAO
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 terms
35
   *
36
   * @return an ArrayList with all the users - in case of a problem, an empty
37
   *         list is returned
38
   */
39
  @SuppressWarnings("unchecked")
40
  public List<Term> listTerm()
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
    return term;
61
  }
62
 
63
  /*
64
   * (non-Javadoc)
65
   *
66
   * @see ch.ffhs.webE.dao.TermDAO#saveOrUpdate(ch.ffhs.webE.domain.Term)
67
   */
68
  public void saveOrUpdate(Term term)
69
  {
70
    try
71
    {
72
      this.session.saveOrUpdate(term);
73
    }
74
    catch (Exception e)
75
    {
76
      this.transaction.rollback();
77
      e.printStackTrace();
78
    }
79
  }
80
 
81
  /*
82
   * (non-Javadoc)
83
   *
84
   * @see ch.ffhs.webE.dao.TermDAO#deleteTerm(int)
85
   */
86
  public void deleteTerm(int termId)
87
  {
88
    try
89
    {
90
      Term user = (Term) this.session.get(Term.class, termId);
91
      this.session.delete(user);
92
    }
93
    catch (Exception e)
94
    {
95
      this.transaction.rollback();
96
      e.printStackTrace();
97
    }
98
  }
99
 
100
  /**
101
   * Returns a single user with this user name (used for login)
102
   *
103
   * @param termName
104
   *          Term name
105
   * @return User: Returns a user object if something is found. If not, null is
106
   *         returned
107
   */
108
  public Term searchTerm(String termName)
109
  {
110
    Term term = null;
111
 
112
    /* Exec query */
113
    try
114
    {
115
      term = (Term) this.session
116
          .createQuery("FROM User " + "WHERE username = :username") //$NON-NLS-1$ //$NON-NLS-2$
117
          .setParameter("username", termName).uniqueResult(); //$NON-NLS-1$
118
    }
119
    catch (Exception e)
120
    {
121
      /* TODO: Log error */
122
    }
123
    return term;
124
  }
125
 
126
  /**
127
   * List a term by ID
128
   *
129
   * @param termId
130
   * @return
131
   */
132
  public Term listTermById(int termId)
133
  {
134
    Term term = null;
135
    try
136
    {
137
      term = (Term) this.session.get(Term.class, termId);
138
    }
139
    catch (Exception e)
140
    {
141
      e.printStackTrace();
142
    }
143
    return term;
144
  }
145
}