/* $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
                       := 'ReviewTemplates';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Manage Performance Review Templates' 
    ,p_path               => 'performance-templates'    
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
           
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteTemplate'
    ,p_description        => 'Deletes a Performance Review Template'
    ,p_methods            => 'DELETE'
    ,p_path               => '{reviewTemplateId}'
    ,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_REV_TEMPL_DEL', {pathParam.reviewTemplateId}) THEN

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

          xxas_perf_review_templates_pkg.delete_row
            (p_perf_review_template_id => {pathParam.reviewTemplateId});
              
          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)
                       ,'performance-setup')
                         )
                      .build();
         {response} := l_resp;                      
         END IF;
        END
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteTemplateStep'
    ,p_description        => 'Deletes a Performance Review Template Step'
    ,p_methods            => 'DELETE'
    ,p_path               => '{reviewTemplateId}/steps/{templateStepId}'
    ,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_TEMPL_STEP_DEL', {pathParam.templateStepId},{pathParam.reviewTemplateId}) THEN

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

          xxas_perf_review_templates_pkg.delete_step_row
            (p_perf_review_templ_step_id => {pathParam.templateStepId});
              
          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        => 'GetQuestionnaireDFF'
    ,p_description        => 'Get all available contexts for Questionnaire DFF'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'questionnaires'    
    ,p_sql                =>
      q'[SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,XMLAgg
                   (XMLElement
                     ("participant"
                     ,XMLForest
                       (fdfc.descriptive_flex_context_code as "id"
                       ,fdfc.descriptive_flex_context_name as "label"
                       ,fdfc.description   as "value"
                       )
                     )
                   )
                 )"data"
    FROM fnd_descr_flex_contexts_vl  fdfc
      WHERE  fdfc.application_id             = xxas_util_pkg.app_id
      AND    fdfc.descriptive_flexfield_name = 'Performance Questionnaires'
      AND    fdfc.enabled_flag               = 'Y'
      AND    fdfc.global_flag                = 'N' 
      AND {authFunctionPredicate.XXAS_DT_TEMPL_QUESTION}
    ORDER BY fdfc.descriptive_flex_context_name  
        ]'
    ); 
    
END;
/
COMMIT;
EXIT;
