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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Wf Message Names'
    ,p_path               => '/wfmsgnames'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindMsgNames'
    ,p_description        => 'Find Message Names matching given criteria'
    ,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
                     ("role"
                     ,XMLForest
                       (p.name "id"
                         ,p.DISPLAY_NAME
                        "value"
                         ,p.display_name
                         "label")
                     )
                   )
                 ) "data"
            FROM  (
                    SELECT DISTINCT wm.NAME
                               ,wm.display_name 
                    FROM   WF_MESSAGES_vl wm
                               ,wf_notifications wn
                    WHERE wm.NAME = wn.message_name
                    AND wn.message_type = nvl({requestParam.itemtype},wn.message_type)
                    AND wn.recipient_role IN (SELECT USERROLESEO.ROLE_NAME
                                              FROM WF_USER_ROLES USERROLESEO
                                              WHERE USERROLESEO.USER_NAME = FND_GLOBAL.USER_NAME
                                             )
                 
                  )p
        ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindMsgNamesByPath'
    ,p_description        => 'Find Message Names matching given criteria'
    ,p_path               => '/{itemtype}'
    ,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
                     ("role"
                     ,XMLForest
                       (p.DISPLAY_NAME "id"
                         ,p.NAME
                        "value"
                         ,p.name
                         "label")
                     )
                   )
                 ) "data"
            FROM  (
			SELECT '*'  AS DISPLAY_NAME,
                   '--' || WF_CORE.TRANSLATE('ALL')
                 ||'--' AS name
               FROM SYS.DUAL
               WHERE 'Y' = 'Y'
               UNION
                    SELECT DISTINCT wm.NAME
                               ,wm.display_name 
                    FROM   WF_MESSAGES_vl wm
                               ,wf_notifications wn
                    WHERE wm.NAME = wn.message_name
                    AND wn.message_type = nvl({pathParam.itemtype},wn.message_type)
                    AND wn.recipient_role IN (SELECT USERROLESEO.ROLE_NAME
                                              FROM WF_USER_ROLES USERROLESEO
                                              WHERE USERROLESEO.USER_NAME = FND_GLOBAL.USER_NAME
                                             )
                 
                  )p
        ]'
    );

END;
/
COMMIT;
EXIT;
