Invoking REST webservice from JQuery using an AJAX call

Below code helps you to understand how we can Invoke a REST web service on click of a button using JQuery and AJAX.

Note: In Java you can Invoke REST web service using ApacheHTTPClient library.

HTML File:

<!DOCTYPE html>
<html>
<head>
<sscript type=”text/javascript”
src=”http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js”></script&gt;
<sscript type=”text/javascript”>
$(document).ready(function () {
$(“#mybutton”).click(function () {
alert(‘welcome’);

$.ajax( {
type : “GET”, url : “http://services.groupkt.com/country/get/iso2code/IN&#8221;, dataType : “json”, success : function (data) {
alert(“Reading Msg: ” + data.RestResponse.messages);
alert(“Reading Name: ” + data.RestResponse.result.name);
}
});

});

});
</script>
</head>
<body>
<button type=”button” id=”mybutton”>Invoke REST WS</button>
</body>
</html>

Sample REST Services (thanks for making them available to developers for testing purpose):

http://www.groupkt.com/post/c9b0ccb9/country-and-other-related-rest-webservices.htm
http://services.groupkt.com/country/get/iso2code/IN
http://services.groupkt.com/country/get/all
http://rest-service.guides.spring.io/greeting
Advertisements

Showing Parameters passed in the URL in a page – Oracle ADF

In ADF Application we can read the parameters passed in the URL and display them as an output text using a simple EL (Expression language).

#{param.paramName}

Sample Jspx page:

1

Page in browser:

2If your requirement is to show the URL passed in parameter data in the input text field and if there is no parameter data passed then show it as an input text field, then you can use below given EL which is having the ternary condition.

<af:inputText value=”#{param.dept eq null? bindings.DepartmentId.inputValue: param.dept}” />

Browser output when URL: 

http://127.0.0.1:7101/Application2-ViewController-context-root/faces/home.jspx

3.PNG

Browser output when URL: 

http://127.0.0.1:7101/Application2-ViewController-context-root/faces/home.jspx?dept=Marketing

4.PNG

 

This post is intended to show how the requirement can be fulfilled with the help of EL.

Invoke a WebService with the Inserted Rows data of Excel sheet in ADF-DI:

In ADF-DI you don’t have any direct option available to get the control over all the inserted rows to invoke WS (Web Service). So you must implement it with the help of programmatic approach.

First what you need to understand is, If you want to upload 100 records of data from Excel sheet to DB, first you must set a property [InsertBeforeRowActionID : ‘CreateInsert‘] which means before reading each row of data, a new empty row object is created and the data of that row is stored into that row object cache. The same thing happens for all the rows. Once all the rows of Excel sheet are read, the cached data has to be committed to DB. So set the property [CommitBatchActionID: Commit] which inserts the cached data to DB.

The above properties must be set to upload data to DB from Excel sheet. To achieve this your page definition file must contain the bindings [tree binding, Commit, CreateInsert bindings]

To get the Handle over the Excel sheet data, you must set below proprieties.
Property [InsertBeforeRowActionID : ‘eachRowInvokeAM‘] which is being used to Invoke a custom method where current row data can be read and stored to a List. This will repeat for all the rows.
Then set the property [SuccessActionID : afterAllRowsInvokeAM] for a ribbon command ‘Update‘. This triggers the custom method ‘afterAllRowsInvokeAM’ after successful Insert/Update to DB. In this custom method, you can work with the list which has the data of all rows inserted in the Update operation. Using this data you can Invoke a Web Service.

You must write your custom methods in AMImpl class. ADF12c supports only AMImpl class methods, where as ADF11g supports AMImpl & VOImpl class methods to invoke.

Eg: Let’s take an example, where we insert a record to Departments table. And after all the rows got inserted to DB from ADF-DI Excel sheet a webservice should be invoked with the inserted data as input. For this set the below properties.

InsertBeforeRowActionID : ‘CreateInsert’
InsertBeforeRowActionID : ‘eachRowInvokeAM’
InsertRowEnabled: True
CommitBatchActionID: Commit
RibbonCommand- Update -> SelectActionSet -> ActionOptions -> SuccessActionID : afterAllRowsInvokeAM

