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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Users'
    ,p_path               => '/users'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindUsers'
    ,p_description        => 'Find users'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      '
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   (''true'' as "json:force-array"
                   ,''http://json.org/'' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("obj"
                     ,XMLForest
                       (rslt.user_id "id"
                       , REGEXP_REPLACE (rslt.user_name, ''[[:cntrl:]]'', '''') "value"
                       ,REGEXP_REPLACE (rslt.user_name, ''[[:cntrl:]]'', '''') "label"
                       )
                     )
                   )
                 ) "data"
        FROM  fnd_user rslt
        WHERE trunc(sysdate) between start_date and nvl(end_date, trunc(sysdate))
		AND rslt.user_name like (upper({requestParam.q})||''%'') 
		AND   {authFunctionPredicate.XXAS_DT_USERS}
        ORDER BY rslt.user_name
        '
    );

END;
/
COMMIT;
EXIT;
