/* $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
                       := 'RestService';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Rest Service'
    ,p_path               => 'rest-service'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
      
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetRestServices'
    ,p_description        => 'Get Rest Services list'
    ,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(data) ) "data" from
       (SELECT XMLElement
                 ("restservice"
                  ,XMLElement
                     ("data"
                     ,XMLForest
                       (r.service_name "id"
					             ,'note' "type"
                       ,r.service_name "author"
                       ,r.path "noteType"
					             ,'rest-service-view' "pageId"
                       ,r.service_name "objectId"
                       ,'modal' "target")
					             ).extract('/data/*')
                  ,XMLElement
                   ("labels"
	                   ,XMLAttributes
                     ('true' as "json:force-array"
                      ,'http://json.org/' as "xmlns:json")
	                    ,CASE WHEN xxas_rest_service_pkg.get_methods(r.service_name) > 0 THEN
                       XMLElement ("data"
                                   ,XMLForest
                                    (xxas_rest_service_pkg.get_methods(r.service_name)|| ' GET' "title"
                                    ,'balanced' "color"))
                       END
                      ,CASE WHEN xxas_rest_service_pkg.put_methods(r.service_name) > 0 THEN
                       XMLElement ("data"
		                               ,XMLForest
                                   (xxas_rest_service_pkg.put_methods(r.service_name)||' PUT' "title"
                                   ,'energized' "color"))
                       END
                      ,CASE WHEN xxas_rest_service_pkg.post_methods(r.service_name) > 0 THEN
                       XMLElement ("data"
		                               ,XMLForest
                                   (xxas_rest_service_pkg.post_methods(r.service_name)|| ' POST' "title"
                                   ,'positive' "color"))
                       END
                      ,CASE WHEN xxas_rest_service_pkg.del_methods(r.service_name) > 0 THEN
                       XMLElement ("data" 
		                               ,XMLForest
                                   (xxas_rest_service_pkg.del_methods(r.service_name)||' DELETE' "title"
                                   ,'assertive' "color"))
                       END
                      ) "labels"
                ) as data
    FROM xxas_rest_services_v r
    where xxas_wf_role_util_pkg.user_has_role('UMX|XXAS_FUNCTIONAL_CONFIG') = 'Y')
        ]'
    );
    
         xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetRestServiceName'
    ,p_description        => 'Get Rest Service Name'
    ,p_path               => 'getname'
    ,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
                       (p.service_name as "id"
                       ,p.service_name      as "label"
                       ,p.service_name      as "value"
                       )
                     )
                   )
                 )"data"
        FROM xxas_com_service_repository p
        WHERE upper(p.service_name) = upper(nvl({requestParam.q},p.service_name))
      ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteRest'
    ,p_description        => 'Delete the given Rest Service'
    ,p_path               => '{SERVICE_NAME}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_rest_service_pkg.delete_service
            (p_service_name          => {pathParam.service_name}
            ,p_response_xml         => l_xml
            );
          
         l_resp := xxas_http_response
                      .ok(l_xml)
                      .build();
                      
         {response} := l_resp;

        END
      ]'
    );
    
END;
/
COMMIT;
EXIT;