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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Application users'
    ,p_path               => 'users/{userId}'
    ,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
                 ("data"
                 ,XMLForest
                   (rslt.user_id "id"
                   ,CASE WHEN fnd_global.user_id = 6 THEN
                           xxas_com_msg_pkg.get_string('XXAS_COM_GUEST_GREETING')
                         WHEN papf.person_id IS NOT NULL THEN
                           nvl(papf.known_as, papf.first_name)
                         ELSE
                           rslt.user_name 
                    END  "greeting"
                   ,CASE WHEN fnd_global.user_id = 6 THEN
                           xxas_com_msg_pkg.get_string('XXAS_COM_GUEST_NAME')
                         WHEN papf.person_id IS NOT NULL THEN
                           xxas_util_pkg.get_formatted_name
                             (papf.full_name, papf.global_name, papf.local_name
                             ,papf.first_name, papf.last_name, papf.known_as)
                         ELSE
                           rslt.user_name 
                    END  "name"
                   )
                 ,XMLElement
                   ("links"
                   ,XMLElement
                     ("pages"
                     ,decode
                       (fnd_global.user_id, 6, null
                       ,XMLElement
                          ("href"
                          ,xxas_com_rest_util_pkg.uri_from_path('pages')))
                     ,decode
                       (fnd_global.user_id, 6
                       ,XMLElement
                         ("login"
                         ,XMLElement
                           ("href"
                           ,xxas_com_rest_util_pkg.uri_from_path('login-page'))
                         )
                       ,null
                       )
                     )
                   )
                 ,XMLElement
                   ("linked"
                   ,decode
                     (fnd_global.user_name
                     ,'GUEST'
                     ,XMLElement
                       ("image"
                       ,XMLForest
                          (xxas_com_rest_util_pkg.uri_from_path('people/-1/photo') "self"
                          ,xxas_per_image_pkg.url(null, null, 'SMALL') "small"
                          ,xxas_per_image_pkg.url(null, null, 'MEDIUM') "medium"
                          ,xxas_per_image_pkg.url(null, null, 'FULL_SIZE') "fullSize"
                          )
                        )
                     ,XMLElement
                       ("image"
                       ,xxas_com_rest_mgr_pkg.get_xml_entity
                         ('/people/' || papf.person_id || '/photo')))
                   ,xxas_emp_menu_pkg.user_menu_xml
                   ,xxas_emp_menu_pkg.quick_links_xml
                   ,xxas_emp_menu_pkg.global_menu_xml
                   ,decode
                     (fnd_global.user_name, 'GUEST', null
                     ,XMLElement
                        ("menu"
                        ,xxas_com_rest_mgr_pkg.get_xml_entity
                           ('/menus/' || nvl(xxas_com_sec_pkg.menu_name_to_id('XXX_XXAS_GLOBAL_USER_MENU')
                                            ,xxas_com_sec_pkg.menu_name_to_id('XXAS_GLOBAL_USER_MENU')))))
                   )
                 ) "data"
        FROM   fnd_user rslt
              ,(SELECT p.*
                      ,img.last_update_date img_last_update_date
                FROM   per_all_people_f p
                      ,xxas_per_thumbnails img
                WHERE  p.person_id = img.person_id(+)) papf
        WHERE  rslt.user_id = {pathParam.userId}
        AND    rslt.employee_id = papf.person_id(+)
        AND    trunc(sysdate) BETWEEN
               papf.effective_start_date(+) AND papf.effective_end_date(+)
        AND   {authFunctionPredicate.XXAS_FND_USER_VIEW}
        ]'
    );
    
END;
/
COMMIT;
EXIT;
