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

BEGIN

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

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindFndMenu'
    ,p_description        => 'Find existing Menus matching the given criteria'
	,p_path               => '/fnd-menus'
    ,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
                     ("fndmenu"
                     ,XMLForest
                       (rslt.menu_name "id"
                       ,rslt.value     "value"
                       ,rslt.label     "label")
                     )
                   )
                 ) "data"
        FROM   (SELECT fm.menu_name
                      ,fm.user_menu_name value
                      ,fm.user_menu_name label
                FROM   fnd_menus_vl fm
                WHERE  {requestParam.id} IS NULL
                AND    UPPER(fm.user_menu_name) like UPPER('%' || {requestParam.q} || '%')
                UNION
                SELECT fm.menu_name
                      ,fm.user_menu_name value
                      ,fm.user_menu_name label
                FROM   fnd_menus_vl fm
                WHERE  {requestParam.id} IS NOT NULL
                AND    fm.menu_name = {requestParam.id}) rslt

      ]'
    );
	
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Gettags'
    ,p_description        => 'Gets page action tags'
    ,p_path               => '/link-tags/{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
                     ("obj"
                     ,XMLForest
                       (a.tags "id"
                       ,a.tags "value"
                       ,a.tags "label"
                       )
                     )
                   )
                 ) "data"
				 
		FROM  (
		       SELECT DISTINCT (xxas_com_sec_pkg.get_parameter_value(parameters,'pGroups')) tags
               FROM FND_MENU_ENTRIES m , fnd_form_functions f , fnd_menus fm
			   WHERE m.menu_id = fm.menu_id
               AND m.function_id = f.function_id
			   AND fm.menu_name = {pathParam.menuId}
               AND xxas_com_sec_pkg.get_parameter_value(parameters,'pGroups') IS NOT NULL  ) a
        ]'
    );
	

END;
/
COMMIT;
EXIT;
