/* $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
                       := 'FormField';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'List Providers' 
    ,p_path               => 'form-field'    
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindContainers'
    ,p_description        => 'Finds containers'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'form-containers/{formId}'
    ,p_sql                =>
      q'[  SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("container"
                     ,XMLForest
                       (rslt.field_id "id"
                       ,rslt.field_id "value"
                       ,rslt.field_id "label"
                       ,'flexible' "type"
                       ,rslt.field_id "line1")
                     )
                   )
                 ) "data"
           from XMLTable('/Container' 
                  PASSING( NVL(XDBuriTYPE(xxas_forms_pkg.form_xdb_uri({pathParam.formId})).GETxml().extract('//EditFields/Container'),XDBuriTYPE(xxas_forms_pkg.form_xdb_uri({pathParam.formId})).GETxml().extract('//CreateFields/Container')) )
                    COLUMNS field_id VARCHAR2(4000) path '@id' ) rslt
      ]'
    );     
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindValuesets'
    ,p_description        => 'Finds All Value Sets'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'valuesets/{listProviderType}'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("valueset"
                     ,XMLForest
                       (rslt.flex_value_set_name "id"

                       ,rslt.flex_value_set_name "value"
                       ,rslt.flex_value_set_name "label"
                       ,'flexible' "type"
                       ,rslt.flex_value_set_name "line1")
                     )
                   )
                 ) "data" from 
          (SELECT rslt.flex_value_set_name FROM fnd_flex_value_sets rslt
          WHERE
          {requestParam.q} IS NOT NULL AND rslt.flex_value_set_name LIKE UPPER('%'||{requestParam.q}||'%')
          UNION
          SELECT flex_value_set_name
            FROM fnd_flex_value_sets
            WHERE INSTR({pathParam.listProviderType},'VALUE') > 0 AND flex_value_set_name = SUBSTR({pathParam.listProviderType},INSTR({pathParam.listProviderType},':')+1)) rslt
      ]'
    ); 

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindListValues'
    ,p_description        => 'Finds All Value Sets'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'list-values/{listProvider}'
    ,p_sql                =>
      q'[ SELECT xxas_forms_pkg.get_list_values({pathParam.listProvider}) "data"
          FROM DUAL
      ]'
    );

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindListValuesedit'
    ,p_description        => 'Finds All Values'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'list-values-edit/{listProvider}'
    ,p_sql                =>
      q'[ SELECT xxas_forms_pkg.get_list_values_edit({pathParam.listProvider}) "data"
          FROM DUAL
      ]'
    ); 
    	
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindAttachmentEntities'
    ,p_description        => 'Finds Attachment Entities'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'attachment-entities'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("entity"
                     ,XMLForest
                       (rslt.document_entity_id "id"

                       ,rslt.data_object_code "value"
                       ,rslt.data_object_code "label"
                       ,'flexible' "type"
                       ,rslt.data_object_code "line1")
                     )
                   )
                 ) "data" from fnd_document_entities rslt
          WHERE
            rslt.data_object_code LIKE UPPER('%'||{requestParam.q}||'%')
      ]'
    ); 
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindForms'
    ,p_description        => 'Finds forms for multirow'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'form-paths'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("entity"
                     ,XMLForest
                       (rslt.any_path "id"

                       ,rslt.title "value"
                       ,rslt.title "label"
                       ,'flexible' "type"
                       ,rslt.title "line1")
                     )
                   )
                 ) "data" from xxas_forms_v rslt
          
      ]'
    );     

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindCategories'
    ,p_description        => 'Finds Attachment Category Code'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'category-code'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("category"
                     ,XMLForest
                       (--rslt.category_id "id"
                        rslt.name "id"
                       ,rslt.name "value"
                       ,rslt.name "label"
                       ,'flexible' "type"
                       ,rslt.name "line1")
                     )
                   )
                 ) "data" from fnd_document_categories rslt
          WHERE
            rslt.name LIKE UPPER('%'||{requestParam.q}||'%')
      ]'
    ); 
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindLookups'
    ,p_description        => 'Finds All HR Lookups'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'lookups/{listProviderType}'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("lookup"
                     ,XMLForest
                       (rslt.lookup_type "id"

                       ,rslt.lookup_type "value"
                       ,rslt.lookup_type "label"
                       ,'flexible' "type"
                       ,rslt.lookup_type "line1")
                     )
                   )
                 ) "data" from 
                 (SELECT lookup_type FROM xxas_lookup_types_list_v
                  WHERE
            ({requestParam.q} IS NOT NULL AND {requestParam.q} &lt;&gt; '{searchText}' AND lookup_type LIKE UPPER('%'||{requestParam.q}||'%'))
            UNION
            SELECT lookup_type FROM xxas_lookup_types_list_v
                  WHERE
              INSTR({pathParam.listProviderType},'LOOKUP') > 0 AND lookup_type = SUBSTR({pathParam.listProviderType},INSTR({pathParam.listProviderType},':')+1)
            )rslt
                 
                 --xxas_lookup_types_list_v rslt 
          --WHERE
           -- ({requestParam.q} IS NOT NULL AND {requestParam.q} &lt;&gt; '{searchText}' AND rslt.lookup_type LIKE UPPER('%'||{requestParam.q}||'%'))
            --OR
            --(INSTR({pathParam.listProviderType},'LOOKUP') > 1 AND rslt.lookup_type = SUBSTR({pathParam.listProviderType},INSTR({pathParam.listProviderType},':')+1))
           -- OR ({requestParam.q} IS NOT NULL AND rslt.lookup_code LIKE UPPER('%'||{requestParam.q}||'%'))
            
      ]'
    );   
 
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindAttachmentEntities'
    ,p_description        => 'Finds All Attachment Entities'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'attachment-entities'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("lookup"
                     ,XMLForest
                       (rslt.data_object_code "id"

                       ,rslt.data_object_code "value"
                       ,rslt.data_object_code "label"
                       ,'flexible' "type"
                       ,rslt.data_object_code "line1")
                     )
                   )
                 ) "data" from fnd_document_entities rslt             
      ]'
    ); 
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindFlexfields'
    ,p_description        => 'Finds All Flexfields in a FND Application'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'flexfield/{applicationId}'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("flexfield"
                     ,XMLForest
                       (rslt.flexfield_name "id"
                       ,rslt.application_id
                       ,rslt.flexfield_name "value"
                       ,rslt.flexfield_name "label"
                       ,'flexible' "type"
                       ,rslt.flexfield_name "line1")
                     )
                   )
                 ) "data" from xxas_flexfield_list_v rslt
                 WHERE ({pathParam.applicationId} IS NOT NULL and rslt.application_id = {pathParam.applicationId})
                 AND UPPER(rslt.flexfield_name) LIKE '%'||UPPER({requestParam.q})||'%'
                                               
               
             
      ]'
    );        

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindFlexCodes'
    ,p_description        => 'Finds All flex codes in a KFF'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'flex-code/{flexfield}'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("flexcode"
                     ,XMLForest
                       (rslt.id_flex_code "id"
                       ,rslt.id_flex_structure_name
                       ,rslt.id_flex_code "value"
                       ,rslt.id_flex_code "label"
                       ,'flexible' "type"
                       ,rslt.id_flex_code "line1")
                     )
                   )
                 ) "data" from FND_ID_FLEX_STRUCTURES_VL rslt
                 WHERE ({pathParam.flexfield} IS NOT NULL and rslt.id_flex_structure_name = {pathParam.flexfield})
             
      ]'
    );        

    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindApplications'
    ,p_description        => 'Finds all the installed FND Applications'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'applications'
    ,p_sql                =>
      q'[ SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("application"
                     ,XMLForest
                       (rslt.application_id "id"
                       ,rslt.application_name "value"
                       ,rslt.application_name "label"
                       ,'flexible' "type"
                       ,rslt.application_name "line1")
                     )
                   )
                 ) "data" from FND_APPLICATION_VL rslt
          WHERE upper(rslt.application_name) LIKE '%'||UPPER({requestParam.q})||'%'
      ]'
    ); 

    
END;
/
COMMIT;
EXIT;
