/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2015 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
                       := 'BIPublisher';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Bi Publisher Defs'
    ,p_path               => '/bipub'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindApplicationShortName'
    ,p_description        => 'Find Application Short Name'
    ,p_methods            => 'GET'
    ,p_path               => '/app'
    ,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
                     ("App"
                     ,XMLForest
                       (rslt.app_name "id"
                       ,rslt.app_name "value"
                       ,rslt.app_name "label")
                     )
                   )
                 ) "data"
        FROM  (SELECT fa.application_short_name app_name
		           FROM fnd_application fa
               WHERE fa.application_short_name like UPPER({requestParam.q}||'%')
              ) rslt
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindBIPubDataDefCode'
    ,p_description        => 'Find Bi Publisher Data Definitin Code'
    ,p_methods            => 'GET'
    ,p_path               => '/code/{appShortName}'
    ,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
                     ("app"
                     ,XMLForest
                       (rslt.data_source_code "id"
                       ,rslt.data_source_code "value"
                       ,rslt.data_source_code "label")
                     )
                   )
                 ) "data"
        FROM  (select xdd.data_source_code from XDO_DS_DEFINITIONS_B xdd
               where xdd.application_short_name = upper({pathParam.appShortName})) rslt
        ]'
    );

END;
/
COMMIT;
EXIT;