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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Advert Page Links'
    ,p_path               => '/advert-page-links'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindAdvertPageLinks'
    ,p_description        => 'Find existing pages matching the given criteria'
    ,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
                     ("pages"
                     ,XMLForest
                       (rslt.slug  "id"
                       ,rslt.value "value"
                       ,rslt.label "label")
                     )
                   )
                 ) "data"
        FROM   (SELECT p.slug
                      ,p.slug value
                      ,p.slug label
                FROM   xxas_pages_v p
                WHERE  {requestParam.id} IS NULL
                AND    UPPER(p.slug) like UPPER('%' || {requestParam.q} || '%')
                UNION
                SELECT p.slug
                      ,p.slug value
                      ,p.slug label
                FROM   xxas_pages_v p
                WHERE  {requestParam.id} IS NOT NULL
                AND    p.slug = substr({requestParam.id},decode(NVL(INSTR({requestParam.id},'#/',1,1),0),0,-1,NVL(INSTR({requestParam.id},'#/',1,1),0)) + 2)) rslt

      ]'
    );

END;
/
COMMIT;
EXIT;
