/* $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
                       := 'Contacts';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Maintain personal contacts'
    ,p_path               => 'contacts'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'AddNewContact'
    ,p_path               => '{functionId}'
    ,p_description        => 'Initiates creating a new contact'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml                XMLType;
          l_instance_pk1_value varchar2(400);
          l_instance_pk2_value varchar2(400);
          l_instance_pk3_value varchar2(400);
          l_instance_pk4_value varchar2(400);
          l_instance_pk5_value varchar2(400);
          l_resp               xxas_http_resp_obj;
          
        BEGIN

          xxas_emp_view_ui_pkg.create_emp_view_row
            (pViewName         => xxas_com_sec_pkg.get_function_name({pathParam.functionId})
            ,pEffectiveDate    => trunc(sysdate)
            ,pPrimaryKeyId     => xxas_com_asg_pkg.get_primary_work_asg
                                    (fnd_global.employee_id, trunc(sysdate))
            ,pInstancePk1Value => fnd_global.employee_id);
            
          xxas_emp_views_pkg.update_instance_keys
            (p_instance_pk1_value => l_instance_pk1_value
            ,p_instance_pk2_value => l_instance_pk2_value
            ,p_instance_pk3_value => l_instance_pk3_value
            ,p_instance_pk4_value => l_instance_pk4_value
            ,p_instance_pk5_value => l_instance_pk5_value);
              
          l_resp := xxas_http_response
                      .ok(xxas_com_rest_util_pkg.app_route_redirect_response
                           ('contact-edit/'
                         || fnd_global.employee_id
                         || '__'
                         || l_instance_pk3_value
                         || '_'
                         || l_instance_pk4_value
                           ,'modal'
                           )
                         )
                      .build();
         
         {response} := l_resp;

        END
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteContact'
    ,p_description        => 'Delete the given contact'
    ,p_path               => '{context}'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE
        
          l_xml                 XMLType;
          l_function_id         number;
          l_person_id           number;
          l_contact_person_id   number;
          l_transaction_id      number;
          l_transaction_step_id number;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
        
          xxas_per_contact_pkg.context_to_values
            (p_context             => {pathParam.context}
            ,p_function_id         => l_function_id
            ,p_person_id           => l_person_id
            ,p_contact_person_id   => l_contact_person_id
            ,p_transaction_id      => l_transaction_id
            ,p_transaction_step_id => l_transaction_step_id);
          
          xxas_per_contact_pkg.contact_delete
            (p_function_id          => l_function_id
            ,p_person_id            => l_person_id
            ,p_contact_person_id    => l_contact_person_id
            ,p_transaction_id       => l_transaction_id
            ,p_transaction_step_id  => l_transaction_step_id
            ,p_response_xml         => l_xml);
          
          /*l_resp := xxas_http_response
                      .ok(xxas_com_rest_util_pkg.app_route_reload_response)
                      .build();
         */
         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        => 'ViewContactDetails'
    ,p_description        => 'View Contact Details'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_path               => 'contact-view/{context}'
    ,p_sql                =>
      q'[  
          SELECT xxas_per_contact_pkg.get_contact_details({pathParam.context}) "data"
          FROM DUAL
      ]'
    );         
    
        
END;
/
COMMIT;
EXIT;
