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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Retrieve function data'
    ,p_path               => 'link-tags'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetFunctionDataByPk1'
    ,p_description        => 'Get function data by primary key 1'
    ,p_methods            => 'GET'
    ,p_path               => '{functionName}/{instancePk2Value}'
	,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
     SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
      	 ,xxas_com_sec_pkg.get_obj_data_w_binds
          ({pathParam.functionName}
          ,xxas_com_varchar2_table
            (xxas_rslt_cache_pkg.object_pk2_column_name
              (xxas_com_sec_pkg.get_object({pathParam.functionName}))
          || ' = :INSTANCE_PK2_VALUE')
          ,xxas_com_srv_param_varray
            (xxas_com_srv_param_value('INSTANCE_PK2_VALUE', {pathParam.instancePk2Value}))
          ,null /* p_rowset_tag_name */
          ,'item' /* p_row_tag_name */
          ,null
          ,null)
          )  "data"
      FROM dual
      WHERE xxas_rslt_cache_pkg.object_pk2_column_name
             (xxas_com_sec_pkg.get_object({pathParam.functionName})) IS NOT NULL
        ]'
    );
	
	xxas_com_sr_pkg.set_rest_method
    ( p_service_name       => l_SERVICE
    ,p_method_name        => 'Security profile'
    ,p_description        => 'get enable security profile'
	,p_path               => '/security_group'
    ,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.id     "id"
                       ,rslt.id     "value"
                       ,rslt.id     "label")
                     )
                   )
                 ) "data"
        FROM   (select fnd_profile.value('ENABLE_SECURITY_GROUPS') id 
		        from dual
                ) rslt

      ]'
    );
	
 xxas_com_sr_pkg.set_rest_method
    ( p_service_name       => l_SERVICE
    ,p_method_name        => 'get link tags'
    ,p_description        => 'get link tags'
	,p_path               => '/get_tags'
    ,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 = fnd_profile.value('XXAS_COM_REG_ACTIONS_MENU')
               AND xxas_com_sec_pkg.get_parameter_value(parameters,'pGroups') IS NOT NULL  ) a

      ]'
    );
	
	 xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Findfunctions'
    ,p_description        => 'Find functions for given resp'
	,p_path               => 'get_function/{respId}'
    ,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.function_id "id"
                       ,rslt.value     "value"
                       ,rslt.label     "label")
                     )
                   )
                 ) "data"
        FROM   (SELECT p.function_id
                      ,p.prompt value
                      ,p.prompt label
                FROM table (xxas_block_pkg.user_resp_functions({pathParam.respId})) p
               where upper(p.prompt) like  ('%' || upper({requestParam.q}) || '%')
                ) rslt

      ]'
    );
    
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteAction'
    ,p_description        => 'Delete the given link'
    ,p_path               => 'delete-tag/{function_id}/{link_function_id}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_block_pkg.delete_link
            (p_function_id          => {pathParam.function_id}
            ,p_link_function_id     => {pathParam.link_function_id}
            ,p_response_xml         => l_xml
            );
          
         l_resp := xxas_http_response
                      .ok(l_xml)
                      .build();
                      
         {response} := l_resp;

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