/* $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
                       := 'FNDLobs';
                       
BEGIN
      
  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View people''s resumes and letters'
    ,p_path               => 'documents'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetDocument'
    ,p_path               => '{documentId}'
    ,p_description        => 'Gets the given document'
    ,p_methods            => 'GET'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
          SELECT XMLElement
                   ("data"
                   ,xxas_attachment_util_pkg.get_file_xml({pathParam.documentId})
                   ) AS "data"
          FROM   dual
          WHERE  xxas_com_sec_pkg.test_instance
                   ('XXAS_DATA_FNDLOB_VIEW', {pathParam.documentId}) = 'Y'
      ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'UploadDocument'
    ,p_description        => 'Uploads a document'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
        DECLARE
          l_resp    xxas_http_resp_obj;
          l_blob    BLOB;
          l_file_id number;
          l_xml     XMLType;
        BEGIN

          IF NOT xxas_com_sec_pkg.test_rtn_boolean('XXAS_DATA_FNDLOB_UPL') THEN
            l_resp := xxas_http_response
                        .status(xxas_http_resp_status_pkg.unauthorised)
                        .build();
          ELSE
          
            l_blob := xxas_util_pkg.decode_blob({requestBody});

            l_file_id := xxas_util_pkg.insert_fnd_lob
              (p_file_name => {requestParam.fileName}
              ,p_file_data => l_blob);
           
            -- Fix for GIT#1167 
            -- Added COMMIT explicitly after insert_fnd_lob so that call to 
            -- autonomous function fnd_gfm.construct_download_url
            -- can read the record from fnd_lobs
            
            COMMIT;
            
            IF l_file_id IS NULL THEN
              l_resp := xxas_http_response
                          .status(xxas_http_resp_status_pkg.bad_request)
                          .build();        
            ELSE
              
              SELECT XMLElement
                       ("data"
                       ,xxas_attachment_util_pkg.get_file_xml(l_file_id)
                       ,XMLForest
                         (l_file_id            as "fileId"
                         ,fl.file_name         as "fileName"
                         ,fl.file_content_type as "fileMimeType"
                         ,fnd_gfm.construct_download_url
                            (fnd_web_config.gfm_agent, l_file_id) as "fileHref"
						 ,CASE WHEN fl.file_content_type = 'text/html'
							THEN 'language-html5'
						  WHEN fl.file_content_type = 'text/xml' or fl.file_content_type = 'application/xml'
							THEN 'file-xml'
						  WHEN fl.file_content_type = 'application/zip'
							THEN 'zip-box'
						  WHEN fl.file_content_type = 'application/msword'
							THEN 'file-word'
						  WHEN (fl.file_content_type = 'application/ppt') or (fl.file_content_type = 'application/vnd.openxmlformats-officedocument.presentationml.presentation')
							THEN 'file-powerpoint'
						  WHEN fl.file_content_type = 'application/doc'
							THEN 'file-document'
						  WHEN fl.file_content_type = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'
						    THEN 'file-word'
						  WHEN (fl.file_content_type = 'application/msexcel') or (fl.file_content_type = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
							THEN 'file-excel'
						  WHEN (fl.file_content_type = 'text') or (fl.file_content_type = 'application/json') or (fl.file_content_type = 'application/rtf') or (fl.file_content_type = 'text/richtext') or (fl.file_content_type = 'text/plain') or (fl.file_content_type = 'binary')
							THEN 'file'
						  END as "icon"
                         )
                       )
              INTO   l_xml
              FROM   fnd_lobs fl
              WHERE  fl.file_id = l_file_id;
              
              l_resp := xxas_http_response
                          .created(xxas_com_rest_util_pkg.uri_from_path
                                    ('/documents/' || l_file_id))
                          .entity(l_xml)
                          .build();
            END IF;
          END IF;
         
         {response} := l_resp;

        END
      ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'DeleteDocument'
    ,p_description        => 'Deletes the given document'
    ,p_methods            => 'DELETE'
    ,p_plsql              =>
      q'[
        DECLARE

          l_resp        xxas_http_resp_obj;

        BEGIN

          IF {requestParam.documentId} is not null THEN

            IF NOT xxas_com_sec_pkg.test_rtn_boolean('XXAS_DATA_FNDLOB_DEL') THEN

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

            ELSE

              xxas_util_pkg.delete_fnd_lob
                (p_file_id => {requestParam.documentId});

            END IF;

          END IF;

          l_resp := xxas_http_response.ok().build();

          {response} := l_resp;

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