Statistics
| Branch: | Revision:

m2u-upass-core / src / my / com / upass / dao / hibernate / UserDAOHibernate.java @ 0:02300db8682b

History | View | Annotate | Download (8.13 KB)

1
/**
2
 * Copyright (M) 2011 Penril Datability (M) Sdn Bhd All rights reserved.
3
 *
4
 * This software is copyrighted. Under the copyright laws, this software
5
 * may not be copied, in whole or in part, without prior written consent
6
 * of Penril Datability (M) Sdn Bhd or its assignees. This software is
7
 * provided under the terms of a license between Penril Datability (M)
8
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
9
 * license.
10
 */
11

    
12
package my.com.upass.dao.hibernate;
13

    
14
import java.util.List;
15

    
16
import my.com.upass.Constants;
17
import my.com.upass.dao.UserDAO;
18
import my.com.upass.pojo.UserBean;
19
import my.com.upass.pojo.UserBeanBackup;
20
import net.penril.generic.hibernate.GenericDAOHibernate;
21

    
22
import org.apache.log4j.Logger;
23
import org.hibernate.Criteria;
24
import org.hibernate.FetchMode;
25
import org.hibernate.HibernateException;
26
import org.hibernate.Session;
27
import org.hibernate.criterion.Restrictions;
28

    
29
/**
30
 * 
31
 * PROGRAMMER: Danniell CHANGE-NO: TASK-NO: DATE CREATED: Aug 3, 2011 TAG AS:
32
 * REASON(S): MODIFICATION:
33
 */
34

    
35
public class UserDAOHibernate extends GenericDAOHibernate<UserBean, Long>
36
                implements UserDAO
