6- Oracle Mediator in SOA 12c (Routing and Transformation)



In this post we will use the routing and transformation functionality of Oracle Mediator in SOA 12c.

Transformation

Suppose we have schema 1 (left side) but our application accepts the schema 2 (right side), so in this case we would be using Transformation functionality of mediator to transform one schema to another as shown below




Routing

Mediator can also route the incoming data to external service(refrences) or another components like a mediator, a BPEL process, a human task etc. For example, we received the above schema 1, then we transformed it to the schema 2 and then we finally written the transformed data to a file. This is simple example in which we will be doing routing of the data to a File adapter. For this you have to follow the below steps:


  • Create a new SOA Project. (Click here to see) Here we have created one project using empty composite template.

  • Right click Schemas folder and Select New --> From Gallery 

  • In XML category, choose XML Schema and click OK button to create XML Schema for schema 1

  • Change the file name and click OK

  • Enter the following source for Student.xsd



<?xml version="1.0" encoding="windows-1252" ?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.example.org"
            targetNamespace="http://www.example.org" elementFormDefault="qualified">
  <xsd:element name="Student">
    <xsd:annotation>
      <xsd:documentation>A sample element</xsd:documentation>
    </xsd:annotation>
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="First_Name" type="xsd:string"/>
        <xsd:element name="Last_Name" type="xsd:string"/>
        <xsd:element name="Mobile_Number" type="xsd:string"/>
        <xsd:element name="Email" type="xsd:string"/>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>


  • Also, create one more schema file Student1.xsd for schema 2. Also, make sure you change the targetNamespace of this schema to avoid naming conflicts

  • Application Navigator will look like :


  • Open the designer of composite.xml. It will displayed as same name as that of our project, here RoutingAndTransformation in our case. When you choose this file, you can also see it's location in status bar.


Part 1- Routing using Mediator
  • Drag and drop the Mediator component from the component window. Create Mediator window will appear.
  • Choose the Template as "One-way Interface"
  • Click the browse icon and choose Student element of Student.xsd as the Input element. 

  • You will see mediator as below in composite.xml. Since you have already chosen the student element as an input element and "Expose as a SOAP service" checkbox was checked, so you will have Mediator1_ep as your endpoint in the exposed services area

  • Drag the File Adpater from components palette to  external references area and then you will see the below wizard which will guide you through the process of creating the File Adapter. Enter the name of File Adapter whatever you want. We will keep the default name as fileReference and click Next button.
  • Since we are not exposing the file adapter as a service, we will choose "Define from operation and schema (specified later)" and then click Next.
  • Enter the JNDI name for File adapter and click Next. Normally we use the convention as eis/<Name>, so here we are using eis/FileAdapter. You can enter any name here and then click Next.
  • Select operation type as "Write File" and enter any name for the operation and click Next.
  • Enter the directory where you want to store the output files or use the Browse button to choose the output directory. In File naming convention textbox enter the output file name you want to save as. For example, if you want that all output files will be stored as Student_<Sequence_Number>.xml (like Student_1.xml, Student_2.xml etc) then you can use %SEQ%. You can also choose other conventions like appending time, date, timestamp etc. Click Next button to proceed.
  • In the next window, you will choose the Message Schema, i.e. structure of the message to be written in the file. Here we will choose the Student element of Student1.xsd (Remember this Student element has a different structure as compared to Student element of Student.xsd, as it has Full_Name as compared to First_Name and Last_Name in Student.xsd) and then click Next Button and then click Finish button to finish creating the File Adapter.

  • You will then see the File Adapter icon (fileReference) placed in the Exposed Services area. The mediator will have no routing rule as of now. To create the routing rule i.e. to enable mediator to route the incoming data (Student element of Student.xsd) to File Adapter (which accepts Student element of Student1.xsd) connect the mediator and File Adapter using a wire as shown below. It will then create a routing rule in the mediator.


Part 2- Transformation using Mediator
  • Since the structure of incoming data is different from that of File Adapter, we will need to transform the incoming data to the format that File Adapter accepts. So here comes the role of mediator to transform the data. In Mediator1.mplan file create a new mapper file to perform the transformation.


  • Drag "concat" icon from String Functions section of Component Palette and drop to the Full_Name in right side. Concat icon will be then visible in middle section. Double click the little concat icon and then edit Concat Function window will appear. Drag the First_Name and Last_Name to make the concat expression and click OK. Select ns0:Mobile_Number and draw a line from it to ns0:Mobile_Number in right side. Similarly do for ns0:Email. So, basically we are doing transformation from source data to target data.
  • You can also see the transformation in the source:
<xsl:template match="/">
      <ns0:Student>
         <ns0:Full_Name>
            <xsl:value-of select="concat (/ns0:Student/ns0:First_Name, /ns0:Student/ns0:Last_Name )"/>
         </ns0:Full_Name>
         <ns0:Mobile_Number>
            <xsl:value-of select="/ns0:Student/ns0:Mobile_Number"/>
         </ns0:Mobile_Number>
         <ns0:Email>
            <xsl:value-of select="/ns0:Student/ns0:Email"/>
         </ns0:Email>
      </ns0:Student>
   </xsl:template>

Comments