1 package org.javabb.dao.hibernate; 2 3 import java.sql.SQLException ; 4 import java.text.MessageFormat ; 5 import java.util.ArrayList ; 6 import java.util.Collections ; 7 import java.util.Comparator ; 8 import java.util.List ; 9 10 import net.sf.hibernate.Criteria; 11 import net.sf.hibernate.HibernateException; 12 import net.sf.hibernate.Query; 13 import net.sf.hibernate.Session; 14 import net.sf.hibernate.expression.Expression; 15 import net.sf.hibernate.expression.Order; 16 import net.sf.hibernate.type.LongType; 17 18 import org.javabb.dao.entity.IPostDAO; 19 import org.javabb.infra.Paging; 20 import org.javabb.lucene.index.Indexer; 21 import org.javabb.lucene.search.LuceneSearcher; 22 import org.javabb.vo.Forum; 23 import org.javabb.vo.Post; 24 import org.javabb.vo.PostText; 25 import org.javabb.vo.Topic; 26 import org.springframework.orm.hibernate.HibernateCallback; 27 28 43 44 52 public class PostHibernateDAO extends HibernateDAO implements IPostDAO { 53 54 private LuceneSearcher searcher; 55 56 private Indexer indexer; 57 58 private String [] fields = new String [] { "text", "subject" }; 59 60 63 public LuceneSearcher getSearcher() { 64 65 return searcher; 66 67 } 68 69 73 public void setSearcher(LuceneSearcher seacher) { 74 75 this.searcher = seacher; 76 77 } 78 79 83 public void setIndexer(Indexer indexer) { 84 85 this.indexer = indexer; 86 } 87 88 91 public Indexer getIndexer() { 92 93 return indexer; 94 } 95 96 99 public Post load(Long id) { 100 101 return (Post) getHibernateTemplate().load(Post.class, id); 102 } 103 104 107 public Long create(Post post) { 108 109 Long result = (Long ) getHibernateTemplate().save(post); 110 111 indexer.index(post); 112 113 return result; 114 } 115 116 119 public void delete(Long postId) { 120 121 getHibernateTemplate().delete( 122 "FROM " + Post.class.getName() + " o WHERE o.idPost=?", postId, 123 new LongType()); 124 125 getHibernateTemplate().delete( 126 "FROM " + PostText.class.getName() + " o WHERE o.idPost=?", 127 postId, new LongType()); 128 129 indexer.delete(postId); 130 } 131 132 135 public int countPostsByTopic(Long idTopic) { 136 137 return countRowsWhere(Post.class, "o.idPost", "o.topic.idTopic=?", 138 new Object [] { idTopic }); 139 } 140 141 144 public List findByTopic(Long topicId, int pageNumber, int itemsPerPage) { 145 146 return find(Post.class, "o.topic.idTopic=?", new Object [] { topicId }, 147 "o.idPost ASC", pageNumber, itemsPerPage); 148 } 149 150 153 public List findByUser(final Long userId, final int pageNumber, 154 final int itemsPerPage) { 155 return getHibernateTemplate().executeFind(new HibernateCallback() { 156 public Object doInHibernate(Session session) 157 throws HibernateException, SQLException { 158 return session.createCriteria(Post.class) .add(Expression.eq("user.idUser", userId)) .addOrder(Order.desc("idPost")) .setFirstResult((pageNumber - 1) * itemsPerPage) .setMaxResults(itemsPerPage) .list(); 164 } 165 }); 166 } 167 168 public int countPostsByUser(Long userId) { 169 return countRowsWhere(Post.class, "o.idPost", "o.user.idUser=?", 170 new Object [] { userId }); 171 } 172 173 176 public List findUnanswered(int pageNumber, int itemsPerPage) { 177 178 String hql = "SELECT new Post(" 179 + "post.topic.id, post.topic.pageLastPost, post.id," 180 + "post.topic.titleTopic, post.topic.forum.id, post.topic.forum.nome, " 181 + "post.postDate, post.user.id, post.user.user, post.topic.respostas, " 182 + "post.topic.visualizacoes) " 183 + " FROM Post as post where post.topic.respostas = ? ORDER BY post.idPost DESC"; 184 185 return find(hql , new Object [] {new Integer (0)}, pageNumber, itemsPerPage); 186 } 187 188 public int countUnanswered() { 189 return countRowsWhere(Post.class, "o.idPost", "o.topic.respostas = ?", 190 new Object [] { new Integer (0) }); 191 } 192 193 196 public void update(Post post) { 197 198 getHibernateTemplate().update(post); 199 200 indexer.update(post); 201 } 202 203 206 public List findByTopicDesc(Topic topic) { 207 208 String [] whereField = { "topic.idTopic" }; 209 String [] whereValue = { String.valueOf(topic.getIdTopic()) }; 210 String [] orderBy = { "postDate" }; 211 String [] orderType = { "desc" }; 212 213 return findAll(PostText.class, whereField, whereValue, orderBy, 214 orderType); 215 216 } 217 218 221 public List findLastPosts(int limit) { 222 223 String [] orderBy = { "idPost" }; 224 String [] orderType = { "desc" }; 225 return findAll(PostText.class, orderBy, orderType, 1, limit); 226 } 227 228 231 public Post findLastPost() { 232 233 String [] orderBy = { "idPost" }; 234 String [] orderType = { "desc" }; 235 Post p = null; 236 List lst = findAll(Post.class, orderBy, orderType, 1, 1); 237 238 if ((lst != null) && !lst.isEmpty()) { 239 p = (Post) lst.get(0); 240 } 241 return p; 242 } 243 244 247 public Integer findCountOfPostsByForum(Forum forum) { 248 249 String sql = "Post as p where p.topic.forum.idForum =" 250 + forum.getIdForum(); 251 252 return this.countRowsOfTable(sql, "p.idPost"); 253 254 } 255 256 public int getTotalRowsOfLucene(String query) { 257 List ids = searchIdsByQueryAndFilds(query, fields); 258 if (ids != null) { 259 return ids.size(); 260 } 261 return 0; 262 } 263 264 267 public List search(String query, int pageNumber, int itemsPerPage) { 268 269 int first = Math.max(0, pageNumber - 1) * itemsPerPage; 270 first = Math.max(0, first); 271 int limit = Math.round(pageNumber * itemsPerPage); 273 274 List ids = searchIdsByQueryAndFilds(query, fields); 275 276 long postsCount = ids.size(); 278 int totalPages = Paging.getNroPages(itemsPerPage, postsCount); 279 Paging.setPageList(pageNumber, totalPages); 280 281 return search(ids, first, limit); 283 } 284 285 291 public List searchIdsByQueryAndFilds(String query, String [] fields) { 292 return searcher.search(query, fields); 293 } 294 295 305 private List search(final List ids, final int start, final int limit) { 306 307 HibernateCallback callback = new HibernateCallback() { 308 public Object doInHibernate(Session session) 309 throws HibernateException { 310 List result = new ArrayList (); 311 if (!ids.isEmpty()) { 312 int _start = Math.max(0, start); 313 int _limit = Math.min(limit, ids.size()); 314 List subListIds = ids.subList(_start, _limit); 315 String hql = "SELECT new PostText(" 316 + "post.topic.id, post.topic.pageLastPost, post.id," 317 + "post.topic.titleTopic, post.topic.forum.id, post.topic.forum.nome, " 318 + "post.postDate, post.user.id, post.user.user, post.topic.respostas, " 319 + "post.topic.visualizacoes, post.postBody) " 320 + " FROM PostText as post where post.idPost in"; 321 322 hql += "("; 323 if (!subListIds.isEmpty()) { 324 for (int i = 0; i < subListIds.size(); i++) { 325 hql += subListIds.get(i) + ","; 326 } 327 hql = hql.substring(0, hql.length() - 1); 328 hql += ")"; 329 330 result = session.find(hql); 331 } 332 } 333 return result; 334 } 335 }; 336 337 Comparator sorter = new LuceneComparator(ids); 338 339 List result = getHibernateTemplate().executeFind(callback); 340 341 Collections.sort(result, sorter); 342 343 return result; 344 345 } 346 347 public List search(final String query, final int page) { 348 return (List ) getHibernateTemplate().execute(new HibernateCallback() { 349 public Object doInHibernate(Session session) 350 throws HibernateException, SQLException { 351 Criteria criteria = session.createCriteria(PostText.class); 352 353 String [] tokens = query.split(" "); 354 for (int i = 0; i < tokens.length; i++) { 355 String token = tokens[i]; 356 criteria.add(Expression.like("postBody", token)); 357 } 358 return criteria.list(); 360 } 361 }); 362 } 363 364 367 public int countAllPosts() { 368 369 return countRows(Post.class, "idPost"); 370 } 371 372 private static class LuceneComparator implements Comparator { 373 374 private final List ids; 375 376 private LuceneComparator(List ids) { 377 378 this.ids = ids; 379 380 } 381 382 385 public int compare(final Object o1, final Object o2) { 386 387 final Long id1 = ((Post) o1).getId(); 388 final Long id2 = ((Post) o2).getId(); 389 for (int i = 0; i != ids.size(); i++) { 390 final Long longId = (Long ) ids.get(i); 391 if (longId.equals(id1)) { 392 return -1; 393 } 394 if (longId.equals(id2)) { 395 return 1; 396 } 397 } 398 return 0; 399 } 400 401 } 402 403 public List findByForumDesc(Forum forum) { 404 return null; 406 } 407 408 } 409 | Popular Tags |