37
{
38
        private static Logger        logger        = Logger.getLogger (UserDAOHibernate.class);
39

    
40
        /**
41
         * @see my.com.upass.dao.UserDAO#getUserFromStore(java.lang.String, int)
42
         */
43
        @Override
44
        public UserBean getUserFromStore (String userAlias, int sqlMode)
45
        {
46
                UserBean profile = null;
47
                Criteria criteria;
48
                Session session = null;
49

    
50
                try
51
                {
52
                        session = getSession ();
53
                        criteria = session.createCriteria (UserBean.class);
54
                        criteria.add (Restrictions.eq ("userAlias", userAlias));
55

    
56
                        switch (sqlMode)
57
                        {
58
                                case Constants.MODE_QTACBEAN:
59
                                        criteria.setFetchMode ("tacBean", FetchMode.JOIN);
60
                                        break;
61
                                case Constants.MODE_QVASCOBEAN:
62
                                        criteria.setFetchMode ("upassUserTokens", FetchMode.JOIN);
63
                                        break;
64
                                default:
65
                                        break;
66
                        }
67

    
68
                        profile = (UserBean) criteria.uniqueResult ();
69
                }
70
                catch (Exception e)
71
                {
72
                        logger.error (e, e);
73
                }
74
                finally
75
                {
76
                        if (session != null && session.isConnected ())
77
                        {
78
                                session.close ();
79
                        }
80
                }
81

    
82
                return profile;
83
        }
84

    
85
        /**
86
         * @see my.com.upass.dao.UserDAO#getUserFromStore(java.lang.String)
87
         */
88
        @Override
89
        public UserBean getUserFromStore (String userAlias)
90
        {
91
                return getUserFromStore (userAlias, Constants.MODE_QUSERBEAN);
92
        }
93

    
94
        /**
95
         * @see my.com.upass.dao.UserDAO#updateUserToStore(my.com.upass.pojo.UserBean)
96
         */
97
        @Override
98
        public boolean updateUserToStore (UserBean userBean)
99
        {
100
                boolean isUpdated = false;
101
                Session session = null;
102

    
103
                try
104
                {
105
                        session = getSession ();
106
                        session.beginTransaction ();
107
                        session.update (userBean);
108
                        session.getTransaction ().commit ();
109
                        isUpdated = true;
110
                }
111
                catch (Exception e)
112
                {
113
                        if (e instanceof HibernateException)
114
                        {
115
                                if (session != null)
116
                                {
117
                                        session.getTransaction ().rollback ();
118
                                }
119
                        }
120
                        logger.error (e, e);
121
                }
122
                finally
123
                {
124
                        if (session != null && session.isConnected ())
125
                        {
126
                                session.close ();
127
                        }
128
                }
129
                return isUpdated;
130
        }
131

    
132
        /**
133
         * @see my.com.upass.dao.UserDAO#insertUserToStore(my.com.upass.pojo.UserBean)
134
         */
135
        @Override
136
        public boolean insertUserToStore (UserBean userBean)
137
        {
138
                boolean isCreated = false;
139
                Session session = null;
140

    
141
                try
142
                {
143
                        session = getSession ();
144
                        session.beginTransaction ();
145
                        session.save (userBean);
146
                        session.getTransaction ().commit ();
147
                        isCreated = true;
148
                }
149
                catch (Exception e)
150
                {
151
                        if (e instanceof HibernateException)
152
                        {
153
                                if (session != null)
154
                                {
155
                                        session.getTransaction ().rollback ();
156
                                }
157
                        }
158
                        logger.error (e, e);
159
                }
160
                finally
161
                {
162
                        if (session != null && session.isConnected ())
163
                        {
164
                                session.close ();
165
                        }
166
                }
167
                return isCreated;
168
        }
169

    
170
        /**
171
         * @see my.com.upass.dao.UserDAO#updateUserToStore(my.com.upass.pojo.UserBean)
172
         */
173
        @Override
174
        public boolean deleteUser (UserBean userBean)
175
        {
176
                boolean isUpdated = false;
177
                Session session = null;
178

    
179
                try
180
                {
181
                        session = getSession ();
182
                        session.beginTransaction ();
183
                        session.delete (userBean);
184
                        session.getTransaction ().commit ();
185
                        isUpdated = true;
186
                }
187
                catch (Exception e)
188
                {
189
                        if (e instanceof HibernateException)
190
                        {
191
                                if (session != null)
192
                                {
193
                                        session.getTransaction ().rollback ();
194
                                }
195
                        }
196
                        logger.error (e, e);
197
                }
198
                finally
199
                {
200
                        if (session != null && session.isConnected ())
201
                        {
202
                                session.close ();
203
                        }
204
                }
205
                return isUpdated;
206
        }
207

    
208
        /**
209
         * @see my.com.upass.dao.UserDAO#insertUserToStore(my.com.upass.pojo.UserBean)
210
         */
211
        @Override
212
        public boolean insertUserToBackupStore (UserBeanBackup userBean)
213
        {
214
                boolean isCreated = false;
215
                Session session = null;
216

    
217
                try
218
                {
219
                        session = getSession ();
220
                        session.beginTransaction ();
221
                        session.save (userBean);
222
                        session.getTransaction ().commit ();
223
                        isCreated = true;
224
                }
225
                catch (Exception e)
226
                {
227
                        if (e instanceof HibernateException)
228
                        {
229
                                if (session != null)
230
                                {
231
                                        session.getTransaction ().rollback ();
232
                                }
233
                        }
234
                        logger.error (e, e);
235
                }
236
                finally
237
                {
238
                        if (session != null && session.isConnected ())
239
                        {
240
                                session.close ();
241
                        }
242
                }
243
                return isCreated;
244
        }
245

    
246
        public UserBeanBackup getUserBackupFromStore (String userAlias)
247
        {
248
                UserBeanBackup profile = null;
249
                Criteria criteria;
250
                Session session = null;
251

    
252
                try
253
                {
254
                        session = getSession ();
255
                        criteria = session.createCriteria (UserBeanBackup.class);
256
                        criteria.add (Restrictions.eq ("userAlias", userAlias));
257

    
258
                        profile = (UserBeanBackup) criteria.uniqueResult ();
259
                }
260
                catch (Exception e)
261
                {
262
                        logger.error (e, e);
263
                }
264
                finally
265
                {
266
                        if (session != null && session.isConnected ())
267
                        {
268
                                session.close ();
269
                        }
270
                }
271

    
272
                return profile;
273
        }
274

    
275
        @Override
276
        public UserBean getTbAmUserByUserAlias (String userAlias) throws Exception
277
        {
278
                UserBean TbAmUser;
279
                try
280
                {
281
                        getSession ().beginTransaction ();
282

    
283
                        Criteria m_oCriteria = getSession ()
284
                                        .createCriteria (UserBean.class);
285
                        m_oCriteria.add (Restrictions.eq ("userAlias", userAlias));
286
                        TbAmUser = (UserBean) m_oCriteria.uniqueResult ();
287

    
288
                        getSession ().getTransaction ().commit ();
289
                }
290
                catch (Exception e)
291
                {
292
                        if (e instanceof HibernateException)
293
                        {
294
                                getSession ().getTransaction ().rollback ();
295
                        }
296
                        throw e;
297
                }
298
                finally
299
                {
300
                        getSession ().close ();
301
                }
302
                return TbAmUser;
303
        }
304

    
305
        public UserBean getTbAmUserByUserId (Long tUserId) throws Exception
306
        {
307
                UserBean TbAmUser;
308
                try
309
                {
310
                        getSession ().beginTransaction ();
311

    
312
                        Criteria m_oCriteria = getSession ()
313
                                        .createCriteria (UserBean.class);
314
                        m_oCriteria
315
                                        .add (Restrictions.eq ("userID", Long.valueOf (tUserId)));
316
                        TbAmUser = (UserBean) m_oCriteria.uniqueResult ();
317

    
318
                        getSession ().getTransaction ().commit ();
319
                }
320
                catch (Exception e)
321
                {
322
                        if (e instanceof HibernateException)
323
                        {
324
                                getSession ().getTransaction ().rollback ();
325
                        }
326
                        throw e;
327
                }
328
                finally
329
                {
330
                        getSession ().close ();
331
                }
332
                return TbAmUser;
333
        }
334

    
335
        public void deleteTbAmUser (Integer tUserId) throws Exception
336
        {
337
                // ID ID = tUserId;
338
                Long ID = Long.valueOf (String.valueOf (tUserId));
339
                UserBean tbAmUser;
340
                try
341
                {
342
                        getSession ().beginTransaction ();
343
                        Criteria m_oCriteria = getSession ()
344
                                        .createCriteria (UserBean.class);
345
                        m_oCriteria.add (Restrictions.eq ("userId", ID));
346
                        tbAmUser = (UserBean) m_oCriteria.uniqueResult ();
347

    
348
                        makeTransient (tbAmUser);
349
                        getSession ().getTransaction ().commit ();
350
                        getSession ().close ();
351
                }
352
                catch (Exception e)
353
                {
354
                        getSession ().getTransaction ().rollback ();
355
                        throw e;
356
                }
357
                finally
358
                {
359
                        getSession ().close ();
360
                }
361
        }
362
        
363
        public List<UserBean> getAllUser ()
364
        {
365
                List<UserBean> profile = null;
366
                Criteria criteria;
367
                Session session = null;
368

    
369
                try
370
                {
371
                        session = getSession ();
372
                        criteria = session.createCriteria (UserBean.class);
373
                        
374
                        profile = criteria.list();
375

    
376
                }
377
                catch (Exception e)
378
                {
379
                        logger.error (e, e);
380
                }
381
                finally
382
                {
383
                        if (session != null && session.isConnected ())
384
                        {
385
                                session.close ();
386
                        }
387
                }
388

    
389
                return profile;
390
        }
391

    
392
}