/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2014 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
                       := 'Pages';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Page'
    ,p_path               => 'pages'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPage'
    ,p_description        => 'Get a page'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp    xxas_http_resp_obj;
          l_xml     XMLType;
                    
        BEGIN

          l_xml := xxas_emp_menu_pkg.get_page_xml
            (p_page_id   => {pathParam.pageId}
            ,p_object_id => {requestParam.id}
            ,p_view_mode => {requestParam.view});
        
          IF l_xml IS NOT NULL THEN
            l_resp := xxas_http_response
                        .ok(l_xml)
                        .build();
          ELSIF l_xml IS NULL AND fnd_global.user_name = 'GUEST' THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.not_found)
                        .build();                      
          END IF;
          
         {response} := l_resp;
         
       END

        ]'
    );
xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Findpages'
    ,p_description        => 'Find Applaud pages'
    ,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
                     ("fndmenu"
                     ,XMLForest
                       (rslt.slug "id"
                       ,rslt.value     "value"
                       ,rslt.label     "label")
                     )
                   )
                 ) "data"
        FROM   (SELECT p.slug
                      ,p.title value
                      ,p.title label
                FROM   xxas_pages_v p
                WHERE   (UPPER(p.title) like UPPER('%' || {requestParam.q} || '%')
				OR UPPER(p.slug) like UPPER('%' || {requestParam.q} || '%'))
                ) rslt

      ]'
    );    
END;
/
COMMIT;
EXIT;
