1 package org.javabb.action; 2 3 import java.util.ArrayList ; 4 import java.util.Date ; 5 import java.util.List ; 6 7 import org.apache.commons.lang.StringUtils; 8 import org.javabb.action.infra.BaseAction; 9 import org.javabb.component.PostFormatter; 10 import org.javabb.component.UserFormatter; 11 import org.javabb.infra.UserContext; 12 import org.javabb.infra.Utils; 13 import org.javabb.transaction.BadWordTransaction; 14 import org.javabb.transaction.ForumTransaction; 15 import org.javabb.transaction.PostTransaction; 16 import org.javabb.transaction.TopicTransaction; 17 import org.javabb.transaction.UserTransaction; 18 import org.javabb.vo.Forum; 19 import org.javabb.vo.Post; 20 import org.javabb.vo.PostText; 21 import org.javabb.vo.Topic; 22 import org.javabb.vo.User; 23 24 import com.opensymphony.webwork.ServletActionContext; 25 26 41 42 48 public class PostAction extends BaseAction { 49 private PostText _post = new PostText(); 50 51 private ForumTransaction _forumTransaction; 52 53 private PostTransaction _postTransaction; 54 55 private TopicTransaction topicTransaction; 56 57 private Topic _topic = new Topic(); 58 59 private BadWordTransaction badWordTransaction; 60 61 private UserTransaction _userTransaction; 62 63 private PostFormatter postFormatter; 64 65 private UserFormatter userFormatter; 66 67 private List _posts = new ArrayList (); 68 69 private String quote; 70 71 private String whoQuote; 72 73 77 80 public void setForumTransaction(ForumTransaction forumTransaction) { 81 this._forumTransaction = forumTransaction; 82 } 83 84 87 public void setUserTransaction(UserTransaction userTransaction) { 88 this._userTransaction = userTransaction; 89 } 90 91 94 public void setTopicTransaction(TopicTransaction topicTransaction) { 95 this.topicTransaction = topicTransaction; 96 } 97 98 101 public void setPostFormatter(PostFormatter postFormatter) { 102 this.postFormatter = postFormatter; 103 } 104 105 108 public void setPostTransaction(PostTransaction postTransaction) { 109 this._postTransaction = postTransaction; 110 } 111 112 115 public void setBadWordTransaction(BadWordTransaction badWordComponent) { 116 this.badWordTransaction = badWordComponent; 117 } 118 119 public void setUserFormatter(UserFormatter userFormatter) { 120 this.userFormatter = userFormatter; 121 } 122 123 127 128 131 public String listaPost() { 132 _posts = _postTransaction.findByTopic(_post.getTopic().getIdTopic(), getPage()); 133 return SUCCESS; 134 } 135 136 140 public String returnQuoteMsg() { 141 if (log.isDebugEnabled()) { 142 log.debug("whoQuote = '" + whoQuote + "'"); 143 } 144 if (StringUtils.trimToNull(whoQuote) != null) { 145 Post tmpPost = new Post(); 146 tmpPost = _postTransaction.loadPost(_post.getId()); 147 quote = "[quote=\"" + whoQuote + "\"]" + tmpPost.getPostBody() + " [/quote]"; 148 _post = (PostText) tmpPost; 149 } 150 151 return SUCCESS; 152 } 153 154 157 public String loadPost() { 158 if(_post.getIdPost() != null){ 159 _postId = _post.getIdPost(); 160 } 161 _post = (PostText) _postTransaction.loadPost(_postId); 162 return SUCCESS; 163 } 164 165 168 public String updatePost() { 169 Post alterPost = _postTransaction.loadPost(_post.getId()); 170 User userLogado = UserContext.getContext().getUser(); 171 172 if (userLogado.getAdmin().equals(new Integer (0))) { 174 if (!alterPost.getUser().equals(userLogado)) { 176 addActionError(getText("you_can_edit_yours_messages")); 177 178 return ERROR; 179 } 180 } 181 182 alterPost.setSubject(_post.getSubject()); 183 alterPost.setPostBody(_post.getPostBody()); 184 185 alterPost.setSig(_post.getSig()); 186 _postTransaction.updatePost(alterPost); 187 188 if(getUrl() != null && "topic".equals(getUrl())){ 189 setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic()); 190 } else { 191 setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic() 192 + "&page="+ _page + "#" + _post.getIdPost()); 193 } 194 195 196 _post.setIdPost(_post.getId()); 198 _forumTransaction.refreshPost(_post.getId()); 199 200 201 202 return SUCCESS; 203 } 204 205 208 public String updatePostTopic() { 209 Post alterPost = _postTransaction.loadPost(_post.getId()); 210 User userLogado = UserContext.getContext().getUser(); 211 212 if (userLogado.getAdmin().equals(new Integer (0))) { 214 if (!alterPost.getUser().equals(userLogado)) { 216 addActionError(getText("you_can_edit_yours_messages")); 217 return ERROR; 218 } 219 } 220 221 alterPost.setSubject(_post.getSubject()); 222 alterPost.setPostBody(_post.getPostBody()); 223 alterPost.setSig(_post.getSig()); 224 _postTransaction.updatePost(alterPost); 225 226 Topic alterTopic = topicTransaction.loadTopic(_post.getTopic().getId()); 227 alterTopic.setTitleTopic(Utils.replaceHTML(_topic.getTitleTopic())); 228 alterTopic.setTopicModel(_post.getTopic().getTopicModel()); 229 alterTopic.setNotifyMe(_post.getTopic().getNotifyMe()); 230 231 if(userLogado.getAdmin().intValue() != 1){ 232 alterTopic.setTopicModel(new Integer (0)); 233 } 234 235 topicTransaction.updateTopic(alterTopic); 236 237 setUrl("viewtopic.jbb?t=" + _post.getTopic().getIdTopic()); 238 239 240 _post.setIdPost(_post.getId()); 242 _forumTransaction.refreshPost(_post.getId()); 243 _forumTransaction.refreshTopic(_post.getTopic().getIdTopic()); 245 246 return SUCCESS; 247 } 248 249 252 public String insertPost() throws Exception { 253 Topic topic = _post.getTopic(); 254 topic = topicTransaction.loadTopic(topic.getId()); 255 256 UserContext.getContext().setTopicUnread(topic.getId()); 258 259 long currTime = System.currentTimeMillis(); 261 Long lastPost = (Long ) getSessionAttribute("my_last_post"); 262 263 if ((lastPost != null) && ((currTime - lastPost.longValue()) < 7000)) { 264 this.addActionError(this.getText("message_so_quickly")); 266 return ERROR; 267 268 } 269 setSessionAttribute("my_last_post", new Long (currTime)); 270 271 if ((topic.getTopicStatus() != null) && topic.getTopicStatus().equals(new Integer (1))) { 272 this.addActionError(this.getText("answer_locked_topic")); 273 return ERROR; 274 } 275 276 _post.setPostDate(new Date ()); 277 _post.setIp(ServletActionContext.getRequest().getRemoteAddr()); 278 _post.setPost_state(new Integer (0)); 279 _post.setPostBody(_post.getPostBody()); 280 281 User up = UserContext.getContext().getUser(); 282 _post.setUser(up); 283 284 Long idPost = _postTransaction.createPost(_post); 285 286 287 topicTransaction.sumNumberReplysByTopic(_post.getTopic().getIdTopic()); 289 topicTransaction.updateDatePostTopic(_post.getTopic().getIdTopic()); 290 291 _userTransaction.sumNumberMsgUser(up.getIdUser()); 293 294 Integer currentPage = _postTransaction.getPageOfLastPostByTopic(topic); 296 setPage(currentPage.intValue()); 297 298 int lastId = idPost.intValue(); 299 int topicId = _post.getTopic().getIdTopic().intValue(); 300 String url = "viewtopic.jbb?t=" + topicId + "&page=" + currentPage + "#" + lastId; 301 setUrl(url); 302 303 _post.setIdPost(idPost); 305 _forumTransaction.refreshPost(_post.getId()); 306 _forumTransaction.refreshTopic(_post.getTopic().getId()); 308 309 _postTransaction.notifyUserTopicByMail(topic); 311 312 _postTransaction.nofityWatchUsers(topic, url, getText("watch_topics_message1"), 314 getText("watch_topics_message2"), getText("topic"), 315 getText("watch_topic")); 316 317 return SUCCESS; 318 } 319 320 324 public String insertTopicPost() { 325 326 Long idTopicInserted = topicTransaction.createTopic(_topic); 327 328 _post.getUser().setId(UserContext.getContext().getUser().getId()); 329 _post.setPost_state(new Integer (0)); 330 _post.setPostDate(new Date ()); 331 _post.setPostBody(_post.getPostBody()); 332 _post.setSubject(badWordTransaction.verifyBadWords(_post.getSubject())); 333 334 Topic topic = topicTransaction.loadTopic(idTopicInserted); 335 topic.setTitleTopic(badWordTransaction.verifyBadWords(topic.getTitleTopic())); 336 topic.setTopicStatus(new Integer (0)); 337 topic.setPageLastPost(new Integer (1)); 338 339 _post.setTopic(topic); 340 341 Long lastPostId = _postTransaction.createPost(_post); 342 _topic.setLastPost(_postTransaction.loadPost(lastPostId)); 343 344 User up = UserContext.getContext().getUser(); 346 _userTransaction.sumNumberMsgUser(up.getIdUser()); 347 348 int topicId = _post.getTopic().getIdTopic().intValue(); 349 setUrl("viewtopic.jbb?t=" + topicId + "#" + lastPostId.toString()); 350 351 _post.setIdPost(lastPostId); 353 _forumTransaction.refreshPost(_post.getId()); 354 _forumTransaction.refreshTopic(topic.getId()); 356 357 return SUCCESS; 358 } 359 360 363 public String deletePost() { 364 Topic topic = null; 365 Forum forum = null; 366 367 User up = UserContext.getContext().getUser(); 369 370 log.debug("Deleting post User:" + up.getUser()); 372 log.debug("Deleting post ID:" + up.getIdUser()); 373 374 if (_postTransaction.canDeletePost(_post)) { 376 377 topic = _post.getTopic(); 379 forum = topic.getForum(); 380 381 _postTransaction.deletePost(_post.getId()); 382 383 topicTransaction.subNumberReplysByTopic(_post.getTopic().getId()); 385 386 _userTransaction.subNumberMsgUser(_post.getUser().getId()); 388 389 List postsByT = _postTransaction.findByTopic(_post.getTopic().getIdTopic(), getPage()); 393 394 if (postsByT.size() <= 0) { 395 _topic = new Topic(); 397 _topic.setIdTopic(_post.getTopic().getIdTopic()); 398 topicTransaction.deleteTopic(_topic.getId()); 399 400 topic = new Topic(); 402 403 setUrl("viewforum.jbb?f=" + _post.getTopic().getForum().getIdForum()); 404 } else { 405 int topicId = _post.getTopic().getIdTopic().intValue(); 407 setUrl("viewtopic.jbb?t=" + topicId); 408 } 409 410 _forumTransaction.refreshTopic(topic.getId()); 412 _forumTransaction.refreshForum(forum.getId()); 414 415 return SUCCESS; 416 } 417 418 addActionError(getText("no_permission_to_delete_post")); 419 420 return ERROR; 421 } 422 423 426 public String searchAuthor() { 427 _posts = _postTransaction.listPostsByUser(_userId, _page); 428 return SUCCESS; 429 } 430 431 434 public String listUnAnswaredPosts() { 435 _posts = _postTransaction.listUnAnswaredPosts(_page); 436 return SUCCESS; 437 } 438 439 440 public String listAllLastPosts() { 441 _posts = _postTransaction.findLasPosts(); 442 return SUCCESS; 443 } 444 445 449 450 451 454 public Post getPost() { 455 return _post; 456 } 457 458 461 public List getPosts() { 462 return _posts; 463 } 464 465 468 public String getQuote() { 469 return quote; 470 } 471 472 475 public void setQuote(String quote) { 476 this.quote = quote; 477 } 478 479 482 public String getWhoQuote() { 483 return whoQuote; 484 } 485 486 489 public void setWhoQuote(String whoQuote) { 490 this.whoQuote = whoQuote; 491 } 492 493 497 public String formatPost(Post post) { 498 return postFormatter.formatPost(post); 499 } 500 501 505 public String formatEscaped(String text) { 506 return postFormatter.formatEscaped(text); 507 } 508 509 512 public Topic getTopic() { 513 return _topic; 514 } 515 516 public UserFormatter getUserFormatter() { 517 return userFormatter; 518 } 519 520 } | Popular Tags |