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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Root service path'
    ,p_path               => '/lookups/{lookupType}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetLookup'
    ,p_description        => 'Gets the lookup'
    ,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
                     ("value"
                     ,XMLForest
                      (rslt.lookup_code "id"
                      ,rslt.lookup_code "code"
                      ,rslt.meaning "value"
                      ,rslt.meaning "meaning")
                      )
                    )
                  ) "data"
      FROM     hr_lookups rslt
      WHERE    rslt.lookup_type = {pathParam.lookupType}
      AND      rslt.enabled_flag = 'Y'
      AND      trunc(sysdate) BETWEEN
               nvl(rslt.start_date_active, trunc(sysdate)) AND
               nvl(rslt.end_date_active, trunc(sysdate))
      AND    ({requestParam.q} IS NULL AND
              (nvl({requestParam.id}, rslt.lookup_code) = rslt.lookup_code)
           OR ({requestParam.q} IS NOT NULL AND
               rslt.meaning LIKE {requestParam.q} || '%')
             )
      AND     {authFunctionPredicate.XXAS_DATA_HRLOOKUP_VIEW}
      ORDER BY rslt.meaning
       ]'
    );
    
END;
/
COMMIT;
EXIT;
