/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2017 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
                       := 'CalendarEvents';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Calendar Events of Absences and Time Cards'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    ,p_path               => 'calendar-events');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetIndividualCalendarEntries'
    ,p_description        => 'Get the formatted Individual Calendar Entries'
    ,p_path               => 'for-individual/{personId}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT extract
                 (xxas_cal_event_pkg.individual_cal_items
                   ({pathParam.personId}
                   ,fnd_date.canonical_to_date({requestParam.start})
                   ,fnd_date.canonical_to_date({requestParam.end})
                   ,'N'
                   ,{requestParam.includeAbs}
                   ,{requestParam.includeTCM}
                   ,{requestParam.includeTrn}
                   ,{requestParam.includeBD}
                   ,{requestParam.includeWA}
                   ,{requestParam.includePH}
                   ,{requestParam.includePayDates}
                   ,{requestParam.includeOTLH}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.comEventsResMsg}
                   ,{requestParam.includeOTL}
				   ,NVL({requestParam.includeMC},'N')
				   ,NVL({requestParam.includeClockedIn},'N')
				   ,nvl({requestParam.excludeTermBy},'FPD'))
                 ,'/data/*') "data"
        FROM   dual
        WHERE  {pathParam.personId} is not null
        AND    {requestParam.sourceBlock} is not null
        AND    xxas_com_sec_pkg.test_instance
                 ({requestParam.sourceBlock}
                 ,to_number({pathParam.personId})
                 ,null
                 ,null
                 ,null
                 ,null) = 'Y'
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetTeamCalendarEntries'
    ,p_description        => 'Get the formatted Team Calendar Entries'
    ,p_path               => 'for-team/{managerPersonId}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT extract
                 (xxas_cal_event_pkg.team_cal_items
                   ({pathParam.managerPersonId}
                   ,fnd_date.canonical_to_date({requestParam.start})
                   ,fnd_date.canonical_to_date({requestParam.end})
                   ,{requestParam.savedSearchId}
                   ,'N'
                   ,{requestParam.includeAbs}
                   ,{requestParam.includeTCM}
                   ,{requestParam.includeTrn}
                   ,{requestParam.includeBD}
                   ,{requestParam.includePH}
                   ,{requestParam.includePayDates}
                   ,{requestParam.includeOTLH}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.comEventsResMsg}
                   ,{requestParam.includeOTL}
				   ,NVL({requestParam.includeMC},'N')
				   ,nvl({requestParam.excludeTermBy},'FPD')
				   ,NVL({requestParam.includeClockedIn},'N')
				   ,nvl({requestParam.asgsFilterFlag},'N')
				   ,{requestParam.otlAsgAttrCat}
				   ,{requestParam.otlAsgAttribute}
				   ,nvl({requestParam.showSecondaryAsg},'Y'))				   
                 ,'/data/*') "data"
        FROM   dual
        WHERE  {pathParam.managerPersonId} is not null
        AND    ({requestParam.savedSearchId} is null
                OR ({requestParam.savedSearchId} is not null 
                    AND xxas_com_sec_pkg.test_instance
                          ('XXAS_DATA_SAVED_SEARCH_VIEW'
                          ,{requestParam.savedSearchId}) = 'Y')
               )
        AND    {requestParam.sourceBlock} is not null
        AND    xxas_com_sec_pkg.test_instance
                 ({requestParam.sourceBlock}
                 ,to_number({pathParam.managerPersonId})
                 ,null
                 ,null
                 ,null
                 ,null) = 'Y'
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetPeerCalendarEntries'
    ,p_description        => 'Get the formatted Peer Calendar Entries'
    ,p_path               => 'for-peer/{personId}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT extract
                 (xxas_cal_event_pkg.peer_cal_items
                   ({pathParam.personId}
                   ,fnd_date.canonical_to_date({requestParam.start})
                   ,fnd_date.canonical_to_date({requestParam.end})
                   ,'N'
                   ,{requestParam.includeAbs}
                   ,{requestParam.includeTCM}
                   ,{requestParam.includeTrn}
                   ,{requestParam.includeBD}
                   ,{requestParam.includePH}
                   ,{requestParam.includePayDates}
                   ,{requestParam.includeOTLH}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.comEventsResMsg}
                   ,{requestParam.includeOTL}
				   ,nvl({requestParam.excludeTermBy},'FPD')
				   ,nvl({requestParam.asgsFilterFlag},'N'))
                 ,'/data/*') "data"
        FROM   dual
        WHERE  {pathParam.personId} is not null
        AND    {requestParam.sourceBlock} is not null
        AND    xxas_com_sec_pkg.test_instance
                 ({requestParam.sourceBlock}
                 ,to_number({pathParam.personId})
                 ,null
                 ,null
                 ,null
                 ,null) = 'Y'
      ]'
    );

END;
/
COMMIT;
EXIT;