Sunday, July 28, 2013

Compare two collections or xmls and set value in output using xsl/transformation

We come across situations wherein we need to compare some elements from two collection and generate the output
I would like to explain the solution using the example:
Input1 contains list of employees and their current salary and Input2 contains the increment percentage for the employees.
Now input2 might not contain all the employees, so for those missing employees, the increment is default which is 5%
We need to calculate the new salary for all the employees from input1
Sample inputs are as below:

Input1:


<EmployeeDetails>
      <employeeid>1</employeeid>
      <employeename>employeename10</employeename>
      <salary>1000</salary>
   </EmployeeDetails>
   <EmployeeDetails>
      <employeeid>2</employeeid>
      <employeename>employeename12</employeename>
      <salary>10000</salary>
   </EmployeeDetails>
   <EmployeeDetails>
      <employeeid>3</employeeid>
      <employeename>employeename14</employeename>
      <salary>1500</salary>
   </EmployeeDetails>

Input2:


<IncrementDetails>
      <employeeid>1</employeeid>
      <incrementPercentage>10</incrementPercentage>
   </IncrementDetails>
   <IncrementDetails>
      <employeeid>2</employeeid>
      <incrementPercentage>20</incrementPercentage>
   </IncrementDetails>

Expected output:


<output xmlns:client="http://xmlns.oracle.com/CopyTables/LoopTwoNodes/LoopTwoNodesBPEL" xmlns="http://xmlns.oracle.com/CopyTables/LoopTwoNodes/LoopTwoNodesBPEL">
         <client:output>
            <client:employeeid>1</client:employeeid>
            <client:employeename>employeename10</client:employeename>
            <client:salary>1100</client:salary>
            <client:comment>Congratulation!! Your increment percentage is 10</client:comment>
         </client:output>
         <client:output>
            <client:employeeid>2</client:employeeid>
            <client:employeename>employeename12</client:employeename>
            <client:salary>12000</client:salary>
            <client:comment>Congratulation!! Your increment percentage is 20</client:comment>
         </client:output>
         <client:output>
            <client:employeeid>3</client:employeeid>
            <client:employeename>employeename14</client:employeename>
            <client:salary>1575</client:salary>
            <client:comment>Default increment</client:comment>
         </client:output>
      </output>

xsl/Transformation to achieve this looks like below:


<xsl:param name="input2"/>
  <xsl:template match="/">
    <client:output>
      <xsl:for-each select="/client:input1/client:EmployeeDetails">
        <xsl:variable name="loopvar" select="position()"/>
        <xsl:choose>
          <xsl:when test='contains(oraext:create-delimited-string($input2/client:input2/client:IncrementDetails/client:employeeid,","),client:employeeid)'>
            <xsl:for-each select="$input2/client:input2/client:IncrementDetails">
              <xsl:if test="client:employeeid = /client:input1/client:EmployeeDetails[$loopvar]/client:employeeid">
                <client:output>
                  <client:employeeid>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:employeeid"/>
                  </client:employeeid>
                  <client:employeename>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:employeename"/>
                  </client:employeename>
                  <client:salary>
                    <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:salary + (/client:input1/client:EmployeeDetails[$loopvar]/client:salary * (client:incrementPercentage div 100.0))"/>
                  </client:salary>
                  <client:comment>
                    <xsl:value-of select='concat("Congratulation!! Your increment percentage is ", client:incrementPercentage)'/>
                  </client:comment>
                </client:output>
              </xsl:if>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise>
            <client:output>
              <client:employeeid>
                <xsl:value-of select="client:employeeid"/>
              </client:employeeid>
              <client:employeename>
                <xsl:value-of select="client:employeename"/>
              </client:employeename>
              <client:salary>
                <xsl:value-of select="/client:input1/client:EmployeeDetails[$loopvar]/client:salary + (/client:input1/client:EmployeeDetails[$loopvar]/client:salary * (5.0 div 100.0))"/>
              </client:salary>
              <client:comment>
                <xsl:text disable-output-escaping="no">Default increment</xsl:text>
              </client:comment>
            </client:output>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each>
    </client:output>
  </xsl:template>


Happy Coding J

Cheers,

LetsLearnOracleSOA Team

Thursday, July 25, 2013

Composite Sensors and Composite Set Title

While working on EM console and handling huge number of instances, It get difficult to identify which instance belongs to which process  and then starts a tedious process of searching every instance to find the one which we are looking for JJ.

To reduce this burden we can implement below  solutions and save ourselves from long never ending search :
1)      Composite Sensors
2)      Composite Set Title


1) Composite Sensors:

Composite sensors provide a method for implementing track able fields on messages.

Note:  
1.You add sensors to the following components of a SOA composite application in the SOA Composite Editor:
·        Service or reference binding components
·        Service components such as a BPEL process or Oracle Mediator that have subscribed to business events

 2. It uses a run time table COMPOSITE_SENSOR_VALUE  to store the value of the sensor in DEV_SOAINFRA.

To Add Composite Sensors:

Let’s take an example where you have a BPEL process which insert Employee Information to DB. This is a web service which receive information from an external system and then do data manipulation to insert data into DB.

XSD:

Steps 1: Click on the External or Exposed service and select the option Composite sensor:


Step 2: Click on add and give it a name (this name will be visible in EM console).Once the name is given we have 3 options to choose from to create the sensor.

A)     If we select Variable then we can pass variable directly to this sensor:

