Subversion Repositories WebE

Rev

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

Rev 31 Rev 33
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
   * Creates 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 users - 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> listTerm()
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
    return term;
61
    return term;
61
  }
62
  }
62
63
63
  /*
64
  /*
64
   * (non-Javadoc)
65
   * (non-Javadoc)
65
   *
66
   *
66
   * @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)
67
   */
68
   */
68
  public void saveOrUpdate(Term term)
69
  public boolean saveOrUpdate(Term term)
69
  {
70
  {
70
    try
71
    try
71
    {
72
    {
-
 
73
      term.setObjectId(term.getObjectId());
72
      this.session.saveOrUpdate(term);
74
      this.session.saveOrUpdate(term);
-
 
75
      return true;
73
    }
76
    }
74
    catch (Exception e)
77
    catch (Exception e)
75
    {
78
    {
76
      this.transaction.rollback();
79
      this.transaction.rollback();
77
      e.printStackTrace();
80
      e.printStackTrace();
-
 
81
      return false;
78
    }
82
    }
79
  }
83
  }
80
84
81
  /*
85
  /*
82
   * (non-Javadoc)
86
   * (non-Javadoc)
83
   *
87
   *
84
   * @see ch.ffhs.webE.dao.TermDAO#deleteTerm(int)
88
   * @see ch.ffhs.webE.dao.TermDAO#deleteTerm(int)
85
   */
89
   */
86
  public void deleteTerm(int termId)
90
  public void deleteTerm(int termId)
87
  {
91
  {
88
    try
92
    try
89
    {
93
    {
90
      Term user = (Term) this.session.get(Term.class, termId);
94
      Term term = (Term) this.session.get(Term.class, termId);
91
      this.session.delete(user);
95
      this.session.delete(term);
92
    }
96
    }
93
    catch (Exception e)
97
    catch (Exception e)
94
    {
98
    {
95
      this.transaction.rollback();
99
      this.transaction.rollback();
96
      e.printStackTrace();
100
      e.printStackTrace();
97
    }
101
    }
98
  }
102
  }
99
103
100
  /**
104
  /*
101
   * Returns a single user with this user name (used for login)
105
   * (non-Javadoc)
102
   *
106
   *
103
   * @param termName
-
 
104
   *          Term name
-
 
105
   * @return User: Returns a user object if something is found. If not, null is
107
   * @see ch.ffhs.webE.dao.TermDAO#getTermById(int)
106
   *         returned
-
 
107
   */
108
   */
108
  public Term searchTerm(String termName)
109
  public Term getTermById(int termId)
109
  {
110
  {
110
    Term term = null;
111
    Term term = null;
111
112
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
113
    try
136
    {
114
    {
137
      term = (Term) this.session.get(Term.class, termId);
115
      term = (Term) this.session.get(Term.class, termId);
138
    }
116
    }
139
    catch (Exception e)
117
    catch (Exception e)
140
    {
118
    {
141
      e.printStackTrace();
119
      e.printStackTrace();
142
    }
120
    }
-
 
121
143
    return term;
122
    return term;
144
  }
123
  }
145
}
124
}