/* $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
                       := 'HtmlvacApplicants';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View Html Vacancy Applicants'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'PutApplicantData'
    ,p_description        => 'Put applicant rating/interested'
    ,p_methods            => 'PUT'
    ,p_path               => '/vacancy/{vacancyId}/{applicantId}/save'
    ,p_remove_root_ele    => true
    ,p_plsql                =>
      q'[
         DECLARE
          
          l_resp         xxas_http_resp_obj;
		  l_request_xml  xmltype;
          
        BEGIN
        
		  l_request_xml := {requestBody};
		  
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_DATA_VAC_VIEW', {pathParam.vacancyId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
           xxas_applicant_pkg.update_html_applicant
              (p_assignment_id  => {pathParam.applicantId}
			  ,p_vacancy_id     => {pathParam.vacancyId}
              ,p_effective_date => trunc(sysdate)
              ,p_form_data_xml  => l_request_xml);
              
            l_resp := xxas_http_response
                      .ok(xxas_com_msg_pkg.get_success_message_xml
                           (xxas_com_msg_type
                         (xxas_util_pkg.app, 'XXAS_COM_CHNG_SAVE', null)
                       ,null
                       ,'modal')
                         )
                      .build();
         
          END IF;
          
          {response} := l_resp;

        END
		]'
    );

    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'PutApplicantDataDetailPage'
    ,p_description        => 'Put applicant rating/interested in Applicant Detail page'
    ,p_methods            => 'PUT'
    ,p_path               => '/vacancy/{vacancyId}/{applicantId}/saveDetail'
    ,p_remove_root_ele    => true
    ,p_plsql                =>
      q'[
         DECLARE
          
          l_resp         xxas_http_resp_obj;
		  l_request_xml  xmltype;
		  l_resp_xml     xmltype;
		  l_changed_items_xml  xmltype;
          
        BEGIN
        
		  l_request_xml := {requestBody};
		  
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_DATA_VAC_VIEW', {pathParam.vacancyId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
           xxas_applicant_pkg.update_html_applicant
              (p_assignment_id  => {pathParam.applicantId}
			  ,p_vacancy_id     => {pathParam.vacancyId}
              ,p_effective_date => trunc(sysdate)
              ,p_form_data_xml  => l_request_xml);
			  
		   SELECT XMLAgg
                   (XMLElement
                     ("item"
                     ,XMLForest
                       (va.assignment_id   "id"
                       ,xxas_com_rest_util_pkg.uri_from_app_route
                           ('/applicant-detail/' || va.assignment_id || '_' || va.vacancy_id) "href"
					   ,va.town_or_city  "overline"
                       ,va.person_name "title"
                       ,va.is_terminated
					   ,'&lt;ul&gt;&lt;/ul&gt;&lt;p&gt;Tel: '||va.phone_number|| '&lt;/p&gt;&lt;p&gt;Email: ' 
					   || va.email_address || '&lt;/p&gt;'  "subTitle"
                       ,decode
                         (va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) "create_offer_allowed"
					   ,va.applicant_rank "rank"
                       ,CASE nvl(va.interested,0)
							        WHEN 0 THEN 'Unscreened'
									WHEN 1 THEN 'No'
									WHEN 2 THEN 'Maybe'
									WHEN 3 THEN 'Yes'
						END  "interested"
					   ,va.assignment_status "Det_status"
					   ,va.system_meaning "status"
					   ,'modal' "target"
					   ,'large' "modalWidth"
                       )
					  ,(XMLELEMENT
					      ("chips"
						  ,XMLAttributes
                                        ('true' as "json:force-array"
                                        ,'http://json.org/' as "xmlns:json")
						  ,xxas_applicant_pkg.get_applicant_docs(va.vacancy_id,va.assignment_id)
						 )
						 )
					  ,xmlelement
                       ("form"
					    ,XMLElement
                         ("fields"
                          ,XMLAttributes
                           ('true' as "json:force-array"
                            ,'http://json.org/' as "xmlns:json")
                          ,XMLElement
                            ("field"
                             ,XMLFOREST
                             ('rating'            "type"
							  ,'input'            "state"
							  ,'starfilled'       "icon"
                              ,'energized'        "iconColor"
                              ,'APPLICANT_RANK'   "id"
                              ,'Rating'           "label"
							  ,'false'            "multiColor"
							  ,'false'            "required"
                             )
							,XMLElement
                            ("value"
							,XMLForest
							  (round(va.applicant_rank/20) "id"
							  )
							)
							,XMLElement
                            ("values"
                              ,XMLAttributes
                                ('true' as "json:force-array"
                                 ,'http://json.org/' as "xmlns:json")
							 ,XMLElement
                             ("value"
                              ,XMLForest
                              (1 "id"
                               )
                             )
                             ,XMLElement
                             ("value"
                             ,XMLForest
                              (2 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (3 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (4 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (5 "id"
                              )
                              )
                         )
						)
						,xxas_emp_ui_list_type
                                 (p_list_type              => 'Poplist'
                                 ,p_id                     => 'APPLICANT_STATUS'
                                 ,p_label                  => 'Status'
                                 ,p_description            => null
                                 ,p_refresh_on_change      => 'false'
                                 ,p_trigger_name           => null
                                 ,p_trigger_method         => null
                                 ,p_enabled                => 'false'
                                 ,p_required               => 'false'
                                 ,p_visible                => 'true'
                                 ,p_hide_if                => null
                                 ,p_list_provider          => null
                                 ,p_values_in_payload      => 'true'
                                 ,p_longlist               => 'false'
                                 ,p_show_as_radio          => 'false'
                                 ,p_show_as_boolean        => 'N'
                                 ,p_positive_boolean_value => ''
                                 ,p_multiple_selection     => 'false'
                                 ,p_style                  => null
                                 ,p_allow_new_values       => 'false'
                                 ,p_empty_value            => null
                                 ,p_value                  => va.assignment_status_type_id
                                 ,p_transient              => null
                                 ,p_value_set              => xxas_vacancy_pkg.assignment_status_value_set(va.assignment_id)
                                 ).get_json_friendly()
                       ,XMLElement
                            ("field"
                             ,XMLFOREST
                             ('select-single'       "type"
							  ,'input'              "state"
							  ,'INTERESTED'         "id"
                              ,'Interested'         "label"
							  ,'false'              "visible"
							  ,'false'              "required"
							  ,'false'              "refreshOnChange"
							  ,'Choose one:'        "placeholder"
                             )
							,XMLElement
                            ("value"
							,XMLForest
							  (nvl(va.interested,0) "id"
							  ,CASE nvl(va.interested,0)
							        WHEN 0 THEN '#C0C0C0'
									WHEN 1 THEN 'assertive'
									WHEN 2 THEN 'energized'
									WHEN 3 THEN 'balanced'
							  END "color"
							  )
							)
							,xxas_vacancy_pkg.interested_value_set()
						)
					   )
					   )
					  ,XMLELEMENT
					   ("links"
					   ,XMLElement
					    ("self"
						,XMLForest(xxas_com_rest_util_pkg.uri_from_path('vacancy/'||va.vacancy_id||'/'||va.assignment_id||'/save') "href"
					    )
					   )
					   ,XMLElement
					    ("item"
						,va.assignment_id)
					   )
					  ,XMLELEMENT
					   ("linked"
					   ,XMLElement
					    ("image"
						,XMLElement
						 ("self"
						  ,xxas_com_rest_util_pkg.uri_from_path
                             ('people/' || nvl(va.apl_person_id, -1) || '/photo'))
						  ,CASE WHEN va.image_href IS NULL THEN
							XMLForest
							(xxas_util_pkg.get_initials(va.first_name, va.last_name, va.known_as) "initials")
							ELSE 
							XMLForest
							(xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'SMALL') "small"
                            ,xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'MEDIUM') "medium"
                            ,xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'FULL_SIZE') "fullSize"
							)
						  END
						)
					   ,XMLElement
                       ("actions"
                       ,XMLAttributes
                         ('true' as "json:force-array"
                         ,'http://json.org/' as "xmlns:json")
                       ,CASE WHEN 
					     decode
                         (va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) = 'Y' THEN
						 XMLElement
                         ("action"
                          ,XMLForest
                           ('OFFER'     "id"
                           ,'Create Offer'  "title"
                           ,'POST'       "operation"
                           ,xxas_com_rest_util_pkg.uri_from_path('vacancies/' || {pathParam.vacancyId} || '/offers/create' || '?applicantId=' || va.assignment_id)  "href"
						               ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive'     as "color"
                               ,'light'        as "weight"
                               ,'creation' as "icon"
                               )
                             )
                           )
                         )
						END
                       ,XMLElement
                         ("action"
                         ,XMLForest
                           ('STATUSCHANGE'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_STATUS_CHANGE')  "title"
                           ,'POST'     "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path(
						    'applicant/status-change/' || {pathParam.personId} || '/'|| va.vacancy_id)  "href"
						   ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive' as "color"
                               ,'light'    as "weight"
							   ,'message' as "icon"
                               )
                             )
                           )
                         )
                       ,XMLElement
                         ("action"
                         ,XMLForest
                           ('SENDMESSAGE'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_SEND_MESSAGE')  "title"
                           ,'POST'       "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path(
						    'applicant/send-msg/' || {pathParam.personId} || '/' || va.vacancy_id)  "href"
						   ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive'     as "color"
                               ,'light'        as "weight"
                               ,'message-text' as "icon"
                               )
                             )
                           )
                         )
						,XMLElement
                         ("action"
                         ,XMLForest
                           ('EXPORT'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_EXPORT')  "title"
                           ,'GET'        "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path
                              ('applicant-export/' || va.vacancy_id ) "href"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive' as "color"
                               ,'light'    as "weight"
                               ,'export' as "icon"
                               )
                             )
                           )
                         )
                       )
					  )
					)
                   ORDER BY va.last_name, va.person_name
                   ) AS "data" INTO l_changed_items_xml
        FROM   xxas_vacancy_applicants_v va
		      ,xxas_per_thumbnails img
        WHERE  va.vacancy_id = {pathParam.vacancyId}
		AND    va.apl_person_id = img.person_id(+)
		AND    xxas_com_sec_pkg.test_instance('XXAS_DATA_VAC_VIEW', va.vacancy_id) = 'Y';
			  
			l_resp_xml := xxas_com_msg_pkg.get_success_message_xml
                        (p_message       => xxas_com_msg_type
                                              (xxas_util_pkg.app
                                              ,'XXAS_COM_CHNG_SAVE'
                                              ,null)
                        ,p_redirect      => null
                        ,p_target        => 'modal'
                        ,p_persist_cache => 'false'
                        ,p_block_id      => xxas_rslt_cache_pkg.get_appl_list_func_id('applicant-list')
                        ,p_cre_upd_items => l_changed_items_xml
						,p_del_items     => null
						,p_invalid_urls  => xxas_com_varchar2_table(xxas_com_rest_util_pkg.uri_from_path
                           ('/applicant-detail-html/' || {pathParam.applicantId} || '/' || {pathParam.vacancyId} ) )
                        );
						
			l_resp := xxas_http_response
                      .ok(l_resp_xml)
                      .build();
         
          END IF;
          
          {response} := l_resp;

        END
		]'
    );
	
	  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetHTMLVacancyApplicants'
    ,p_description        => 'Get a list of applicants for a vacancy'
    ,p_methods            => 'GET'
	,p_path               => '/vacancy/{vacancyId}/{personId}/applicants'
    ,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
                     ("item"
                     ,XMLForest
                       (va.assignment_id   "id"
                       ,xxas_com_rest_util_pkg.uri_from_app_route
                           ('/applicant-detail/' || va.assignment_id || '_' || va.vacancy_id) "href"
					   ,va.town_or_city  "overline"
                       ,va.person_name "title"
                       ,va.is_terminated
					   ,'&lt;ul&gt;&lt;/ul&gt;&lt;p&gt;Tel: '||va.phone_number|| '&lt;/p&gt;&lt;p&gt;Email: ' 
					   || va.email_address || '&lt;/p&gt;'  "subTitle"
                       ,decode
                         (va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) "create_offer_allowed"
					   ,va.applicant_rank "rank"
                       ,CASE nvl(va.interested,0)
							        WHEN 0 THEN 'Unscreened'
									WHEN 1 THEN 'No'
									WHEN 2 THEN 'Maybe'
									WHEN 3 THEN 'Yes'
						END  "interested"
					   ,va.assignment_status "Det_status"
					   ,va.system_meaning "status"
                       )
					  ,(XMLELEMENT
					      ("chips"
						  ,XMLAttributes
                                        ('true' as "json:force-array"
                                        ,'http://json.org/' as "xmlns:json")
						  ,xxas_applicant_pkg.get_applicant_docs(va.vacancy_id,va.assignment_id)
						 )
						 )
					  ,xmlelement
                       ("form"
					    ,XMLElement
                         ("fields"
                          ,XMLAttributes
                           ('true' as "json:force-array"
                            ,'http://json.org/' as "xmlns:json")
						  ,CASE WHEN 
							decode
							(decode
							(va.has_offer, 'N'
							,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
									xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
							),'Y','Y','N') = 'N' THEN
                           XMLElement
                            ("field"
                             ,XMLFOREST
                             ('rating'            "type"
							  ,'input'            "state"
							  ,'starfilled'       "icon"
                              ,'energized'        "iconColor"
                              ,'APPLICANT_RANK'   "id"
                              ,'Rating'           "label"
							  ,'false'            "multiColor"
							  ,'false'            "required"
                             )
							,XMLElement
                            ("value"
							,XMLForest
							  (round(va.applicant_rank/20) "id"
							  )
							)
							,XMLElement
                            ("values"
                              ,XMLAttributes
                                ('true' as "json:force-array"
                                 ,'http://json.org/' as "xmlns:json")
							 ,XMLElement
                             ("value"
                              ,XMLForest
                              (1 "id"
                               )
                             )
                             ,XMLElement
                             ("value"
                             ,XMLForest
                              (2 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (3 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (4 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (5 "id"
                              )
                              )
                         )
						)
						END
						,CASE WHEN 
							decode
							(decode
							(va.has_offer, 'N'
							,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
									xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
							),'Y','Y','N') = 'N' THEN
						xxas_emp_ui_list_type
                                 (p_list_type              => 'Poplist'
                                 ,p_id                     => 'APPLICANT_STATUS'
                                 ,p_label                  => 'Status'
                                 ,p_description            => null
                                 ,p_refresh_on_change      => 'false'
                                 ,p_trigger_name           => null
                                 ,p_trigger_method         => null
                                 ,p_enabled                => 'false'
                                 ,p_required               => 'false'
                                 ,p_visible                => 'true'
                                 ,p_hide_if                => null
                                 ,p_list_provider          => null
                                 ,p_values_in_payload      => 'true'
                                 ,p_longlist               => 'false'
                                 ,p_show_as_radio          => 'false'
                                 ,p_show_as_boolean        => 'N'
                                 ,p_positive_boolean_value => ''
                                 ,p_multiple_selection     => 'false'
                                 ,p_style                  => null
                                 ,p_allow_new_values       => 'false'
                                 ,p_empty_value            => null
                                 ,p_value                  => va.assignment_status_type_id
                                 ,p_transient              => null
                                 ,p_value_set              => xxas_vacancy_pkg.assignment_status_value_set(va.assignment_id)
                                 ).get_json_friendly()
						END 
						,CASE WHEN 
							decode
							(decode
							(va.has_offer, 'N'
							,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
									xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
							),'Y','Y','N') = 'Y' THEN
							xxas_emp_ui_list_type
                                 (p_list_type              => 'Poplist'
                                 ,p_id                     => 'APPLICANT_STATUS'
                                 ,p_label                  => 'Status'
                                 ,p_description            => xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_CREATE_OFFER_MSG') 
                                 ,p_refresh_on_change      => 'false'
                                 ,p_trigger_name           => null
                                 ,p_trigger_method         => null
                                 ,p_enabled                => 'false'
                                 ,p_required               => 'false'
                                 ,p_visible                => 'true'
                                 ,p_hide_if                => null
                                 ,p_list_provider          => null
                                 ,p_values_in_payload      => 'true'
                                 ,p_longlist               => 'false'
                                 ,p_show_as_radio          => 'false'
                                 ,p_show_as_boolean        => 'N'
                                 ,p_positive_boolean_value => ''
                                 ,p_multiple_selection     => 'false'
                                 ,p_style                  => null
                                 ,p_allow_new_values       => 'false'
                                 ,p_empty_value            => null
                                 ,p_value                  => va.assignment_status_type_id
                                 ,p_transient              => null
                                 ,p_value_set              => xxas_vacancy_pkg.assignment_status_value_set(va.assignment_id)
                                 ).get_json_friendly()
						END
                        ,CASE WHEN 
							decode
							(decode
							(va.has_offer, 'N'
							,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
									xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
							),'Y','Y','N') = 'N' THEN
							XMLElement
                            ("field"
                             ,XMLFOREST
                             ('select-single'       "type"
							  ,'input'              "state"
							  ,'INTERESTED'         "id"
                              ,'Interested'         "label"
							  ,'false'              "visible"
							  ,'false'              "required"
							  ,'false'              "refreshOnChange"
							  ,'Choose one:'        "placeholder"
                             ),
              CASE when va.interested is NOT NULL THEN
							  XMLElement
                            ("value"
							,XMLForest
							  (nvl(va.interested,0) "id"
							  ,CASE nvl(va.interested,0)
							        WHEN 0 THEN '#C0C0C0'
									WHEN 1 THEN 'assertive'
									WHEN 2 THEN 'energized'
									WHEN 3 THEN 'balanced'
							  END "color"
							  )
							)
              END
							,xxas_vacancy_pkg.interested_value_set()
						)
						END
					   )
					   )
					  ,XMLELEMENT
					   ("links"
					   ,XMLElement
					    ("self"
						,XMLForest(xxas_com_rest_util_pkg.uri_from_path('vacancy/'||va.vacancy_id||'/'||va.assignment_id||'/save') "href"
					    )
					   )
					   ,XMLElement
					    ("item"
						,va.assignment_id)
					   )
					  ,XMLELEMENT
					   ("linked"
					   ,XMLElement
					    ("image"
						,XMLElement
						 ("self"
						  ,xxas_com_rest_util_pkg.uri_from_path
                             ('people/' || nvl(va.apl_person_id, -1) || '/photo'))
						  ,CASE WHEN va.image_href IS NULL THEN
							XMLForest
							(xxas_util_pkg.get_initials(va.first_name, va.last_name, va.known_as) "initials")
							ELSE 
							XMLForest
							(xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'SMALL') "small"
                            ,xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'MEDIUM') "medium"
                            ,xxas_per_image_pkg.url(va.apl_person_id, img.last_update_date, 'FULL_SIZE') "fullSize"
							)
						  END
						)
					   ,XMLElement
                       ("actions"
                       ,XMLAttributes
                         ('true' as "json:force-array"
                         ,'http://json.org/' as "xmlns:json")
                       ,CASE WHEN 
					     decode
                         (va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) = 'Y' THEN
						 XMLElement
                         ("action"
                          ,XMLForest
                           ('OFFER'     "id"
                           ,'Create Offer'  "title"
                           ,'POST'       "operation"
                           ,xxas_com_rest_util_pkg.uri_from_path('vacancies/' || {pathParam.vacancyId} || '/offers/create' || '?applicantId=' || va.assignment_id)  "href"
						               ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive'     as "color"
                               ,'light'        as "weight"
                               ,'creation' as "icon"
                               )
                             )
                           )
                         )
						END
                       ,XMLElement
                         ("action"
                         ,XMLForest
                           ('STATUSCHANGE'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_STATUS_CHANGE')  "title"
                           ,'POST'     "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path(
						    'applicant/status-change/' || {pathParam.personId} || '/'|| va.vacancy_id)  "href"
						   ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive' as "color"
                               ,'light'    as "weight"
							   ,'message' as "icon"
                               )
                             )
                           )
                         )
                       ,XMLElement
                         ("action"
                         ,XMLForest
                           ('SENDMESSAGE'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_SEND_MESSAGE')  "title"
                           ,'POST'       "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path(
						    'applicant/send-msg/' || {pathParam.personId} || '/' || va.vacancy_id)  "href"
						   ,'modal'      "target"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive'     as "color"
                               ,'light'        as "weight"
                               ,'message-text' as "icon"
                               )
                             )
                           )
                         )
						,XMLElement
                         ("action"
                         ,XMLForest
                           ('EXPORT'     "id"
                           ,xxas_com_msg_pkg.get_message(xxas_util_pkg.app,'XXAS_HTML_VAC_EXPORT')  "title"
                           ,'GET'        "operation"
                           ,'true'       "bulkActionEnabled"
                           ,xxas_com_rest_util_pkg.uri_from_path
                              ('applicant-export/' || va.vacancy_id ) "href"
                           )
                         ,XMLElement
                           ("meta"
                           ,XMLElement
                             ("options"
                             ,XMLForest
                               ('positive' as "color"
                               ,'light'    as "weight"
                               ,'export' as "icon"
                               )
                             )
                           )
                         )
                       )
					  )
					)
                   ORDER BY va.last_name, va.person_name
                   )
                 ) AS "data"
        FROM   xxas_vacancy_applicants_v va
		      ,xxas_per_thumbnails img
        WHERE  va.vacancy_id = {pathParam.vacancyId}
		AND    va.apl_person_id = img.person_id(+)
		AND    xxas_com_sec_pkg.test_instance('XXAS_DATA_VAC_VIEW', va.vacancy_id) = 'Y'
      ]'
    );
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetHTMLVacancyApplicantDetail'
    ,p_description        => 'Get a single applicant detail for a vacancy'
    ,p_methods            => 'GET'
	,p_path               => '/applicant-detail-html/{applicantId}/{vacancyId}'
    ,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
                     ("item"
                     ,XMLForest
                       (va.assignment_id   "id"
                       ,va.town_or_city  "overline"
                       ,va.person_name "title"
                       ,va.is_terminated
					   ,'&lt;ul&gt;&lt;/ul&gt;&lt;p&gt;Tel: '||va.phone_number|| '&lt;/p&gt;&lt;p&gt;Email: ' 
					   || va.email_address || '&lt;/p&gt;'  "subTitle"
                       ,decode
                         (va.has_offer, 'N'
                         ,decode(xxas_com_sec_pkg.person_on_rec_team_edit_flag(va.vacancy_id,'Y','Y'),'Y',
                                 xxas_com_sec_pkg.test_instance('XXAS_APL_CREATE_OFFER_PERM', va.assignment_status_type_id),'N')
                          ) "create_offer_allowed"
                       )
					  ,(XMLELEMENT
					      ("chips"
						  ,XMLAttributes
                                        ('true' as "json:force-array"
                                        ,'http://json.org/' as "xmlns:json")
						  ,xxas_applicant_pkg.get_applicant_docs(va.vacancy_id,va.assignment_id)
						 )
						 )
					  ,xmlelement
                       ("form"
					    ,XMLElement
                         ("fields"
                          ,XMLAttributes
                           ('true' as "json:force-array"
                            ,'http://json.org/' as "xmlns:json")
                          ,XMLElement
                            ("field"
                             ,XMLFOREST
                             ('rating'            "type"
							  ,'input'            "state"
							  ,'starfilled'       "icon"
                              ,'energized'        "iconColor"
                              ,'APPLICANT_RANK'   "id"
                              ,'Rating'           "label"
							  ,'false'            "multiColor"
							  ,'false'            "required"
                             )
							,XMLElement
                            ("value"
							,XMLForest
							  (round(va.applicant_rank/20) "id"
							  )
							)
							,XMLElement
                            ("values"
                              ,XMLAttributes
                                ('true' as "json:force-array"
                                 ,'http://json.org/' as "xmlns:json")
							 ,XMLElement
                             ("value"
                              ,XMLForest
                              (1 "id"
                               )
                             )
                             ,XMLElement
                             ("value"
                             ,XMLForest
                              (2 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (3 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (4 "id"
                              )
                              )
                              ,XMLElement
                             ("value"
                             ,XMLForest
                              (5 "id"
                              )
                              )
                         )
						)
						,xxas_emp_ui_list_type
                                 (p_list_type              => 'Poplist'
                                 ,p_id                     => 'APPLICANT_STATUS'
                                 ,p_label                  => 'Status'
                                 ,p_description            => null
                                 ,p_refresh_on_change      => 'false'
                                 ,p_trigger_name           => null
                                 ,p_trigger_method         => null
                                 ,p_enabled                => 'false'
                                 ,p_required               => 'false'
                                 ,p_visible                => 'true'
                                 ,p_hide_if                => null
                                 ,p_list_provider          => null
                                 ,p_values_in_payload      => 'true'
                                 ,p_longlist               => 'false'
                                 ,p_show_as_radio          => 'false'
                                 ,p_show_as_boolean        => 'N'
                                 ,p_positive_boolean_value => ''
                                 ,p_multiple_selection     => 'false'
                                 ,p_style                  => null
                                 ,p_allow_new_values       => 'false'
                                 ,p_empty_value            => null
                                 ,p_value                  => va.assignment_status_type_id
                                 ,p_transient              => null
                                 ,p_value_set              => xxas_vacancy_pkg.assignment_status_value_set(va.assignment_id)
                                 ).get_json_friendly()
                       ,XMLElement
                            ("field"
                             ,XMLFOREST
                             ('select-single'       "type"
							  ,'input'              "state"
							  ,'INTERESTED'         "id"
                              ,'Interested'         "label"
							  ,'false'              "visible"
							  ,'false'              "required"
							  ,'false'              "refreshOnChange"
							  ,'Choose one:'        "placeholder"
                             )
							,XMLElement
                            ("value"
							,XMLForest
							  (nvl(va.interested,0) "id"
							  ,CASE nvl(va.interested,0)
							        WHEN 0 THEN 'positive'
									WHEN 1 THEN 'assertive'
									WHEN 2 THEN 'energized'
									WHEN 3 THEN 'balanced'
							  END "color"
							  )
							)
							,xxas_vacancy_pkg.interested_value_set()
						)
					   )
					   )
					  ,XMLELEMENT
					   ("links"
					   ,XMLElement
					    ("self"
						,XMLForest(xxas_com_rest_util_pkg.uri_from_path('vacancy/'||va.vacancy_id||'/'||va.assignment_id||'/saveDetail') "href"
					    )
					   )
					   ,XMLElement
					    ("item"
						,va.assignment_id)
					   )
					  ,XMLELEMENT
					   ("linked"
					   ,XMLElement
					    ("image"
						,XMLElement
						 ("self"
						  ,xxas_com_rest_util_pkg.uri_from_path
                             ('people/' || nvl(va.person_id, -1) || '/photo'))
						  ,CASE WHEN va.image_href IS NULL THEN
							XMLForest
							(xxas_util_pkg.get_initials(va.first_name, va.last_name, va.known_as) "initials")
							ELSE 
							XMLForest
							(xxas_per_image_pkg.url(va.person_id, img.last_update_date, 'SMALL') "small"
                            ,xxas_per_image_pkg.url(va.person_id, img.last_update_date, 'MEDIUM') "medium"
                            ,xxas_per_image_pkg.url(va.person_id, img.last_update_date, 'FULL_SIZE') "fullSize"
							)
						  END
						)
					  )
					)
                   ORDER BY va.last_name, va.person_name
                   )
                 ) AS "data"
        FROM   xxas_vacancy_applicants_v va
		      ,xxas_per_thumbnails img
        WHERE  va.vacancy_id = {pathParam.vacancyId}
		AND    va.assignment_id = {pathParam.applicantId}
		AND    va.person_id = img.person_id(+)
		AND    xxas_com_sec_pkg.test_instance('XXAS_DATA_VAC_VIEW', va.vacancy_id) = 'Y'
      ]'
    );
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetVacancyApplicantPages'
    ,p_description        => 'Opens vacany applicants pages'
    ,p_path               => 'applicant/{action}/{personId}/{vacacnyId}'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml         XMLType;
          l_resp        xxas_http_resp_obj;
          l_resp_xml    XMLType;
          l_request_xml XMLType;
          l_assignment_list VARCHAR2(4000):= '';
		  
		  CURSOR csr_get_assignment_list IS
		  SELECT item_id
          FROM  XMLTable
            ('/*/*'
            PASSING (l_request_xml)
            COLUMNS item_id   NUMBER         path 'id');
		 
		  CURSOR csr_get_valid_status(cp_asg_id number) IS
		  select count(*)
		  FROM
          xxas_applicant_statuses_v rslt
          WHERE  rslt.assignment_id = cp_asg_id
		  AND xxas_com_sec_pkg.test_instance('XXAS_APL_STAT_NEW_PERM',cp_asg_id,rslt.assignment_status_type_id) = 'Y';
		  
		  l_valid_applicants_count NUMBER := 0;
		  l_valid_statuses_count   NUMBER := 0;
        BEGIN
        
          l_request_xml := {requestBody};
  
          IF {pathParam.action} = 'status-change' THEN
		     FOR csr_rec IN csr_get_assignment_list LOOP
		         l_assignment_list := l_assignment_list || csr_rec.item_id||'$';
				 
				 OPEN csr_get_valid_status(csr_rec.item_id);
				 FETCH csr_get_valid_status INTO l_valid_statuses_count;
				 CLOSE csr_get_valid_status;
				 
				 IF l_valid_statuses_count > 0 THEN 
				   l_valid_applicants_count := l_valid_applicants_count + 1;
				 END IF;
				 
		     END LOOP;
			 		
              IF l_valid_applicants_count > 0 THEN					
     		  l_resp_xml := xxas_com_rest_util_pkg.app_route_redirect_response
                            (p_app_route     => 'applicant-status/' || {pathParam.personId} || 
							                     '_' || {pathParam.vacacnyId} || '_' || l_assignment_list
                            ,p_target        => 'modal'
                            ,p_persist_cache => 'true');
			  ELSE 
			  
			  
			  l_resp_xml := xxas_com_msg_pkg.get_error_message_xml
							   (xxas_com_msg_type
							 (xxas_util_pkg.app, 'XXAS_NO_VALID_STATUS', null)
						   ,NULL
						   ,NULL);	  
			  END IF;
			  
		  ELSIF {pathParam.action} = 'send-msg' THEN
		      FOR csr_rec IN csr_get_assignment_list LOOP
		         l_assignment_list := l_assignment_list || csr_rec.item_id||'$';
		      END LOOP;
		      l_resp_xml := xxas_com_rest_util_pkg.app_route_redirect_response
                            (p_app_route     => 'ad-hoc/' || {pathParam.personId} || 
							                     '_' || {pathParam.vacacnyId} || '_' || l_assignment_list
                            ,p_target        => 'modal'
                            ,p_persist_cache => 'true');
		  END IF;
						
          l_resp := xxas_http_response
                      .ok(l_resp_xml
                         )
                      .build();						  
              
		  
         {response} := l_resp;

        END
      ]'
    );	
	
	xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetVacancyApplicantExport'
    ,p_description        => 'Opens vacany applicants Export'
    ,p_path               => 'applicant-export/{vacancyId}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp xxas_http_resp_obj;
		  l_xml XMLType;
        BEGIN
        
        SELECT XMLElement
           ("data"
           ,XMLElement
             ("redirect"
             ,xxas_com_rest_util_pkg.uri_from_path('vacancies/' || {pathParam.vacancyId} || '/applicants/' || {requestParam.ids} || '?includeViewSchema=true&amp;mode=download_blob&amp;_accept=application/pdf')
             )
           ,XMLElement("target", '_blank')
           ,XMLElement("persistCache", 'true')
		   )
        INTO   l_xml
		FROM   dual; 

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

         {response} := l_resp;
        END
      ]'
	   );

     xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetVacancyApplicantExportAppDet'
    ,p_description        => 'Opens vacany applicants Export'
    ,p_path               => 'applicant-export/{vacancyId}/{applicantId}'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp xxas_http_resp_obj;
		  l_xml XMLType;
        BEGIN
        
        SELECT XMLElement
           ("data"
           ,XMLElement
             ("redirect"
             ,xxas_com_rest_util_pkg.uri_from_path('vacancies/' || {pathParam.vacancyId} || '/applicants/' || {pathParam.applicantId} || '?includeViewSchema=true&amp;mode=download_blob&amp;_accept=application/pdf')
             )
           ,XMLElement("target", '_blank')
           ,XMLElement("persistCache", 'true')
		   )
        INTO   l_xml
		FROM   dual; 

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

         {response} := l_resp;
        END
      ]'
	   );
END;
/
COMMIT;
EXIT;