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

BEGIN

    xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'publish/unpublish pages'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Publish'
    ,p_path               => 'publish/{menuId}'
    ,p_description        => 'publish the given page'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml         XMLType;
          l_resp        xxas_http_resp_obj;
          l_request_xml XMLType;
          
        BEGIN
          
          IF xxas_wf_role_util_pkg.user_has_role
               ('UMX|XXAS_FUNCTIONAL_CONFIG') = 'N'
          THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
                      
            xxas_page_pkg.publish_page
              (p_menu_id => {pathParam.menuId}
             , p_response_xml => l_xml );
             
               IF l_xml IS NOT NULL THEN
                 l_resp := xxas_http_response
                        .ok(l_xml)
                        .build();
             ELSE    
                 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)
                             )
                           )
                        .build();
          END IF;
        END IF; 
         {response} := l_resp;

        END
      ]'
    );
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Unpublish'
    ,p_path               => 'unpublish/{menuId}'
    ,p_description        => 'publish the given page'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml         XMLType;
          l_resp        xxas_http_resp_obj;
          l_request_xml XMLType;
          
        BEGIN
          
          IF xxas_wf_role_util_pkg.user_has_role
               ('UMX|XXAS_FUNCTIONAL_CONFIG') = 'N'
          THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
             xxas_page_pkg.unpublish_page
              (p_menu_id => {pathParam.menuId}
             , p_response_xml => l_xml );
             
                IF l_xml IS NOT NULL THEN
                   l_resp := xxas_http_response
                        .ok(l_xml)
                        .build();
              ELSE
                
            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)
                             )
                           )
                        .build();
          END IF;
      END IF;   
         {response} := l_resp;

        END
      ]'
    );
    
END;
/
COMMIT;
EXIT;