/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2010 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_sql varchar2(32000);

BEGIN

  --
  -- Register Query Person for iPhone
  --
  l_sql :=
'        SELECT papf.person_id
              ,xxas_util_pkg.get_formatted_name
                (papf.full_name, papf.global_name, papf.local_name, papf.first_name, papf.last_name) short_name
              ,null override_image_url
              ,(SELECT XMLAgg
                         (XMLElement
                           (CONTACT_DETAIL
                           ,XMLForest
                             (cd.contact_type_code
                             ,cd.contact_type_name
                             ,cd.contact_data)))
                FROM   TABLE(xxas_com_contact_pkg.contact_details_tbl
                              (:pPersonId, trunc(sysdate))) cd) contact_details
              ,XMLForest
                (xxas_com_avail_pkg.get_free_busy(paaf.assignment_id) free_busy
                ,xxas_com_avail_pkg.get_color(paaf.assignment_id) color
                ,xxas_com_avail_pkg.get_availability(paaf.assignment_id) status
                ,xxas_com_avail_pkg.get_description(paaf.assignment_id) description
                ,xxas_com_avail_pkg.get_update_datetime(paaf.assignment_id) last_update
                ) availability
              ,xxas_com_asg_pkg.get_job_title
                (paaf.assignment_id, trunc(sysdate)) job_title
              ,xxas_com_asg_pkg.get_location
                (paaf.assignment_id, trunc(sysdate)) location
              ,xxas_com_asg_pkg.get_location_address
                (paaf.assignment_id, trunc(sysdate)) location_address
              ,xxas_com_asg_pkg.get_latitude
                (paaf.assignment_id, trunc(sysdate)) latitude
              ,xxas_com_asg_pkg.get_longtitude
                (paaf.assignment_id, trunc(sysdate)) longtitude
              ,(SELECT /*+ cardinality(hier 1)*/
                 XMLAgg
                   (XMLElement
                     (MANAGER
                     ,XMLForest
                       (hier.person_id
                       ,xxas_util_pkg.get_formatted_name
                         (mgr.full_name, mgr.global_name, mgr.local_name, mgr.first_name, mgr.last_name) short_name
                       ,xxas_com_asg_pkg.get_job_title
                         (hier.assignment_id, trunc(sysdate)) job_title
                   )))
                FROM   TABLE(xxas_com_hier_pkg.mgrs_for_asg_tbl
                         (paaf.assignment_id, trunc(sysdate))) hier
                      ,per_all_people_f mgr
                WHERE  mgr.person_id = hier.person_id
                AND    trunc(sysdate) BETWEEN
                       mgr.effective_start_date AND mgr.effective_end_date) managers
              ,(SELECT /*+ cardinality(hier 1)*/
                 XMLAgg
                   (XMLElement
                     (DIRECT_REPORT
                     ,XMLForest
                       (hier.person_id
                       ,xxas_util_pkg.get_formatted_name
                         (mgr.full_name, mgr.global_name, mgr.local_name, mgr.first_name, mgr.last_name) short_name
                       ,xxas_com_asg_pkg.get_job_title
                         (hier.assignment_id, trunc(sysdate)) job_title
                   )))
                FROM   TABLE(xxas_com_hier_pkg.drs_for_asg_tbl
                         (paaf.assignment_id, trunc(sysdate))) hier
                      ,per_all_people_f mgr
                WHERE  mgr.person_id = hier.person_id
                AND    trunc(sysdate) BETWEEN
                mgr.effective_start_date AND mgr.effective_end_date) direct_reports
        FROM   per_all_people_f papf
              ,per_all_assignments_f paaf
        WHERE  papf.person_id = :pPersonId
        AND    papf.person_id = paaf.person_id
        AND  ((papf.current_employee_flag IS NOT NULL AND
               papf.current_employee_flag = ''Y'' AND
               paaf.assignment_type = ''E'') OR
              (papf.current_npw_flag IS NOT NULL AND
               papf.current_npw_flag = ''Y'' AND
               paaf.assignment_type = ''C''))
        AND    paaf.primary_flag IS NOT NULL
        AND    paaf.primary_flag = ''Y''
        AND    trunc(sysdate) BETWEEN
               papf.effective_start_date AND papf.effective_end_date 
        AND    trunc(sysdate) BETWEEN
               paaf.effective_start_date AND paaf.effective_end_date ';

  xxas_com_sr_pkg.seed_query_service
    (p_service_name      => 'QueryPersonforiPhone'
    ,p_securing_function => 'XXAS_SRV_Q_PER_IPHONE_PERM'
    ,p_sql               => l_sql
    ,p_authenticate      => TRUE
    ,p_parameters        => xxas_com_sr_param_varray
                              (xxas_com_sr_param_type('pPersonId', xxas_com_sr_pkg.g_NUMBER)));

END;
/
COMMIT;
EXIT;
