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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Reuse Existing Block'
    ,p_path               => '/reuse-block'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Block'
    ,p_description        => 'Find existing blocks matching the given criteria'
    ,p_methods            => 'GET'
    ,p_path               => 'block'
    ,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
                     ("block"
                     ,XMLForest
                       (rslt.function_id   "id"
                       ,decode(rslt.prompt,null,'Untitled',rslt.prompt) || ' (' || rslt.function_name || ')' "value"
                       ,decode(rslt.prompt,null,'Untitled',rslt.prompt) || ' (' || rslt.function_name || ')' "label")
                     )
                   )
                 ) "data"
        FROM   xxas_blocks_v rslt
        WHERE  UPPER(rslt.function_name) like UPPER('%' || {requestParam.q} || '%')
        AND    rslt.function_id NOT IN (SELECT b.function_id 
                                        FROM   xxas_blocks_v b
                                        WHERE  b.menu_id = {requestParam.excludeFromPage})
        AND    EXISTS (SELECT 1 
                       FROM   xxas_pages_v p 
                       WHERE  p.menu_id = {requestParam.excludeFromPage}
                       AND    ((p.object_id_format is null AND rslt.object_id is null) OR 
                               (p.object_id_format is not null)))
      ]'
    );
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlocksList'
    ,p_description        => 'Get blocks list'
    ,p_methods            => 'GET'
	,p_path               => 'block-list'
    ,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
                     ("block"
                     ,XMLForest
                       (rslt.function_name   "id"
                       ,decode(rslt.prompt,null,'Untitled',rslt.prompt) || ' (' || rslt.function_name || ')' "value"
                       ,decode(rslt.prompt,null,'Untitled',rslt.prompt) || ' (' || rslt.function_name || ')' "label")
                     )
                   )
                 ) "data"
        FROM   xxas_blocks_v rslt
        WHERE  UPPER(rslt.function_name) like UPPER('%' || {requestParam.q} || '%')
        ]'
    );

	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetAllBlocksList'
    ,p_description        => 'Get all blocks list'
    ,p_methods            => 'GET'
  	,p_path               => 'block-all-list'
    ,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
                     ("block"
                     ,XMLForest
                       (rslt.function_name   "id"
                       ,rslt.function_name   "value"
                       ,rslt.function_name   "label")
                     )
                   )
                 ) "data"
        FROM   xxas_blocks_all_v rslt
        WHERE  UPPER(rslt.function_name) like UPPER('%' || {requestParam.q} || '%')
        ]'
    );

	
END;
/
COMMIT;
EXIT;
