Tuesday, December 22, 2015

Oracle MAF: Programmatically call REST service ( REST with JSON output)

This example is using RESTServicesAdapter. Using that programmatically you can call a REST service and you can manipulate response as per need.

My REST service details are as follows:

URL: localhost:1111/HrServiceFirst-ViewController-context-root/resources/view/emp

{"lstEmp":[{"address":"ss","empId":22222,"empName":"sss"},{"address":"SKING","empId":100,"empName":"Steven"},{"address":"NKOCHHAR","empId":101,"empName":"Neena"},{"address":"LDEHAAN","empId":102,"empName":"Lex"},{"address":"AHUNOLD","empId":103,"empName":"Alexander"}]}

Step 1: Create new Mobile Application Framework (MAF) Application:



Step 2 :Right click on Connenction create new REST Connection

Give the name of rest Connection and URL End point click ok as follow:

Step 3: Create Employee Class and add following variable and generate accessors

    public Integer empId;
    public String empName;
    public String address;


Step 4: create  CallService class and add follwing code in it

package mobile;

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

import oracle.adfmf.dc.ws.rest.RestServiceAdapter;
import oracle.adfmf.framework.api.Model;
import oracle.adfmf.json.JSONArray;
import oracle.adfmf.json.JSONException;
import oracle.adfmf.json.JSONObject;

public class CallService {
    public CallService() {
        super();
    }
    List<Employee> lstEmp=new ArrayList<Employee>();
    private Employee emp;
    public void setLstEmp(List<Employee> lstEmp) {
        this.lstEmp = lstEmp;
    }

    public List<Employee> getLstEmp() {
        return lstEmp;
    }
    public void getEmployees() {
           RestServiceAdapter restServiceAdapter = Model.createRestServiceAdapter(); 
           restServiceAdapter.clearRequestProperties();  
           restServiceAdapter.setConnectionName("restConn");  
           restServiceAdapter.setRequestType(RestServiceAdapter.REQUEST_TYPE_GET);    
           restServiceAdapter.setRetryLimit(0);
           restServiceAdapter.setRequestURI("/emp");
           try {
               String response = restServiceAdapter.send(""); 
               JSONObject res = null;  
               res = new JSONObject(response);     
               JSONArray lstOfEmp=res.getJSONArray("lstEmp");
               
               for(int i=0;i<lstOfEmp.length();i++){
                  JSONObject obj=lstOfEmp.getJSONObject(i);
                  System.out.println("Emp name "+obj.getString("empName"));
                   Employee e=new Employee();
                   e.setEmpName(obj.getString("empName"));
                   lstEmp.add(e);
               }
           } catch (JSONException jsone) {
               // TODO: Add catch code
               jsone.printStackTrace();
           } catch (Exception e) {
               // TODO: Add catch code
               e.printStackTrace();
           }
       }
}

Step 5:create Data Control of CallService Class;


 Step 6: create feature in maf-feature.xml with type MAF Task Flow and create Task Flow.
create ViewActivity call named RestServiceCall and Create .amx Page

Then select getEmployees() method from data control drop in task flow:


           

In PageDef of method View add accessor iterator in executables

Step 6: drag and drop DC in Page with empName view Attribute in List View


Step 7: Deploy project on emulator

Output:


Monday, December 21, 2015

ADF : Exposing BC Component using REST Service

In this blog, we are Exposing BC Component using Rest Service.

For example write service to get 5 Employees name from Employees table.
Use following steps:

Step 1: create ADF Application 
Step 2: Create EO and VO for Employee

Step 3:Create java class name Employee and 3 variable as :
    private Integer empId;
    private String empName;
    private String address;

and generate accessors for this

Step 4: Create java class for EmpWrapper ,In this class create  private List<Emplyee> lstEmp; variable to store Objects of Employee.

write @XmlRootElement above EmpWapper Class

Step 5: create java class EmpService
write  public EmpWapper getEmp() {}
In this method write bc4j  press Ctrl+Enter you will get follwing suggestion

Press Enter, code is generated to access AM instances

        ADFContext oldContext = ADFContext.initADFContext(null, null, null, null);
        try {
            String amDef = "test.TestModule"; 
            String config = "TestModuleLocal";
            ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
            ViewObject vo = am.findViewObject("TestView"); // VO to Access
            // Work with your appmodule and view object here
            Configuration.releaseRootApplicationModule(am, true);
        } finally {
            ADFContext.resetADFContext(oldContext);
        }

Add your logic to get 5 Employes name:

Copy and Paste following method code in Class


public EmpWapper getEmp() {
        EmpWapper objEmp=new EmpWapper();
       
        ADFContext oldContext = ADFContext.initADFContext(null, null, null, null);
        try {
            String amDef = "model.AppModule";
            String config = "AppModuleLocal";
            ApplicationModule am = Configuration.createRootApplicationModule(amDef, config);
            ViewObject vo = am.findViewObject("EmployeesView");
            int i=0;
            List<Emplyee> lstEmp=new ArrayList<Emplyee>();
            while(vo.hasNext()){
                
              Row row= vo.next();
               
              Emplyee emp=new Emplyee();
              emp.setAddress(row.getAttribute("Email").toString());
              emp.setEmpId(new Integer(row.getAttribute("EmployeeId").toString()));
              emp.setEmpName(row.getAttribute("FirstName").toString()); 
              lstEmp.add(emp);
              
               
             i++;
             if(i==5)
               break;
            }
            objEmp.setLstEmp(lstEmp);   
            // Work with your appmodule and view object here
            Configuration.releaseRootApplicationModule(am, true);
           
        } finally {
            ADFContext.resetADFContext(oldContext);
        }
        return objEmp;

    }

Step 6: right click on EmpService class and click on Create Restful Service

you will window like as follws:
Click on Produce and select application/xml media Type

Step 7: No win Configure HTTP method set

Type = Get
Produce = application/json
path =emp

and last finish.

then WebService is created . EmpService class looks like as follws:


Step 8:Now Deploye project and Test service

Output:

Note : suppose you will select in Step 7 :
Produce = application/xml

then output will be like this;

Hope this will help you