Sample for implementing an asynchronous interface
You might want to implement an asynchronous interface when an asynchronous call must be made and the return content is either JSON, TEXT_PLAIN, or TEXT_XML.
Implementing an asynchronous interface
Here
is the sample code for when an asynchronous call has to be made and
the return content is either TEXT/XML, JSON, or TEXT_PLAIN.
public interface AsyncEnabled {
public static String TEXT_PLAIN="text/plain";
public static String TEXT_XML="text/xml";
public static String JSON="json";
/**
* the implementation class should set the resonse string so that
* WPC UI Infrastructure can process the AJAX calls
* @return java.lang.String
*/
public String getResponseContent();
/**
* possible selection of the content type
* set either TEXT_PLAIN or TEXT_XML
* @return
*/
public String getContentType();
}
Here is the sample code for the implementation
class:
public class WorklistPageCommand implements AsyncEnabled
{
String responseContent ="";
public String getContentType() {
return JSON;
}
public String () {
return responseContent;
}
public String getCollaborationAreaEntries() throws Exception {
AustinContext.setCurrentContext(
(AustinContext)request.getSession().getAttribute("ctx"));
ctx = PIMContextFactory.getCurrentContext();
manager = ctx. getCollaborationAreaManager();
JSONObject topJsonObj = new JSONObject();
topJsonObj.put("identifier","abbreviation");
JSONArray itemsArr = new JSONArray();
//Getting the collaboration areas form the pim context
PIMCollection allCAs = manager.getAllCollaborationAreas();
CollaborationArea colArea = null;
if(allCAs!=null && allCAs.size() > 0){
for (Iterator i = allCAs.iterator(); i.hasNext(); ){
JSONObject itemJsonObj = new JSONObject();
colArea = (CollaborationArea) i.next();
String caName = colArea.getName();
itemJsonObj.put("abbreviation", caName);
itemJsonObj.put("label", caName);
itemJsonObj.put("name", caName);
itemsArr.add(itemJsonObj);
}
}
topJsonObj.put("items",itemsArr);
responseContent = topJsonObj.serialize(true);
return null;
}
}
Here is the sample configuration in the flow-config.xml
file:
<async-flows>
<async-flow path="WorkflowQueryStore"
command="com.ibm.ccd.ui.worklist.WorklistPageCommand"
method="getCollaborationAreaEntries"/>
</async-flows>
Note: For all of the asynchronous calls,
one of them must implement the AsyncEnabled interface and the corresponding
configuration should be
<async-flows><async-flow ….
/></async-flow>
.After the operation is performed
the response content should be set to responseContent which is an
instance variable and the same variable should passed when the framework
calls the getResponseContent() method. The getContentType() method
should return either JSON/ TEXT_PLAIN/ TEXT_XML
.
If this is not set by default, it will take the TEXT_PLAIN
.