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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Wf Item Types'
    ,p_path               => '/wfitemtypes'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindItemTypes'
    ,p_description        => 'Find Item Types 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 ITEMTYPESEO.DISPLAY_NAME,
                 ITEMTYPESEO.name
               FROM WF_ITEM_TYPES_VL ITEMTYPESEO,
                 WF_NOTIFICATIONS NOTIFICATIONSEO
               WHERE ITEMTYPESEO.name              = NOTIFICATIONSEO.MESSAGE_TYPE
               AND NOTIFICATIONSEO.RECIPIENT_ROLE IN
                 (SELECT USERROLESEO.ROLE_NAME
                 FROM WF_USER_ROLES USERROLESEO
                 WHERE USERROLESEO.USER_NAME = FND_GLOBAL.USER_NAME
                 )
               UNION
               SELECT DISTINCT ITEMTYPESEO.DISPLAY_NAME,
                 ITEMTYPESEO.name
               FROM WF_ITEM_TYPES_VL ITEMTYPESEO
               WHERE ITEMTYPESEO.name IN
                 (SELECT LOOKUP_CODE
                 FROM FND_LOOKUPS
                 WHERE LOOKUP_TYPE                               = 'WF_RR_ITEM_TYPES'
                 AND ENABLED_FLAG                                ='Y'
                 AND TRUNC(NVL(START_DATE_ACTIVE,sysdate),'dd') &lt;= TRUNC(sysdate,'dd')
                 AND TRUNC(NVL(END_DATE_ACTIVE,sysdate),'dd')   &gt;= TRUNC(sysdate,'dd')
                 )
              ) p
        ]'
    );

END;
/
COMMIT;
EXIT;
