Subversion Repositories bacoAlunos

Rev

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