Custom methods given above are [‘eachRowInvokeAM’, ‘afterAllRowsInvokeAM’] are mehtods created in AMImpl class .
Another class also created ‘Dept’ which is a Pojo class with the column names as variables and having parameterized constructor.
In the AMImpl class, a list is created of type ‘Dept’ pojo class. And the parameterized constructor is being invoked to insert the department type object to the list. Two custom methods are created in it [‘eachRowInvokeAM’, ‘afterAllRowsInvokeAM’] and are exposed and then added to the page definition file as method bindings. During each row insert ‘eachRowInvokeAM’ will be invoked and that row data in inserted to the List. Once the commit operation is susccessful another method ‘afterAllRowsInvokeAM’ will be invoked where the list data is read and used as input to Invoke the webservice.

The page definition file should be:

ADF-DI_1.PNG

Excel properties should be:

ADF-DI_3

ADF-DI_2

 

AmImpl.java:


package model;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.net.HttpURLConnection;
import java.net.URL;

import java.util.ArrayList;
import java.util.List;

import model.common.AppModule;

import oracle.jbo.Row;
import oracle.jbo.server.ApplicationModuleImpl;
import oracle.jbo.server.ViewLinkImpl;
import oracle.jbo.server.ViewObjectImpl;

public class AppModuleImpl extends ApplicationModuleImpl implements AppModule {

/* Custom Code */
List dept = new ArrayList();
public void eachRowInvokeAM(){
System.out.println("Entered before Upload..");
System.out.println("row count:"+getDepartmentsView1().getRowCount());
Row r = getDepartmentsView1().getCurrentRow();
System.out.println(r.getAttribute("DepartmentId"));
System.out.println(r.getAttribute("DepartmentName"));
System.out.println(r.getAttribute("LocationId"));
System.out.println(r.getAttribute("ManagerId"));

int deptId = Integer.valueOf(r.getAttribute("DepartmentId").toString());
String deptName = r.getAttribute("DepartmentName").toString();
int locId = Integer.valueOf(r.getAttribute("LocationId").toString());
int mgrId = Integer.valueOf(r.getAttribute("ManagerId").toString());

//adding current row to list
dept.add(new Dept(deptId, deptName, locId, mgrId));
}

public void afterAllRowsInvokeAM() {
System.out.println("after Insert success..");
System.out.println("rows inserted:"+dept.size());
for(int i=0; i< dept.size(); i++){
System.out.println(dept.get(i).getDepartmentId());
System.out.println(dept.get(i).getDepartmentName());
System.out.println(dept.get(i).getLocationId());
System.out.println(dept.get(i).getManagerId());
}
System.out.println("Now Invoking WebService");
try {
http_client();  // Invoke Webserive method
} catch (Exception e) {
}
}

public static void http_client() throws Exception {
System.out.println("Invoke service using direct HTTP call with Basic Auth");
String payload =
"\n" +
" \n" +
" \n" +
" \n" +
" INR\n" +
" USD\n" +
" \n" +
" \n" +
"";

httpPost("http://www.webservicex.net/CurrencyConvertor.asmx" + "?invoke=", payload);
}
private static String httpPost(String destUrl, String postData) throws Exception {
URL url = new URL(destUrl);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
if (conn == null) {
return null;
}
conn.setRequestProperty("Content-Type", "text/xml; charset=UTF-8");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setFollowRedirects(true);
conn.setAllowUserInteraction(false);
conn.setRequestMethod("POST");

//byte[] authBytes = authStr.getBytes("UTF-8");
//String auth = com.sun.org.apache.xml.internal.security.utils.Base64.encode(authBytes);
// conn.setRequestProperty("Authorization", "Basic " + auth);

System.out.println("post data size:" + postData.length());

OutputStream out = conn.getOutputStream();
OutputStreamWriter writer = new OutputStreamWriter(out, "UTF-8");
writer.write(postData);
writer.close();
out.close();

System.out.println("connection status: " + conn.getResponseCode() +
"; connection response: " +
conn.getResponseMessage());

InputStream in = conn.getInputStream();
InputStreamReader iReader = new InputStreamReader(in);
BufferedReader bReader = new BufferedReader(iReader);

String line;
String response = "";
System.out.println("==================Service response: ================ ");
while ((line = bReader.readLine()) != null) {
System.out.println(line);
response += line;
}
iReader.close();
bReader.close();
in.close();
conn.disconnect();

return response;
}

Dept.java:

</pre>
package model; public class Dept { int DepartmentId; String DepartmentName; int LocationId; int ManagerId; public Dept(int DeptId, String DeptName, int LocId, int MgrId) { //super(); this.DepartmentId = DeptId; this.DepartmentName = DeptName; this.LocationId = LocId; this.ManagerId = MgrId; } public void setDepartmentId(int DepartmentId) { this.DepartmentId = DepartmentId; } public int getDepartmentId() { return DepartmentId; } public void setDepartmentName(String DepartmentName) { this.DepartmentName = DepartmentName; } public String getDepartmentName() { return DepartmentName; } public void setLocationId(int LocationId) { this.LocationId = LocationId; } public int getLocationId() { return LocationId; } public void setManagerId(int ManagerId) { this.ManagerId = ManagerId; } public int getManagerId() { return ManagerId; } } 

Your Excel in Design Time:

ADF-DI_4.PNG

Your Excel at run time:

ADF-DI_5


After Click on Ribbon button ‘Upload’, all the rows inserted to DB and also a web service is invoked at back end.

ADF-DI_6

Hope the post is Informative!

ADF Desktop Integration – showing table data in Excel workbook

This post explains how to to display table data in the Excel sheet using ADF Desktop Integration.

We’ll be discussing below items

  • Installing ADF- DI extension
  • Creating ADF Application
  • Working with Excel workbook

Installing ADF- DI extension:

Prerequisites: Before proceeding with ADF-DI extension installation first your system should have below soft wares installed, otherwise you end up getting an error when you try to install ADF-DI extension.

  • Microsoft .NET Framework 4
  • Microsoft Visual Studio 2010 Tools for Office Runtime
  • MS Office [I’ve used 2013 version]

Now go to JDeveloper -> ‘Tools’ -> Install ADF Desktop Integration.

Creating ADF Application:

  • Create Fusion Web Application (ADF). Application Name: ADFApp2, project names (keep default as model and viewcontroller)
  • Right click on Model project and create ‘Business Components from Tables’, crete DB connection and select ‘Departments’ table. Drag this table to right pane for updatableVO.
  • Right click on ViewController project and create JSPX page (dept.jspx)
  • Drag and drop the Deaprtments datacontrols from datacontrol section on to the dept.jspx page as table. (which will creates deptPagDef.xml file and along with tree binding in it)
  • Right click on viewcontroller project and create Microsoft Excel Workbook from client tier. (give Name: dept-DT)

6

7

8

Working with Excel workbook:

  • Double click on the excel file and it’ll prompt to select a page definition file from the available all page definition files in the project. Select view_deptPageDef file and click OK.

9

  • You can see a tree binding (DepartmentsView1) under Binidngs section of the Excel workbook.

10

  • Drag and drop the tree binding onto the cell and select ‘ADF Read-only Table’ -> OK.

11

  • It’ll open workbook properties window, there just click on OK. (we’ll configure it later)

12

  • Now you can see all the tree bindings in the Excel file.

13

  • in Jdev Right click on ViewController project -> project properties -> Java EE Application -> set ‘Java EE WebContext Root’ as ‘departments’ -> ok.

14

15

  • In excel work book click on worksheet properties and configure there to display data.

Worksheet properties -> Events […] -> Add -> StartUp -> ActionSet -> Actions […] -> Add button (Component Action) -> select Download -> OK -> OK -> OK.

16

17

18

19

20

21

  • In Jdev Right click on dept.jspx page and click on Run.
  • In excel workbook under oracle ADF menu tab, click on Run.

22

  • You can see the table data in the excel sheet as below. After review click on Stop and close the excel.

23

Hope You enjoyed the post!

In future will come up with more posts related to ADF-DI ‘update records in excel and upload to DB’ , ‘WebService Integration’ etc.

Disable few items in Select One Choice LOV in Oracle ADF

This post covers how to disable certain items in selectOneChoice LOV based on certain condition.

Existing code:

<af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" 
label="#{bindings.DepartmentId.label}"
required="#{bindings.DepartmentId.hints.mandatory}"
shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1">
<f:selectItems value="#{bindings.DepartmentId.items}" id="si1"/>
</af:selectOneChoice>

Replace with:

<af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" 
label="#{bindings.DepartmentId.label}"
required="#{bindings.DepartmentId.hints.mandatory}"
shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1">
<af:forEach items="#{bindings.DepartmentsView1.rangeSet}" var="list">
<af:selectItem label="#{list.DepartmentName}" id="si1" value="#{list.DepartmentId}"
disabled="#{list.DepartmentId eq 10 or list.DepartmentId eq 20 or list.DepartmentId eq 30}"/>
</af:forEach>
</af:selectOneChoice>

 

Reference:

https://mjabr.wordpress.com/2012/01/23/how-to-make-some-lov-items-non-selectable/

 

 

Using Partial Triggers in Oracle Applications Cloud, Oracle Fusion Applications

This post explains how can we refresh a component when another component value is changed. You must be already aware that this can be achieved easily in ADF code by (a) setting the partial Triggers property of target component to the ID of source component & (b) setting the autoSubmit property of source component to ‘true‘.

But in Oracle Applications Cloud, Oracle Fusion Applications we won’t be having access to the source code and we need to implement it from the browser itself using customization feature.

partial triggers property is available for customization, but we need to find the ID of the source component which is not displayed anywhere to us. So to get the ID of the source component click on F12 (browser Inspect Element) then navigate to the source component and click on it. You can find the ID of that component over there.

What ever discussed above, let’s see step by step.

  • Login to Oracle Applications Cloud

1

  • You’ll be taken to the home page.

2

  • Select the sandbox if not selected any from the ‘Manage Sandboxes’ link (you can create a new sandbox also and its mandatory to select one sandbox before we proceed doing the customization to the application)

3

  • After selecting sandbox, click on ‘Set as Active’

4

  • Now navigate to the page where you wanted to do customization (eg: like in Order Management -> Orders on Hold page). Here let’s take a requirement that on selecting some value in ‘Hold Name’ LOV, the ‘Release comments’ component should display some custom message.

5

  • Now click on Inspect element (F12) and click on source attribute to find the ID of it. Copy the ID  [if source and destination components present inside same parent component then the last part if ID will be sufficient].

6

  • Click on Customize Pages link which you can get from user drop down.

7

  • Go to the Structure tab  and click on required component to customize

8

Note: If component is present inside a popup then click on button which brings the popup and in the component structure scroll down little where you can see multiple popups. Click on each popup and edit and read the ‘popup fetch listener’ property to understand why that popup is being used and check it is the correct one that we need to edit. After you find required popup if the content inside that is not visible, then set the ‘Content Delivery’ property from ‘lazyUncached’ to ‘Immediate’, then the content inside popup will be available to edit.

  • Click on source component -> If any popup comes select Edit -> Click Edit icon or right click on component at source code and click on Edit -> Set the ‘autoSubmit’ property to ‘true’

9

  • Click on Destination component and click on Edit and set the ‘partialTriggers’ to the ID which we copied earlier. [don’t set the entire path of ID, if source and destination components present inside same parent component then the last part if ID will be sufficient]. For the Value property set the EL logic as [#{bindings.HoldNameHeader.inputValue eq ‘CUSTOM HOLD’? ‘This is a custom Hold.’: bindings.HoldReleaseComments.inputValue] which means when ever ‘CUSTOM HOLD’ is selected in the LOV then below target component shows the message as ‘This is a custom Hold.’, and whenever other value is selected it won’t show any message and user can enter text over there. 

10

  • Now close the customization and you are ready to test. Select ‘CUSTOM HOLD’ from Hold Name LOV, then Release Comments shows the message as ‘This is a custom Hold‘. If you select other value then input text will be empty and editable. Now exit from the sandbox.

11

Hope you enjoyed the post 🙂