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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Objects'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindObject'
    ,p_description        => 'Find existing objects matching the given criteria'
    ,p_path               => '/objects'
    ,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
                     ("object"
                     ,XMLForest
                       (rslt.object_id    "id"
                       ,rslt.display_name "value"
                       ,rslt.display_name "label")
                     )
                   )
                 ) "data"
        FROM   fnd_objects_vl rslt
        WHERE  UPPER(rslt.display_name) like UPPER('%' || {requestParam.q} || '%')
      ]'
    );
	
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindDataObjectColumns'
    ,p_description        => 'Find Data Objects columns'
    ,p_path               => '/data-object-columns/{dataobjId}'
    ,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
                     ("obj"
                     ,XMLForest
                       (rslt.column_name "id"
                       ,rslt.column_name "value"
                       ,rslt.column_name "label"
                       )
                     )
                   )
                 ) "data"
          FROM  (SELECT column_name 
                 FROM   all_tab_columns t
                       ,fnd_objects f  
		 WHERE  t.table_name = f.database_object_name
                 AND    f.object_id = {pathParam.dataobjId}
                 ORDER BY column_name) rslt
        ]' 
	) ;

END;
/
COMMIT;
EXIT;
