EducationSoftwareStrategy.com
StrategyCommunity

Knowledge Base

Product

Community

Knowledge Base

TopicsBrowse ArticlesDeveloper Zone

Product

Download SoftwareProduct DocumentationSecurity Hub

Education

Tutorial VideosSolution GalleryEducation courses

Community

GuidelinesGrandmastersEvents
x_social-icon_white.svglinkedin_social-icon_white.svg
Strategy logoCommunity

© Strategy Inc. All Rights Reserved.

LegalTerms of UsePrivacy Policy
  1. Home
  2. Topics

KB44404: How to Create Standalone Code to Answer Multiple Nested Prompts Using the MicroStrategy Web SDK


Community Admin

• Strategy


This example demonstrates how to answer a nested object prompt consisting of any number of element prompts and value prompts. This code can be structured to answer any number of prompts of any type.

This example demonstrates how to answer a nested object prompt consisting of any number of element prompts and value prompts. This code can be structured to answer any number of prompts of any type.
The following workflow is used in the code below:

  1. Retrieve the ReportBean/RWBean and call collect data.
  2. Check the bean XML status then call the answer prompts method as long as the bean status is "waiting for user input".
  3. Within the answer prompts method, iterate through all of the prompts then call the appropriate method based on the type of prompt.
  4. Validate and answer all of the prompts (per level) simultaneously then call collect data on the bean.
  5. Within the individual helper methods for answering the prompts, an answer is manually supplied and is used to answer the prompt.
  6. Once all of the prompts has been answered, convert the results to JSON then export to a .TXT file.

Quick Tips for Programmatically Answering Prompts

  • The bean xml status should be checked outside a loop used to answer prompts; this enables the application to answer nested prompts.
  • The bean's collectData method should be called after answering the prompt on each level (if the prompts are nested, this enables the application to progress to the next level).
  • Do not use answerPrompt method on each individual WebPrompt object; the prompts should be answered simultaneously using the WebPrompts object's answerPrompts method (if there are multiple levels of nested prompts, each level would be answered simultaneously).
  • Similarly, the prompt validation should take place on all prompts simultaneously, not on each individual prompt.


package com.Strategy.sdk.examples.standalone;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import com.Strategy.sdk.utils.SessionUtil;
import com.Strategy.web.app.WebAppRuntimeException;
import com.Strategy.web.app.beans.EnumWebStateLevel;
import com.Strategy.web.app.tasks.architect.json.JSONException;
import com.Strategy.web.app.tasks.architect.json.XML;
import com.Strategy.web.beans.BeanFactory;
import com.Strategy.web.beans.EnumRequestStatus;
import com.Strategy.web.beans.RWBean;
import com.Strategy.web.beans.ReportBean;
import com.Strategy.web.beans.WebBeanException;
import com.Strategy.web.objects.EnumWebPromptType;
import com.Strategy.web.objects.WebConstantPrompt;
import com.Strategy.web.objects.WebElementSource;
import com.Strategy.web.objects.WebElements;
import com.Strategy.web.objects.WebElementsPrompt;
import com.Strategy.web.objects.WebFolder;
import com.Strategy.web.objects.WebObjectInfo;
import com.Strategy.web.objects.WebObjectsException;
import com.Strategy.web.objects.WebObjectsFactory;
import com.Strategy.web.objects.WebObjectsPrompt;
import com.Strategy.web.objects.WebPrompt;
import com.Strategy.web.objects.WebPrompts;

public class AnswerNestedPromptsStandalone {
	public static final String REPORT_ID = "DFD6FF1A40A62212FC8889BB6EC7E336";
	public static final String DOC_ID = "C2EBF7794605BC1F25350E82ECB895D9";
	private static final String OBJ_PROMPT_ID = "8879FB6542BEA042F503A4892F2D890E";
	
	private static final boolean DEBUG_MODE = true;
	private static final String OUTPUT_FILENAME = "C:\\results.txt";
	
