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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Block administration'
    ,p_path               => 'block-admin/{functionId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlocks'
    ,p_description        => 'Get blocks'
    ,p_path               => 'block-actions'
    ,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"
                     ,ba.function_id "id"
                     ,ba.action_title "line1"
                     ,ba.operation || ' ' || nvl(ba.href, ba.page_id || '/' || ba.object_id) "line3"
                     ,'block-action' "pageId"
                     ,ba.function_id "objectId"
					 ,'modal' "target")
                   )
                 )
               ) "data"
      FROM   TABLE(xxas_block_pkg.block_actions_for_function_id
                    ({pathParam.functionId}).actions) ba
      ORDER BY ba.order_sequence
        ]'
    );

    
END;
/
COMMIT;
EXIT;
