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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Retrieves accessible functions for the given user responsibility'
    ,p_path               => 'responsibility-functions'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetResponsibilityFunctions'
    ,p_description        => 'Get responsibility-functions'
    ,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
                     ("function"
                     ,XMLForest
                       (f.responsibility_id
                         || '_' || f.application_id
                         || '_' || f.security_group_id
                         || '_' || f.function_id
                         || '_' || f.function_type "id"
                       ,f.prompt
                         || ' ('
                         || f.responsibility_name
                         || decode(f.nav_path, null, null, ' -> ' || f.nav_path)
                         || ')' "value"
                       ,f.prompt
                         || ' ('
                         || f.responsibility_name
                         || decode(f.nav_path, null, null, ' -> ' || f.nav_path)
                         || ')' "label"
                       )
                     )
                   )
                 ) "data"
        FROM   TABLE(xxas_com_sec_pkg.search_user_functions({requestParam.q})) f
        ]'
    );
 
END;
/
COMMIT;
EXIT;