	public static void main(String[] args) {
		out("Method Call: main");
		
		ReportBean rb = (ReportBean) BeanFactory.getInstance().newBean("ReportBean");
		rb.setSessionInfo(SessionUtil.getSession());
		rb.setObjectID(REPORT_ID);
		rb.setDefaultStateLevel(EnumWebStateLevel.TYPICAL_STATE_INFO);
		
		RWBean rwb = (RWBean) BeanFactory.getInstance().newBean("RWBean");
		rwb.setSessionInfo(SessionUtil.getSession());
		rwb.setObjectID(DOC_ID);
		rwb.setDefaultStateLevel(EnumWebStateLevel.TYPICAL_STATE_INFO);
		
		String results = "", error = "";
		try {
			if (rb != null) {
				rb.collectData();
				while (rb.getXMLStatus() == EnumRequestStatus.WebBeanRequestWaitingForUserInput)
				answerPrompts(rb);
				results += rb.getReportInstance().getResultsAsXML();
			}
			if (rwb != null) {
				rwb.collectData();
				while (rwb.getXMLStatus() == EnumRequestStatus.WebBeanRequestWaitingForUserInput)
				answerPrompts(rwb);
				results += rwb.getRWInstance().getResultsAsXML();
			}
			
			out(XML.toJSONObject(results).toString(1), OUTPUT_FILENAME);
			
		} catch (WebBeanException ex1) {
			error += "No Results";
			throw new WebAppRuntimeException(ex1);
		} catch (WebObjectsException ex2) {
			error += "No Results";
			throw new WebAppRuntimeException(ex2);
		} catch (JSONException e) {
			error += "JSON error";
			e.printStackTrace();
		} finally {
			out(error);
			SessionUtil.closeSession();
		}
	}
	
	private static void answerPrompts(ReportBean rb) throws WebBeanException, WebObjectsException {
		out("\nMethod Call: answerPrompts (ReportBean)");
		
		WebPrompts prompts = rb.getReportInstance().getPrompts();
		int index = 0;
		while (index < prompts.size()) {
		
			WebPrompt prompt = prompts.get(index);
			if (prompt == null) continue;
			
			if (!prompt.isClosed()) {
				switch (prompt.getPromptType()) {
					case EnumWebPromptType.WebPromptTypeObjects:
						WebObjectsPrompt wop = (WebObjectsPrompt) prompt;
						answerObjectPrompt(wop);
						break;
					case EnumWebPromptType.WebPromptTypeElements:
						WebElementsPrompt welp = (WebElementsPrompt) prompt;
						answerElementPrompt(welp);
						break;
					case EnumWebPromptType.WebPromptTypeConstant:
						WebConstantPrompt wcp = (WebConstantPrompt) prompt;
						answerConstantPrompt(wcp);
						break;
				}
			}
			index++;
		}
		
		prompts.validate();
		prompts.answerPrompts();
		rb.collectData();
	}
	
	private static void answerPrompts(RWBean rwb) throws WebBeanException, WebObjectsException {
		out("\nMethod Call: answerPrompts (RWBean)");
		
		WebPrompts prompts = rwb.getRWInstance().getPrompts();
		int index = 0;
		while (index < prompts.size()) {
		
			WebPrompt prompt = prompts.get(index);
			if (prompt == null) continue;
			
			if (!prompt.isClosed()) {
				switch (prompt.getPromptType()) {
					case EnumWebPromptType.WebPromptTypeObjects:
						WebObjectsPrompt wop = (WebObjectsPrompt) prompt;
						answerObjectPrompt(wop);
						break;
					case EnumWebPromptType.WebPromptTypeElements:
						WebElementsPrompt welp = (WebElementsPrompt) prompt;
						answerElementPrompt(welp);
						break;
					case EnumWebPromptType.WebPromptTypeConstant:
						WebConstantPrompt wcp = (WebConstantPrompt) prompt;
						answerConstantPrompt(wcp);
						break;
				}
			}
			index++;
		}
		
		prompts.validate();
		prompts.answerPrompts();
		rwb.collectData();
	}
	
	private static void answerConstantPrompt(WebConstantPrompt prompt) throws WebObjectsException, IllegalArgumentException {
		out("\nMethod Call: answerConstantPrompt");
		
		if (prompt.getName().equals("Start Date")) {
			prompt.setAnswer("07/09/2012");
		}
		
		if (prompt.getName().equals("End Date")) {
			prompt.setAnswer("07/12/2012");
		}
	}
	
	private static void answerObjectPrompt(WebObjectsPrompt prompt) throws WebObjectsException, IllegalArgumentException {
		out("\nMethod Call: answerObjectPrompt");
		
		WebFolder defaultWebFolder = prompt.getAnswer();
		defaultWebFolder.clear();
		
		WebObjectInfo object = WebObjectsFactory.getInstance().getObjectSource().getObject(OBJ_PROMPT_ID, 1);
		prompt.getAnswer().add(object);
		return;
	}
	
