The following code demonstrates how to programmatically answer an element prompt within an event handler using the Strategy Java Web SDK.
package customclasses.handlers;
import com.Strategy.web.beans.AggregatedEventHandler;
import com.Strategy.web.beans.PromptsBean;
import com.Strategy.web.beans.ReportBean;
import com.Strategy.web.beans.RequestKeys;
import com.Strategy.web.beans.WebBeanException;
import com.Strategy.web.beans.WebEvent;
import com.Strategy.web.beans.WebException;
import com.Strategy.web.objects.WebElements;
import com.Strategy.web.objects.WebElementsPrompt;
import com.Strategy.web.objects.WebPrompt;
import com.Strategy.web.objects.WebPrompts;
public class CustomPromptEventHandler extends AggregatedEventHandler {
public CustomPromptEventHandler() {
super();
//this line signifies prompt type handler.
this.setHandlerType(8);
}
public boolean processRequest(RequestKeys keys) throws WebException {
System.out.println("Entering mycustom event handler");
WebEvent event = getWebEvent(keys);
System.out.println(event);
if (event == null) {
System.out.println("null Entering mycustom event handler");
return super.handleDefaultRequest(keys);
} else {
System.out.println("null Entering mycustom event handler");
}
PromptsBean pb = null;
ReportBean rb = null;
//answer prompt for report: ID = B03E01874A35ED80F75F7D8DF148ED62
//check to make sure this is the report before preceeding
pb = (PromptsBean) getWebComponent();
rb = (ReportBean) pb.getParent();
try {
if (!rb.getObjectID().equalsIgnoreCase("B03E01874A35ED80F75F7D8DF148ED62")) {
System.out.println("Entering mycustom event handler");
return super.handleDefaultRequest(keys);
} else {
System.out.println("calling else");
}
} catch (WebBeanException wbe) {
wbe.printStackTrace();
}
System.out.println("continue");
/*The following Object IDs represent the Employee Attribute ID and the Prompt ID.Hardcoding it here
* because this prompt object will be associated to my custom style. This information can always be dynamically retrieved.
*/
String EmpAttID = "8D679D3F11D3E4981000E787EC6DE8A4";
String CustomPromptID = "BCC357384DD9A4C97BEFCDA94D34CBB6";
//format 8D679D3F11D3E4981000E787EC6DE8A4;8D679D3F11D3E4981000E787EC6DE8A4:ID:LastName:FirstName
//sample: &elementsPromptAnswers=8D679D3F11D3E4981000E787EC6DE8A4;8D679D3F11D3E4981000E787EC6DE8A4:1:Bates:Michael;8D679D3F11D3E4981000E787EC6DE8A4:100:test:test
switch (event.getID()) {
case 8001:
//AnswerAllPrompts event triggered
String name;
String val;
for (int i = 0; i < keys.getNameCount(); i++) {
name = keys.getName(i);
val = keys.getValue(name);
System.out.println(name + " = " + val);
}
//Get prompt answers from RequestKeys
String ElemAnswers = keys.getValue("Emps");
if (ElemAnswers == null) {
return super.handleDefaultRequest(keys);
}
WebPrompts wps = pb.getPrompts();
System.out.println("Prompts Count: " + wps.size());
WebPrompt wp = null;
WebElementsPrompt wep = null;
WebElements wes = null;
//The following loop will iterate through the prompts collection and locate our custom prompt
for (int x = 0; x < wps.size(); x++) {
wp = wps.get(x);
if (wp.getID().equalsIgnoreCase(CustomPromptID)) {
//build answer for custom prompt
wep = (WebElementsPrompt) wp;
wes = wep.getAnswer();
//clear whatever might currently be there
wes.clear();
System.out.println(EmpAttID + ":" + ElemAnswers);
//add the new answer. This assumes I have one element. if there are more than 1 answers, then iterate through the
//list and call wes.add(..) for each employee element.
wes.add(EmpAttID + ":" + ElemAnswers);
}
}
return super.handleDefaultRequest(keys);
default:
System.out.println("other event called:" + event.getID());
return super.handleDefaultRequest(keys);
}
}
}