/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2016 Applaud Solutions UK Ltd                |
 |                             All rights reserved                            |
 *===========================================================================*/

SET VERIFY OFF
SET FEEDBACK OFF
SET SCAN OFF
WHENEVER OSERROR  EXIT FAILURE ROLLBACK
WHENEVER SQLERROR EXIT FAILURE ROLLBACK

DECLARE

  --
  -- Local constants and variables
  --
  l_SERVICE CONSTANT xxas_com_service_repository.service_name%TYPE
                       := 'FlexibleFields';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Flexible fields'
    ,p_path               => '/flexible-fields'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetFlexibleFields'
    ,p_description        => 'Get flexible fields'
    ,p_methods            => 'GET'
	  ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("value"
                     ,XMLForest
                       (rslt.flex_value   "id"
                       ,rslt.flex_value   "value"
                       ,rslt.flex_value   "label")
                     )
                   )
                 ) "data"
        FROM (select ffv.flex_value
        FROM   fnd_flex_values ffv, fnd_flex_value_sets ffvs
        where ffv.flex_value_set_id = ffvs.flex_value_set_id
        and ffvs.flex_value_set_name = 'XXAS_FILTER_FIELDS'
        and ffv.flex_value like {requestParam.q}||'%'
        order by ffv.flex_value_id) rslt
        ]'
    );

END;
/
COMMIT;
EXIT;
