1 package org.javabb.dao.hibernate; 2 3 import java.util.Date ; 4 import java.util.List ; 5 6 import net.sf.hibernate.HibernateException; 7 import net.sf.hibernate.Session; 8 9 import org.javabb.dao.entity.ITopicDAO; 10 import org.javabb.vo.AnswerNotify; 11 import org.javabb.vo.AnswerNotifyPK; 12 import org.javabb.vo.FavUserTopic; 13 import org.javabb.vo.Forum; 14 import org.javabb.vo.Topic; 15 import org.springframework.orm.hibernate.HibernateCallback; 16 17 32 33 38 public class TopicHibernateDAO extends HibernateDAO implements ITopicDAO { 39 40 43 public Topic findLastTopic() { 44 String sql = " from {vo}Topic as p " + " order by p.idTopic desc"; 45 Topic t = null; 46 List lst = getList(sql, 0, 1); 47 48 if (!lst.isEmpty()) { 49 t = (Topic) lst.get(0); 50 } 51 52 lst = null; 53 54 return t; 55 } 56 57 60 public Integer findCountOfTopicsByForum(Forum forum) { 61 String sql = "Topic as t " + " where t.forum.idForum =" + forum.getIdForum(); 62 63 return this.countRowsOfTable(sql, "t.idTopic"); 64 } 65 66 69 public Topic load(Long id) { 70 return (Topic) getHibernateTemplate().load(Topic.class, id); 71 } 72 73 76 public Long create(Topic topic) { 77 return (Long ) getHibernateTemplate().save(topic); 78 } 79 80 83 public void delete(Long topicId) { 84 getHibernateTemplate().delete(load(topicId)); 85 } 86 87 90 public void deleteAllPostOfTopic(Long topicId) { 91 Topic topic = load(topicId); 93 this.getHibernateTemplate().deleteAll(topic.getPosts()); 94 } 95 96 99 public void lockTopic(Topic topic, Integer lock) { 100 topic = load(topic.getId()); 101 topic.setTopicStatus(lock); 102 } 103 104 107 public void moveTopic(Topic topic, Long idForumDest) { 108 109 String sql = "update jbb_topics set id_forum=" + idForumDest 110 + " where id_forum=" 111 + topic.getForum().getIdForum() 112 + " and id_topic=" 113 + topic.getIdTopic(); 114 this.executeSQL(sql); 115 116 117 } 118 119 122 public List findAll() { 123 return findAll(Topic.class, "o.idTopic", ALL_PAGES, 0); 124 } 125 126 129 public int countTopicsByForum(Long forumId, Integer forumModel) { 130 return countRowsWhere(Topic.class, 131 "o.idTopic", 132 "o.forum.idForum=? and o.topicModel=?", 133 new Object [] { forumId, forumModel}); 134 } 135 136 139 public List findByForum(Long forumId, Integer forumModel, int pageNumber, int itemsPerPage) { 140 149 150 String sql = " SELECT o FROM Topic as o " + 151 " inner join fetch o.user as user " + 152 " inner join fetch o.forum as forum " + 153 " inner join fetch forum.category as cat " + 154 " WHERE o.forum.idForum=? and o.topicModel=? " + 155 " ORDER BY o.lastPostDate desc, o.idTopic desc " + 156 " "; 157 158 List topics = find(sql, new Object [] { forumId, forumModel },pageNumber,itemsPerPage); 159 160 return topics; 161 162 } 163 164 167 public List findByForum(Long forumId, Integer forumModel) { 168 169 return find(Topic.class, 170 "o.forum.idForum=? and o.topicModel=?", 171 new Object [] { forumId, forumModel }, 172 "o.lastPostDate desc, o.idTopic desc", 173 ALL_PAGES, 0); 174 } 175 176 177 180 public List findPostedAfter(Date date, int pageNumber, int itemsPerPage) { 181 182 return find(Topic.class, 183 "o.lastPostDate >= ?", 184 new Object [] { date }, 185 "o.lastPostDate DESC", 186 pageNumber, 187 itemsPerPage); 188 } 189 190 193 public int countPostedAfter(Date date) { 194 if (logger.isDebugEnabled()) { 195 logger.debug("count posts after " + date); 196 } 197 return countRowsWhere(Topic.class, 198 "o.idTopic", 199 "o.lastPostDate >= ?", 200 new Object [] { date }); 201 } 202 203 206 public void update( Topic topic ) { 207 getHibernateTemplate().update(topic); 208 } 209 210 213 public List wathTopicByTopicUser(Long userId, Long topicId) { 214 return find(AnswerNotify.class, 215 "o.user.idUser = ? AND o.topic.idTopic = ?", 216 new Object [] { userId, topicId }, 217 "o.user.idUser asc", 218 ALL_PAGES, 0); 219 } 220 221 222 public List favoriteTopicByTopicUser(Long userId, Long topicId) { 223 return find(FavUserTopic.class, 224 "o.user.idUser = ? AND o.topic.idTopic = ?", 225 new Object [] { userId, topicId }, 226 "o.user.idUser asc", 227 ALL_PAGES, 0); 228 } 229 230 231 public List favoriteTopicByTopic(Long topicId) { 232 return find(FavUserTopic.class, 233 "o.topic.idTopic = ?", 234 new Object [] { topicId }, 235 "o.topic.idTopic asc", 236 ALL_PAGES, 0); 237 } 238 239 242 public void insertWatchTopicUser(Long topicId, Long userId){ 243 AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId)); 244 this.getHibernateTemplate().save(answerNotify); 245 } 246 247 250 public void deleteWatchTopicUser(Long topicId, Long userId){ 251 AnswerNotify answerNotify = new AnswerNotify(new AnswerNotifyPK(topicId, userId)); 252 this.getHibernateTemplate().delete(answerNotify); 253 } 254 255 258 public List wathTopicByUser(Long userId) { 259 return find(AnswerNotify.class, 260 "o.user.idUser = ?", 261 new Object [] { userId }, 262 "o.user.idUser asc", 263 ALL_PAGES, 0); 264 } 265 266 267 public List favoriteTopicByUser(Long userId) { 268 return find(FavUserTopic.class, 269 "o.user.idUser = ?", 270 new Object [] { userId }, 271 "o.user.idUser asc", 272 ALL_PAGES, 0); 273 } 274 275 276 279 public List wathTopicByTopic(Long topicId) { 280 return find(AnswerNotify.class, 281 "o.topic.idTopic = ?", 282 new Object [] { topicId }, 283 "o.topic.idTopic asc", 284 ALL_PAGES, 0); 285 } 286 287 290 public List findLastTopics(int limit) { 291 String sql = " SELECT o FROM Topic as o " + 292 " inner join fetch o.user as user " + 293 " inner join fetch o.forum as forum " + 294 " ORDER BY o.lastPostId desc "; 295 return find(sql, new Object [] {}, 1, limit); 296 } 297 298 301 public List favoriteTopics() { 302 HibernateCallback callback = new HibernateCallback() { 303 public Object doInHibernate(Session session) 304 throws HibernateException { 305 String hql = "SELECT distinct f.topic.idTopic from FavUserTopic as f order by f.topic.idTopic desc"; 306 List favs = find(hql, new Object [] {}, ALL_PAGES, 50); 307 308 if (!favs.isEmpty()) { 309 hql = "SELECT t from Topic as t where t.idTopic in ("; 310 for (int i = 0; i < favs.size(); i++) { 311 Long fav = (Long )favs.get(i); 312 hql += fav + ","; 313 } 314 hql = hql.substring(0, hql.length() - 1); 315 hql += ")"; 316 317 favs = session.find(hql); 318 } 319 return favs; 320 } 321 }; 322 return getHibernateTemplate().executeFind(callback); 323 } 324 325 } 326 | Popular Tags |