IBMSBT in XPages: My Communities

In this article I will describe how to display a list of “My Communities” in an XPage. To do this I will have to create an ArrayList of communities and use a repeat control in an XPage to display them.

Getting My Communities

As I showed in this previous blog post there were some interesting issues in setting up the managed beans for getting the communities out of connections/smartcloud.

But in quick review this is the code for getting my communities.

package com.xomino.sbt;

import java.util.Collection;
import javax.faces.context.FacesContext;

import com.ibm.sbt.services.client.ClientServicesException;
import com.ibm.sbt.services.client.connections.communities.Community;
import com.ibm.sbt.services.client.connections.communities.CommunityList;
import com.ibm.sbt.services.client.connections.communities.CommunityService;
import com.ibm.sbt.services.client.connections.communities.CommunityServiceException;
import com.ibm.sbt.services.endpoints.Endpoint;
import com.ibm.xsp.extlib.util.ExtLibUtil;

public class Utils {

	public static void checkAuthentication() throws ClientServicesException{
		Endpoint theEndpoint = getEndpoint();
		if (!theEndpoint.isAuthenticated()){
			theEndpoint.authenticate(true);
		}
	}

	public static Endpoint getEndpoint(){
		Endpoint endpoint = (Endpoint) ExtLibUtil.resolveVariable(FacesContext.getCurrentInstance(), "smartcloud");
		return endpoint;
	}

	public static Collection<Community> getCommunities() throws CommunityServiceException{
		CommunityService svc = new CommunityService();
		CommunityList communities = svc.getMyCommunities();
		return communities;
	}
}

What is interesting is that the svc.getMyCommunities() returns a “Collection” and the code assist not only told me this, but it changed the return of the method “Collection” and added the import java.util.Collection.

So that is all well and good – but then how do I get that only my XPage?

Well I actually used the JavaDoc !
c1

So then in the XPage I used a repeat control to iterate through the “collection”

<xp:repeat id="repeat2" rows="30"
	value="#{javascript:com.xomino.sbt.Utils.getCommunities()}" var="myCommunities">
	<xp:div styleClass="readonly" disableTheme="true">
		<xp:text disableTheme="true">
		  <xp:this.value><![CDATA[#{javascript:myCommunities.getTitle()}]]></xp:this.value>
		 </xp:text>
	</xp:div>
</xp:repeat>

and there we have it.

c2

So what is a Collection exactly? I guess I ought to find out……

XPages and Java, starting over, again…..Hello World

OK to set the stage for what may just be about to happen – blogging is a form of self documentation for me. If I write it – it helps me remember it. I have said this before (multiple times) and failed, but I need to learn Java. It will probably fail again, let’s not kid ourselves, we all know Marky’s preferred language of choice (*coughs politely*). But I want to properly wrap my head around IBM Social Business Toolkit, and not just learn the limitations of the JavaScript API. To do this I need to learn Java and execute on it. So no promises, but we may get some blog posts on IBMSBT and my learning the Java way.

So with the help of Toby, the mockery of Brad and Kathy and the adulation of Eric, we will begin.

All of this code will be done in R9.0.1 (something)

Hello World (part 1)

1) Create a new Java Class in your database

There are apparently multiple ways to do this but I just went to the Java elements and clicked on the New Java Class button

j1
from the resulting popup enter a package name – in this case com.xomino.sbt, and the name for the new class, HelloWorld

The normal format for naming convention is

  • Package names are lower case and look like a website URL except backwards
  • Classes are Propercase

j2

 

Click OK and a new Class is created in Designer

j3

and the crowd went wild……well ok maybe not but I know Paul and Russ are laughing their butts off right now.

2) Create a new XPage (xHelloWorld.xsp)

In there I am going to create a button which when clicked will display hello world on the screen. To do this I am going to need a button and a computed field.

When the button is clicked I will set a viewScope variable and refresh the field.

The field is bound to the viewScope variable which I hope to set in the Java code…..Clicking the button will call the com.xomino.sbt.HelloWorld.setMessage() method, which will set the viewScope variable value.

<xp:button value="Click me" id="button1">
	<xp:eventHandler event="onclick" submit="true"
		refreshMode="partial" refreshId="aWrapper">
		<xp:this.action><![CDATA[#{javascript:com.xomino.sbt.HelloWorld().setMessage();}]]></xp:this.action>
	</xp:eventHandler>
</xp:button>

<xp:div id="aWrapper">
	<xp:text escape="true" id="computedField1"
		value="#{javascript:viewScope.message}">
	</xp:text>
</xp:div>

Once the viewScope variable has been set the aWrapper will be refreshed and I will see my value.

The class looks like this

package com.xomino.sbt;

import com.ibm.xsp.extlib.util.ExtLibUtil;

public class HelloWorld {

	public static void setMessage(){

		/*This is how you get XPages scopes in Java */
		ExtLibUtil.getViewScope().put("message", "Hello World");

	}
}

To enable the use of ExtLib code you must make sure the box is Checked in the database XSP properties
j4The output

helloWorld

 

In the dumbest way, this has made me very happy 🙂