MicroStrategy ONE

Passing Prompt Answers through the URL

These code samples illustrate how to pass prompt answers through the URL rather than from the Web interface of the application. This is useful for establishing a link from an external system to the MicroStrategy Web interface. These code samples also illustrate how to use an add-on to modify the data collection of the application.

To understand the concepts explained in this example, it is recommended you become familiar with the Web architecture and page execution in the Understanding MicroStrategy Web section. You should also read sections on using add-ons in the application, as well as the WebObjectsPrompts interface in the Prompts Web Objects API section.

The application is configured to read a request parameter—whose value will be used as a prompt answer—passed from the browser. This parameter is passed through the URL query string, as form data or a cookie. To prevent the GUI for prompts from being displayed, this prompt answer must be passed to the report bean before it requests report execution from MicroStrategy Intelligence Server.

Since the data collection process is being modified, using an add-on is the best choice for this implementation. Create a new add-on that reads the value of a request parameter, checks that the PageComponent is a report bean, and then passes the value of that request parameter as the value of the prompt answer to the report bean. If the answer is complete and correctly structured, Intelligence Server accepts the prompt answer and executes the prompt without requesting more information from the user. The GUI for prompt is not displayed.

Code samples are provided to illustrate the following:

  • Answering a set of prompt questions

    This code sample accepts the prompt answer as an XML data structure. Since this XML structure represents the prompt answer for all prompts, it allows any number of prompts and prompt types to be answered. 

  • Answering a particular prompt question

    This code sample resolves a prompt answer for a specific prompt question, such as the value of the Year attribute. This allows the URL to include a parameter such as "year=2001" and pass this answer to a constant prompt in the report. 

Answering a set of prompt questions

The following code sample illustrates the execute method for the add-on. It first uses the getReportBean method implemented in the add-on creation example to verify that the page component instance is a report bean. If it can be verified, it gets the prompt answer from the request keys as an XML data structure and uses this XML to answer the prompts in the report. If it cannot be verified, it exits.  

public void execute(com.microstrategy.web.app.beans.PageComponent pageComponent) {

   ReportBean rb=getReportBean(pageComponent);

   if (rb==null) return;

   // read the value of the prompt answer from the application context object

   AppContext appContext=pageComponent.getAppContext();

   // obtain the RequestKeys instance from the page component. The RequestKeys is a collection of parameters that are passed in the request object using a form (either GET or POST method) or from the cookies.

   RequestKeys keys=appContext.getRequestKeys();

   

   // ask for the value of the prompt answer parameter. If it is empty or NULL, then exit.

   String answerXml=keys.getValue(PROMPT_ANSWER_KEY);

   if (answerXml==null || answerXml.equals("")) return;

  

   // simplified exception handling around whole method

   try {

      // get the report instance object from the report bean

      WebReportInstance reportInstance=rb.getReportInstance();

      // obtain the prompts collection (the collection ofWebPrompts)from the report instance

      WebPrompts prompts=reportInstance.getPrompts();

      // populate the prompt answers with the supplied answer XML

      // generate a webobjectsException if invalid structure

      prompts.populateAnswers(answerXml);

      // since the answers are populated, validate them ( check for minimum and maximum values or numberof prompt answers specified by the prompt creator)  and submit them to Intelligence Server

      prompts.validate();

      prompts.answerPrompts();

   }

   catch (WebObjectsException ex) {

      ex.printStackTrace();

      return;

   }

   catch (WebBeanException ex1) {

      ex1.printStackTrace();

      return;

   }

}

Answering a particular prompt question

These code samples resolve a prompt answer for a specific prompt question—in this case, the value of a year. This allows the URL to include a parameters such as "year=2001" and pass this answer to a constant prompt in the report. The particular report is identified using the reportID. Both code samples assume that a valid reportID is available for the report that includes a prompt.

The code snippet below illustrates the execute method, which is used as the starting point for the add-on. In this case, the report bean is interrogated for the reportID of the report it represents. It is then compared with the reportID specified in the add-on. If the reportIDs are identical, then prompt answering continues.

// verify that the page component instance is a report bean. If it cannot be verified, then exit.

public void execute(com.microstrategy.web.app.beans.PageComponent pageComponent) {

   ReportBean rb=getReportBean(pageComponent);

   if (rb==null) return;

   // reads the value of the prompt answer from the application context object. The reportID can be a hardcoded variable

   // or can be supplied from the add-on parameters.

   AppContext appContext=pageComponent.getAppContext();

   try {

      if (reportid.equals(rb.getObjectID())) {         

         //see code sample below for implementation

         specificReportAnswer(appContext,rb);

      }        

   }

   catch (WebBeanException ex) {

      Log.logger.log(Level.ERROR,"WebBeanException in prompt answer addon");

      ex.printStackTrace();

      return;

   }   

}

The code snippet below illustrates the specificReportAnswer method, which handles the answering of the prompt. It is also located within the add-on and is called from within the execute method shown above. The value of the year to be used as an answer to the prompt is obtained from the request keys. The RequestKeys interface represents the information—stored in key-values pairs—passed to the application from the browser. In this example, there is a key that is configured by the YEAR_PROMPT_ANSWER static variable. If the value is empty, the method exits. Otherwise, it uses the value to answer the prompt.

private void specificReportAnswer(AppContext appContext,ReportBean rb) {

   RequestKeys keys=appContext.getRequestKeys();

   String year=keys.getValue(YEAR_PROMPT_ANSWER);

   if (year==null) return;

 

   // simplified exception handling around whole method

   try {

      // obtain the report instance from the report bean

      WebReportInstance reportInstance=rb.getReportInstance();

      //obtain the prompts collection (the collection of WebPrompts) from the report instance

      // obtains . The method then loops through the prompts  

      WebPrompts prompts=reportInstance.getPrompts();

      int index=0;

 

      // loop through the promptsattempting to find a constant prompt.If a constant prompt is found, pass the answer supplied to the prompt

      while (index<prompts.size()) {

         WebPrompt prompt=prompts.get(index);

         if (prompt==null) continue;

         //if we find a constant prompt type integer, then answer it with our year

         switch (prompt.getPromptType()) {

            case EnumWebPromptType.WebPromptTypeConstant:

            WebConstantPrompt constantPrompt=(WebConstantPrompt)prompt;

            constantPrompt.setAnswer(year);

         }

         index++;

      }

 

      // validate the answers ( check for minimum and maximum values or number of prompt answers specified by the prompt creator) and submit them to Intelligence Server.

      prompts.validate();

      prompts.answerPrompts();

   }

   catch (WebObjectsException ex) {

      ex.printStackTrace();

      return;

   }

   catch (WebBeanException ex1) {

      ex1.printStackTrace();

      return;

   }

}