/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2016 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
                       := 'PerformancePlans';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Manage performance plans'
    ,p_path               => 'performance-plans'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'PlansByManager'
    ,p_description        => 'Plans for direct reports by manager '
    ,p_methods            => 'GET'
    ,p_path               => 'by-manager/{personId}'
    ,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
                      (xpp.perf_plan_id "id"
                     ,'flexible' "type"
                     ,CASE WHEN xpp.plan_start_date &gt; trunc(sysdate) THEN
                                'calendar'
                           WHEN xpp.plan_end_date &lt; trunc(sysdate) THEN
                                'tick'
                           ELSE 'spinner'
                      END "icon"
                      ,CASE WHEN xpp.plan_start_date &gt; trunc(sysdate) THEN
                                xxas_rslt_cache_pkg.resolve_color_name('grey')
                           WHEN xpp.plan_end_date &lt; trunc(sysdate) THEN
                                xxas_rslt_cache_pkg.resolve_color_name('green')
                           ELSE xxas_rslt_cache_pkg.resolve_color_name('orange')
                      END "statusColor" 
                      ,xpp.title "line1"
                      ,'&lt;div class="pre"&gt;&lt;font color="'
                         || xxas_rslt_cache_pkg.resolve_color_name('grey')
                         || '"&gt;'
                         || xxas_perf_plans_pkg.drs_enrolled_in_plan(rslt.person_id,xpp.perf_plan_id,2)
                         || '&lt;/font&gt;&lt;/div&gt;' "line3"
                     ,'performance-dashboard' "pageId"
                     ,rslt.person_id || '_' || xpp.perf_plan_id "objectId"

                    )
                  ) ORDER BY xpp.plan_start_date desc
                )
              ) "data"
         FROM xxas_perf_plans_v xpp
             ,per_all_people_f rslt
        WHERE rslt.person_id = {pathParam.personId}
          AND {authFunctionPredicate.XXAS_DT_PERF_PEOPLE}
          AND EXISTS 
                (SELECT column_value 
                  FROM xxas_perf_plan_enrollments_v xppe,
                      TABLE(xxas_com_hier_pkg.drs_for_per_tbl(rslt.person_id,trunc(sysdate))) drs
                WHERE xppe.person_id = drs.column_value
                  AND xppe.perf_plan_id = xpp.perf_plan_id)
          AND trunc(sysdate) between rslt.effective_start_date and rslt.effective_end_date
          

        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'UpdatePlan'
    ,p_description        => 'Publish a performance plan'
    ,p_methods            => 'PUT'
    ,p_path               => 'publish/{planId}'
    ,p_plsql              =>
      q'[
        DECLARE
          l_perf_plan_id varchar2(100):= {pathParam.planId};
          l_resp xxas_http_resp_obj;
        BEGIN
        
          -- Check for publish flag and the access to publish
          IF xxas_com_sec_pkg.test_instance('XXAS_BA_PUB_PLAN',l_perf_plan_id) = 'Y' THEN
               
            xxas_perf_plans_pkg.request_publication(l_perf_plan_id);
            
          END IF;
          
          l_resp := xxas_http_response
                      .ok(xxas_com_msg_pkg.get_success_message_xml
                           (xxas_com_msg_type(xxas_util_pkg.app, 'XXAS_PERF_PLAN_INFO_MSG', null)
                           ,'performance-setup')
                         )
                      .build();
         
         {response} := l_resp;
         
        END
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeletePlan'
    ,p_description        => 'Deletes a performance plan'
    ,p_path               => '{planId}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
          l_perf_plan_id varchar2(100):= {pathParam.planId};
          l_resp xxas_http_resp_obj;
          
        BEGIN
        
          -- Check access to delete the plan
          IF xxas_com_sec_pkg.test_instance('XXAS_BA_DELETE_PLAN',l_perf_plan_id) = 'Y' THEN
               
            xxas_perf_plans_pkg.delete_row(l_perf_plan_id);
          
          END IF;
          
          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
      ]'
    );
    
   xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetReviewsByEnrollment'
    ,p_description        => 'Gets reviews by enrollment'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'reviews/by-enrollment/{functionName}/{enrollmentId}'
    ,p_sql                =>
      q'[
         SELECT XMLElement("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                   , xxas_com_sec_pkg.get_obj_data_w_binds
          ({pathParam.functionName}
          ,xxas_com_varchar2_table('PERF_PLAN_ENROLLMENT_ID = :PERF_PLAN_ENROLLMENT_ID')
          ,xxas_com_srv_param_varray
            (xxas_com_srv_param_value('PERF_PLAN_ENROLLMENT_ID', {pathParam.enrollmentId}))
          , null -- p_rowset_tag_name 
          ,'item' -- p_row_tag_name 
          ,{requestParam.pageNumber}
          ,{requestParam.perPage})) "data"
      FROM dual
      ]'
    );     
    
END;
/
COMMIT;
EXIT;
