1 package org.javabb.transaction; 2 3 import java.util.Collections ; 4 import java.util.HashMap ; 5 import java.util.HashSet ; 6 import java.util.Iterator ; 7 import java.util.List ; 8 import java.util.Map ; 9 import java.util.Set ; 10 11 import org.javabb.component.VelocityTemplate; 12 import org.javabb.dao.DAOConstants; 13 import org.javabb.dao.entity.IPostDAO; 14 import org.javabb.dao.entity.ITopicDAO; 15 import org.javabb.infra.Configuration; 16 import org.javabb.infra.ConfigurationFactory; 17 import org.javabb.infra.Constants; 18 import org.javabb.infra.Email; 19 import org.javabb.infra.Paging; 20 import org.javabb.infra.UserContext; 21 import org.javabb.lucene.index.Indexer; 22 import org.javabb.vo.AnswerNotify; 23 import org.javabb.vo.Forum; 24 import org.javabb.vo.Post; 25 import org.javabb.vo.Topic; 26 import org.javabb.vo.User; 27 28 43 44 51 public class PostTransaction extends Transaction { 52 53 private IPostDAO _postDAO; 54 55 private ITopicDAO _topicDAO; 56 57 private UserTransaction _userTransaction; 58 59 63 public void setUserTransaction(UserTransaction userTransaction) { 64 this._userTransaction = userTransaction; 65 } 66 67 71 public void setPostDAO(IPostDAO postDAO) { 72 this._postDAO = postDAO; 73 } 74 75 78 public void setTopicDAO(ITopicDAO topicDAO) { 79 this._topicDAO = topicDAO; 80 } 81 82 private Indexer indexer; 83 84 public void setIndexer(Indexer indexer) { 85 this.indexer = indexer; 86 } 87 88 92 96 public Post loadPost(Long id) { 97 return _postDAO.load(id); 98 } 99 100 107 public List findByTopic(Long topicId, int pageNumber) { 108 109 int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue(); 111 long postCountInTopic = _postDAO.countPostsByTopic(topicId); 112 int pageCount = Paging.getNroPages(itemsPerPage, postCountInTopic); 113 Paging.setPageList(pageNumber, pageCount); 114 115 return _postDAO.findByTopic(topicId, pageNumber, itemsPerPage); 116 117 } 118 119 122 public int findIdLastPost() { 123 124 Post p = _postDAO.findLastPost(); 125 int i = -1; 126 127 if (p != null) { 128 i = p.getIdPost().intValue(); 129 } 130 131 return i; 132 133 } 134 135 141 public Post findbyTopicDesc(Topic topic) { 142 143 Post p = null; 144 List lst = _postDAO.findByTopicDesc(topic); 145 146 if (!lst.isEmpty()) { 147 148 p = (Post) lst.get(0); 149 150 } 151 return p; 152 } 153 154 public List findPagesByTopic(Topic topic) { 155 int rowsPerPage = ConfigurationFactory.getConf().postsPage.intValue(); 157 long nroRecords = topic.getRespostas().intValue() + 1; 158 int totalPages = Paging.getNroPages(rowsPerPage, nroRecords); 159 160 return Paging.createQuickPaging(totalPages); 161 } 162 163 169 public Post findLastPostByForum(Forum forum) { 170 171 Post p = null; 172 List lst = _postDAO.findByForumDesc(forum); 173 174 if (!lst.isEmpty()) { 175 176 p = (Post) lst.get(0); 177 178 } 179 180 lst = null; 181 182 return p; 183 184 } 185 186 193 public Integer getPageOfLastPostByTopic(Topic topic) { 194 195 int rowsPerPage = ConfigurationFactory.getConf().postsPage.intValue(); 197 long nroRecords = _postDAO.countPostsByTopic(topic.getIdTopic()); 198 int totalPages = Paging.getNroPages(rowsPerPage, nroRecords); 199 200 return new Integer (totalPages); 201 202 } 203 204 208 public Integer findCountOfPostsByForum(Forum forum) { 209 Integer number = _postDAO.findCountOfPostsByForum(forum); 210 if (number == null) { 211 number = new Integer (0); 212 } 213 return number; 214 } 215 216 222 public boolean canDeletePost(Post post) { 223 224 if (!UserContext.getContext().isAuthenticated()) { 225 return false; 226 } 227 228 User user = UserContext.getContext().getUser(); 229 230 if (user.isAdministrator()) { 231 return true; 232 } 233 post = loadPost(post.getId()); 234 235 return (post.getUser().getId().equals(user.getId())); 236 } 237 238 241 public void deleteAllPostsByTopic(Topic topic) { 242 List posts = this.findByTopic(topic.getIdTopic(), 1); 243 Iterator it = posts.iterator(); 244 while (it.hasNext()) { 245 Post post = (Post) it.next(); 246 _userTransaction.subNumberMsgUser(post.getUser().getIdUser()); 247 } 248 _topicDAO.deleteAllPostOfTopic(topic.getId()); 249 } 250 251 255 public List listPostsByTopicRev(Topic topic) { 256 List posts = _postDAO.findByTopic(topic.getId(), 257 DAOConstants.ALL_PAGES, 0); 258 Collections.reverse(posts); 259 return posts; 260 } 261 262 266 public List listPostsByUser(Long userId, int pageNumber) { 267 268 int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue(); 270 long postsCount = _postDAO.countPostsByUser(userId); 271 int totalPages = Paging.getNroPages(itemsPerPage, postsCount); 272 Paging.setPageList(pageNumber, totalPages); 273 274 List posts = _postDAO.findByUser(userId, pageNumber, itemsPerPage); 275 return posts; 276 } 277 278 281 public List listUnAnswaredPosts(int pageNumber) { 282 int itemsPerPage = ConfigurationFactory.getConf().postsPage.intValue(); 284 return _postDAO.findUnanswered(pageNumber, itemsPerPage); 285 } 286 287 291 public List findByQuery(String query, int pageNumber) { 292 293 if ((query == null) && query.trim().equals("")) { 294 throw new IllegalArgumentException ( 295 "You should fill the query field"); 296 } 297 int itemsPerPage = ConfigurationFactory.getConf().getTopicsPage().intValue(); 298 return _postDAO.search(query, pageNumber, itemsPerPage); 299 } 300 301 306 public int getTotalRowsOfLucene(String query){ 307 return _postDAO.getTotalRowsOfLucene(query); 308 } 309 310 311 314 public void updatePost(Post post) { 315 _postDAO.update(post); 316 } 317 318 323 public List findLasPosts() { 324 return _postDAO.findLastPosts(new Configuration().getTopicsPage() 325 .intValue()); 326 } 327 328 333 public List findAllByTopicDesc(Topic topic) { 334 return _postDAO.findByTopicDesc(topic); 335 } 336 337 public List findInPosts(final String query, final int page){ 338 343 344 return _postDAO.search(query, page); 345 } 346 347 353 public void notifyUserTopicByMail(Topic topic) throws Exception { 354 long userId = UserContext.getContext().getUser().getIdUser() 355 .longValue(); 356 357 if (topic != null && topic.getNotifyMe() != null) { 358 if (topic.getNotifyMe().intValue() == 1 359 && topic.getUser().getIdUser().longValue() != userId) { 360 Configuration conf = new Configuration(); 361 362 String bodyMail = conf.emailNofityTopic 363 .replaceAll("\n", "<br>") 364 + "<br><br>" 365 + "\"<b>" 366 + topic.getTitleTopic() 367 + "</b>\"" 368 + "<br><Br> " 369 + "<b>Link:</b> <a HREF=\"" 370 + conf.domain 371 + "viewtopic.jbb?t=" 372 + topic.getIdTopic() 373 + "\">" 374 + conf.domain 375 + "viewtopic.jbb?t=" 376 + topic.getIdTopic(); 377 378 Email.sendMail(conf.adminMail, topic.getUser().getEmail(), 379 conf.forumName, bodyMail, true); 380 } 381 } 382 } 383 384 387 public void nofityWatchUsers(Topic topic, String url, String message1_i18n, 388 String message2_i18n, String topic_i18n, String watch_i18n) { 389 390 long userId = UserContext.getContext().getUser().getIdUser() 392 .longValue(); 393 394 topic = _topicDAO.load(topic.getIdTopic()); 396 397 if (topic.getAnswerNotifies() != null) { 399 Configuration conf = new Configuration(); 400 401 Map mailMap = new HashMap (); 402 mailMap.put("conf", conf); 403 mailMap.put("message1", message1_i18n); 404 mailMap.put("message2", message2_i18n); 405 mailMap.put("topicId", topic.getIdTopic()); 406 mailMap.put("topicName", topic.getTitleTopic()); 407 mailMap.put("url", url); 408 mailMap.put("topic", topic_i18n); 409 410 String message = VelocityTemplate.makeTemplate(mailMap, 411 Constants.watchTopicTemplate); 412 413 Iterator it = topic.getAnswerNotifies().iterator(); 414 Set users = new HashSet (); 415 while (it.hasNext()) { 416 AnswerNotify answer = (AnswerNotify) it.next(); 417 User user = answer.getUser(); 418 if (user.getIdUser().longValue() != userId) { 420 users.add(user.getEmail()); 421 } 422 } 423 424 String subject = conf.forumName + " - " + watch_i18n + " - " 425 + topic.getTitleTopic(); 426 427 it = users.iterator(); 428 while (it.hasNext()) { 429 String mail = (String ) it.next(); 430 Email.sendMail(conf.adminMail, mail, subject, message, true); 431 } 432 } 433 } 434 435 public void indexPost(Post post){ 436 indexer.index(post); 437 } 438 439 443 public Long createPost(Post post) { 444 return _postDAO.create(post); 445 } 446 447 450 public void deletePost(Long postId) { 451 _postDAO.delete(postId); 452 } 453 }
| Popular Tags
|