/* $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
                       := 'BlockAction';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Page Administration'
    ,p_path               => 'page-action'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
      
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPageList'
    ,p_description        => 'Get page list'
    ,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
                     ("role"
                     ,XMLForest
                       (p.slug as "id"
                       ,p.page      as "label"
                       ,p.page      as "value"
                       )
                     )
                   )
                 )"data"
    FROM ( SELECT slug, (slug || ' (' || title|| ')') page
           FROM xxas_pages_v 
           WHERE upper(slug) = upper(nvl({requestParam.q},slug))
           UNION
           select slug, (slug || ' (' || title|| ')') page
           from xxas_pages_v
           where upper(slug) like upper('%'||{requestParam.q}||'%')
           OR
           (upper(title) like upper('%' ||{requestParam.q}|| '%'))
          )P         
        ]'
    );
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPageActions'
    ,p_description        => 'Get page actions'
	,p_path               => '{menuId}/page-actions'
    ,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
                   ("item"
                   ,XMLForest
                     ('flexible' "type"
                     ,ba.function_id "id"
                     ,ba.action_title "line1"
                     ,ba.operation || ' ' || nvl(ba.href, ba.page_id || '/' || ba.object_id) "line3"
                     ,'page-action' "pageId"
                     ,ba.function_id "objectId"
                     )
                   )
                 )
               ) "data"
     FROM TABLE(xxas_block_pkg.page_actions_for_menu_id({pathParam.menuId}).actions) ba
           ORDER BY ba.order_sequence         
        ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteAction'
    ,p_description        => 'Delete the given Action'
    ,p_path               => '{function_id}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_block_pkg.delete_action
            (p_function_id          => {pathParam.function_id}
            ,p_response_xml         => l_xml
            );
          
         l_resp := xxas_http_response
                      .ok(l_xml)
                      .build();
                      
         {response} := l_resp;

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