Subversion Repositories bacoAlunos

Rev

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