Implement Comment service for a KM document using Java
Implement Comment service for a document in KM using Java
Summary
This document provides the detailed steps for implementing comment service for a document in KM. Using this we can create a new comment entry, read all the comments for a document
Applies to:
SAP Enterprise Portal, NWDS 7.0
Pre-requisites:
Knowledge of PDK application development
Basic knowledge on KM
Access to KM folders on the portal
Steps to enable comment service in KM
- 1. Launch SAP Enterprise and login with user ID having system admin role
- 2. Navigate to System Configuration à Content Management à Repository Managers à CM Repository
- 3. Select any working repository and click on “Edit” to add service “Comment” from the list as shown below
- 4. Save the settings.
- 5. Add few documents to the repository selected.
- 6. Navigate to http://<host>:<port>/irj/go/km/docs/<repository path>.
- 7. Select the context menu for the document and click “Details”. The Write a Review link is displayed for adding a new review.
Steps to create the application in NWDS:
- 1. Launch NWDS and navigate to Enterprise Portal perspective.
- 2. Choose File à New à Project.
- 3. Select “Portal Application” from the wizard and click “Next”.
- 4. Enter the project name and the location where the project need to be saved.
- 5. Click on “Finish” and a new project with the name given in step 4 is displayed.
- 6. Right click on the created project àNew àOther
- 7. Select Portal application from the right hand side menu item as shown below
- 8. Click on “Next” and select the project.
- 9. Click on “Next” and select “Abstract Portal Component” under portal component as shown below.
- 10. Click on “Next” and fill in the required details and click on “Finish”.
- 11. Once the component is created double click to open in NWDS.
- 12. Copy the below code and paste it in the doContent() method
String rLocationtest = "/collaboration/comment";
IResourceContext resourceContext =
ResourceFactory.getInstance().getServiceContext("cmadmin_service");
ICollection collection =
(ICollection)ResourceFactory.getInstance().getResource(RID.getRID
(rLocationtest),resourceContext);
loopFolder(collection,resourceContext);
- 13. Create a new method loopFolder() with parameters ICollection and IResourceContext and paste the code below
public void loopFolder(ICollection collection, IResourceContext resourceContext, IPortalComponentResponse response)
{
resList = collection.getChildren();
resItr = resList.listIterator();
while(resItr.hasNext())
{
IResource restemp = resItr.next();
if(restemp.isCollection() &&
LinkType.NONE.equals(restemp.getLinkType()))
{
loopFolder((ICollection)restemp,resourceContext,response);
}
else
{
docRID = RID.getRID(""+restemp);
kmResource =(IResource)
ResourceFactory.getInstance().getResource(docRID,resourceContext);
retrieveCommentsForFileInKM(kmResource);
createNewComment(docRID.toString,resourceContext);
}
restemp = null;
}// End of while
}
- 14. Create method retrieveCommentsForFileInKM and paste the below code.
private void retrieveCommentsForFileInKM(IResource kmResource, IPortalComponentResponse response){
if(kmResource != null)
{
IContent content = kmResource.getContent();
InputStream is = content.getInputStream();
fileLength = content.getContentLength();
BufferedReader br = new BufferedReader(new
InputStreamReader(is));
String str = "";
StringBuffer sbuf = new StringBuffer();
while ((str = br.readLine()) != null)
{
if (!str.trim().equals(""))
{
sbuf.append(str);
}
}
br.close();
strContent = sbuf.toString();
response.write("Content in comment file: - "+strContent);
br = null;str = null;sbuf = null;content = null;is = null;
}
}
- 15. Create a method createNewComment() and add the code as shown below
private void createNewComment(String kmPath, IPortalComponentResponse response, IResourceContext resourceContext)
{
stkRID = new StringTokenizer(kmPath,"/");
numberOfTokens = stkRID.countTokens();
difference = 1;
RIDForCreatingFile = "";
while(numberOfTokens != difference)
{
RIDForCreatingFile += "/"+stkRID.nextToken();
difference++;
}
RIDForCreatingFile = RIDForCreatingFile + "/";
newCommentFileName =
"comment_"+ep5User.getName()+"_"+System.currentTimeMillis()+".html";
//code for creating a new file
docRID = RID.getRID(RIDForCreatingFile);
folder = (ICollection)
ResourceFactory.getInstance().getResource(docRID,resourceContext);
kmResource = folder.createResource(newCommentFileName,null,null);
newCommentFileName = RIDForCreatingFile+newCommentFileName;
docRID = RID.getRID(newCommentFileName);
kmResource =(IResource)
ResourceFactory.getInstance().getResource(docRID,resourceContext);
strContent = "Adding contect to empty files to check KM write
functionality";
is = new ByteArrayInputStream(strContent.getBytes("UTF-8"));
content = new Content(is, "text/html", -1, "UTF-8", null);
kmResource.updateContent(content);
response.write("Content updated successfully<br>");
}
- 16. Add the missing imports and add any required jars and deploy the application on the portal. The application will list out all the reviews for a document and also create a comment file dynamically
- 17. Location of the required jars can be found at http://sapjarfinder.com/
Be the first to leave a comment
You must be Logged on to comment or reply to a post.