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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Key generator'
		,p_path               => 'key-generator'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GenerateDBSequence'
    ,p_description        => 'Generates a database sequence key'
		,p_path               => 'SEQUENCE/{sequenceName}'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
				DECLARE

          l_resp       		xxas_http_resp_obj;
          l_response_xml  XMLType;
          l_key           varchar2(400);
          l_xml           XMLType;
          
        BEGIN
				
          EXECUTE IMMEDIATE
                ' SELECT ' || {pathParam.sequenceName} || '.NextVal'
             || ' FROM dual '
          INTO l_key;
          
          SELECT XMLElement
                  ("data"
                  ,XMLElement
                    ("value"
                    ,l_key))
          INTO l_xml
          FROM dual;

				  l_resp := xxas_http_response.ok(l_xml).build();

				  {response} := l_resp;
                      
				END
			]'
		);

END;
/
COMMIT;
EXIT;
