In my previous blog you learnt how to work with SaaS and PaaS based engagement models of Business Rules in Cloud Foundry environment. You also saw how you can model the rules and execute them via rest client. In this blog, I will show you how you can set up the sample custom application which internally calls the business rules deployed in cloud foundry.
What is this sample custom application?
The sample custom application that you will be downloading and setting up in your cloud foundry environment is Shopping Cart Application. This application lists down various categories of products, that customers can choose from. Once done, the customer is provided an option to proceed and order the list of chosen products. Before, he completes the order – a set of rules are executed that determine the discounts on the product(s) and shipment details.
Note: These business rules are the same as used in my previous blog
This sample package have two type of applications bundles in it (a) Web application which is actual SAPUI5 application based on shopping cart scenario and (b) Java application which is backend service that makes the actual call to the business rules services. From Web Application, when a rule is executed – it is actually routed via this backend service which in turn gets the authorisation token and calls the business rules via the public APIs
How to setup the application in Cloud Foundry?
Sample application together with all the steps to setup the application can be found in the README
- Download and extract the shopping cart application from GitHub
- To execute the business rules APIs you will need OAuth 2.0 access token, to get the tokens so you need to download the latest version of SAP XS security libraries from Service Marketplace and build the your application again using Maven commands as shown here:
- Download and extract Security Libraries from SAP Service Marketplace in libs folder
- Build the library using Maven commands
cd cf-shoppingcart/libs mvn clean install
——————————— build will progress ————————————
- Next, you have to configure NPM and build your application in Cloud Foundry as shown: https://github.com/SAP/cloud-businessrules-samples/blob/master/cf-apps/README.md#3-configure-npm
- Configure NPM
npm config set @sap:registry https://npm.sap.com
- Build the application with the latest security libraries
Note: app contains the backend router java application that routes the API call from the SAPUI5 application to the required UAA and web contains the actual shopping cart SAPUI5 application. Both applications need to be build
- As this is a sample application, you need to update manifest file to use your cloud foundry trial host and user credentials, create business rules service instance and push/deploy your application in cloud foundry via CF commands as shown:
- Login to your Cloud Foundry
cf api https://api.cf.eu10.hana.ondemand.com cf login
Note: API endpoint you can find from the cockpit as shown
- Modify the manifest file to change the host
- Create service instances
cf create-service business-rules lite java-ruleservice cf create-service xsuaa application java-rule-sample-uaa -c ./security/xs-security.json
You will see the service instance for the application and UAA created
- Deploy the application in cloud foundry
You will see the applications deployed in your space from the cockpit
- Once the application is successfully deployed, you need to assign application-specific roles to your user as done in previous blog and create & assign tenant specific routes
Note: You can either add the role into existing collections or create a new collection
- Go to Security à Trust Configuration à SAP ID Service and assign the role collection to your user, if not done already
(note: this step is needed only if you have created a new role collection)
- Map the application routes
cf map-route java-rule-sample-web cfapps.eu10.hana.ondemand.com -n [subdomain]-java-rule-sample-web
You will see the routes created in your tenant specific space
Now your application is all ready to be used
How to access the application and execute business rules?
You can use the following URL to access the shopping cart application deployed in your Cloud Foundry environment. The same link can also be found from the cockpit, as shown:
- Choose the products and add them to the cart
- Click on the shopping cart icon on top-right of the master page
- In the shopping cart page, click Proceed button
Note: It is when you click this button when the business rules are executed.
- You will see Order dialog with discounts calculated and shown under ‘You Save’ column. The shipping information is also determined by the rules.
You can use this as reference to access the business rules in your custom application. The backend Java application is a generic one, you can embed and use the same in your application as well.
While this is one way of calling the rules, there could be others as well. While I explore other options, I thought to share this one that would at-least help you to execute rules from your application. Try this out and let me know your thoughts and suggestions on this option.