Usecase:
In this usecase, we have a web service that returns employee details(like name, designation, salary etc.) for an employee with given ID. In the mobile app, we will be calculating tax on
the returned salary and displaying it to the user in a pop-up.
To do this, we will be parsing the returned XML to fetch the
current salary and add a tax of 5% on it.
Here, we are assuming that tax is 5% of the current salary.
Pre-requisites:
Create a RESTful service as shown. We will be using this service at the mobile end to create the DC.
To create the service, first create a java class called Employee.java as shown below:
package project1;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Employee {
String name;
int id;
String desig;
int salary;
public void setDesig(String desig) {
this.desig = desig;
}
public String getDesig() {
return desig;
}
public void setSalary(int salary) {
this.salary = salary;
}
public int getSalary() {
return salary;
}
public void setDept(String dept) {
this.dept = dept;
}
public String getDept() {
return dept;
}
String dept;
public Employee() {
super();
}
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
}
package project1;
import java.net.URI;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlElement;
@XmlRootElement
public class EmployeeList {
private Listlist = new ArrayList ();
public EmployeeList() {
}
public EmployeeList(Listlist) {
super();
this.list = list;
}
@XmlElement
public ListgetList() {
return list;
}
}
package project1;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
@Path("project1")
public class EmployeeService {
private static ListemployeeList = new CopyOnWriteArrayList ();
public EmployeeService() {
addEmp();
}
@GET
@Produces("application/xml")
public EmployeeList getEmpList() {
return new EmployeeList(employeeList);
}
@PUT
@Consumes("application/xml")
@Produces("application/xml")
public EmployeeList addEmployee(Employee emp) {
employeeList.add(emp);
return new EmployeeList(employeeList);
}
@DELETE
@Produces("application/xml")
public EmployeeList deleteEmployee(@QueryParam("name") String name) {
employeeList.remove(getEmp(name));
return new EmployeeList(employeeList);
}
@POST
@Produces("application/xml")
public EmployeeList updateEmployee(@QueryParam("id") int id,@QueryParam("name") String name) {
Employee emp1 = new Employee();
emp1 = getEmpById(id);
if(emp1 != null)
emp1.setName(name);
return new EmployeeList(employeeList);
}
public Employee getEmp(String name) {
Iterator i = employeeList.iterator();
while (i.hasNext()) {
Employee e = (Employee)i.next();
if (e.getName().equalsIgnoreCase(name))
return e;
}
return null;
}
@GET
@Path("byID")
public Employee getEmpById(@QueryParam("id") int id) {
Iterator i = employeeList.iterator();
while (i.hasNext()) {
Employee e = (Employee)i.next();
if (e.getId() == id)
return e;
}
return null;
}
public void addEmp() {
if (employeeList.isEmpty()) {
Employee emp1 = new Employee();
emp1.setId(1);
emp1.setName("Arnold");
emp1.setDept("IT");
emp1.setDesig("Director");
emp1.setSalary(80000);
employeeList.add(emp1);
Employee emp2 = new Employee();
emp2.setId(2);
emp2.setName("Myra");
emp2.setDept("HR");
emp2.setDesig("Officer");
emp2.setSalary(70000);
employeeList.add(emp2);
}
}
}
Run the service class and copy the target URL.
Steps:
Create a new ADF Mobile application.
Name the application ‘XmlParsingSample’
and finish the wizard.
Create a URL Connection in the ApplicationController
project.
Name the connection ‘conn’
and suppy the target URL copied earlier. Remember to use IP instead of
localhost. Test the connection and click OK to close the wizard.
Since the service returns XML data, also create a Data
control. This will be useful for designing the UI page. To create DC, invoke
URL Service data control wizard in the ApplicationController project.
Test the URL connection in the last step and finish the
wizard.
Now, let us create the AMX pages. Open adfmf-feature.xml
present in ViewController project. Add a new feature ‘EmpFeature’ by clicking the plus icon as shown.
Go to the content tab and add an AMX page.
Call it EmpPage.amx. We
don’t need the primary and secondary actions. So, uncheck them.
Drag and drop ‘id’
parameter from the DC palette onto the Panel Page of the Structure pane for
this page.
Drag and drop a button from the component palette onto the
page. Change the text of the button to ‘Get salary’.
As we want to invoke the service on click of this button,
let us associate an ActionListener with this. Focus on the button and from the
PI, select the arrow button for Action
Listener propery and click Edit
to bring up the below dialog. Click New to create a new managed bean.
Name the manged bean as
myBean and class as myClass.
Next, create a new method called calculateTax.
Go to myClass.java. In calculateTax method, write the code provided in the below table. Use the imports:
import oracle.adfmf.amx.event.ActionEvent;
import oracle.adfmf.dc.ws.rest.RestServiceAdapter;
import oracle.adfmf.framework.api.AdfmfContainerUtilities;
import oracle.adfmf.framework.api.Model;
import org.xmlpull.v1.XmlPullParser;
import org.kxml2.io.KXmlParser;
import java.io.Reader;
import java.io.StringReader;
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. | public // Add event code here... RestServiceAdapter restServiceAdapter restServiceAdapter.clearRequestProperties(); restServiceAdapter.setConnectionName("conn"); restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET); restServiceAdapter.addRequestProperty("Content-Type", restServiceAdapter.addRequestProperty("Accept", restServiceAdapter.setRetryLimit(0); Object emppIDObj = String empIDString = restServiceAdapter.setRequestURI("/byID?id="+empIDString); String response = ""; try { response = System.out.println("Response String name = ""; int sal = 0; int salAfterTax = 0; KXmlParser parser = new Reader stream = new parser.setInput(stream); parser.nextTag(); parser.require(XmlPullParser.START_TAG, while (parser.nextTag() != parser.require(XmlPullParser.START_TAG, null, null); //go to start tag String tagName = String tagValue = if name = tagValue; } if System.out.println("Salary:" + tagValue); sal = salAfterTax = (int)(sal - } parser.require(XmlPullParser.END_TAG, null, tagName); //go to end tag //parse through all the tags } parser.require(XmlPullParser.END_TAG, null, "employee"); parser.next(); parser.require(XmlPullParser.END_DOCUMENT, null, null); AdfmfContainerUtilities.invokeContainerJavaScriptFunction("EmpFeature", new " is " + sal + ". After tax salAfterTax, "", "Note", "OK!" }); } catch (Exception e) { System.out.println("Exception is:" + e); } } } |
Lines 3 to 15 set various properties required to invoke the
web service. In lines 10 and 11, we are getting the value input by the user and
converting that to String inorder to pass the parameter to the service. In line 20,
we are creating a kXml parser instance. kXML is one of the core ADF Mobile libraries
that provides API that you can use to parse XML. As the parser needs a Reader
object, we are converting our services’ response into Reader in line 21 and
passing it in line 22.
The response returned by our service is of the form
To parse this response, we are setting the parser to the first tag
‘employee’ in line 24. In lines 25 through 42, we are going through the XML
elements. The comments are in-line for these lines. Finally, lines 43 through 47
are for displaying the pop-up. To display the pop-up, we are utilizing AdfmfContainerUtilities.invokeContainerJavaScriptFunction. For more
information, see link.
As mentioned in the article, we make use of the javascript
function which comes from phonegap-1.0.0.js. Hence, we need to include an entry
to this javascript file in our AMX page. Put the below entry in EmpPage.amx
above the panelPage.
<script type =
"text/javascript" charset="utf-8"
src="../../../www/js/phonegap-1.0.0.js"></script>
That’s it. Deploy the app to a device or an emulator. Some
snippets from the final app.
Enjoy coding!