/* $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
                       := 'PeopleResumesAndLetters';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View people''s resumes and letters'
    ,p_path               => '/people/{personId}/resumes-letters'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetResumesAndLetters'
    ,p_description        => 'Get resumes and letters for one person'
    ,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
                       ("document"
                       ,XMLForest
                         (rslt.document_id "id"
                         ,xxas_com_rest_util_pkg.uri_from_path
                           ('/people/' || {pathParam.personId} || '/resumes-letters/' || rslt.document_id || '?mode=download_blob') "href"
                         ,rslt.file_name "fileName"
                         ,rslt.mime_type "contentType"
                         ,rslt.description "description"
                         ,xxas_util_pkg.decode_last_updated
                           (usr.user_name, rslt.last_update_date
                           ,papf.full_name, papf.global_name, papf.local_name
                           ,papf.first_name, papf.last_name, papf.known_as) "lastChange"
                         )
                       )
                     ORDER BY decode(rslt.type, 'RESUME', 1, 'AUTO_RESUME', 1, 2), rslt.last_update_date desc, rslt.file_name 
                     )
                   ) AS "data"
          FROM   irc_documents rslt
                ,fnd_user usr
                ,per_all_people_f papf
          WHERE  rslt.person_id = {pathParam.personId}
          AND    rslt.end_date IS NULL
          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_IRC_DOC_VIEW}
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetResumeOrLetter'
    ,p_description        => 'Get one resume or letter document'
    ,p_methods            => 'GET'
    ,p_path               => '{documentId}'
    ,p_produces           => 'application/*'
    ,p_sql                =>
      q'[
         SELECT xxas_http_response.ok()
               .entity(rslt.binary_doc, rslt.mime_type)
               .header(''Content-Disposition'',''attachment;filename="''||rslt.file_name||''"'')
               .build() "httpResponse"
         FROM   irc_documents rslt
         WHERE  rslt.document_id = {pathParam.documentId}
         AND    {authFunctionPredicate.XXAS_DATA_IRC_DOC_VIEW}
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'UploadResumeOrLetter'
    ,p_description        => 'Uploads a resume or letter'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp    xxas_http_resp_obj;
          l_blob    BLOB;
          l_doc_id  number;
          l_href    varchar2(1000);
          l_xml     XMLType;
          
        BEGIN

          IF NOT xxas_com_sec_pkg.test_instance_rtn_boolean
            ('XXAS_DATA_IRC_DOC_UPL', {pathParam.personId}) THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
            l_blob := xxas_util_pkg.decode_blob({requestBody});
            
            l_doc_id := xxas_applicant_pkg.save_attachments
              (p_person_id        => {pathParam.personId}
              ,p_irc_document_tbl =>
                xxas_irc_document_table
                  (xxas_irc_document_type
                    (document_id => null
                    ,file_name   => {requestParam.fileName}
                    ,type        => {requestParam.type}
                    ,description => null
                    ,mime_type   => {requestParam.fileMimeType}
                    ,binary_doc  => l_blob)
                  )
               );

            IF l_doc_id IS NULL THEN
              l_resp := xxas_http_response
                          .status(xxas_http_resp_status_pkg.bad_request)
                          .build();        
            ELSE
            
              l_href := xxas_com_rest_util_pkg.uri_from_path
                           ('/people/' || {pathParam.personId} || '/resumes-letters/' || l_doc_id || '?mode=download_blob');
            
              SELECT XMLElement
                       ("data"
                       ,XMLForest
                         (doc.document_id as "fileId"
                         ,doc.file_name   as "fileName"
                         ,doc.mime_type   as "fileMimeType"
                         ,l_href          as "fileHref"
                         )
                       )
              INTO   l_xml
              FROM   irc_documents doc
              WHERE  doc.document_id = l_doc_id;
              
              l_resp := xxas_http_response
                          .created(l_href)
                          .entity(l_xml)
                          .build();
            END IF;
          END IF;
         
         {response} := l_resp;

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