B)     If we want to have some expressions then we can select expression :
C)     We can set properties too:
Step 3:  Create the sensors and deploy the BPEL Process to EM:
Step 4: Test Payload:

<ns1:EmployeeID>123</ns1:EmployeeID>
 <ns1:EmployeeName>Scott</ns1:EmployeeName>
 <ns1:EmployeeDOB>010190</ns1:EmployeeDOB>
 <ns1:EmployeePhone>12234</ns1:EmployeePhone>

Now go to EM console Instance tab and Click on AddField to search for the sensor:



Some restrictions on using composite sensors:
·        Functions can only be used with the payload. For example, XPath functions such as concat() and others cannot be used with properties.
·        Any composite sensor that uses expressions always captures values as strings. This action makes the search possible only with the like comparison operator. Also, even if the value is a number, you cannot use other logical operators such as <, >, =, and any combination of these.
·        Composite sensors only support two types of sensor actions: Enterprise Manager and JMS.
·        Header-based sensors are only supported for web service bindings.
·        Sensor actions for Oracle B2B, service data objects (SDOs), web services invocation framework (WSIF), and Oracle Business Activity Monitoring bindings are not supported.
·        Sensor values can only be one of the following types.
1.       The following scalar types:
§  STRING
§  NUMBER
§  DATE
§  DATE_TIME
2.       Complex XML elements
·        When creating an XPath expression for filtering, all functions that return a node set must be explicitly cast as a string:
         xpath20:upper-case(string($in.request/inp1:updateOrderStatus/inp1:orderStatus) ) = "PENDING"
·        Sensors can only be configured on service components that subscribe to business events. The option to configure sensors is not available if there are no subscribed business events associated with the service component.
·        Sensors cannot be configured on service components that publish business events.
·        Sensors based on business event headers are not allowed (only payloads are allowed).
·        PL/SQL subscriptions are not supported.


2) Composite Set Title function :


 Drag the function ora:setCompositeInstanceTitle() on the variable which needs to be set as Title.

Note: The variable which is used shall be type cased to string variable or else the setTitle won’t work.




The value of the variable which has been set title is displayed on Name tab.


When to use Composite sensor and when to use Composite set title:


The answer of this question is relative , completely based on the requirement. But a general thumb rule can be used.


A)      If your required can be full filled by searching 1 variable then Set title is an easy and effective solution. It can be achieved by a simple browser search.
B)      If your requirement needs to have search on multiple variables and on some complex logic then composite sensors are the only solutions.
 


Saturday, July 13, 2013

Validate XML schema In BPEL

Hello Friends,

Many time we came across the situation where we need to validate our input based on the schema before doing any processing on the received input.

Oracle BPEL provides 3 ways to do validation.

One way is to use Validate Activity and the second is by enabling Validate option in Assign/Transform activity and third way is Payload Validation on EM Console. Later in this Blog we will explain the implementation details.
 
Implementation of Validate Activity:
This activity enables you to validate variables in the list. The variables are validated against their XML schema.

[Note] : In BPEL 2.0, the Validate dialog includes a Documentation tab, Targets tab, and Sources tab, and does not include a Skip Condition tab

Steps to Implement:

1.     Create a BPEL process (For the Demo purpose below mentioned schema has been used to validate)




2.     From the BPEL Constructs section of the Component Palette, drag a Validate activity into the designer.
[Note]: In BPEL 1.1 Validate activity is not displayed in BPEL Constructs drop down menu. It needs to be added on source code (Mentioned in step 4).



3.     Double-click the Validate and Enter a name for the activity. Add the variable which needs to be validated.




4.     Click the Source tab to view the syntax. Note that the syntax for validating XML data with the validate activity is slightly different between BPEL versions 1.1 and 2.0.

                       <validate name="Validate_XMLSchema" variables="inputVariable"/> (For BPEL 2.0)

                       <bpelx:validate variables="inputVariable" name="Validate_XMLSchema"/> (For BPEL 1.1)

5.     Deploy to server.
6.     As mentioned in the XSD (step 1) we have put constraint of length 8 to field CompanyName. To Check the validation we will have 9 charaters for CompanyName
                  
                       Input:

                      <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
                            <soap:Body>
                               <ns1:process xmlns:ns1="http://xmlns.oracle.com/Demo/DemoValidateActivity">
                                       <ns1:CompanyName>123456789</ns1:CompanyName>
                                       <ns1:CompanyID>12345</ns1:CompanyID>
                              </ns1:process>
                           </soap:Body>
                        </soap:Envelope>

                      Output:

                         




Implementation of Assign Activity with validate option enabled:

Steps to implement:

1.      From the BPEL Constructs section of the Component Palette, drag an Assign activity into the designer.
2.      Double-click the Assign activity.
3.      In the General tab, enter a name for the activity and select the Validate checkbox.

                        


4.      Click Apply, then OK.
5.      Click the Source tab to view the syntax. Note that the syntax for validating XML data with the assign activity is slightly different between BPEL versions 1.1 and 2.0.
<assign name="Assign_ValidateXML" validate="yes"/> (For BPEL 2.0)
<assign name="Validate_XMLSchema" bpelx:validate="yes"/> (For BPEL 1.1)
 
6.      Deploy to server
 

 Implementation of Payload Validation:

          1.     Login to EM Console and go to the composite for which Payload validation is needed. Go to settings -> Payload Validation and select option Enable.                                                                                              
 
2.       Schema validation failed then an error is thrown :
 
                                        
                3.      Fault Message :
                                        
 
 
 
Cheers,
Lets Learn Oracle SOA Team