During my recent engagements, working with SOA Suite 12c, I have realized the strength of the XQuery library feature (introduced in OSB 12c) to simplify SOA 12c implementations by avoiding copying code in various places, and instead centralizing it. In this article we will discuss following:
- Overview of XQuery library
- Effective Use Cases for XQuery library
- Working Example: "Storing customer specific business logic"
- Without using XQuery lib
- Using XQuery lib - detailed steps involved
Overview of XQuery library
Creating an XQuery library was a new feature introduced in SOA Suite 12c. This feature provides functionality to reuse XQuery functions and avoid repeatedly coding the same logic, as logic can now be centralized at one place. This is a very handy feature and can be used to strengthen and simplify solutions implemented on the SOA Suite platform.
Often the XQuery lib feature may be somewhat underestimated, and therefore in this article, I wanted to focus on some of the use cases where I have found these XQuery lib feature's to be very effective.
Effective Use Cases for XQuery library
- Storing customer specific business logic that is required repeatedly within the solution. For example, we can store business validation as a set of regular expressions and reuse them across all services.
- Creating a common XML wrapper For example, providing a consistent and common approach for generating a particular header or type of envelope that is required when calling out to a third party system or to interact with services or other integrations. Instead of repeatedly creating the envelope or header in every invocation to the third party interface, we can utilize an XQuery library function to generate an envelope or header and simply call out during the services or operation level transformation in XQuery.
- Centralizing mappings in the XQuery library for reuse at a particular service level in the event of having the same Enterprise Business Object (EBO) structures used in many components such as services or operations.
- And finally, very common use cases writing any time and date conversion function or any other calculative function.
An important note: Unlike XQuery transformation, XQuery library functions can’t be tested standalone as it doesn’t define a function signature and declare variables. This does mean that logic placed in the XQuery library, if required testing, then you need to copy the content to an XQuery transformation and test against it.
Working Example: "Storing customer specific business logic"
Now we will walk through an example using XQuery library with the first use case described above "Storing customer specific business logic". In my recent engagement we had a requirement to place the business validations. These business validations are in the form of regular expressions, but are heavily used in all services and operations for data integrity.
So we had two elements typeType and typeValue, typeType can have a value such as Phone, Email, ABN or Organisation. And corresponding to each type there are different sets of validations needed to perform on typeValue. I have illustrated both approaches, that is, with and without XQuery lib, to compare how effective it could be to use XQuery lib.
Pseudo code without using XQuery lib
Check for the element type, if typeType equals to Phone then compare phone value that is typeValue to the regular expression conforming its valid phone number
else if typeType equals to email address then compare the email value that is typeValue to the corresponding regular expression and so on.
You can see the implementation below.
Pseudo code when using XQuery lib
All the logic can be placed in the XQuery and XQuery lib function and we simply need to call XQuery in our service/operation or any stage with the assign activity and just pass both elements i.e. typeType and typeValue to XQuery which will in turn use XQuery library functions for business validation by placing the regular expression in it. The result of the XQuery will be boolean, that is true if validation passes, or false otherwise.
Not raising the error when validation doesn't pass has been intentionally kept separately out of the XQuery to maintain modularity of the XQuery.
Here are the steps to accomplish this:
Select following from the gallery to create xquery lib function.
Write the XQuery lib name, target namespace, function name, input and output parameters as marked in the screenshot below. In the subsequent screenshot, I have shown the actual values used while creating the XQuery lib function for Email Validator.
If you click OK on above step, it will generate the following file then place the regular expression validation for email, as shown below:
Similarly, we can create other business validations for phone, ABN and Organisation.
Now we will create the XQuery with name cmn.util.businessValidator.xqy with two inputs for typeType and typeValue and output as Boolean. This is the XQuery where we import the library for using XQuery function such as EmailValidator, etc which we created in Step 3. The first screenshot shows how to select XQuery File and second where to import the lib.
Step 5 :
Browse the XQuery library module
Step 6 :
You can see the lib has been imported as module and namespace has been assigned. Now you can see the function on the right hand side pane and this can be used as a normal function by calling the function name, along with namespace.
Now jump to the stage of the service where you would like to do business validation. And now its as simple as calling XQuery with typeType and typeValue, as shown below. Also, if we compare the implementation without XQuery, you can see the difference. Using XQuery is so neat and handy and easily maintainable as core logic lies in one place and not staggered throughout different locations in the services.
Also, please note we can place the XQuery library in the MDS, which will help to use the XQuery functions across the project and further extend its benefits.
In this article we have seen how by using XQuery library we can avoid rewriting code at various places in the service, which is hard to maintain. Instead, we can place code in the XQuery library and extend as an XQuery function, where ever required.
I hope you have found this article useful and it will guide you to use some of these additional use cases, in your own engagements.