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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Value Sets and their values'
    ,p_path               => '/value-sets'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetValuesByName'
    ,p_path               => '/by-name/{valueSetName}'
    ,p_description        => 'Gets the value set values by name'
    ,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
                     (v.id "id"
                     ,v.value "value"
                     ,v.value "label"
                     ,v.meaning "description")
                   )
                 )
               ) "data"
      FROM   TABLE(xxas_com_vset_pkg.get_values
                    (xxas_com_vset_pkg.get_vset_id_by_name
                      ({pathParam.valueSetName})
                    ,nvl({requestParam.id}, {requestParam.q})
                    ,CASE WHEN {requestParam.q} IS NOT NULL
                          THEN 'Y'
                          ELSE 'N'
                     END
                    ,{requestParam.per_page})) v
      WHERE  xxas_com_sec_pkg.test_instance
              ('XXAS_DATA_VSET_VIEW'
              ,xxas_com_vset_pkg.get_vset_id_by_name
                 ({pathParam.valueSetName})) = 'Y'
       ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetValuesById'
    ,p_path               => '/by-id/{valueSetId}'
    ,p_description        => 'Gets the value set values by Id'
    ,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
                     (v.id "id"
                     ,v.value "value"
                     ,v.value "label"
                     ,v.meaning "description")
                   )
                 )
               ) "data"
      FROM   TABLE(xxas_com_vset_pkg.get_values
                    ({pathParam.valueSetId}
                    ,nvl({requestParam.id}, {requestParam.q})
                    ,CASE WHEN {requestParam.q} IS NOT NULL
                          THEN 'Y'
                          ELSE 'N'
                     END
                    ,{requestParam.per_page})) v
      WHERE  xxas_com_sec_pkg.test_instance
              ('XXAS_DATA_VSET_VIEW'
              ,{pathParam.valueSetId}) = 'Y'
       ]'
    );
    
END;
/
COMMIT;
EXIT;
