Subversion Repositories WebE

Rev

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

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