Subversion Repositories bacoAlunos

Rev

Rev 113 | Rev 186 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 fvelez 1
package pt.estgp.estgweb.domain.dao.impl;
2
 
3
import jomm.dao.DaoException;
4
import org.hibernate.HibernateException;
5
import org.hibernate.Query;
6
import org.hibernate.Hibernate;
113 fvelez 7
import org.hibernate.Criteria;
8
import static org.hibernate.criterion.Restrictions.*;
9
import org.hibernate.criterion.Criterion;
10
import org.hibernate.criterion.SimpleExpression;
179 fvelez 11
import org.apache.log4j.Logger;
1 fvelez 12
 
13
 
14
import java.util.List;
45 fvelez 15
import java.util.Date;
16
import java.io.Serializable;
1 fvelez 17
 
18
import pt.estgp.estgweb.domain.User;
45 fvelez 19
import pt.estgp.estgweb.domain.Announcement;
1 fvelez 20
 
21
/**
22
 * @author Jorge Machado
23
 * @date 28/Fev/2008
24
 * @time 2:51:06
25
 * @see pt.estgp.estgweb.domain.dao.impl
26
 */
45 fvelez 27
public class UserDaoImpl<Announcement> extends UserDao
1 fvelez 28
{
179 fvelez 29
 
30
    private static final 1.5.0/docs/api/java/util/logging/Logger.html">Logger logger = 1.5.0/docs/api/java/util/logging/Logger.html">Logger.getLogger(UserDaoImpl.class);
31
 
1 fvelez 32
    public static UserDaoImpl getInstance()
33
    {
34
        if (myInstance == null)
35
            myInstance = new UserDaoImpl();
36
        return (UserDaoImpl) myInstance;
37
    }
38
 
39
    /**
40
     * Used by the base DAO classes but here for your modification Load object
41
     * matching the given key and return it.
42
     *
43
     * @param username to load
44
     * @return a user
45
     */
46
    public final User loadByUsername(1.5.0/docs/api/java/lang/String.html">String username)
47
    {
48
        try
49
        {
50
            5+0%2Fdocs%2Fapi+List">List list =
51
                    createCriteria()
52
                            .add(eq("username",username))
53
                            .setMaxResults(1)
54
                            .list();
55
 
56
            if(list.size() == 0)
57
                return null;
58
            return (User) list.get(0);
59
        }
60
        catch (HibernateException e)
61
        {
62
            throw new DaoException(e);
63
        }
64
    }
65
    /**
66
     * Used by the base DAO classes but here for your modification Load object
67
     * matching the given key and return it.
68
     *
69
     * @param email to load
70
     * @return a user
71
     */
72
    public final User loadByEmail(1.5.0/docs/api/java/lang/String.html">String email)
73
    {
74
        try
75
        {
76
            return (User) createCriteria()
77
                    .add(eq("email",email))
78
                    .uniqueResult();
79
        }
80
        catch (HibernateException e)
81
        {
82
            throw new DaoException(e);
83
        }
84
    }
85
 
113 fvelez 86
    /**
44 fvelez 87
     * Used by the base DAO classes but here for your modification Load object
88
     * matching the given key and return it.
89
     *
90
     * @param ID to load
91
     * @return a user
92
     */
113 fvelez 93
    public final User loadByID(1.5.0/docs/api/java/lang/String.html">String ID)
38 fvelez 94
    {
95
        try
96
        {
97
            return (User) createCriteria()
98
                    .add(eq("id",ID))
99
                    .uniqueResult();
100
        }
101
        catch (HibernateException e)
102
        {
103
            throw new DaoException(e);
104
        }
105
    }
106
 
1 fvelez 107
    /**
108
     * Used by the base DAO classes but here for your modification Load object
109
     * matching the given key and return it.
110
     *
111
     * @param username to load
112
     * @param password to load
113
     * @return a user
114
     */
115
    public final User loadByUsernameAndPassword(1.5.0/docs/api/java/lang/String.html">String username, 1.5.0/docs/api/java/lang/String.html">String password)
116
    {
117
        try
118
        {
119
            return (User) createCriteria()
120
                    .add(eq("username",username))
121
                    .add(eq("password", jomm.utils.BytesUtils.getDigestMD5Hex(password)))
122
                    .uniqueResult();
123
        }
124
        catch (HibernateException e)
125
        {
126
            throw new DaoException(e);
127
        }
128
    }
129
 
113 fvelez 130
    public List<User> findUsers (1.5.0/docs/api/java/lang/String.html">String textToSearch)
131
    {
179 fvelez 132
        return findUsers(textToSearch,null);
133
    }
134
 
135
    public List<User> findUsers (1.5.0/docs/api/java/lang/String.html">String textToSearch,1.5.0/docs/api/java/lang/String.html">String typeToSearch)
136
    {
137
        Criteria criteria=null;
138
        if(typeToSearch!=null && typeToSearch.trim().length()!=0)
139
        {
140
            try
141
            {
142
                criteria = createCriteria(1.5.0/docs/api/java/lang/Class.html">Class.forName(typeToSearch));
143
            }
144
            catch (1.5.0/docs/api/java/lang/ClassNotFoundException.html">ClassNotFoundException e)
145
            {
146
                logger.error(e,e);
147
            }
148
        }
149
        else
150
        {
151
           criteria=createCriteria();
152
        }
113 fvelez 153
        Criterion name= or(like("name","%"+textToSearch+"%"),like("name","%"+textToSearch+"%"));
154
        Criterion username = or(like("username", "%" + textToSearch + "%"), like("username", "%" + textToSearch + "%"));
155
        Criterion email = or(like("email", "%" + textToSearch + "%"), like("email", "%" + textToSearch + "%"));
156
        criteria.add(or(or(name,username),email));
157
        return criteria.list();
158
    }
159
 
160
 
161
 
45 fvelez 162
    public 1.5.0/docs/api/java/io/Serializable.html">Serializable save(User obj)
163
    {
164
        try {
165
            obj.setSaveDate(new 5+0%2Fdocs%2Fapi+Date">Date());
166
            return super.save(obj);
167
        }
168
        catch (HibernateException e) {
169
            throw new DaoException(e);
170
        }
171
 
172
    }
173
 
1 fvelez 174
}