/* $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 Org Chart DRs
  --
  l_sql :=
'        SELECT /*+ cardinality(hier 15)*/ 
               hier.hier_level
              ,CASE hier.entity_code
                 WHEN ''ASG'' 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)
                 WHEN ''POS'' THEN (SELECT pos.name
                                  FROM   hr_all_positions_f pos
                                  WHERE  pos.position_id = hier.primary_key_id
                                  AND    :pEffectiveDate BETWEEN
                                         pos.effective_start_date AND pos.effective_end_date)
                 WHEN ''VAC'' THEN (SELECT vac.name
                                  FROM   per_all_vacancies vac
                                  WHERE  vac.vacancy_id = hier.primary_key_id)
                 WHEN ''ORG'' THEN (SELECT hou.name
                                  FROM   hr_all_organization_units hou
                                  WHERE  hou.organization_id = hier.primary_key_id)
               END title
              ,CASE hier.entity_code
                 WHEN ''ASG'' THEN xxas_per_image_pkg.url
                                  (hier.person_id, img.last_update_date, ''MEDIUM'')
               END image_href
              ,decode(hier.entity_code, ''ASG''
                     ,xxas_com_asg_pkg.get_worker_type(hier.primary_key_id, :pEffectiveDate)
                     ,null) worker_type
              ,hier.person_id
              ,hier.entity_code
              ,hier.primary_key_id
              ,hier.parent_entity_code
              ,hier.parent_primary_key_id
              ,CASE hier.entity_code
                 WHEN ''ASG'' THEN xxas_org_views_pkg.get_assignment_views_csv(hier.primary_key_id)
                 WHEN ''POS'' THEN xxas_org_views_pkg.get_position_views_csv(hier.primary_key_id)
                 WHEN ''VAC'' THEN xxas_org_views_pkg.get_vacancy_views_csv(hier.primary_key_id)
                 WHEN ''ORG'' THEN xxas_org_views_pkg.get_org_views_csv(hier.primary_key_id)
               END available_views
              ,hier.dr_count
              ,decode(hier.dr_count, null, ''N'', ''Y'') dr_count_known
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_free_busy(hier.primary_key_id), null) free_busy
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_color(hier.primary_key_id), null) avail_color
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_availability(hier.primary_key_id), null) avail
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_description(hier.primary_key_id), null) avail_desc
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_update_datetime(hier.primary_key_id), null) avail_update
              ,decode(hier.entity_code, ''ASG'', xxas_com_avail_pkg.get_icon(hier.primary_key_id), null) avail_icon
        FROM   TABLE(xxas_com_hier_pkg.org_chart_drs_tbl(:pChartType, :pEntityCode, :pPrimaryKeyId, :pEffectiveDate)) hier
              ,per_all_people_f papf
              ,xxas_per_thumbnails img
        WHERE  hier.person_id = papf.person_id(+)
        AND    hier.person_id = img.person_id(+)
        AND    :pEffectiveDate BETWEEN
               papf.effective_start_date(+) AND papf.effective_end_date(+)
        ORDER BY 1, papf.last_name DESC, 2 DESC ';

  xxas_com_sr_pkg.seed_query_service
    (p_service_name      => 'QueryOrgChartDRs'
    ,p_securing_function => 'XXAS_SRV_Q_ORG_CHART_DRS_PERM'
    ,p_sql               => l_sql
    ,p_parameters        => xxas_com_sr_param_varray
                              (xxas_com_sr_param_type('pChartType'         , xxas_com_sr_pkg.g_VARCHAR2)
                              ,xxas_com_sr_param_type('pEntityCode'        , xxas_com_sr_pkg.g_VARCHAR2)
                              ,xxas_com_sr_param_type('pPrimaryKeyId'      , xxas_com_sr_pkg.g_NUMBER)
                              ,xxas_com_sr_param_type('pEffectiveDate'     , xxas_com_sr_pkg.g_DATE)));

END;
/
COMMIT;
EXIT;
