/* $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
                       := 'FunctionLinksGrants';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View and administer function grants for quick links'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetFunctionGrants'
    ,p_description        => 'Get function grants'
    ,p_path               => 'function-links-grants/{functionId}'
    ,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
                   ("item"
                   ,XMLForest
                     ('flexible' "type"
                     ,g.grant_guid "id"
                     ,CASE WHEN NVL(xxas_com_sec_pkg.has_dependencies(g.grant_guid),'N') = 'Y' THEN
                                null
					       ELSE xxas_com_sec_pkg.grant_item_actions
							   (decode(upper(substr(g.function_name, 1, 4))
									  ,'XXAS'
									  ,'N'
									  ,'Y')).get_xml() 
					  END "linked"
                     ,decode
                       (g.function_obj_name
                       ,g.grant_obj_name
                       ,null
                       ,'&lt;font color="#FF9900"&gt;Does not apply: Object on block is different to grant&lt;/font&gt;') "line2"
                     ,decode
                       (g.function_obj_name
                       ,g.grant_obj_name
                       ,'#499324'
                       ,'#FF9900') "statusColor"
                     ,decode
                       (g.function_obj_name
                       ,g.grant_obj_name
                       ,'tick'
                       ,'alert') "icon"
                     ,decode
                       (g.role_display_name
                       ,'GLOBAL'
                       ,'All authenticated users'
                       ,g.role_display_name) "line1"
                     ,CASE WHEN g.grant_obj_name IS NULL THEN
                                null
                           WHEN g.grant_instance_type = 'GLOBAL' THEN
                                g.grant_obj_name || ' (all rows)'
                           ELSE g.grant_obj_name || ' (' || g.instance_set_name || ')'
                      END "line3")
                   )
                 )
               ) "data"
      FROM   TABLE(xxas_com_sec_pkg.get_function_grants({pathParam.functionId})) g
      ORDER BY g.role_display_name
        ]'
    );

    
END;
/
COMMIT;
EXIT;