/* $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
                       := 'Participants';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Manage Participants' 
    ,p_path               => 'participants'    
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
           
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteParticipant'
    ,p_description        => 'Deletes a Participant'
    ,p_methods            => 'DELETE'
    ,p_path               => '{participantId}'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml         XMLType;
          l_resp        xxas_http_resp_obj;
          
        BEGIN
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_DT_PERF_PARTIC_DELETE', {pathParam.participantId}) THEN

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

          xxas_perf_participants_pkg.delete_row
            (p_perf_participant_id => {pathParam.participantId});
              
          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)
                         )
                      .build();
         {response} := l_resp;                      
         END IF;
        END
      ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetParticipants'
    ,p_description        => 'Get Participants'
    ,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
                     ("participant"
                     ,XMLForest
                       (p.perf_participant_id as "id"
                       ,p.participant         as "label"
                       ,p.provider_function   as "value"
                       )
                     )
                   )
                 )"data"
    FROM xxas_perf_participants_v p  
     WHERE UPPER(p.participant) LIKE UPPER({requestParam.q}||'%')
       AND {authFunctionPredicate.XXAS_DT_PERF_PARTIC}     
        ]'
    );       
    
END;
/
COMMIT;
EXIT;
