/* $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
                       := 'Selectsteps';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Performance review steps'
    ,p_path               => '/perf-steps'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'Findsteps'
    ,p_description        => 'Find Performance review steps'
    ,p_methods            => 'GET'
    ,p_path               => '{perfReviewFormId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT XMLElement
                 ("data"
                 ,XMLAgg
                   (XMLElement
                     ("step"
                     ,XMLElement
                       ("id"
                       ,xprs.perf_review_step_id 
                       )
                       ,XmlElement
                         ("label"
                          ,xprtsv.title || ' step ( ' || LISTAGG(papf.full_name, ' + ') WITHIN GROUP (ORDER BY xprtsv.sequence) || ' )'
                          )
                       ,XMLElement
                         ("value"
                         ,xprtsv.title || ' step ( ' || LISTAGG(papf.full_name, ' + ') WITHIN GROUP (ORDER BY xprtsv.sequence) || ' )'
                         )
                     )
                   )
                 )
          FROM   xxas_perf_review_steps xprs 
                ,xxas_perf_review_templ_steps_v xprtsv
                ,xxas_perf_review_participants xprp
                ,per_all_people_f papf
          WHERE  xprs.perf_review_form_id = {pathParam.perfReviewFormId}
          AND    xprs.perf_review_templ_step_id = xprtsv.perf_review_templ_step_id
          AND    xprp.perf_review_step_id = xprs.perf_review_step_id
          AND    papf.person_id = xprp.reviewer_person_id
          AND    trunc(xprs.last_update_date) BETWEEN papf.effective_start_date AND papf.effective_end_date
          GROUP BY xprp.perf_review_step_id,xprs.perf_review_step_id,xprtsv.title
          ORDER BY xprtsv.sequence
        ]'
    );

END;
/
COMMIT;
EXIT;
