/* $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
                       := 'RestMethod';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Rest Method'
    ,p_path               => 'rest-method'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
      
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetRestMethods'
    ,p_description        => 'Get Rest Service Methods list'
    ,p_path               => '{SERVICE_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
                     ("data"
                     ,XMLForest
                       ( CASE WHEN p.method='GET' THEN
                              'book'
                              WHEN p.method='PUT' THEN
                              'edit'
                              WHEN p.method='POST' THEN
                              'addthin'
                              WHEN p.method='DELETE' THEN
                              'delete'
                        END "icon"
                       ,CASE WHEN p.method='GET' THEN
                              'balanced'
                              WHEN p.method='PUT' THEN
                              'energized'
                              WHEN p.method='POST' THEN
                              'positive'
                              WHEN p.method='DELETE' THEN
                              'assertive'
                        END        "statusColor"
                       ,'flexible'       "type"
                       ,p.service_name    "id"
                       ,p.method_name    "line1"
                       ,p.method||' '||p.path_template  "line3"
                       ,'rest-method-view' "pageId"
                       ,p.service_name||'_'||p.method_name  "objectId"
                       ,'modal' "target"
                       )
                     )
                   )
                 )"data"
        FROM xxas_rest_methods_v p
        WHERE upper(p.service_name) = upper({pathParam.SERVICE_NAME})
      ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteRestMethod'
    ,p_description        => 'Delete the given Rest Method'
    ,p_path               => '{METHOD_NAME}/{SERVICE_NAME}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_rest_method_pkg.delete_method
            (p_service_name         => {pathParam.service_name}
            ,p_method_name          => {pathParam.method_name}
            ,p_response_xml         => l_xml
            );
          
         l_resp := xxas_http_response
                      .ok(l_xml)
                      .build();
                      
         {response} := l_resp;

        END
      ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetRestMethodName'
    ,p_description        => 'Get Rest Method Name'
    ,p_path               => 'getmethodname'
    ,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.method_name as "id"
                       ,p.method_name      as "label"
                       ,p.method_name      as "value"
                       )
                     )
                   )
                 )"data"
        FROM xxas_rest_methods_v p
        WHERE upper(p.method_name) = upper(nvl({requestParam.q},p.method_name))
      ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetDataDefinitionCode'
    ,p_description        => 'Get Data Definition Code'
    ,p_path               => 'getddcode'
    ,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.lob_code as "id"
                       ,p.lob_code      as "label"
                       ,p.lob_code      as "value"
                       )
                     )
                   )
                 )"data"
        FROM (select lob_code from xdo_lobs
              WHERE upper(lob_code) like upper('%'||{requestParam.q}||'%')
              UNION
              select lob_code from xdo_lobs
              where upper(lob_code) = upper(nvl({requestParam.q},lob_code))
              ) p
      ]'
    );
    
END;
/
COMMIT;
EXIT;