/projects/exoportal-v1.0.2/portlets/core/communications/src/java/org/exoplatform/portlets/communication/forum/component/UIPostForm.java

https://gitlab.com/essere.lab.public/qualitas.class-corpus · Java · 159 lines · 135 code · 14 blank · 10 comment · 18 complexity · d852a102102b718c4d7684c18981c394 MD5 · raw file

  1. /***************************************************************************
  2. * Copyright 2001-2003 The eXo Platform SARL All rights reserved. *
  3. * Please look at license.txt in info directory for more license detail. *
  4. **************************************************************************/
  5. package org.exoplatform.portlets.communication.forum.component;
  6. import java.util.ResourceBundle;
  7. import javax.faces.context.FacesContext ;
  8. import org.exoplatform.container.SessionContainer;
  9. import org.exoplatform.faces.core.component.UITextArea ;
  10. import org.exoplatform.faces.core.component.UISimpleForm;
  11. import org.exoplatform.faces.core.component.UIStringInput;
  12. import org.exoplatform.faces.core.component.model.*;
  13. import org.exoplatform.faces.core.event.ExoActionEvent;
  14. import org.exoplatform.faces.core.event.ExoActionListener;
  15. import org.exoplatform.services.communication.forum.Forum;
  16. import org.exoplatform.services.communication.forum.ForumService;
  17. import org.exoplatform.services.communication.forum.Post;
  18. import org.exoplatform.services.communication.forum.Topic;
  19. /**
  20. * Sat, Jan 03, 2004 @ 11:16
  21. * @author: Tuan Nguyen
  22. * @email: tuan08@users.sourceforge.net
  23. * @version: $Id: UIPostForm.java,v 1.12 2004/10/20 23:39:59 benjmestrallet Exp $
  24. */
  25. public class UIPostForm extends UISimpleForm {
  26. private ForumService service_ ;
  27. private UIStringInput subject_ ;
  28. private UITextArea message_ ;
  29. private Post post_ ;
  30. private Topic topic_ ;
  31. private Forum forum_ ;
  32. public UIPostForm(ForumService service) throws Exception {
  33. super("postForm", "post", null) ;
  34. setId("UIPostForm") ;
  35. setClazz("UIPostForm");
  36. service_ = service ;
  37. message_ = new UITextArea("message", "") ;
  38. subject_ = new UIStringInput("subject", "") ;
  39. add(new HeaderRow().
  40. add(new Cell("#{UIPostForm.header.title}").
  41. addColspan("2")));
  42. add(new Row().
  43. add(new LabelCell("#{UIPostForm.label.subject}")).
  44. add(new ComponentCell(this, subject_))) ;
  45. add(new Row().
  46. add(new LabelCell("#{UIPostForm.label.message}").addValign("top")).
  47. add(new ComponentCell(this, message_)));
  48. add(new Row().
  49. add(new ListComponentCell().
  50. add(new FormButton("#{UIPostForm.button.save}", SAVE_ACTION)).
  51. add(new FormButton("#{UIPostForm.button.cancel}", CANCEL_ACTION)).
  52. addColspan("2").addAlign("center"))) ;
  53. addActionListener(CancelActionListener.class, CANCEL_ACTION) ;
  54. addActionListener(SaveActionListener.class, SAVE_ACTION) ;
  55. }
  56. public void setNewTopicInfo(Forum forum) {
  57. forum_ = forum ;
  58. topic_ = null ;
  59. post_ = null ;
  60. subject_.setText("") ;
  61. message_.setText("") ;
  62. }
  63. public void setNewPostInfo(Topic topic) {
  64. forum_ = null ;
  65. topic_ = topic ;
  66. post_ = null ;
  67. subject_.setText("") ;
  68. message_.setText("") ;
  69. }
  70. public void setQuotePostInfo(Topic topic, Post post) {
  71. forum_ = null ;
  72. topic_ = topic ;
  73. post_ = null ;
  74. subject_.setText(post.getSubject()) ;
  75. message_.setText(post.getMessage()) ;
  76. }
  77. public void setEditPostInfo(Topic topic, Post post) {
  78. forum_ = null ;
  79. topic_ = topic ;
  80. post_ = post ;
  81. subject_.setText(post.getSubject()) ;
  82. message_.setText(post.getMessage()) ;
  83. }
  84. public boolean canDecodeInvalidState() { return forum_ != null || topic_ != null; }
  85. static public class CancelActionListener extends ExoActionListener {
  86. public void execute(ExoActionEvent event) throws Exception {
  87. UIPostForm uiForm = (UIPostForm) event.getComponent() ;
  88. UIForumPortlet forumPortlet = (UIForumPortlet)uiForm.getAncestorOfType(UIForumPortlet.class);
  89. forumPortlet.addHistoryElement(uiForm);
  90. if (uiForm.topic_ == null) {
  91. forumPortlet.setRenderedComponent(UITopics.class);
  92. } else {
  93. forumPortlet.setRenderedComponent(UIPosts.class);
  94. }
  95. ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true);
  96. }
  97. }
  98. static public class SaveActionListener extends ExoActionListener {
  99. public void execute(ExoActionEvent event) throws Exception {
  100. UIPostForm uiForm = (UIPostForm) event.getComponent() ;
  101. String user = FacesContext.getCurrentInstance().getExternalContext().getRemoteUser() ;
  102. String subject = uiForm.subject_.getText() ;
  103. String message = uiForm.message_.getText() ;
  104. if(subject == null || subject.length() == 0) {
  105. ResourceBundle res = event.getApplicationResourceBundle() ;
  106. subject = res.getString("UIPostForm.msg.no-subject");
  107. }
  108. Topic topic = uiForm.topic_ ;
  109. boolean isNewTopic = false ;
  110. if (topic == null) {
  111. topic = uiForm.service_.createTopicInstance() ;
  112. topic.setOwner(user) ;
  113. topic.setModifiedBy(user) ;
  114. topic.setTopic(subject) ;
  115. topic.setDescription("") ;
  116. uiForm.service_.addTopic(uiForm.forum_, topic) ;
  117. isNewTopic = true ;
  118. }
  119. boolean isNewPost = false ;
  120. Post post = uiForm.post_ ;
  121. if(post == null) {
  122. post = uiForm.service_.createPostInstance() ;
  123. post.setOwner(user) ;
  124. isNewPost = true ;
  125. }
  126. post.setModifiedBy(user) ;
  127. post.setSubject(subject) ;
  128. post.setMessage(message) ;
  129. post.setRemoteAddr(SessionContainer.getInstance().getMonitor().getIPAddress()) ;
  130. if(isNewPost) uiForm.service_.addPost(topic, post) ;
  131. else uiForm.service_.updatePost(post) ;
  132. UITopics uiTopics = (UITopics)uiForm.getSibling(UITopics.class) ;
  133. if(isNewTopic || isNewPost) {
  134. uiTopics.reload() ;
  135. }
  136. uiTopics.visit( topic.getId() );
  137. UIPosts uiPosts = (UIPosts) uiForm.getSibling(UIPosts.class) ;
  138. uiPosts.setUIPostsData(uiTopics.getForum() , topic) ;
  139. UIForumPortlet forumPortlet = (UIForumPortlet)uiForm.getAncestorOfType(UIForumPortlet.class);
  140. forumPortlet.addHistoryElement(uiForm);
  141. forumPortlet.setRenderedComponent(UIPosts.class);
  142. ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true);
  143. }
  144. }
  145. }