Subversion Repositories bacoAlunos

Rev

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