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

  BEGIN

    xxas_com_sr_pkg.set_rest_service
      (p_service_name       => l_SERVICE
      ,p_description        => 'Root service path'
      ,p_path               => '/'
      ,p_consumes           => 'application/xml'
      ,p_produces           => 'application/xml'
      );

    xxas_com_sr_pkg.set_rest_method
      (p_service_name       => l_SERVICE
      ,p_method_name        => 'Discover'
      ,p_description        => 'Browse options for this endpoint'
      ,p_methods            => 'GET'
      ,p_remove_root_ele    => true
      ,p_sql                =>
        q'[
          SELECT XMLElement
                  ("links"
                  ,XMLElement
                     ("globalMenu"
                     ,XMLElement
                       ("href"
                       ,xxas_com_rest_util_pkg.uri_from_path
                         ('menus/' || nvl(xxas_com_sec_pkg.menu_name_to_id('XXX_XXAS_GLOBAL_MENU')
                                         ,xxas_com_sec_pkg.menu_name_to_id('XXAS_GLOBAL_MENU')))))
                  ,CASE  WHEN xxas_util_pkg.chatbots_enabled_flag = 'Y' THEN
                   XMLElement
                     ("chatbots"
                     ,XMLElement("href", replace(xxas_cloud_pkg.root_api,'/api/'))
                     ,XMLElement("enabled", 'true'))
                   END
                  ,XMLElement
                     ("userMe"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('users/' || fnd_global.user_id)))
                  ,XMLElement
                    ("ariaLabels"
                    ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('arialabels/'))
                    )
                  ,XMLElement
                     ("logout"
                     ,XMLElement("href", xxas_com_sec_pkg.get_root_url || '/OA_HTML/OALogout.jsp')
                     ,CASE WHEN xxas_com_sec_pkg.use_sso_flag = 'Y' THEN
                             XMLElement("target", 'sso')
                      END
                      )
                  ,CASE WHEN nvl(fnd_profile.value('XXAS_COM_USE_PAGE_FOOTER'),'N') = 'Y' THEN
                    XMLElement
                        ("footer",
                              CASE WHEN fnd_profile.value('XXAS_FOOTER_REST_SERVICE') IS NOT NULL THEN
                              XMLElement("href", xxas_com_rest_util_pkg.uri_from_path(NVL(fnd_profile.value('XXAS_FOOTER_REST_SERVICE'),'footer')))
                              ELSE
                                CASE WHEN fnd_profile.value('XXAS_COM_CLOUD_TENANT_ID') IS NOT NULL AND fnd_profile.value('XXAS_COM_CLOUD_HOST') IS NOT NULL THEN
                                XMLElement("href",'https://'
                                    || fnd_profile.value('XXAS_COM_CLOUD_TENANT_ID')
                                    || '.'
                                    || fnd_profile.value('XXAS_COM_CLOUD_HOST')
                                    || '/api/footerComponent')
                                END
                              END)
                   END
                  ,XMLElement
                     ("deviceRegistration"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('user-devices')))
                  ,XMLElement
                     ("location"
                     ,XMLElement("href", 'https://acs.applaudsolutions.com/geoip'))
                  ,XMLElement
                     ("favorites"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('favorites/{objectType}/{objectId}?favorite={favorite}')))
                  ,XMLElement
                     ("resourceBundle"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('resource-bundle/' || userenv('LANG') || '/' || xxas_rslt_cache_pkg.get_xxas_msg_lud_md5)))
                  ,XMLElement
                     ("objects"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('{objectType}/{objectId}')))
                  ,XMLElement
                     ("pages"
                     ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('pages/{pageId}?id={objectId}&amp;view={view}'))
                     ,XMLElement
                       ("links"
                       ,XMLElement
                         ("vacancies"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('vacancies-page/{objectId}?view={view}')))
                       ,XMLElement
                         ("apply"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('apply-page/{objectId}?view={view}')))
                       ,XMLElement
                         ("person-home"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('person-home-page?id={objectId}&amp;view={view}')))
                       ,XMLElement
                         ("flexiview"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('flexiview-page/{objectId}')))
                       ,XMLElement
                         ("flexiedit"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('flexiedit-page/{objectId}')))
                       ,XMLElement
                         ("flexireview"
                         ,XMLElement("href", xxas_com_rest_util_pkg.uri_from_path('flexireview-page/{objectId}')))
                       )
                     )
                  ) "links"
                ,XMLElement
                  ("linked"
                  ,XMLElement
                    ("home"
                    ,XMLElement
                      ("data"
                      ,(CASE WHEN fnd_global.user_name = 'GUEST' THEN
                               xxas_com_rest_util_pkg.uri_from_app_route
                                 (fnd_profile.value('XXAS_COM_GUEST_PAGE'))
                        ELSE
                               xxas_com_rest_util_pkg.uri_from_app_route
                                 (fnd_profile.value('XXAS_COM_HOME_PAGE'))
                        END)
                      )
                    )
                  ,XMLElement
                    ("css"
                    ,XMLForest
                      (xxas_com_laf_pkg.css_href "data")
                     )
                  ,XMLElement
                    ("cssDirection"
                    ,xxas_com_mls_pkg.direction)
                  ,decode(xxas_com_sec_pkg.get_branding_image_src, null, null
                         ,XMLElement
                           ("customerLogo"
                           ,XMLElement
                             ("data", xxas_com_sec_pkg.get_branding_image_src))
                         )
                  ,decode(fnd_profile.value('XXAS_COM_BRANDING_FAVICON'), null, null
                         ,XMLElement
                           ("favIcon"
                           ,XMLElement
                             ("data", xxas_com_sec_pkg.get_favicon_src))
                         )
                  ) "linked"
                ,XMLElement
                  ("meta"
                  ,XMLElement
                    ("httpProxy"
                    ,XMLElement
                      ("PUT"
                      ,XMLElement("param", XMLElement("_method", 'PUT'))
                      ,XMLElement("as", 'POST')
                      )
                    ,XMLElement
                      ("DELETE"
                      ,XMLElement("param", XMLElement("_method", 'DELETE'))
                      ,XMLElement("as", 'POST')
                      )
                    )
				  ,XMLElement("checkForOutdatedBrowser", xxas_util_pkg.flag_to_boolean(nvl(fnd_profile.value('XXAS_CHECK_OUTDATED_BROWSERS'),'Y')))
                  ,XMLElement("language", xxas_com_mls_pkg.get_iso_lang_code)
                  ,XMLElement("useCountryFallbackForLocation", 'false')
                  ,XMLElement("authCookie", icx_sec.getSessionCookieName)
                  ,XMLElement("loggedIn", decode(fnd_global.user_name, 'GUEST', 'false', 'true'))
                  ,XMLType(xxas_rslt_cache_pkg.google_analytics_xml)
                  ,XMLElement("googleAnalyticsTrackingCode", fnd_profile.value('XXAS_COM_GOOGLE_ANALYTICS_CODE'))  
                  ,XMLElement("googleMapsApiKey", fnd_profile.value('XXAS_COM_GOOGLE_MAPS_KEY'))
                  ,XMLElement
                    ("emailValidation"
                    ,XMLElement("enabled", 'true')
                    ,XMLElement
                      ("links"
                      ,XMLElement
                        ("href"
                        ,'https://acs.applaudsolutions.com/verifyEmail/{email}'))
                    )
                  ,CASE WHEN 1=2 AND xxas_util_pkg.chatbots_enabled_flag = 'Y' THEN
                    XMLElement
                      ("chatbot"
                      ,XMLElement("title", xxas_com_msg_pkg.get_string('XXAS_COM_ASSISTANCE'))
                      ,XMLElement("greeting", xxas_com_msg_pkg.get_string('XXAS_COM_CHAT_GREETING'))
                      ,XMLElement("prompt", xxas_com_msg_pkg.get_string('XXAS_COM_CHAT_PROMPT'))
                      ,XMLElement("url", xxas_cloud_pkg.root_api || 'lex')
                      ,XMLElement("host",'https://'||fnd_profile.value('XXAS_COM_CLOUD_TENANT_ID')||'.'||fnd_profile.value('XXAS_COM_CLOUD_HOST'))
                      )
                   END
                  ,CASE WHEN fnd_profile.value('XXAS_COM_GOOGLE_TAG_MANAGER') is not null
                   THEN
                       XMLElement 
                        ("googleTagManager"  
                        ,XMLElement
                           ("codes"
                              ,XMLAttributes
                                   ('true' as "json:force-array"
                                   ,'http://json.org/' as "xmlns:json")      
							           ,XMLForest
                                   (					
                                    fnd_profile.value('XXAS_COM_GOOGLE_TAG_MANAGER') "x"  ) 
                              )       		 					   
                          )
                  END  
                  ,XMLElement
                    ("preloadRoutes"
                    ,XMLAttributes
                       ('true' as "json:force-array"
                       ,'http://json.org/' as "xmlns:json")
                    ,XMLElement
                      ("route"
                      ,xxas_com_rest_util_pkg.uri_from_path('pages/search'))
                    ,XMLElement
                      ("route"
                      ,xxas_com_rest_util_pkg.uri_from_path('blocks/search'))
                    ,XMLElement
                      ("route"
                      ,xxas_com_rest_util_pkg.uri_from_path('pages/login'))
                    ,XMLElement
                      ("route"
                      ,xxas_com_rest_util_pkg.uri_from_path('blocks/login'))
                    ,XMLElement
                      ("route"
                      ,xxas_com_rest_util_pkg.uri_from_path
                        ('users/' || fnd_global.user_id))
                    ,(CASE WHEN xxas_com_sec_pkg.test_instance
                                  ('XXAS_COM_VUE_BETA',fnd_global.user_id) = 'Y' AND
                                xxas_com_ctx_pkg.client_gte_version_flag('5') = 'N' AND
                                upper(xxas_com_ctx_pkg.get_device_type) = 'DESKTOP' AND 
                                nvl(fnd_profile.value('XXAS_OVER_LANGUAGE'), userenv('LANG')) = 'US'
                           THEN XMLElement
                                 ("route"
                                 ,xxas_com_rest_util_pkg.uri_from_path
                                    ('vue-beta/messages'))
                           ELSE null
                     END)
                    )
                    ,XMLElement
                      ("options"
                      ,XMLElement
                        ("allowBiometricAuth"
                        ,decode(xxas_com_sec_pkg.allow_bio_auth_flag, 'Y', 'true', 'false'))
                      ,XMLElement
                        ("oracleFormsLoader", 'true')
                      ,XMLElement
                        ("downloadProgressMessage", xxas_com_msg_pkg.get_string('XXAS_COM_DOWNLOAD_PROGRESS'))
					  ,XMLElement
							("cancelLabel", xxas_com_msg_pkg.get_string('XXAS_RB_COM_CANCEL_EDIT_U'))
					  ,XMLElement
							("okLabel", xxas_com_msg_pkg.get_string('XXAS_RB_COM_OK'))
                      ,XMLElement
                        ("search"
                        ,XMLForest
                          (CASE xxas_emp_menu_pkg.has_access_to_page('search',fnd_global.employee_id) 
						               WHEN 'Y'
    						           THEN 
						                    lower(nvl(fnd_profile.value('XXAS_GLOBAL_SEARCH'), 'NORMAL'))
								           ELSE 
								                'hidden'
                           END	 "state"
                          ,xxas_search_pkg.search_prompt "text"
                          ,xxas_com_msg_pkg.get_string('XXAS_RB_COM_CAN') "cancelText"
                          )
                        )
                        --
                       ,xxas_com_rest_util_pkg.get_page_background_xml(fnd_profile.value('XXAS_COM_PG_BG_IMAGE')) 
                        
                      )
                        ,XMLElement
                         ("ios"
                          ,XMLElement
                            ("touchId"
                                ,XMLElement
                                  ("setup"
                                    ,XMLElement
                                      ("title",xxas_com_msg_pkg.get_string('XXAS_COM_TOUCH_ID_TITLE') )
                                    ,XMLElement
                                      ("description",xxas_com_msg_pkg.get_string('XXAS_COM_TOUCH_ID_DESC') ) )
                                ,XMLElement
                                  ("reLogin"
                                    ,XMLElement
                                      ("title",xxas_com_msg_pkg.get_string('XXAS_COM_RELOGIN') )
                                    ,XMLElement
                                      ("description",xxas_com_msg_pkg.get_string('XXAS_COM_RELOGIN_DESC') ) ) )
                                ,XMLElement
                                  ("faceId"
                                    ,XMLElement
                                      ("setup"
                                        ,XMLElement
                                          ("title",xxas_com_msg_pkg.get_string('XXAS_COM_FACE_ID_TITLE'))
                                        ,XMLElement
                                          ("description",xxas_com_msg_pkg.get_string('XXAS_COM_FACE_ID_DESC')) ) ) )   
                        ,XMLElement
                         ("android"
                           ,XMLElement
                            ("fingerprint"
                                ,XMLElement
                                  ("setup"
                                    ,XMLElement
                                      ("title", xxas_com_msg_pkg.get_string('XXAS_COM_ANDROID_TITLE') )
                                    ,XMLElement
                                      ("description",xxas_com_msg_pkg.get_string('XXAS_COM_ANDROID_DESC') ) )
                            ,XMLElement
                              ("reLogin"
                                    ,XMLElement
                                      ("title",xxas_com_msg_pkg.get_string('XXAS_COM_RELOGIN'))
                                    ,XMLElement
                                      ("description",xxas_com_msg_pkg.get_string('XXAS_COM_RELOGIN_ANDROID_DESC')) ) ) 
                                
                      )
                  ) 
          FROM dual
          ]'
      );
      
    xxas_com_sr_pkg.set_rest_method
      (p_service_name       => l_SERVICE
      ,p_method_name        => 'Connect'
      ,p_description        => 'Ping service to check if server is alive'
      ,p_path               => 'connect'
      ,p_methods            => 'GET'
      ,p_sql                =>
        q'[
          SELECT 'true' "alive"
          FROM   dual 
          ]'
      );
      
    xxas_com_sr_pkg.set_rest_method
      (p_service_name       => l_SERVICE
      ,p_method_name        => 'Startup'
      ,p_description        => 'Browse options for this endpoint'
      ,p_path               => 'startup'
      ,p_methods            => 'GET'
      ,p_sql                =>
        q'[
          SELECT XMLElement
                   ("STARTUP"
                   ,XMLElement
                     ("PARAMETERS"
                     ,XMLForest
                      (fnd_global.user_id
                      ,fnd_global.employee_id person_id
                      ,fnd_global.resp_id responsibility_id
                      ,fnd_global.resp_appl_id responsibility_application_id
                      ,fnd_global.security_group_id
                      ,xxas_util_pkg.flag_to_boolean(decode(fnd_global.user_id, 6, 'N', 'Y')) logged_in
                      ,nvl(fnd_profile.value('XXX_FND_SSO_LOCAL_LOGIN_MASK'), fnd_profile.value('FND_SSO_LOCAL_LOGIN_MASK')) login_personalization
                      ,nvl(fnd_profile.value('XXAS_GLOBAL_SEARCH'), 'NORMAL') searchstate
                      ,fnd_profile.value('SIGNON_PASSWORD_FAILURE_LIMIT') password_attempts
                      ,decode
                          (fnd_global.user_id
                          ,6
                          ,null
                          ,xxas_com_rest_util_pkg.uri_from_path('/users/' || fnd_global.user_id || '/saved-searches')) saved_searches_href
                      ,xxas_com_rest_util_pkg.uri_from_path('/time-types') time_types_href
                      ,xxas_com_rest_util_pkg.uri_from_path('/people/' || fnd_global.employee_id || '/time-options') time_options_href
                      ,decode(fnd_profile.value('XXAS_AUTH_CUSTOM_CLASS')
                             ,null, 'N'
                             ,nvl(fnd_profile.value('XXAS_AUTH_USE_2FA'),'N')) use_two_factor_auth
                      ,'Y' using_new_session_mgr
                      ,  CASE
              WHEN EXISTS
                (SELECT 1
                FROM fnd_form_functions fn
                WHERE fn.function_name = 'UMX_LOGIN_HELP'
                )
              THEN
                (SELECT fnd_run_function.get_run_function_url (fn.function_id ,fnd_global.resp_appl_id ,fnd_global.resp_id ,fnd_global.security_group_id)
                FROM fnd_form_functions fn
                WHERE fn.function_name = 'UMX_LOGIN_HELP'
                )
              ELSE
                (SELECT fnd_run_function.get_run_function_url (fn.function_id ,fnd_global.resp_appl_id ,fnd_global.resp_id ,fnd_global.security_group_id)
                FROM fnd_form_functions fn
                WHERE fn.function_name = 'UMX_FORGOT_PWD'
                )
              END password_reset_url
                      ,xxas_com_sec_pkg.get_root_url || '/OA_HTML/OALogout.jsp' logout_url
                      ,(CASE WHEN fnd_profile.value('XXAS_SSO_LOGIN_URL') IS NULL THEN
                               'false'
                             WHEN fnd_profile.value('XXAS_AUTH_CUSTOM_CLASS') IS NOT NULL THEN
                               'false'
                             ELSE
                               'true'
                        END) redirect_to_sso_login_url
                      ,fnd_profile.value('XXAS_SSO_LOGIN_URL') sso_login_url
                      ,fnd_profile.value('UPLOAD_FILE_SIZE_LIMIT') upload_size_limit
                      ,nvl(fnd_profile.value('XXAS_COM_BRANDING_IMAGE')
                          ,fnd_profile.value('FND_CORPORATE_BRANDING_IMAGE')) branding_image
                      ,fnd_profile.value('ICX_SESSION_TIMEOUT') session_timeout_mins
                      ,nvl(fnd_profile.value('FND_DISABLE_LOOK_AHEAD_LOV'), 'N') auto_complete_disable
                      ,nvl(fnd_profile.value('LOOK_AHEAD_MIN_CHARS'), 3) auto_complete_chars
                      ,xxas_com_mls_pkg.get_iso_date_mask iso_date_mask
                      ,fnd_profile.value('ICX_NUMERIC_CHARACTERS') numeric_characters
                      ,280 auto_complete_wait_delay_ms
                      ,'Y' auto_update_desktop_apps
                      ,'Y' track_usage
                      ,'Y' support_submenus
                      ,'Y' support_rtl_phone
                      ,'N' support_rtl_tablet
                      ))
                  ,(SELECT XMLElement
                             ("LOCALES"
                             ,XMLAttributes
                             ('true' as "json:force-array"
                             ,'http://json.org/' as "xmlns:json")
                             ,XMLAgg
                               (XMLElement
                                 (LOCALE
                                 ,XMLForest
                                   (l.language_code orcl_code
                                   ,lower(l.iso_language) || '_' || upper(l.iso_territory) code
                                   ,l.description
                                   ,xxas_com_rest_util_pkg.uri_from_path('/xxas-rb/' || l.language_code || '/' || xxas_rslt_cache_pkg.get_xxas_msg_lud_md5) resources
                                   ,decode
                                     (l.language_code
                                     ,nvl(fnd_profile.value('XXAS_OVER_LANGUAGE'), userenv('LANG'))
                                     ,'Y'
                                     ,'N') current_flag
                                   ,nvl2(xxas_util_pkg.decode_lookup('XXAS_COM_RTL_LANGS', l.language_code), 'rtl', 'ltr') writing_direction)) ORDER BY description))
                    FROM   fnd_languages_vl l
                    WHERE  l.installed_flag IN ('B', 'I'))
                  ,XMLElement
                    ("RESOURCES"
                    ,xxas_com_rest_util_pkg.uri_from_path('/xxas-rb/' || userenv('LANG') || '/' || xxas_rslt_cache_pkg.get_xxas_msg_lud_md5))
                  ,(SELECT XMLElement
                             ("STYLES"
                             ,XMLAttributes
                             ('true' as "json:force-array"
                             ,'http://json.org/' as "xmlns:json")  														 
                             ,XMLAgg
                               (XMLElement
                                 ("STYLE"
                                 ,XMLForest
                                   (s.style_code
                                   ,s.style_name
                                   ,s.style_value))))
                    FROM   TABLE(xxas_com_laf_pkg.get_styles) s)
                  ) DATA
           FROM dual
          ]'
      );
      
    xxas_com_sr_pkg.set_rest_method
      (p_service_name       => l_SERVICE
      ,p_method_name        => 'xxasResources'
      ,p_produces           => 'application/xml'
      ,p_description        => 'Get resources for the given identifier'
      ,p_path               => 'xxas-rb/{langCode}/{id}'
      ,p_methods            => 'GET'
      ,p_sql                =>
        q'[
          SELECT xxas_http_response.ok()
                   .expires(add_months(sysdate, 12))
                   .entity
                     (XMLElement
                       ("RESOURCES"
                       ,XMLAgg
                         (XMLElement
                           ("RESOURCE"
                           ,XMLForest
                             (r.key
                             ,r.value)))))
                   .build() "httpResponse"
          FROM   TABLE(xxas_com_mls_pkg.get_resources(null,{pathParam.langcode})) r
          ]'
      );
      
    xxas_com_sr_pkg.set_rest_method
      (p_service_name       => l_SERVICE
      ,p_method_name        => 'Resources'
      ,p_produces           => 'application/xml'
      ,p_description        => 'Get resources for the given identifier'
      ,p_path               => 'resource-bundle/{langCode}/{id}'
      ,p_methods            => 'GET'
      ,p_sql                =>
        q'[
          SELECT xxas_http_response.ok()
                   .expires(add_months(sysdate, 12))
                   .entity
                     (XMLElement
                       ("data"
                       ,XMLAgg
                         (XMLElement
                           (evalname(lower(r.key))
                           ,r.value))))
                   .build() "httpResponse"
          FROM   TABLE(xxas_com_mls_pkg.get_resources(null,{pathParam.langcode})) r
          ]'
      );
      
  END;
  /
  COMMIT;
  EXIT;