/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2014 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
                       := 'Menu';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Retrieve menus'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetMenu'
    ,p_description        => 'Gets a menu'
    ,p_path               => '/menus/{menuId}'
    ,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
                    (e.function_id "id"
                    ,decode({requestParam.hideTitle},'true',null,e.prompt ) "title"
                    ,f.web_icon "icon"
                    ,nvl(xxas_com_sec_pkg.get_parameter_value(f.parameters, 'pHref')
                        ,xxas_com_sec_pkg.get_function_url
                          (f.function_id, null, null, null
                          ,xxas_com_sec_pkg.get_parameter_value
                            (f.parameters, 'pSecurityGroupId'))
                        ) "href"
                    ,decode(nvl(upper(xxas_com_sec_pkg.get_parameter_value(f.parameters,
                       'pNewTabOrWindow')),'N'),'Y','_blank',null) "target"
                    ,xxas_com_sec_pkg.hide_if(f.parameters) "hideIf"
                    )) ORDER BY e.entry_sequence)) "data"
      FROM   fnd_menus m
            ,fnd_menu_entries_vl e
            ,fnd_form_functions_vl f
      WHERE  m.menu_id = {pathParam.menuId}
      AND    m.menu_id = e.menu_id
      AND    e.prompt IS NOT NULL
      AND    e.function_id = f.function_id
      AND    ((f.object_id is null 
              AND 
             xxas_com_sec_pkg.test(f.function_name) = 'Y')
             OR
             xxas_com_sec_pkg.test_instance(f.function_name,fnd_global.employee_id) = 'Y'
             )
       ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetSubNav'
    ,p_description        => 'Gets a a page''s subnav'
    ,p_path               => '/pages-menu/{menuId}'
    ,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")
                 ,nvl
                   (xxas_emp_menu_pkg.get_subnav_xml
                     ({pathParam.menuId}
                     ,xxas_emp_menu_pkg.subnav_object_id_to_keys
                       ({pathParam.menuId}, {requestParam.id}))
                   ,XMLElement
                     ("item")
                   )
                 ) "data"
              ,XMLElement
                ("links"
                ,XMLElement
                  ("context"
                  ,XMLElement
                    ("href"
                    ,xxas_com_rest_util_pkg.uri_from_path
                       (xxas_emp_menu_pkg.subnav_header_path
                         ({pathParam.menuId}, {requestParam.id}))
                    )
                  )
                ) "links"
        FROM dual
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteMenu'
    ,p_description        => 'Deletes a menu'
    ,p_path               => 'menus/{menuId}/delete-menu'
    ,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',{pathParam.menuId}) = 'Y' THEN

            xxas_emp_menu_pkg.delete_menu({pathParam.menuId});

          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;
