/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2015 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
                       := 'JobApplications';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View and maintain job applications'
    ,p_path               => 'job-applications'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'WithdrawJobApplication'
    ,p_description        => 'Withdraws the given job application'
    ,p_methods            => 'POST'
    ,p_path               => '{assignmentId}'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp        xxas_http_resp_obj;
          l_resp_body   XMLType;
        BEGIN
       
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_EMP_CAND_WD_ACT', {pathParam.assignmentId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
            xxas_applicant_pkg.withdraw_application 
              (p_assignment_id => {pathParam.assignmentId});

            l_resp_body :=
              xxas_com_msg_pkg.get_success_message_xml
                (xxas_com_msg_type
                  (xxas_util_pkg.app, 'XXAS_CAND_WD_SUCCESS',null));
                  
            SELECT appendChildXML
                     (l_resp_body
                     ,'/data'
                     ,XMLElement("redirect", 'self'))
            INTO   l_resp_body
            FROM   dual;
        
            l_resp := xxas_http_response
                        .ok(l_resp_body)
                        .build();
          END IF;
         
         {response} := l_resp;

        END
      ]'
    );
    
END;
/
COMMIT;
EXIT;
