/* $Id$ */
/*============================================================================*
 |                 Copyright (c) 2014 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
                       := 'PersonTags';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View and change a person''s tags'
    ,p_path               => 'tags/by-person'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindPersonTags'
    ,p_description        => 'Finds person tags in the tag cloud'
    ,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
                     ("tag"
                     ,XMLForest
                       (rslt.tag_id "id"
                       ,rslt.tag "value"
                       ,rslt.tag "label")
                     )
                   )
                 ) "data"
                 FROM  xxas_com_tag_cloud rslt
                 WHERE (({requestParam.id} IS NOT NULL AND
                         rslt.tag_id = {requestParam.id})
                       OR 
                       ({requestParam.id} IS NULL AND
                         UPPER(tag) LIKE UPPER({requestParam.q})||'%'))
      ]'
    ); 
   
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'UpdatePersonTags'
    ,p_description        => 'Updates a person''s tags'
    ,p_path               => '{personId}'
    ,p_methods            => 'POST'
    ,p_plsql              =>
      q'[
				DECLARE

          l_resp       		xxas_http_resp_obj;
          l_request_xml   XMLType;
          l_response_xml  XMLType;
          
        BEGIN
				
          l_request_xml := {requestBody};
        
          --
          -- Security is handled inside this procedure
          -- because there are separate permissions for
          -- create/delete
          --
          xxas_com_tags_pkg.tags_post_handler
            (p_person_id     => {pathParam.personId}
            ,p_form_data_xml => l_request_xml
            ,p_response_xml  => l_response_xml);

          IF l_response_xml IS NULL THEN
            l_resp := xxas_http_response.ok().build();            
          ELSE
            l_resp := xxas_http_response.ok(l_response_xml).build();
          END IF;
          
				 {response} := l_resp;
          
				END
			]'
		);

END;
/
COMMIT;
EXIT;
