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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Wf Roles'
    ,p_path               => '/wfroles'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindRoles'
    ,p_description        => 'Find Roles matching the 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
                       (rslt.name "id"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ', ' || rslt.email_address) "value"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ', ' || rslt.email_address) "label")
                     ) order by rslt.display_name
                   )
                 ) "data"
        FROM  (SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system 
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER') 
               AND    wu.name like upper({requestParam.q} ||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER') 
               AND    upper(wu.display_name) like upper({requestParam.q}||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system 
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER') 
               AND    upper(wu.email_address) like upper({requestParam.q}||'%')
              ) rslt
              WHERE (xxas_menu_rslt_cache_pkg.menu_grants_count('WF_ROLE_LOV_PSET') = 0 OR 
                     {authFunctionPredicate.WF_ROLE_LOV_FN})
              AND   (xxas_menu_rslt_cache_pkg.menu_grants_count('WF_PARTITION_PSET') = 0 OR 
                     {authFunctionPredicate.WF_PARTITION_FN})
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindAllSecResponseRoles'
    ,p_description        => 'Find All Secondary Response Roles matching the given criteria'
    ,p_methods            => 'GET'
    ,p_path               => '/sec-response-roles'
    ,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
                       (rslt.name "id"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ', ' || rslt.email_address) "value"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ', ' || rslt.email_address) "label")
                     ) order by rslt.display_name
                   )
                 ) "data"
        FROM  (SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system  = wdptl.orig_system 
               AND    userenv('LANG') = wdptl.language
               AND    upper(wu.name) like upper({requestParam.q} ||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system  = wdptl.orig_system 
               AND    userenv('LANG') = wdptl.language
               AND    upper(wu.display_name) like upper({requestParam.q}||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system  = wdptl.orig_system 
               AND    userenv('LANG') = wdptl.language
               AND    upper(wu.email_address) like upper({requestParam.q}||'%')
              ) rslt
              WHERE (xxas_menu_rslt_cache_pkg.menu_grants_count('WF_ROLE_LOV_PSET') = 0 OR 
                     {authFunctionPredicate.WF_ROLE_LOV_FN})
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindVacationRuleRoles'
    ,p_description        => 'Find All Vacation Rule Roles matching the given criteria'
    ,p_methods            => 'GET'
    ,p_path               => '/vac-rule-roles'
    ,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
                       (rslt.name "id"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ' (' || rslt.email_address || ')' ) "value"
                       ,decode
                         (rslt.email_address
                         ,null
                         ,rslt.display_name
                         ,rslt.display_name || ' (' || rslt.email_address || ')' ) "label")
                     ) order by rslt.display_name
                   )
                 ) "data"
        FROM  (SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system 
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER')
               AND    wu.name &lt;&gt; fnd_global.user_name
               AND    {requestParam.q} is not null
               AND    wu.name like upper({requestParam.q} ||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER')
               AND    wu.name &lt;&gt; fnd_global.user_name
               AND    {requestParam.q} is not null
               AND    upper(wu.display_name) like upper({requestParam.q}||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system 
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER')
               AND    wu.name &lt;&gt; fnd_global.user_name
               AND    {requestParam.q} is not null
               AND    upper(wu.email_address) like upper({requestParam.q}||'%')
               UNION
               SELECT wu.*
                     ,wu.name user_name
               FROM   wf_role_lov_vl wu
                     ,wf_directory_partitions_tl wdptl
               WHERE  wu.orig_system = wdptl.orig_system 
               AND    userenv('LANG')= wdptl.language
               AND    wu.orig_system in ('FND_USR', 'PQH_ROLE', 'PER')
               AND    wu.name &lt;&gt; fnd_global.user_name
               AND    {requestParam.id} is not null
               AND    upper(wu.name) = upper({requestParam.id})
              ) rslt
              WHERE (xxas_menu_rslt_cache_pkg.menu_grants_count('WF_ROLE_LOV_PSET') = 0 OR 
                     {authFunctionPredicate.WF_ROLE_LOV_FN})
        ]'
    );

END;
/
COMMIT;
EXIT;
