KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exoplatform > portlets > communication > forum > renderer > html > PostsRenderer


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.renderer.html;
6
7 import java.io.IOException JavaDoc;
8 import java.util.*;
9 import javax.faces.component.UIComponent;
10 import javax.faces.context.ExternalContext;
11 import javax.faces.context.FacesContext;
12 import javax.faces.context.ResponseWriter;
13 import org.exoplatform.faces.core.component.model.PageListDataHandler;
14 import org.exoplatform.faces.core.component.model.Parameter;
15 import org.exoplatform.faces.core.renderer.html.HtmlBasicRenderer;
16 import org.exoplatform.portlets.communication.forum.component.UIPosts;
17 import org.exoplatform.services.communication.forum.Post;
18 import org.exoplatform.services.grammar.wiki.WikiEngineService;
19 import org.exoplatform.services.organization.* ;
20 /**
21  * @email: tuan08@users.sourceforge.net
22  * @version: $Id: PostsRenderer.java,v 1.13 2004/10/20 18:46:31 benjmestrallet Exp $
23  */

24 public class PostsRenderer extends HtmlBasicRenderer {
25   private static Parameter QUOTE_POST = new Parameter(ACTION, "quotePost") ;
26   private static Parameter EDIT_POST = new Parameter(ACTION, "editPost") ;
27
28   private static Parameter[] viewForumsParams_ = {new Parameter(ACTION , "viewForums") } ;
29   private static Parameter[] viewTopicsParams_ = {new Parameter(ACTION , "viewTopics") } ;
30   private static Parameter[] replyParams_ = {new Parameter(ACTION , "reply") } ;
31   
32   final public void encodeChildren(FacesContext context, UIComponent component ) throws IOException JavaDoc {
33     ResponseWriter w = context.getResponseWriter() ;
34     ExternalContext eContext = context.getExternalContext() ;
35     ResourceBundle res = getApplicationResourceBundle(eContext) ;
36     UIPosts uiPosts = (UIPosts) component ;
37     
38     String JavaDoc forumName = uiPosts.getForumName();
39     w.write("<table class='UIPosts'>") ;
40     w. write("<tr>");
41     w. write("<td>");
42     linkRenderer_.render(w,uiPosts, res.getString("UIPosts.button.forum-index"), viewForumsParams_) ;
43     w.write(" >> ");
44     linkRenderer_.render(w,uiPosts, forumName, viewTopicsParams_) ;
45     w. write("</td>");
46     w. write("<td align='right'>");
47     if(uiPosts.hasReplyTopicRole()) {
48       linkRenderer_.render(w,uiPosts, res.getString("UIPosts.button.reply"), replyParams_) ;
49     }
50     w. write("</td>");
51     w. write("</tr>");
52     w. write("<tr>");
53     w. write("<td colspan='2'>");
54     addPostsSection(w, res, uiPosts) ;
55     w. write("</td>");
56     w. write("</tr>");
57     w. write("<tr>");
58     w. write("<td>");
59     linkRenderer_.render(w,uiPosts, res.getString("UIPosts.button.forum-index"), viewForumsParams_) ;
60     w.write(" >> ");
61     linkRenderer_.render(w,uiPosts, forumName, viewTopicsParams_) ;
62     w. write("</td>");
63     w. write("<td align='right'>");
64     if(uiPosts.hasReplyTopicRole()) {
65       linkRenderer_.render(w,uiPosts, res.getString("UIPosts.button.reply"), replyParams_) ;
66     } else {
67       w. write(res.getString("UIPosts.button.reply")) ;
68     }
69     w. write("</tr>");
70     w.write("</table>") ;
71   }
72
73   private void addPostsSection(ResponseWriter w, ResourceBundle res,
74                                  UIPosts uiPosts) throws IOException JavaDoc {
75     String JavaDoc editButton = res.getString("UIPosts.button.edit") ;
76     String JavaDoc quoteButton = res.getString("UIPosts.button.quote") ;
77     
78     Parameter postIdParam = new Parameter("postId", "") ;
79     Parameter[] quoteParams = {QUOTE_POST , postIdParam } ;
80     Parameter[] editParams = { EDIT_POST , postIdParam } ;
81
82     PageListDataHandler dh = uiPosts.getUIPageIterator().getPageListDataHandler() ;
83     boolean hasReplyRole = uiPosts.hasReplyTopicRole() ;
84     boolean hasModeratorRole = uiPosts.hasModeratorRole() ;
85     String JavaDoc remoteUser = uiPosts.getRemoteUser() ;
86     String JavaDoc guestUser = res.getString("UIPosts.label.guest-user") ;
87     WikiEngineService weService_ = uiPosts.getWikiEngineService() ;
88     OrganizationService orgService = uiPosts.getOrganizationService() ;
89     w.write("<table class='UIPost'>") ;
90     w. write("<tr>");
91     w. write("<th style='width: 200px'>"); w.write(res.getString("UIPosts.header.author"));w.write("</th>");
92     w. write("<th colspan='2'>"); w.write(res.getString("UIPosts.header.messages"));w.write("</th>");
93     w. write("</tr>");
94     dh.begin() ;
95     while(dh.nextRow()) {
96       Post post = (Post) dh.getCurrentObject() ;
97       postIdParam.setValue(post.getId()) ;
98       String JavaDoc owner = post.getOwner() ;
99       if(owner == null ) owner = guestUser ;
100       String JavaDoc avatarURL = null ;
101       String JavaDoc signature = null ;
102       try {
103         UserProfile profile = orgService.findUserProfileByName(owner) ;
104         if(profile != null ) {
105           Map profileMap = profile.getUserInfoMap() ;
106           avatarURL = (String JavaDoc)profileMap.get("user.other-info.avatar.url") ;
107           signature = (String JavaDoc)profileMap.get("user.other-info.signature") ;
108         }
109       } catch (Exception JavaDoc ex) { ex.printStackTrace() ; }
110       if(avatarURL == null) avatarURL = "/user/images/no_pic.gif" ;
111       w.write("<tr>");
112       w. write("<td rowspan='3' class='post-owner'>");
113       w. write("<img SRC='"); w.write(avatarURL) ; w.write("'/><br/>") ;
114       w. write(owner);
115       w. write("</td>");
116       w. write("<td>"); w.write(post.getSubject()); w.write("</td>");
117       w. write("<td style='width: 100px; text-align: center;'>");
118       if (hasReplyRole) {
119         linkRenderer_.render(w,uiPosts, quoteButton, quoteParams) ;
120       } else {
121         w.write(quoteButton);
122       }
123       w. write(" - ");
124       if (owner.equals(remoteUser) || hasModeratorRole) {
125         linkRenderer_.render(w,uiPosts, editButton, editParams) ;
126       } else {
127         w.write(editButton);
128       }
129       w. write("</td>");
130       w.write("</tr>");
131       w.write("<tr>");
132       w. write("<td colspan='2' style='padding: 10px 5px 10px 5px' class='wiki'>");
133       String JavaDoc message = weService_.toXHTML(post.getMessage()) ;
134       w. write(message);
135       if(signature != null) {
136         renderSignature(w, signature) ;
137       }
138       w. write("</td>");
139       w.write("</tr>");
140       w.write("<tr>");
141       w. write("<td colspan='2'>");
142       w. write(ft_.format(post.getModifiedDate()));
143       w. write("<br>IP: "); w.write(ft_.format(post.getRemoteAddr()));
144       w. write("</td>");
145       w.write("</tr>");
146     }
147     dh.end() ;
148     w. write("<tr>");
149     w. write("<td colspan='3' align='right'>");
150     uiPosts.getUIPageIterator().encodeBegin(FacesContext.getCurrentInstance()) ;
151     w. write("</td>");
152     w. write("</tr>");
153     w.write("</table>") ;
154   }
155   
156   private void renderSignature(ResponseWriter w, String JavaDoc signature) throws IOException JavaDoc {
157     w.write("<div class='signature'>") ;
158     StringBuffer JavaDoc b = new StringBuffer JavaDoc() ;
159     char[] buf = signature.toCharArray() ;
160     for(int i = 0; i < buf.length; i++) {
161       switch(buf[i]) {
162         case '\n' : b.append("<br/>") ; break ;
163         case '\r' : break ;
164         case '&' : b.append("&amp;") ; break ;
165         case '>' : b.append("&gt;") ; break ;
166         case '<' : b.append("&lt;") ; break ;
167         case ' ' : {
168           b.append(" ") ;
169           i++ ;
170           while(i < buf.length && buf[i] == ' ') {
171             b.append("&nbsp;") ;
172             i++ ;
173           }
174           i-- ;
175           break ;
176         }
177         default: b.append(buf[i]) ; break ;
178       }
179     }
180     w.write(b.toString()) ;
181     w.write("</div>") ;
182   }
183 }
Popular Tags