/**
* Copyright (c)2020, 2022, Oracle and/or its affiliates.
* Licensed under The Universal Permissive License (UPL), Version 1.0
* as shown at [Link]
*/
//
// Example transforms for ORDS based REST service.
// Note that the demostration here showcases only how transforms
// can be used to leverage the service capabilities. This example
// does not enable all the capbilities of VBCS transforms and
// hence of the service.
//
define(['urijs/URI'], function (URI) {
'use strict';
var Request = function () { };
/**
* Filter transform function
* @param configuration
* @param options the JSON payload that defines the filterCriteria
* @returns {object} configuration object the url looks like ?filter=foo eq
'bar'.
*/
[Link] = function (configuration, options, transformsContext) {
var filterCriterion = options;
if (filterCriterion === undefined) {
return configuration;
}
//
following code is just an example and it assumes that
//
the SDP does not have any preconfigured filter criterion and
//
that there is only single "vb-textFilterAttributes" attribute set
if
(transformsContext['vb-textFilterAttributes'] !== undefined) {
// in case the filtering comes from single select ui component
// the search criteria details come in transformsContext
var searchText = options && [Link];
var textFilterAttributes = transformsContext && transformsContext['vb-
textFilterAttributes'][0];
options = { attribute: textFilterAttributes, op: "$co", value: searchText };
filterCriterion = options;
}
var operation;
var criteria = [Link];
if (criteria === undefined || [Link] === 0) {
if ([Link] && [Link] && [Link])
{
criteria = [filterCriterion];
operation = "$and";
}
else {
return configuration;
}
}
function transformOperator(fop) {
switch (fop) {
case '$co':
return '$instr';
case '$le':
return '$lte';
case '$ge':
return '$gte';
default:
return fop;
}
}
function isEmpty(val) {
return (val === undefined || val === null || val === '');
}
if (filterCriterion && criteria && [Link] > 0) {
var q = [];
[Link](function (item) {
if ([Link] === undefined || [Link] === '%%' || [Link]
=== 0) {
return;
}
//********** special case for date starts **********//
var queryItem = {};
var dateNode = {};
if ([Link] && typeof [Link] === 'string' && [Link]
.match('[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}') && [Link] && [Link] !=
'$between') {
if ([Link] === '$le')
dateNode['$date'] = [Link] + "T[Link]Z";
else
dateNode['$date'] = [Link] + "T[Link]Z";
queryItem[transformOperator([Link])] = dateNode;
} else
queryItem[transformOperator([Link])] = [Link];
//********** special case for date ends **********//
//********** special case for between starts **********//
if ([Link] && [Link] === '$between' && [Link][0] && [Link][1]) {
if ([Link][0].match('[0-9]{4}-[0-9]{1,2}-[0-9]{1,2}'))
queryItem[transformOperator([Link])] = [[Link][0] + "T[Link]Z",
[Link][1] + "T[Link]Z"];
else {
if (typeof [Link][0] === 'string')
queryItem[transformOperator([Link])] = [[Link][0].toUpperCase(),
[Link][1].toUpperCase()];
else
queryItem[transformOperator([Link])] = [[Link][0],
[Link][1]];
}
}
//********** special case for between ends **********//
var queryJSON = {};
queryJSON[[Link]] = queryItem;
[Link](queryJSON);
});
if ([Link] > 0) {
var query = {};
if (operation === undefined) {
operation = [Link];
}
if (operation === undefined) {
operation = "$and";
}
query[operation] = q;
// ORDS query URL is for example:
// .../ords/hr/emp?q={"$or":[{"ename":{"$instr":"martin"}},{"job":
{"$like":"%developer%"}}]}
[Link] = addQParam([Link], query);
}
}
return configuration;
};
/**
* Pagination function appends limit and offset parameters to the url
* @param configuration
* @param options the JSON payload that defines the pagination criteria
* @returns {object} configuration object.
*/
[Link] = function (configuration, options) {
var newUrl = [Link];
if (options && [Link]) {
newUrl = URI(newUrl).addSearch({ limit: [Link], offset:
[Link] }).toString();
}
[Link] = newUrl;
return configuration;
};
/**
* Sort transform function
* @param configuration
* @param options the JSON payload that defines the sort criteria
* @returns {object} configuration object. the url looks like ?orderBy=foo:asc
*/
[Link] = function (configuration, options) {
if ([Link](options) && [Link] > 0) {
var firstItem = options[0];
if ([Link]) {
var dir = [Link] === 'descending' ? 'DESC' : 'ASC';
var sort = { "$orderby": {} };
sort["$orderby"][[Link]] = dir;
// ORDS sort URL is for example:
// ...ords/hr/emp?q={"$orderby":{"sal":"ASC"}}
[Link] = addQParam([Link], sort);
}
}
return configuration;
};
var addQParam = function (url, toAdd) {
var query = URI(url).search(true);
if (query.q) {
toAdd = [Link]([Link](query.q), toAdd);
}
query.q = [Link](toAdd);
return URI(url).search(query).toString();
};
var Response = function () { };
/**
* Paginate Response Transform Function Implementation
*/
[Link] = function (result) {
var tr = {};
if (result && [Link]) {
var cb = [Link];
// ORDS does not support "totalCount" but only "hasMore"
[Link] = [Link];
}
return tr;
};
return { request: Request, response: Response };
});