1 25 26 package com.j2biz.blogunity.dao; 27 28 import java.io.Serializable ; 29 import java.util.List ; 30 31 import net.sf.hibernate.Criteria; 32 import net.sf.hibernate.HibernateException; 33 import net.sf.hibernate.Query; 34 import net.sf.hibernate.Session; 35 import net.sf.hibernate.expression.Expression; 36 import net.sf.hibernate.expression.MatchMode; 37 38 import org.apache.commons.logging.Log; 39 import org.apache.commons.logging.LogFactory; 40 41 import com.j2biz.blogunity.BlogunityManager; 42 import com.j2biz.blogunity.exception.BlogunityException; 43 import com.j2biz.blogunity.i18n.I18N; 44 import com.j2biz.blogunity.i18n.I18NStatusFactory; 45 import com.j2biz.blogunity.pojo.SystemConfiguration; 46 import com.j2biz.blogunity.pojo.User; 47 import com.j2biz.blogunity.util.HibernateUtil; 48 import com.j2biz.blogunity.util.PasswordEncryptionService; 49 50 57 public class UserDAO extends AbstractDAO { 58 61 private static final Log log = LogFactory.getLog(UserDAO.class); 62 63 66 public UserDAO() { 67 super(); 68 } 69 70 77 public User getUserByID(long id) throws BlogunityException { 78 return getUserByID(new Long (id)); 79 } 80 81 88 public User getUserByID(Long id) throws BlogunityException { 89 User user = null; 90 try { 91 user = (User) getByID(User.class, id); 92 } catch (HibernateException ex) { 93 log.error("getUserByID(Long)", ex); 94 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_ID, 95 "User", ex)); 96 } 97 98 return user; 99 } 100 101 109 public User getUserByNameAndPassword(String nickname, String password) 110 throws BlogunityException { 111 112 Session session = HibernateUtil.getSession(); 113 try { 114 117 boolean isEncryptionEnbaled = false; 119 SystemConfiguration config = BlogunityManager.getSystemConfiguration(); 120 if (config != null) isEncryptionEnbaled = config.isPasswordEncriptionEnabled(); 121 122 String encodedPass = password; 123 if (isEncryptionEnbaled) { 124 try { 125 encodedPass = PasswordEncryptionService.encrypt(password, BlogunityManager 126 .getSystemConfiguration().getPasswordEncryptionType()); 127 } catch (Throwable t) { 128 throw new BlogunityException(I18NStatusFactory.createUnknown(t)); 129 } 130 } 131 132 Query query = session 133 .createQuery("from User u where u.nickname = :nickname and u.password = :password"); 134 query.setString("nickname", nickname); 135 query.setString("password", encodedPass); 136 137 return (User) query.uniqueResult(); 138 139 } catch (HibernateException ex) { 140 log.error("getUserByNameAndPassword(String, String)", ex); 141 throw new BlogunityException(I18NStatusFactory.create( 142 I18N.ERRORS.USER_BY_NICKNAME_AND_PASS_NOT_FOUND, ex)); 143 } 144 } 145 146 153 public User getUserByName(String nickname) throws BlogunityException { 154 155 Session session = HibernateUtil.getSession(); 156 try { 157 Query query = session.createQuery("from User u where u.nickname = :nickname"); 160 query.setString("nickname", nickname); 161 162 return (User) query.uniqueResult(); 163 164 } catch (HibernateException ex) { 165 log.error("getUserByName(String)", ex); 166 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.NOT_FOUND_BY_NAME, 167 "User", ex)); 168 } 169 170 } 171 172 179 public Serializable createUser(User user) throws BlogunityException { 180 try { 181 return create(user); 182 } catch (HibernateException e) { 183 log.error("createUser(User)", e); 184 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.CREATE, "User", e)); 185 } 186 } 187 188 194 public void deleteUser(User user) throws BlogunityException { 195 try { 196 delete(user); 197 } catch (HibernateException e) { 198 log.error("deleteUser(User)", e); 199 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.DELETE, "User", e)); 200 } 201 } 202 203 209 public void updateUser(User user) throws BlogunityException { 210 try { 211 update(user); 212 } catch (HibernateException e) { 213 log.error("updateUser(User)", e); 214 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.UPDATE, "User", e)); 215 } 216 } 217 218 224 public void attachToSession(User user) throws BlogunityException { 225 226 Session session = HibernateUtil.getSession(); 227 try { 228 229 232 session.refresh(user); 235 236 } catch (HibernateException e) { 237 log.error("attachToSession(user)", e); 238 throw new BlogunityException(I18NStatusFactory.create( 239 I18N.ERRORS.USER_UNABLE_ATTACH_TO_SESSION, e)); 240 241 } 242 } 243 244 251 public List getLastRegisteredUsers(int count) throws BlogunityException { 252 Session session = HibernateUtil.getSession(); 253 try { 254 Query q = session.createQuery("from User u order by u.registerTime desc"); 255 q.setFirstResult(0); 256 q.setMaxResults(count); 257 258 List returnList = q.list(); 259 return returnList; 260 } catch (HibernateException e) { 261 log.error("getLastRegisteredUsers(int)", e); 262 throw new BlogunityException(I18NStatusFactory.create( 263 I18N.ERRORS.USER_FETCH_LAST_REGISTERED, e)); 264 } 265 } 266 267 273 public List getAllUsers() throws BlogunityException { 274 Session session = HibernateUtil.getSession(); 275 try { 276 Query q = session.createQuery("from User u order by u.registerTime desc"); 277 return q.list(); 278 } catch (HibernateException e) { 279 log.error("getAllUsers()", e); 280 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.USER_FETCH_ALL, e)); 281 } 282 } 283 284 291 public List getFriendOfList(User u) throws BlogunityException { 292 Session session = HibernateUtil.getSession(); 293 try { 294 Criteria criteria = session.createCriteria(User.class).createCriteria("friends").add( 295 Expression.eq("id", u.getId())); 296 297 List returnList = criteria.list(); 298 return returnList; 299 300 } catch (HibernateException e) { 301 log.error("getFriendOfList(User)", e); 302 throw new BlogunityException(I18NStatusFactory.create( 303 I18N.ERRORS.USER_FETCH_FRIEND_OF_LIST, e)); 304 } 305 } 306 307 315 public List getUsersWithNicknameLike(String likeNickname) throws BlogunityException { 316 Session session = HibernateUtil.getSession(); 317 try { 318 319 Criteria criteria = session.createCriteria(User.class); 320 criteria.add(Expression.like("nickname", likeNickname, MatchMode.ANYWHERE)); 321 return criteria.list(); 322 323 } catch (HibernateException e) { 324 log.error("getUsersWithNicknameLike(likeNickname)", e); 325 throw new BlogunityException(I18NStatusFactory.create(I18N.ERRORS.LIKE_NAME_NOT_FOUND, 326 new String []{"users", likeNickname}, e)); 327 } 328 } 329 } | Popular Tags |