Connector for OData – Offer data – Functions
The OData specification also defines function imports, which can be used to run functions or methods via an OData service. With the Intrexx OData Provider, function imports can be used to run server-side Groovy scripts. They are called via an OData request and can receive input parameters and send back return values to the requester.
Here you can find out how to define functions and write the corresponding Groovy script.
To track the contents, a service should be created and enabled. Find out how to create your own service here. The following description refers to the example used there. In principle, functions can of course be defined as required and, therefore, extend the standard of an OData service offering accordingly.
General properties
You can access the "Functions" dialog by double-clicking on the active OData service – here in our example it has the name "Calendar1". This opens the service properties. "Service is active" should be set here.
Data groups
In the example, the "Calendar1" application and the data group it contains are included under "Data groups". By clicking on "Edit data group", the set name ("IX_CAL_CALENDAR") can be determined and copied here, as this will be used later in our sample function. Close the dialog by clicking "Cancel".
Then select the "Functions" entry in the left pane.
Functions
Click on "Add function" to create a new function.
Function properties
Name
Give the function a unique name here which cannot contain special characters or spaces. In our example, we used the name "getIX_CAL_CALENDAR".
HTTP method
Then select the HTTP method which will be used to call the function. In our example, we used the "GET" method.
Entity set name (optional)
Defines the entity set on which the function operates. The set name that was previously determined in the data group properties is entered here.
Return type
Defines the type of return value of the function. This can be either an OData type (such as Edm.String, Edm.Int32, Edm.Double, etc.) or an Entity type. More information about the topic "Entity type as Return type" can be found here.
In the example, the set name "IX_CAL_CALENDAR" is also used here.
Is collection
Defines whether the return value contains one or multiple data records/values.
Parameters
List of input parameters of a function. A new parameter can be added by clicking on "Add parameter" and an existing, highlighted parameter can be edited by clicking on "Edit parameter".
Edit parameter
A unique name and a data type must be defined for each parameter. The name must not contain spaces or special characters.
The type defines the parameter data type – either an OData data type (e.g. Edm.String, Edm.Int32, Edm.Double...) or an entity type.
The mode is purely informative and indicates whether the parameter is used for input or output or both.
Click on "Finish".
Groovy script
The actual function logic is implemented with a Groovy script. The Groovy editor can be opened via the "Edit Groovy script" link at the bottom of the dialog. All standard Intrexx context objects and classes are available in Groovy. In addition to this, a number of OData-specific context objects are provided:
-
g_guidODataService (Type String)
Contains the GUID of the OData service.
-
g_oFunctionParameters (Type Map)
Contains the function parameters and their values.
-
g_oProducerContext (Type IProducerCommandContext)
Context object of the OData Producer. Provides access to the OData service configuration and the processing context.
-
g_oFunctionContext (Type CallFunctionCommandContext)
Context object of the OData function. Provides access to the function properties, the input parameters and values, and optional OData query string variables.
Access to function parameters in Groovy
Input parameters in a function can be accessed in Groovy as follows:
def l_param = g_oFunctionParameters['BusinessPartnerName']
Definition of the return values of a function
The Groovy return value of a function must correspond to the data type defined in the function properties.
Example 1: Simple return value of the type Edm.String
The following Groovy script returns the input value of type Edm.String as result:
def l_param = g_oFunctionParameters['BusinessPartnerName']
def l_ret = "Parameter value: " + l_param
return l_ret
Example 2: Return value of the Entity type
To define a single data record as the return value of the function, the fields of the Entity Type are added to a map, which are then returned.
def l_param = g_oFunctionParameters['ID']
// load record from database
// return record value as entity type
def l_entity = [:]
l_entity['ID'] = 1;
l_entity['DeliveryStatus'] = 'OPEN';
l_entity['OrderStatus'] = 1;
l_entity['BillingStatus'] = 'OPEN';
return l_entity
Example 3: Return value of the Entity Collection type
In this example, data records are loaded based on an input parameter, then the result returned as an Entity Collection (To clarify the example, the instructions to load the data records are not included):
def l_param = g_oFunctionParameters['BusinessPartnerName']
// load records from database
// return record values as entities
def l_entity1 = [:]
l_entity1['ID'] = 1;
l_entity1['DeliveryStatus'] = 'OPEN';
l_entity1['OrderStatus'] = 1;
l_entity1['BillingStatus'] = 'OPEN';
l_entity1['TotalSum'] = 1200.00d;
def l_entity2 = [:]
l_entity2['ID'] = 2;
l_entity2['DeliveryStatus'] = 'OPEN';
l_entity2['OrderStatus'] = 1;
l_entity2['BillingStatus'] = 'OPEN';
l_entity2['TotalSum'] = 2300.00d;
return [l_entity1, l_entity2]
A map is initialized for each data record here. Here, the field names are stored as a key and the values as values. Last, the map objects are stored in a Groovy list object and returned as the result. The OData Provider then converts the data structure corresponding to the data types automatically into the required OData format (JSON/XML).
Entity type as return type
Using the entity type as return type, the data sets of data groups can be supplied. Here is a sample configuration.
Click on "Edit Groovy script" to open the Groovy script editor.
Insert the following script there:
return [["STRID":1,"STR_TITLE":"Hello world"],["STRID":2,"STR_TITLE":"Hello world2"]]
"STRID" corresponds to the data field name of the primary key, "STR_TITLE" is the data field name of the term title. Both data field names can be easily determined in the "Application structure" tab and added to the script.
Browser request
The data record collection can now be called up. The URL can be identified on the "General" tab. It matches the endpoint, on which the object "<Funktionsname>?$format=json" can be attached.
If only one data record is to be accessed, formulate the Groovy script as follows:
return ["LID":1,"STRHEADLINE":"Hello world"]
Additionally, disable the "Is Collection" setting on the "Functions tab".