Skip to Content
Technical Articles

HANA XSOData Consumption in XCode Project using SAP Frameworks

Hello Developers,

 

This is my another blog based on XCode and frameworks provided by SAP Cloud Platform SDK for iOS.

SAP has already provided SAP Cloud IOS SDK (https://developer.apple.com/sap/) to connect with SAP Cloud and XCode.

Using this tool, we can create a XCode application without writing codes.

But what if, we need to develop an application as per our busines needs, using same SAP oData frameworks for IOS.

Here is the way, Where I have performed CURD functionality using SAP Frameworks and developed an XCode application using Swift Language.

Initially, used below tutorial link and developed a sample XCode application using SAP Cloud Platform SDK for IOS to display deliveries.

https://developers.sap.com/mission.sdk-ios-one-code-line.html?url_id=text-us-recommendation

Below is the directory details, where SAP frameworks copied on creation of delivery application.

Copied the marked frameworks into newly custom application and used the functionalities to display, create, update and delete records using XSOdata servicee created in HANA Cloud.

or

open SAP Cloud Platform SDK for IOS Assistant.

select “Export Frameworks..” menu item as below screen and download the all frameworks into project directory.

 

Setup of XSOdata Services in HANA Cloud:

  1. Created below tables in HANA Cloud server

a.Employee

                B.Country

               C.City

      2. Created below XSOdata service based on above all three tables

     3.Uploaded the City and Country data based on maintained fields into tables.

 

The XCode Application:

Create a single view XCode application:

Go to project target as below screen and add below frameworks from project developed using SAP Cloud Platform SDK for IOS.

Copy all below files into project directory and then use into current project.

a.  SAPOData.cxframework
b.  SAPCommon.cxframework
c.  SAPFoundation.cxframework

I have used the same approach from my previous blog and added few more libraries for learning purpose 😉

The SAP OData Framework Service logic:

Created a class SAPService.swift, which is containing all methods to fetch, create, update and delete employee information.

Initially, declare the provider object using service URL, from where OData and respective entity metadata will be fetching,

Used below code to create provider instance

let oDataProvider = OnlineODataProvider(serviceName: serviceName, serviceRoot: mainUrl)

Here, service name would be “ENTITY NAME” and MainUrl would be XSOdata service URL.

Employee service has been created in HANA Cloud server. A valid credential requires to access this service and entities. Below is the code, where entered User id and password would be used to fetch entity information.

oDataProvider.login(username: gUserid, password: gPassword)
oDataProvider.serviceOptions.checkVersion = false

Maintaining another line of code to avoid OData error if version is below 2.0

Below is the code to fetch OData information and loading Metadata information of service.

        if oData.hasMetadata == false{
            do {
                try oData.loadMetadata()
            } catch {
                complition(false)
                return oData
            }
        }
        return oData

The above provider object would be use for all CURD functionalities.

 

The CURD Functionalities:

Code for Fetch Data:

a. Set the Entity into OData service object

let employeeEntitySet = oData.entitySet(withName: EMPLOYEE_ENTITY)

b.  Get the entity types

let employeeEntityType = employeeEntitySet.entityType

c.  Declare variables based on entityset fields name

let id = employeeEntityType.keyProperty(name: FLD_ID)
let fname = employeeEntityType.property(withName: FLD_FNAME)

d.  Setup a query based on fields, entityset and condition

let query = DataQuery().selectAll().from(employeeEntitySet)

e.  Execute the query using OData service object to fetch records

let result = try oData.executeQuery(query).entityList()

f.   Read result using Array loop and declared variables for entity fields.

for item in result.toArray() {
                let emp = employee(id: id.intValue(from: item),
                                   Name: "\(fname.stringValue(from: item)) \(mname.stringValue(from: item)) \(lname.stringValue(from: item))",

Code for Create New Record:

a.   Set the Entity into OData service object

let employeeEntitySet = oData.entitySet(withName: EMPLOYEE_ENTITY)

b.   Get the entity types

let employeeEntityType = employeeEntitySet.entityType

c.   Declare variables based on entityset fields name

let id = employeeEntityType.property(withName:  FLD_ID)
let fname = employeeEntityType.property(withName:  FLD_FNAME)

d.   Create model structure for selected entity based on entity type:

let newEmployee = EntityValue.ofType(employeeEntityType)
id.setIntValue(in: newEmployee, to: employeeId)
fname.setStringValue(in: newEmployee, to: fMame)

e.   Call CREATEENTITY method of OData service by passing entity model to create new record.

do {
            try oData.createEntity(newEmployee)
            complition(true)
        } catch {
            complition(false)
            return
}

Code for Update Existing Record:

a.     Set the Entity into OData service object

let employeeEntitySet = oData.entitySet(withName: EMPLOYEE_ENTITY)

b.     Get the entity types

let employeeEntityType = employeeEntitySet.entityType

c.     Declare variables based on entityset fields name

let id = employeeEntityType.property(withName:  FLD_ID)
let fname = employeeEntityType.property(withName:  FLD_FNAME)

d.     Prepare a query object based on required condition as below

let query = DataQuery().top(1).from(employeeEntitySet).filter(id.equal(employeeId))

e.     Execute query to get required entity model for update.

let getEmployee = try oData.executeQuery(query).requiredEntity()

f.      Update required entity fields values.

fname.setStringValue(in: getEmployee, to: fMame)
lname.setStringValue(in: getEmployee, to: lName)
mname.setStringValue(in: getEmployee, to: mName)

g.     Call UPDATEENTITY method of OData service to update selected record.

do {
     try oData.updateEntity(getEmployee)        
     complition(true)
    } catch {
     complition(false)
 }

Code for Delete an entry:

a.   Set the Entity into OData service object

let employeeEntitySet = oData.entitySet(withName: EMPLOYEE_ENTITY)

b.   Get the entity types

let employeeEntityType = employeeEntitySet.entityType

c.    Declare variables based on entityset fields name

let id = employeeEntityType.property(withName:  FLD_ID)

d.    Prepare a query object based on required condition as below

let query = DataQuery().top(1).from(employeeEntitySet).filter(id.equal(empId))

e.    Execute query to get required entity model for update.

let getEmployee = try oData.executeQuery(query).requiredEntity()

f.     Call DELETEENTITY method to delete selected record.

do {
     try oData.deleteEntity(getEmployee)
     complition(true)
   } catch {
      complition(false)
      return
 }

The Application:

Below screen will be displaying after login, which will be containing name and email id information of all employees’ details from EMPLOYEE table.

Below screen will be open to maintain new employee details, On selection of “New Employee” option.

The Country flag and country ID value would be initialized based on current device location.

Below screen will be open to change the country information.

By default Map will be always pinned to top-most country name from displaying table,

or Map will be change on selection of any specific country from table as displaying in above screen.Used MapKit and Corelocation libraries to display Map and location of device.

The country will be considered for further process on right to left swipe as below screen:

Based on Selected country, flag will be updated into create employee page as below:

Same way, select city search option to get the all cities of selected country.

Below table will be option, which will be containing all cities information.

This screen also has the same functionality as country selection, to select city for further employee creation process:

Final details before creation of employee:

On “Save”, a new record will be creating into employee entity and created employee information will be updated into main employee details view.

Employee details will be display, On selection of display icon from employee records cell.Below screen will be displaying, which will be containing the selected employee information.

All text will not be enabled to edit information.

To update any of existing employee record, Swipe the records from Left-to-Right as below screen:

On “update” selection, selected employee details will be open into new view to update.

Updated information will be update into employee entity and updated information will be displaying into detail table.

Swipe any record from right-to-left from the table to Delete employee information using OData service as below screen:

 

Application Directory information:

 

Below are the reference links, which I have followed to developed this application.

https://help.sap.com/viewer/fc1a59c210d848babfb3f758a6f55cb1/2.2/en-US/6662fbcdac374f4d89adce8c7e9cd269.html

https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/Latest/en-US/Documents/Frameworks/SAPOData/index.html

https://help.sap.com/doc/978e4f6c968c4cc5a30f9d324aa4b1d7/2.2/en-US/Documents/Frameworks/SAPOData/Classes/QueryFilter.html

API link for country flag:

https://www.countryflags.io/

API link for Country area calculation to display in map:

https://restcountries.eu/#api-endpoints-code

 

XCode available in Github.

 

Happy Learning 🙂

Praveer.

 

2 Comments
You must be Logged on to comment or reply to a post.