/* $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
                       := 'VacancyApply';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Vacancy Application Form page'
    ,p_path               => 'apply-page/{vacancyId}'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetApplicationFormPage'
    ,p_description        => 'Gets a vacancy application form'
    ,p_methods            => 'GET'
    ,p_plsql              =>
      q'[
      
        DECLARE
          l_resp    xxas_http_resp_obj;
          l_xml     XMLType;
          
          CURSOR csr_vac_name IS
          SELECT p.name
          FROM   per_all_vacancies v
                ,irc_posting_contents_vl p
          WHERE  v.vacancy_Id = {pathParam.vacancyId}
          AND    v.primary_posting_id = p.posting_content_id;
          
          l_vac_name irc_posting_contents_vl.name%TYPE;
          
        BEGIN

          IF  fnd_global.user_id = 6 THEN

            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();

          ELSIF xxas_applicant_pkg.application_exists(xxas_util_pkg.get_per_from_user(fnd_global.user_id)
                                                     ,{pathParam.vacancyId}) THEN

            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.BAD_REQUEST)
                        .entity(xxas_com_msg_pkg.get_warning_message_xml
                                 (xxas_com_msg_type(xxas_util_pkg.app, 'XXAS_CAND_ALREADY_APPLIED',NULL)))
                        .build();

          ELSE

            OPEN  csr_vac_name;
            FETCH csr_vac_name INTO l_vac_name;
            CLOSE csr_vac_name;
          
            l_xml := xxas_emp_menu_pkg.get_page_xml
                      (p_page_id          => 'vacancy-apply'
                      ,p_object_id        => {pathParam.vacancyId}
                      ,p_view_mode        => {requestParam.view});
          
            l_resp := xxas_http_response
                        .ok(l_xml)
                        .build();
                          
          END IF;
          
         {response} := l_resp;
         
       END
          
      ]'
    );
    
END;
/
COMMIT;
EXIT;
