/* $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
                       := 'SavedSearches';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View and maintain saved searches'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'SaveJobSearch'
    ,p_description        => 'Saves a job search'
    ,p_methods            => 'POST'
    ,p_path               => 'saved-searches'
    ,p_plsql              =>
      q'[
        DECLARE
          l_ss_id    number;
          l_resp     xxas_http_resp_obj;
        BEGIN
       
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_SAVED_SEARCH_CREATE', fnd_global.user_id) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE

            --xxas_saved_search_pkg.create_irc_saved_search 
            xxas_saved_search_pkg.save_search
              (p_user_id       => fnd_global.user_id
              ,p_form_data_xml => {requestBody});

            l_resp := xxas_http_response
                        .ok(xxas_com_msg_pkg.get_success_message_xml
                         (xxas_com_msg_type(xxas_util_pkg.app, 'XXAS_CAND_SAVE_SRCH_SUCCESS',null)))
                        .build();
          END IF;
         
         {response} := l_resp;

        END
      ]'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetSavedSearches'
    ,p_description        => 'Get a list of saved searches'
    ,p_path               => '/users/{userId}/saved-searches'
    ,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
                   ("savedSearch"
                   ,XMLForest
                     (xxas_com_rest_util_pkg.uri_from_path
                       ('users/' || {pathParam.userId} ||
                        '/saved-searches/' || rslt.saved_search_id) "self"
                     ,rslt.saved_search_id "id"
                     ,rslt.search_name "name"
                     ,xxas_util_pkg.decode_last_updated
                       (usr.user_name, usr.last_update_date
                       ,papf.full_name, papf.global_name, papf.local_name
                       ,papf.first_name, papf.last_name, papf.known_as) "lastChange"
                     )
                   ,rslt.search_parameters) ORDER BY rslt.last_update_date DESC)) "data"
      FROM   xxas_saved_searches_v rslt
            ,fnd_user usr
            ,per_all_people_f papf
      WHERE  rslt.user_id = {pathParam.userId}
      AND    rslt.last_updated_by = usr.user_id
      AND    usr.employee_id = papf.person_id(+)
      AND    trunc(sysdate) BETWEEN
             papf.effective_start_date(+) AND papf.effective_end_date(+)
      AND   {authFunctionPredicate.XXAS_DATA_SAVED_SEARCH_VIEW}
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetSavedSearch'
    ,p_description        => 'Get one saved search'
    ,p_methods            => 'GET'
    ,p_path               => '/users/{userId}/saved-searches/{savedSearchId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
      SELECT XMLElement
               ("data"
               ,XMLAttributes
                 ('true' as "json:force-array"
                 ,'http://json.org/' as "xmlns:json")
               ,XMLElement
                 ("savedSearch"
                 ,XMLForest
                   (xxas_com_rest_util_pkg.uri_from_path
                     ('users/' || {pathParam.userId} ||
                      '/saved-searches/' || rslt.saved_search_id) "self"
                   ,rslt.saved_search_id "id"
                   ,rslt.search_name "name"
                   ,xxas_util_pkg.decode_last_updated
                       (usr.user_name, usr.last_update_date
                       ,papf.full_name, papf.global_name, papf.local_name
                       ,papf.first_name, papf.last_name, papf.known_as) "lastChange"
                   )
                 ,rslt.search_parameters)) "data"
      FROM   xxas_saved_searches_v rslt
            ,fnd_user usr
            ,per_all_people_f papf
      WHERE  rslt.user_id = {pathParam.userId}
      AND    rslt.saved_search_id = {pathParam.savedSearchId}
      AND    rslt.last_updated_by = usr.user_id
      AND    usr.employee_id = papf.person_id(+)
      AND    trunc(sysdate) BETWEEN
             papf.effective_start_date(+) AND papf.effective_end_date(+)
      AND   {authFunctionPredicate.XXAS_DATA_SAVED_SEARCH_VIEW}
      ]'
    );
       
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'SaveSearch'
    ,p_description        => 'Saves a search'
    ,p_methods            => 'POST'
    ,p_path               => '/users/{userId}/saved-searches'
    ,p_plsql              =>
      q'[
        DECLARE
          l_ss_id number;
          l_path  varchar2(4000);
          l_resp  xxas_http_resp_obj;
        BEGIN
       
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_SAVED_SEARCH_CREATE', {pathParam.userId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
            l_ss_id :=
              xxas_saved_search_pkg.save_search 
                (p_user_id => {pathParam.userId}
                ,p_search  => xxas_saved_search_pkg.xml_to_obj
                                          (XMLType({requestBody})));

            l_path := '/users/' || {pathParam.userId} ||
                      '/saved-searches/' || to_char(l_ss_id);
                
            l_resp := xxas_http_response
                        .created(xxas_com_rest_util_pkg.uri_from_path(l_path))
                        .entity(xxas_com_rest_mgr_pkg.get_xml_entity(l_path))
                        .build();
          END IF;
         
         {response} := l_resp;

        END
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteSearch'
    ,p_description        => 'Deletes the given saved search'
    ,p_methods            => 'DELETE'
    ,p_path               => '/users/{userId}/saved-searches/{savedSearchId}'
    ,p_plsql              =>
      q'[
        DECLARE
              
          l_resp  xxas_http_resp_obj;
              
        BEGIN
       
          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
                  ('XXAS_DATA_SAVED_SEARCH_DEL', {pathParam.savedSearchId})
          THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
            xxas_saved_search_pkg.delete_search
              (p_saved_search_id => {pathParam.savedSearchId});       
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.no_content)
                        .build();
          END IF;
         
          {response} := l_resp;

        END
      ]'
    );
    
END;
/
COMMIT;
EXIT;