	private static void answerElementPrompt(WebElementsPrompt prompt) throws WebObjectsException {
		out("\nMethod Call: answerElementPrompt");
		
		WebElementSource eltsrc=prompt.getOrigin().getElementSource();
		WebElements elements = eltSrc.getElements();
		WebElements we = prompt.getAnswer();
		
		for (int e = 0; e < elements.size(); e++) {
			String originName = prompt.getOrigin().getName();
			String elemName = elements.get(e).getDisplayName();
			out("originName: " + originName + "; elemName: " + elemName);
			
			if (originName.equals("Year") && elemName.equals("2012")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Quarter") && elemName.equals("2012 Q3")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Category") && elemName.equals("Books")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Country") && elemName.equals("USA")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Subcategory") && elemName.equals("Science & Technology")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Quarter") && elemName.equals("2012 Q3")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Region") && elemName.equals("Central")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Quarter") && elemName.equals("2012 Q3")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Month") && elemName.equals("Jul 2012")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Item") && elemName.equals("Nanotechnology")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Call Center") && elemName.equals("Fargo")) {
				we.add(elements.get(e).getElementID());
				break;
			}
			if (originName.equals("Day") && (elemName.equals("7/9/2012"))) {
				we.add(elements.get(e).getElementID());
				continue;
			}
			if (originName.equals("Day") && (elemName.equals("7/12/2012"))) {
				we.add(elements.get(e).getElementID());
				break;
			}
		}
		prompt.setAnswer(we);
		return;
	}
	
	private static void out(String message) {
		if (DEBUG_MODE) System.out.println(message);
	}
	
	private static void out(String message, String filename) {
		PrintStream out = null;
		
		try {
			out = new PrintStream(new FileOutputStream(filename));
			out.print(message);
			out("\nFile output successful");
		} catch (IOException e) {
			out("\nError: could not write to the file");
		} finally {
			if (out != null) out.close();
		}
	}
}

 
Note: The SessionUtils classes referenced in this sample is a simple class that contains three methods: getSession, handleError, and closeSession. For an example of how these methods are coded, see KB44181: How to Programmatically Change Metric Formatting Using the MicroStrategy Web SDK 9.3.1 - 9.4.1
The example provided in this document is provided “as-is” to the user and assumes that the user:
•Can program, compile (e.g. javac, jikes, etc.), and execute Java programs
•Can configure environment variables (e.g. JAR files, property files, etc.)
•Have all the necessary tools to create Java applications (JDK, IDE, etc.)
•Has access to the Strategy SDK documentation.
•Has read the customization warning at the bottom of this document
 
For the sample to work, the Strategy Web JAR files must be accessible by the Java Runtime
Environment. The Strategy Web JAR files can be found under:
•Strategy Web Universal (JSP): {web_root}/WEB-INF/lib directory.
•Strategy Web (.NET): Program Files\Common Files\Strategy directory.
 
ADDITIONAL INFORMATION:
The Strategy SDK allows you to customize the standard Strategy Web interface, and extend and integrate the Strategy business intelligence functionality into other applications. However, before changing the way Strategy Web products look or behave, it is helpful to understand how the application is built. For more information regarding the Strategy Web architecture or the process of customizing Strategy Web, please refer to Strategy Developer Zone (https://community.strategy.com/topic/0TO44000000FliLGAS/sdk).
To access the Strategy Developer Zone, you must have access to the Strategy Knowledge Base, you must have purchased the Strategy SDK, and you must be current on your Strategy maintenance agreement. If you are a US-based business and believe that you satisfy all three of these conditions but you do not have access to the Strategy Developer Zone, please contact Strategy Technical Support at support@microstrategy.com or at (703) 848-8700. If you are an international business, please contact Strategy Technical Support at the appropriate email address or phone number found at https://www.microstrategy.com/en/support/contact-support.
CUSTOMIZATION WARNING:
This customization is provided as a convenience to Strategy users and is only directly applicable to the version stated. While this code may apply to other releases directly, Strategy Technical Support makes no guarantees that the code provided will apply to any future or previous builds. In the event of a code change in future builds, Strategy Technical Support makes no guarantee that an updated version of this particular customization will be provided. In the event of a code change in future builds, Strategy may not be able to provide additional code on this matter even though this customization is provided at this time for this specific build. For enhancements to this customization or to incorporate similar functionality into other versions, contact your Account Executive to inquire about Strategy Consulting assistance.


Comment

0 comments

Details

Knowledge Article

Published:

June 8, 2017

Last Updated:

June 8, 2017