Step 5: Add a Script for XML Mapping

In the previous step, you added an XML map to your web service, but one part of the map was left unfinished. The <us_percentile> element, which gives the applicant's percentile ranking, currently displays a default value of 50% in all cases.

<applicant id="222222222">    
   <name_last>Smith</name_last>
   <name_first>John</name_first>
   <bankrupt>false</bankrupt>
   <balance_remaining>2000</balance_remaining>
   <risk_estimate>Applicant is a high risk.</risk_estimate>
   <score_info>
      <credit_score>560</credit_score>
      <us_percentile>50</us_percentile>
   </score_info>
   <message>Credit Report complete.</message>
</applicant>

In the following step you will finish the XML map by calculating a value for the <us_percentile> element. To do this, you will write a function in ECMAScript (also known as JavaScript), and then refer to the function from the XML map.

The tasks in this step are:

To Add a Script File for Use with Mapping

  1. Right-click the investigateJWS folder and select New-->Other File Types.
    The New File dialog opens.
  2. Enter values as shown in the following illustration:

  3. Click Create. This adds a JSX file called PercentileScript.jsx. WebLogic Workshop displays the new file in Source View.
  4. Paste the following code into PercentileScript.jsx. Overwrite all of the code already in the file.
    /* A function to calculate the applicant's credit score percentile. */
    function calcPercentile (score) {
        var percentile = 0;
        var scoreLowEnd = 300;
        var number = 3162.5;
        percentile = Math.ceil((Math.pow(score, 2) - Math.pow(scoreLowEnd, 2))/(2 * number));
        return percentile;
    }
  5. Press Ctrl+S to save your work. Press Ctrl+F4 to close close PercentileScript.jsx.

To Edit the XQuery Map to Call Script

In this task you will edit the XQuery map to use PercentileScript.jsx to fill in the value of the <us_percentile> element.

  1. If Investigate.jws is not displayed, from the Window menu, select Investigate.jws. Click the Source View tab.
  2. Edit the XQuery map to look like the following. Code to edit appears in red.
        public interface Callback extends com.bea.control.ServiceControl
        {
            /**
             * @common:message-buffer enable="true"
             * @jws:conversation phase="finish"
             * @jws:parameter-xml schema-element="ns0:applicant" xquery::
             * declare namespace ns0 = "http://www.openuri.org/"
             * declare namespace ns1 = "http://openuri.org/bea/samples/workshop"
             * 
             * <ns1:applicant id = "{ xs:int( data($input/ns0:applicant/ns0:taxID) ) }">
             *     <ns1:balance_remaining>{ xs:short( data($input/ns0:applicant/ns0:availableCCCredit) ) }</ns1:balance_remaining>
             *     <ns1:bankrupt>{ data($input/ns0:applicant/ns0:currentlyBankrupt) }</ns1:bankrupt>
             *     {
             *         for $message in $input/ns0:applicant/ns0:message
             *         return
             *             <ns1:message>{ data($message) }</ns1:message>
             *     }
             *     {
             *         for $firstName in $input/ns0:applicant/ns0:firstName
             *         return
             *             <ns1:name_first>{ data($firstName) }</ns1:name_first>
             *     }
             *     {
             *         for $lastName in $input/ns0:applicant/ns0:lastName
             *         return
             *             <ns1:name_last>{ data($lastName) }</ns1:name_last>
             *     }
             *     {
             *         for $approvalLevel in $input/ns0:applicant/ns0:approvalLevel
             *         return
             *             <ns1:risk_estimate>{ data($approvalLevel) }</ns1:risk_estimate>
             *     }
             *     <ns1:score_info>
             *         <ns1:credit_score>{ xs:short( data($input/ns0:applicant/ns0:creditScore) ) }</ns1:credit_score>
             *         <ns1:us_percentile> {investigateJWS.PercentileScript.calcPercentile(data($input/ns0:applicant/ns0:creditScore))} </ns1:us_percentile>
             *     </ns1:score_info>
             * </ns1:applicant>
             * ::
             */
            void onCreditReportDone(investigateJCS.Applicant applicant);
        }
    Note that the value of the <us_percentile> element is based on the value of the <credit_score> element: when the calcPercentile() function is called, the value of the <credit_score> element is passed in as a parameter.
  3. Press Ctrl+S to save your work.

 

To Test the Investigate Web Service

  1. Confirm that Investigate.jws is open and displayed in the main work area.
  2. Press Ctrl+F5 run Investigate.jws. (Pressing Ctrl+F5 is equivalent to clicking the Start button: )

  3. Your web service compiles and Workshop Test Browser launches.
  4. In the Workshop Test Browser, in the taxID field enter the 9 digit number 222222222.

    Note:  Use one of the following (9 digit) taxID's to test your web service throughout the tutorial:

    123456789, 111111111, 222222222, 333333333, 444444444, and 555555555.
  5. Click the requestCreditReport button.
  6. Click Refresh until the Message Log shows an entry for callback.onCreditReportDone.

    Note the value of the <us_percentile> element.

Related Topics

Handling XML with ECMAScript Extensions

Click one of the following arrows to navigate through the tutorial: