1 5 package org.exoplatform.portlets.communication.forum.component; 6 7 import org.exoplatform.faces.core.Util ; 8 import org.exoplatform.faces.application.ExoFacesMessage ; 9 import org.exoplatform.faces.core.component.InformationProvider; 10 import org.exoplatform.faces.core.component.UIExoCommand; 11 import org.exoplatform.faces.core.component.UIPageListIterator; 12 import org.exoplatform.faces.core.component.model.PageListDataHandler; 13 import org.exoplatform.faces.core.component.model.Parameter; 14 import org.exoplatform.faces.core.event.ExoActionEvent; 15 import org.exoplatform.faces.core.event.ExoActionListener; 16 import org.exoplatform.portlets.communication.forum.ForumACL; 17 import org.exoplatform.services.communication.forum.Forum; 18 import org.exoplatform.services.communication.forum.ForumService; 19 import org.exoplatform.services.communication.forum.Watcher ; 20 import org.exoplatform.services.communication.forum.Topic; 21 import org.exoplatform.services.organization.OrganizationService ; 22 import org.exoplatform.services.organization.User ; 23 24 import java.util.Date ; 25 import java.util.Map ; 26 import java.util.HashMap ; 27 33 public class UITopics extends UIExoCommand { 34 public static final String TOPIC_ID = "topicId"; 35 private static final String NEW_TOPIC = "newTopic"; 36 private static final String VIEW_FORUMS = "viewForums"; 37 private static final String WATCH_TOPIC = "watchTopic"; 38 private static final String STOP_WATCH_TOPIC = "stopWatchTopic"; 39 private static final String WATCH_FORUM = "watchForum"; 40 private static final String STOP_WATCH_FORUM = "stopWatchForum"; 41 42 public static Parameter VIEW_POSTS = new Parameter( ACTION , "viewPosts") ; 43 public static Parameter DELETE_TOPIC = new Parameter(ACTION , "deleteTopic") ; 44 public static Parameter[] newTopicParams = { new Parameter(ACTION , NEW_TOPIC)} ; 45 public static Parameter[] viewForumsParams = { new Parameter(ACTION , VIEW_FORUMS)} ; 46 public static Parameter[] watchForumParams = { new Parameter(ACTION , WATCH_FORUM)} ; 47 public static Parameter[] stopWatchForumParams = { new Parameter(ACTION , STOP_WATCH_FORUM)} ; 48 public static Parameter watchTopicParam = new Parameter(ACTION , WATCH_TOPIC ) ; 49 public static Parameter stopWatchTopicParam = new Parameter(ACTION , STOP_WATCH_TOPIC) ; 50 51 private Forum forum_ ; 52 private ForumService forumService_ ; 53 private OrganizationService orgService_ ; 54 private ForumACL acl_ ; 55 private UIPageListIterator uiPageIterator_ ; 56 private User user_ ; 57 private Map visitedTopics_ = new HashMap (); 59 60 public UITopics(OrganizationService orgService, 61 ForumService service,ForumACL acl) throws Exception { 62 setRendererType("TopicsRenderer") ; 63 setId("UITopics") ; 64 uiPageIterator_ = new UIPageListIterator(new PageListDataHandler()) ; 65 getChildren().add(uiPageIterator_); 66 forumService_ = service ; 67 orgService_ = orgService ; 68 acl_ = acl ; 69 String userName = Util.getRemoteUser(); 70 if( userName != null ){ 71 user_ = orgService_.findUserByName( userName ); 72 } 73 74 addActionListener(ViewPostsActionListener.class, "viewPosts") ; 75 addActionListener(ViewForumsActionListener.class, VIEW_FORUMS) ; 76 addActionListener(NewTopicActionListener.class, NEW_TOPIC); 77 addActionListener(DeleteTopicActionListener.class, "deleteTopic") ; 78 addActionListener(WatchTopicActionListener.class, WATCH_TOPIC) ; 79 addActionListener(StopWatchTopicActionListener.class, STOP_WATCH_TOPIC) ; 80 addActionListener(WatchForumActionListener.class, WATCH_FORUM) ; 81 addActionListener(StopWatchForumActionListener.class, STOP_WATCH_FORUM) ; 82 } 83 84 public boolean isModerator() { return acl_.hasModeratorRole(forum_) ; } 85 86 public boolean hasViewForumRole() { return acl_.hasViewForumRole(forum_) ; } 87 88 public boolean hasCreateTopicRole() { return acl_.hasCreateTopicRole(forum_) ; } 89 90 public boolean hasReplyTopicRole() { return acl_.hasReplyTopicRole(forum_) ; } 91 92 public User getUser() { return user_; } 93 94 public Forum getForum() { return forum_ ; } 95 96 public boolean setForum(Forum forum) throws Exception { 97 if(!acl_.hasViewForumRole(forum)) return false ; 98 forum_ = forum ; 99 uiPageIterator_.setPageList(forumService_.getTopics(forum.getId())) ; 100 return true ; 101 } 102 103 public void reload() throws Exception { 104 if (forum_ == null) return ; 105 uiPageIterator_.setPageList(forumService_.getTopics(forum_.getId())) ; 106 } 107 108 public UIPageListIterator getUIPageIterator() { return uiPageIterator_ ; } 109 110 public boolean canDecodeInvalidState() { return forum_ != null ;} 111 public void visit( String topicId) { 112 visitedTopics_.put( topicId, new Date () ); 113 } 114 public boolean hasNewPosts( Topic topic ) { 115 boolean retVal = false; 116 Date lastVisitDate = (Date )visitedTopics_.get( topic.getId() ); 117 if( lastVisitDate != null ) { 118 retVal = lastVisitDate.getTime() < topic.getLastPostDate().getTime(); 120 } else if( user_ != null && user_.getLastLoginTime().getTime() < topic.getLastPostDate().getTime() ){ 121 retVal = true; 123 }; 124 return retVal; 125 } 126 static public class ViewPostsActionListener extends ExoActionListener { 127 public void execute(ExoActionEvent event) throws Exception { 128 UITopics uiTopics = (UITopics) event.getComponent() ; 129 String topicId = event.getParameter(TOPIC_ID) ; 130 Topic topic = uiTopics.forumService_.getTopic(topicId) ; 131 UIPosts uiPosts = (UIPosts)uiTopics.getSibling(UIPosts.class) ; 132 uiPosts.setUIPostsData(uiTopics.forum_, topic) ; 133 UIForumPortlet forumPortlet = (UIForumPortlet)uiTopics.getAncestorOfType(UIForumPortlet.class); 134 forumPortlet.addHistoryElement(uiTopics); 135 forumPortlet.setRenderedComponent(UIPosts.class); 136 ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true); 137 uiTopics.visit( topicId ); 138 } 139 } 140 141 static public class ViewForumsActionListener extends ExoActionListener { 142 public void execute(ExoActionEvent event) throws Exception { 143 UITopics uiTopics = (UITopics) event.getComponent() ; 144 UIForumPortlet forumPortlet = (UIForumPortlet)uiTopics.getAncestorOfType(UIForumPortlet.class); 145 forumPortlet.addHistoryElement(uiTopics); 146 forumPortlet.setRenderedComponent(UIViewCategories.class); 147 ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true); 148 } 149 } 150 151 static public class NewTopicActionListener extends ExoActionListener { 152 public void execute(ExoActionEvent event) throws Exception { 153 UITopics uiTopics = (UITopics) event.getComponent() ; 154 UIPostForm uiForm = (UIPostForm) uiTopics.getSibling(UIPostForm.class) ; 155 uiForm.setNewTopicInfo(uiTopics.forum_) ; 156 UIForumPortlet forumPortlet = (UIForumPortlet)uiTopics.getAncestorOfType(UIForumPortlet.class); 157 forumPortlet.addHistoryElement(uiTopics); 158 forumPortlet.setRenderedComponent(UIPostForm.class); 159 ((UIToolbarPanel)forumPortlet.getChildComponentOfType(UIToolbarPanel.class)).setRendered(true); 160 } 161 } 162 163 static public class DeleteTopicActionListener extends ExoActionListener { 164 public void execute(ExoActionEvent event) throws Exception { 165 UITopics uiTopics = (UITopics) event.getComponent() ; 166 if(!uiTopics.acl_.hasModeratorRole(uiTopics.forum_)) return ; 167 String topicId = event.getParameter(TOPIC_ID) ; 168 uiTopics.forumService_.removeTopic(topicId) ; 169 uiTopics.reload() ; 170 } 171 } 172 173 static public class WatchTopicActionListener extends ExoActionListener { 174 public void execute(ExoActionEvent event) throws Exception { 175 UITopics uiTopics = (UITopics) event.getComponent() ; 176 User user = uiTopics.getUser() ; 177 Topic topic = uiTopics.forumService_.getTopic(event.getParameter(TOPIC_ID)) ; 178 Watcher watcher = uiTopics.forumService_.getWatcher(topic, user.getUserName()) ; 179 if(watcher == null) { 180 watcher = uiTopics.forumService_.createWatcher(topic) ; 181 watcher.setUserName(user.getUserName()) ; 182 watcher.setAddress(user.getEmail()) ; 183 } 184 UIWatchForm uiForm = (UIWatchForm) uiTopics.getSibling(UIWatchForm.class) ; 185 uiForm.setWatcher(watcher) ; 186 uiTopics.setRenderedSibling(UIWatchForm.class) ; 187 } 188 } 189 190 static public class StopWatchTopicActionListener extends ExoActionListener { 191 public void execute(ExoActionEvent event) throws Exception { 192 UITopics uiTopics = (UITopics) event.getComponent() ; 193 Topic topic = uiTopics.forumService_.getTopic(event.getParameter(TOPIC_ID)) ; 194 Watcher watcher = uiTopics.forumService_.getWatcher(topic, uiTopics.user_.getUserName()) ; 195 InformationProvider iprovider = findInformationProvider(uiTopics); 196 if(watcher != null) { 197 uiTopics.forumService_.removeWatcher(watcher) ; 198 Object [] args = {topic.getTopic()} ; 199 iprovider.addMessage(new ExoFacesMessage("#{UITopics.msg.stop-topic-watch-success}", args)) ; 200 } else { 201 iprovider.addMessage(new ExoFacesMessage("#{UITopics.msg.stop-topic-watch-fail}")) ; 202 } 203 } 204 } 205 206 static public class WatchForumActionListener extends ExoActionListener { 207 public void execute(ExoActionEvent event) throws Exception { 208 UITopics uiTopics = (UITopics) event.getComponent() ; 209 User user = uiTopics.getUser(); 210 Watcher watcher = uiTopics.forumService_.getWatcher(uiTopics.forum_, user.getUserName()) ; 211 if(watcher == null) { 212 watcher = uiTopics.forumService_.createWatcher(uiTopics.forum_) ; 213 watcher.setUserName(user.getUserName()) ; 214 watcher.setAddress(user.getEmail()) ; 215 } 216 UIWatchForm uiForm = (UIWatchForm) uiTopics.getSibling(UIWatchForm.class) ; 217 uiForm.setWatcher(watcher) ; 218 uiTopics.setRenderedSibling(UIWatchForm.class) ; 219 } 220 } 221 222 static public class StopWatchForumActionListener extends ExoActionListener { 223 public void execute(ExoActionEvent event) throws Exception { 224 UITopics uiTopics = (UITopics) event.getComponent() ; 225 Watcher watcher = uiTopics.forumService_.getWatcher(uiTopics.forum_, uiTopics.getUser().getUserName()) ; 226 InformationProvider iprovider = findInformationProvider(uiTopics); 227 if(watcher != null) { 228 uiTopics.forumService_.removeWatcher(watcher) ; 229 Object [] args = {uiTopics.forum_.getForumName()} ; 230 iprovider.addMessage(new ExoFacesMessage("#{UITopics.msg.stop-forum-watch-success}", args)) ; 231 } else { 232 iprovider.addMessage(new ExoFacesMessage("#{UITopics.msg.stop-forum-watch-fail}")) ; 233 } 234 } 235 } 236 } | Popular Tags |