/* $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
                       := 'PageAdmin';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Page Administration'
    ,p_path               => 'page-admin'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'BulkUpdatePages'
    ,p_description        => 'Bulk update one or more pages'
    ,p_path               => '{action}'
    ,p_methods            => 'PUT'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml  XMLType;
          l_resp xxas_http_resp_obj;
          
        BEGIN

          xxas_page_pkg.bulk_action
            (p_action       => {pathParam.action}
            ,p_request_body => {requestBody});
              
          l_resp := xxas_http_response
                      .ok(xxas_com_msg_pkg.get_success_message_xml
                           (xxas_com_msg_type(xxas_util_pkg.app, 'XXAS_COM_CHNG_SAVE', null)
                           ,'self'))
                      .build();
         
         {response} := l_resp;

        END
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'BulkCreatePages'
    ,p_description        => 'Bulk create one or more pages'
    ,p_path               => '{action}'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml  XMLType;
          l_resp xxas_http_resp_obj;
          
        BEGIN

          xxas_page_pkg.bulk_action
            (p_action       => {pathParam.action}
            ,p_request_body => {requestBody});
              
          l_resp := xxas_http_response
                      .ok(xxas_com_msg_pkg.get_success_message_xml
                           (xxas_com_msg_type(xxas_util_pkg.app, 'XXAS_COM_CHNG_SAVE', null)
                           ,'self'))
                      .build();
         
         {response} := l_resp;

        END
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPageBlocks'
    ,p_description        => 'Get page blocks'
    ,p_path               => '{menuId}/blocks'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
      SELECT  XMLELEMENT("data"
                        ,XMLAttributes('http://json.org/'  AS "xmlns:json"
                                      ,'true'              AS "json:force-array")                        
                        ,xxas_block_pkg.get_blocks_for_page
                          ({pathParam.menuId})
                        )                          "data"
      FROM   dual
        ]'
    );

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPageChildBlocks'
    ,p_description        => 'Get page child blocks'
    ,p_path               => '{menuId}/blocks/{functionId}/childblocks'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
      SELECT  XMLELEMENT("data"
                        ,XMLAttributes('http://json.org/'  AS "xmlns:json"
                                      ,'true'              AS "json:force-array")                        
                        ,null
                        )                          "data"
      FROM   dual
        ]'
    );
    
END;
/
COMMIT;
EXIT;
