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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'OtlTimecardEntry'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    ,p_path               => 'otl-timecard-entry');

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'OtlTimecardEntryForm'
    ,p_description        => 'Get OTL Timecard Entry Form'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'form'
    ,p_sql                =>
      q'[  
          SELECT xxas_otl_timecard_entry_pkg.entry_form
                   ({requestParam.personId}
                   ,{requestParam.startTime}
                   ,{requestParam.endTime}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'OtlTimecardEntryView'
    ,p_description        => 'Get OTL Timecard Entry View'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'view/{personId}/timecard/{timecardId}'
    ,p_sql                =>
      q'[
          SELECT xxas_otl_timecard_entry_pkg.entry_view
                   ({pathParam.personId}
                   ,{pathParam.timecardId}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'OtlTimecardEntryEdit'
    ,p_description        => 'Get OTL Timecard Entry Edit'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'edit/{personId}/timecard/{timecardId}'
    ,p_sql                =>
      q'[
          SELECT xxas_otl_timecard_entry_pkg.entry_edit
                   ({pathParam.personId}
                   ,{pathParam.timecardId}
                   ,{requestParam.times}
                   ,{requestParam.sourceBlock}
                   ,{requestParam.savedSearchId}) "data"
          FROM   dual
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteOTLTimecardEntry'
    ,p_description        => 'Deletes a OTL Timecard Entry'
    ,p_path               => '{personId}/timecard/{timecardId}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp          xxas_http_resp_obj;
          l_response_xml  XMLType;
        BEGIN

          -- Check access to delete the log entry

          IF xxas_com_sec_pkg.test_instance('XXAS_DT_OTL_TC_ENTRY_DELETE',{pathParam.personId},{pathParam.timecardId}) = 'Y' THEN

            l_response_xml := xxas_otl_timecard_entry_pkg.delete_timecard_entry
                                ({pathParam.personId}
                                ,{pathParam.timecardId}
                                ,replace({requestParam.sourceBlock},'_','$')
                                ,{requestParam.savedSearchId});

          END IF;

          l_resp := xxas_http_response
                      .ok(l_response_xml
                         )
                      .build();

          {response} := l_resp;

        END
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'RespondToDraftEntries'
    ,p_description        => 'Respond to one or more draft entries'
    ,p_path               => '{action}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE

          l_resp         xxas_http_resp_obj;
          l_request_xml  XMLType;
          l_response_xml XMLType;
          l_status       varchar2(20);

        BEGIN

          l_request_xml := {requestBody};

          l_response_xml:= xxas_otl_timecard_entry_pkg.bulk_draft_respond
                             (p_action       => {pathParam.action}
                             ,p_request_body => l_request_xml
                             ,p_status       => l_status);

          IF l_status = 'SUCCESS' THEN
              
              l_resp := xxas_http_response
                          .ok(l_response_xml
                             )
                          .build();
          ELSE

              l_resp := xxas_http_response
                          .status(500).entity(l_response_xml)
                          .build();
          END IF;

          {response} := l_resp;

        END
      ]'
    );

END;
/
COMMIT;
EXIT;
