/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2014 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
                       := 'ApplicantListStatuses';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View available applicant statuses'
    ,p_path               => '/applicants/{applicantList}/statuses'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetApplicantListStatuses'
    ,p_description        => 'Get a list of allowed statuses for a given applicant list'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT XMLElement
                   ("data"
                   ,XMLAttributes
                     ('true' as "json:force-array"
                     ,'http://json.org/' as "xmlns:json")
                   ,XMLAgg
                     (XMLElement
                       ("status"
                       ,XMLForest
                         (
						  p.assignment_status_type_id "id"
                         ,p.system_meaning "description"
                         ,p.customer_status "label"                         
                         )
                       )                     
                     )
                   ) AS "data"
          FROM(  SELECT distinct rslt.assignment_status_type_id , rslt.system_meaning,rslt.customer_status
		         FROM
		         xxas_applicant_statuses_v rslt,
		         TABLE(xxas_util_pkg.string_to_varchar2tbl({pathParam.applicantList},'$')) appl
                 WHERE  rslt.assignment_id = appl.column_value
                 AND    {authFunctionPredicate.XXAS_APL_STAT_NEW_PERM}
				 ORDER BY rslt.customer_status
			  )p	 
      ]'
    );
    
END;
/
COMMIT;
EXIT;
