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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Block Used in Pages'
    ,p_path               => 'block-used-pages/{functionId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPages'
    ,p_description        => 'Get Pages using same block'
    ,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"
                     ,p.menu_id "id"
                     ,p.title || decode(p.slug, null, null
                                       ,' (' || p.slug || ')') "line1"
                     ,decode
                       (p.published_flag, 'Y', null
                       ,'&lt;font color="'
                        || xxas_rslt_cache_pkg.resolve_color_name('grey')
                        || '"&gt;Draft&lt;/font&gt;') "line3"
                     ,p.icon "icon"
                     ,p.icon_color_hex "statusColor"
                     ,xxas_page_util_pkg.blocks_count_string(p.menu_id) "decorator"
                     ,'page-details' "pageId"
                     ,p.menu_id "objectId"
                     )
                   )
                 )) "data"
      FROM   xxas_blocks_v b,
             xxas_pages_v  p
      WHERE  b.function_id = {pathParam.functionId}
      AND    b.menu_id     = p.menu_id
      ORDER BY p.slug
        ]'
    );

    
END;
/
COMMIT;
EXIT;
