Subversion Repositories WebE

Rev

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

Rev Author Line No. Line
18 moos 1
package ch.ffhs.webE.dao;
2
 
37 PointedEar 3
import java.util.ArrayList;
18 moos 4
import java.util.List;
5
 
37 PointedEar 6
import org.hibernate.Session;
7
import org.hibernate.Transaction;
8
 
18 moos 9
import ch.ffhs.webE.domain.User;
10
 
37 PointedEar 11
import com.googlecode.s2hibernate.struts2.plugin.annotations.SessionTarget;
12
import com.googlecode.s2hibernate.struts2.plugin.annotations.TransactionTarget;
13
 
14
public class UserDAO
27 moos 15
{
18 moos 16
 
37 PointedEar 17
  @SessionTarget
18
  Session session;
19
  @TransactionTarget
20
  Transaction transaction;
27 moos 21
 
37 PointedEar 22
  /**
23
   * Creates a list of all the registered users
24
   *
25
   * @return an ArrayList with all the users - in case of a problem, an empty
26
   *         list is returned
27
   */
28
  @SuppressWarnings("unchecked")
29
  public List<User> getList()
30
  {
31
    List<User> user = null;
32
    try
33
    {
34
      user = this.session.createQuery("from User").list();
35
    }
36
    catch (Exception e)
37
    {
38
      e.printStackTrace();
39
    }
27 moos 40
 
37 PointedEar 41
    // If no user was checked, return an empty list to mitigate null pointer
42
    // exceptions
43
    if (user == null)
44
    {
45
      user = new ArrayList<User>();
46
    }
47
    return user;
48
  }
27 moos 49
 
37 PointedEar 50
  /**
51
   * Executes the query to save the user
52
   *
53
   * @param user
54
   *          Domain object to be saved
55
   * @return void
56
   */
57
  public void saveOrUpdate(User user)
58
  {
59
    try
60
    {
61
      this.session.saveOrUpdate(user);
62
    }
63
    catch (Exception e)
64
    {
65
      this.transaction.rollback();
66
      e.printStackTrace();
67
    }
68
  }
27 moos 69
 
37 PointedEar 70
  /**
71
   * Used to delete a user.
72
   *
73
   * @param userId
74
   */
75
  public void delete(int userId)
76
  {
77
    try
78
    {
79
      User user = (User) this.session.get(User.class, userId);
80
      this.session.delete(user);
81
    }
82
    catch (Exception e)
83
    {
84
      this.transaction.rollback();
85
      e.printStackTrace();
86
    }
87
  }
88
 
89
  /**
90
   * Returns a single user with this user name (used for login)
91
   *
92
   * @param username
93
   *          : String - entire user name
94
   * @return User: Returns a user object if something is found. If not, null is
95
   *         returned
96
   */
97
  public User getByUsername(String username)
98
  {
99
    User user = null;
100
 
101
    // Exec query
102
    try
103
    {
104
      user = (User) this.session
105
          .createQuery("FROM User " + "WHERE username = :username")
106
          .setParameter("username", username).uniqueResult();
107
    }
108
    catch (Exception e)
109
    {
110
      // TODO: Log error
111
    }
112
    return user;
113
  }
114
 
115
  /**
116
   * Used to list a single user by Id.
117
   *
118
   * @param userId
119
   * @return
120
   */
121
  public User getById(int userId)
122
  {
123
    User user = null;
124
    try
125
    {
126
      user = (User) this.session.get(User.class, userId);
127
    }
128
    catch (Exception e)
129
    {
130
      e.printStackTrace();
131
    }
132
    return user;
133
  }
134
}