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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Menuitem'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    ,p_path               => 'menu-item');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindMenuitemPageLinks'
    ,p_description        => 'Find existing pages matching the given criteria'
    ,p_path               => 'page-links'
    ,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
                     ("pages"
                     ,XMLForest
                       (rslt.id  "id"
                       ,rslt.value "value"
                       ,rslt.label "label")
                     )
                   )
                 ) "data"
        FROM   (SELECT p.slug id
                      ,p.slug value
                      ,p.slug label
                FROM   xxas_pages_v p
                WHERE  {requestParam.id} IS NULL
                AND    ((to_char({requestParam.objId}) = '-1' AND p.object_id_format is null) OR (p.object_id_format is not null AND p.object_id_format = to_char({requestParam.objId})))
                AND    UPPER(p.slug) like UPPER('%' || {requestParam.q} || '%')
                UNION
                SELECT p.slug id
                      ,p.slug value
                      ,p.slug label
                FROM   xxas_pages_v p
                WHERE  {requestParam.id} IS NOT NULL
                AND    ((to_char({requestParam.objId}) = '-1' AND p.object_id_format is null) OR (p.object_id_format is not null AND p.object_id_format = to_char({requestParam.objId})))
                AND    p.slug = {requestParam.id}) rslt

      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteMenuItem'
    ,p_description        => 'Deletes a Menu item'
    ,p_path               => 'delete/{menuId}_{entrySeq}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp xxas_http_resp_obj;
        BEGIN

          -- Check access to delete the menu
          IF xxas_com_sec_pkg.test_instance('XXAS_BA_DELETE_MENU_ITEM',{pathParam.menuId},{pathParam.entrySeq}) = 'Y' THEN

            xxas_emp_menu_pkg.delete_menu_item({pathParam.menuId},{pathParam.entrySeq});

          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)
                           ,'menus'
                           )
                         )
                      .build();

         {response} := l_resp;

        END
      ]'
    );

END;
/
COMMIT;
EXIT;
