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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Random Advert blocks used in Advert'
    ,p_path               => 'random-advert-blocks/{menuId}/{functionId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetRandomAdvertBlocks'
    ,p_description        => 'Get Random Advert Blocks used in Single Advert'
    ,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"
                     ,fme.menu_id
                     ,fme.function_id
                     ,fme.function_id "id"
                     /*,ff.function_name "line2"
                     ,NVL(fme.prompt,xxas_com_msg_pkg.get_string('XXAS_BK_UNTITLED_TITLE')) "line1"
                     ,xxas_util_pkg.decode_lookup('XXAS_BLOCK_TYPES',UPPER(b.block_type)) "line3"
                     ,NVL(xxas_page_util_pkg.page_icon(fme.menu_id),'wrench') "icon"
                     ,'balanced' "statusColor"*/
                     ,NVL(fme.prompt ,xxas_com_msg_pkg.get_string('XXAS_BK_UNTITLED_TITLE')) || ' ('
                      || xxas_util_pkg.decode_lookup('XXAS_BLOCK_TYPES',UPPER(b.block_type)) || ')' "line1"
                     ,'block-' || b.block_type "pageId"
                     ,fme.menu_id || '_' || fme.function_id  "objectId"
                     ,xxas_block_pkg.random_advert_block_actions({pathParam.menuId},b.function_id,b.function_name,xxas_com_sec_pkg.get_parameter_value(b.function_parameters, 'pAdGroup'),fme.function_id).get_xml() "linked"
                     )
                   )
                 )) "data"
      FROM   xxas_blocks_v b
            ,fnd_menus fm
            ,fnd_menu_entries_vl fme
            ,fnd_form_functions_vl ff
      WHERE  b.function_id = {pathParam.functionId}
      AND    upper(b.block_type) = 'ADVERT'
      AND    xxas_com_sec_pkg.get_parameter_value(b.function_parameters, 'pAdGroup') IS NOT NULL
      AND    fm.menu_name = xxas_com_sec_pkg.get_parameter_value(b.function_parameters, 'pAdGroup')
      AND    fm.menu_id   = fme.menu_id
      AND    ff.function_id = fme.function_id
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteRandomBlock'
    ,p_description        => 'Deletes a Random Block from Advert'
    ,p_path               => 'delete/{advtGroupMenuId}/{entrySequence}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp xxas_http_resp_obj;
        BEGIN

          -- Check access to delete the random block
          IF xxas_com_sec_pkg.test_instance('XXAS_BA_DEL_RANDOM_ADVT_BLOCK',{pathParam.menuId},{pathParam.functionId}) = 'Y' THEN

            xxas_block_pkg.delete_random_advert_block({pathParam.advtGroupMenuId},{pathParam.entrySequence});

          END IF;

          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();

         {response} := l_resp;

        END
      ]'
    );

END;
/
COMMIT;
EXIT;
