Untitled Document
Table of contents | Next | Previous
WCM API Samples used for the RiverBend site
In the following article, we discuss the first of two specific samples.
Sample 1 - Add Comments to News
This sample provides an easy method to add comments and ratings to News
items in riverbend site. There are many number of ways this can be implemented
and this is one of the ways to implement comments.
To accomplish this, add a new text element into News Authoring Template as
below

Add the below code into comments.jsp and store it into
<wp_profile>\installedApps\<nodename>\
PA_WCMLocalRendering.ear\ilwwcm-localrende.war\jsp\html.
Create a JSP Component named “ commentsJSP ” pointing to “
jsp/html/comments.jsp ”
<%@ page import="com.ibm.workplace.wcm.api.*"%>
<%@ page import="com.ibm.wps.services.ServiceManager"%>
<%@ page import="java.util.*,java.security.Principal"%>
<%@ page import="java.text.SimpleDateFormat"%>
<%//Use WebSphere Portal Service Manager to obtain the WCM Service
try
{
// Construct and inital Context
javax.naming.InitialContext ctx = new javax.naming.InitialContext();
// Retrieve WebContentService using JNDI name
WebContentService webContentService = (WebContentService)
ctx.lookup("portal:service/wcm/WebContentService");
//Get the WCM Repository
Repository rep = webContentService.getRepository();
//Get an authenticated WCM Workspace
Workspace wsa = rep.getWorkspace();
//Set the current library
wsa.setCurrentDocumentLibrary(wsa.getDocumentLibrary("riverbend"));
//Get the rendering context
RenderingContext renderingContext = (RenderingContext)
request.getAttribute(Workspace.WCM_RENDERINGCONTEXT_KEY);
//Constants
String COMMENTFIELDNAME = "comment";
String SEPERATOR = "~";
String INTSEPERATOR = ";";
//Get the current content object
Content current = renderingContext.getContent();
//The comments component
TextComponent commentscomponent = null;
//Get the comment component
commentscomponent = (TextComponent)
current.getComponent(COMMENTFIELDNAME);
//Display items from the post
String comment = null;
String user = null;
String date = null;
String rating = null;
//Get the current date
String format = "dd/MM/yy";
Date today = new Date();
SimpleDateFormat formatter = new SimpleDateFormat(format);
String datenewformat = formatter.format(today);
//Get posted details
if(request.getMethod().equalsIgnoreCase("POST")){
comment = request.getParameter("comment");
user = request.getParameter("user");
rating = request.getParameter("Rating");
String currentComments = "";
//Get the current comments
currentComments = commentscomponent.getText();
if(user == null) user = "Anonymous";
if (comment != null && (!comment.equals(""))) {
//Add this comment
if (commentscomponent!=null) {
//Remove the seperator if this exists in the comments
comment.replace('~', ' ');
//Add the comments
currentComments = currentComments + user + INTSEPERATOR +
datenewformat + INTSEPERATOR + comment;
}
}
if(rating != null)
{
if(comment == null || comment.equals(""))
currentComments = currentComments + user + INTSEPERATOR + datenewformat
+ INTSEPERATOR + " " + INTSEPERATOR + rating ;
else
currentComments = currentComments + INTSEPERATOR + rating ;
}
if(rating != null || comment != null)
currentComments = currentComments + SEPERATOR;
commentscomponent.setText(currentComments);
current.setComponent(COMMENTFIELDNAME, commentscomponent);
//Save the content
wsa.save(current);
}
//Display the comments
if(commentscomponent != null){
//Get the current comments
String currentComments = commentscomponent.getText();
//The HTML output
String html = "";
if(currentComments != null || (!currentComments.equals("")))
html = html + "<b> Comments Posted </b>
<br>";
StringTokenizer st = new StringTokenizer(currentComments, SEPERATOR);
while (st.hasMoreTokens()) {
//Get the token
String thiscomment = st.nextToken();
//Split this comment
StringTokenizer st2 = new StringTokenizer(thiscomment, INTSEPERATOR);
String[] results = {"
","","","-"};
int n = 0;
while (st2.hasMoreTokens()) {
results[n] = st2.nextToken();
n = n + 1;
}
//Add light formatting and add to the html
if(results[2].equals(" "))
html = html + "<hr/><i>" + "Created by:
"+ results[0] + " on " + results[1] +
"</i><br/><i>Content rating:
"+results[3]+" <hr/><br/>";
else
html = html + "<hr/>" + results[2] +
"<br/><i>" + "Created by:
"+ results[0] + " on " + results[1] +
"</i><br/><i>Content rating: "+results[3]+"
<hr/><br/>";
}
//Write out the html
out.print(html);
}
webContentService.getRepository().endWorkspace();
} catch (Exception t) {
out.println("error in the code: " + t.toString());
}
%>
Add an HTML component to create a comment form and add the below html code
into the HTML Element
<form name="addacomment" action=""
method="POST">
<input type="hidden" name="user"
value="<Component name="riverbend/username"/>" />
<table border="1" cellspacing="0"
cellpadding="1" ><tr>
<td> <b>Rate this page:</b><br/>
<table border="0" cellspacing="0"
cellpadding="1" >
<tr>
<td>
<table cellspacing="0" cellpadding="0"
border="0" width="100%">
<tr>
<td width="15%">
Comment
</td>
<td>
<TEXTAREA NAME="comment" COLS=40
ROWS=6></TEXTAREA><br/>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td>
<table cellspacing="0" cellpadding="0"
border="0" width="100%">
<tbody>
<tr>
<td width="15%">
Rating
</td>
<td align="left" width="58"
valign="top">
<input type="radio" name="Rating"
id="Rating1" value="1"/>
<label for="Rating1">1 (Low)</label>
</td>
<td align="left" width="58"
valign="top">
<input type="radio" name="Rating"
id="Rating2" value="2"/>
<label for="Rating2">2</label>
</td>
<td align="left" width="58"
valign="top">
<input type="radio" name="Rating"
id="Rating3" value="3"/>
<label for="Rating3">3</label>
</td>
<td align="left" width="58"
valign="top">
<input type="radio" name="Rating"
id="Rating4" value="4"/>
<label for="Rating4">4</label>
</td>
<td align="left" width="61"
valign="top">
<input type="radio" name="Rating"
id="Rating5" value="5"/>
<label for="Rating5">5 (High)</label>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
<tr>
<td>
<input type="SUBMIT" name="submit"
value="Submit"/>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
This form includes a user name component which is used to store the comment
creator's name and name it as “ UserName ”

Add the reference of these comment jsp and the comments html into the
appropriate presentation template. In the case of riverbend, add to the News
Presentation Template.

Now you will be able to add and view the comments.
