/* $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
                       := 'Blocks';

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Blocks'
    ,p_path               => 'blocks'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlocks'
    ,p_description        => 'Get a page''s blocks'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_emp_menu_pkg.get_page_blocks_xml
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,null
                   ,{requestParam.view})
                 ) "data"
        FROM dual
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockFlexibleListItems'
    ,p_description        => 'Get a flexible block''s list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/flexiview-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_emp_views_pkg.get_flexible_list_items
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId}
                   ,{requestParam.jobId})) "data"
        FROM dual
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockContactsListItems'
    ,p_description        => 'Get a contact block''s list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/contacts-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_per_contact_pkg.get_contacts_items
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId})) "data"
        FROM dual
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockTeamListItems'
    ,p_description        => 'Get a team block''s list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/team-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_emp_teams_pkg.get_team_list_items
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId}
                   ,{requestParam.jobId})
                 ) "data"
        FROM dual
        ]'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockTeamHierarchyListItems'
    ,p_description        => 'Get a team block''s hierarchical list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/team-hier-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT xxas_emp_teams_pkg.get_team_hier_list_items
                 ({pathParam.pageId}
                 ,{requestParam.id}
                 ,{pathParam.blockId}
                 ,{requestParam.jobId}
                 ,{requestParam.chartType}
                 ,xxas_util_pkg.boolean_to_flag(nvl({requestParam.allowNavUp}, 'true'))
                 ,xxas_util_pkg.boolean_to_flag(nvl({requestParam.allowNavDown}, 'true'))
                 ,{requestParam.primaryKeyId}
				 ,nvl({requestParam.ExcludeFilterFlag},'N')
				 ,nvl({requestParam.asgsFilterFlag},'N')) "data"
        FROM   dual
        WHERE  xxas_emp_menu_pkg.has_access_to_block
                 ({pathParam.pageId}
                 ,{requestParam.id}
                 ,{pathParam.blockId}) = 'Y'
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockTeamHierarchyDRListItems'
    ,p_description        => 'Get a team block''s hierarchical DR list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/team-hier-list-dr-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("children"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_emp_teams_pkg.get_org_chart_children_xml
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId}
                   ,{requestParam.chartType}
                   ,{requestParam.parentId}
                   ,null
                   ,xxas_util_pkg.boolean_to_flag(nvl({requestParam.allowNavDown}, 'true'))
				   ,nvl({requestParam.asgsFilterFlag},'N'))
                  ) "data"
        FROM   dual
        WHERE  xxas_emp_menu_pkg.has_access_to_block
                 ({pathParam.pageId}
                 ,{requestParam.id}
                 ,{pathParam.blockId}) = 'Y'
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockLinksListItems'
    ,p_description        => 'Get a link block''s list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/links-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT XMLElement
                 ("data"
                 ,XMLAttributes
                   ('true' as "json:force-array"
                   ,'http://json.org/' as "xmlns:json")
                 ,xxas_emp_links_pkg.get_links_list_items
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId})
                 ) "data"
        FROM dual
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockMiniHierarchyListItems'
    ,p_description        => 'Get a mini hierarchy block''s list items'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/mini-hierarchy-list-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT extract
                 (xxas_emp_teams_pkg.get_mini_hierarchy_list_items
                   ({pathParam.pageId}
                   ,{requestParam.id}
                   ,{pathParam.blockId}
                   ,{requestParam.jobId})
                 ,'/data/*') "data"
        FROM dual
        ]'
    );
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetBlockMiniHierarchyItems'
    ,p_description        => 'Get a team block''s hierarchical list items in the org chart'
    ,p_methods            => 'GET'
    ,p_path               => '{pageId}/team-hier-items/{blockId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
        SELECT nvl(xxas_emp_teams_pkg.get_mini_hierarchy_items
                     ({pathParam.pageId}
                     ,{requestParam.id}
                     ,{pathParam.blockId}
                     ,{requestParam.jobId})
                  ,(SELECT XMLElement("data"
                                     ,XMLForest('1' "test")
                                     ) 
                    from dual)
                  ) "data"
        FROM   dual
        WHERE  xxas_emp_menu_pkg.has_access_to_block
                 ({pathParam.pageId}
                 ,{requestParam.id}
                 ,{pathParam.blockId}) = 'Y'
        ]'
    );
	
	  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'RemoveBlock'
    ,p_description        => 'Remove the Block from Page'
    ,p_path               => 'remove/{menuId}/{functionId}'
    ,p_methods            => 'DELETE'
    ,p_plsql                =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_block_pkg.remove_block
            (p_menu_id              => {pathParam.menuId}
            ,p_function_id          => {pathParam.functionId}
            ,p_response_xml         => l_xml
            );
          
         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        => 'DeleteBlock'
    ,p_description        => 'Delete the Block completly'
    ,p_methods            => 'DELETE'
    ,p_path               => '/delete/{menuId}/{functionId}'
    ,p_plsql                =>
      q'[
        DECLARE
          
          l_xml                 XMLType;
          l_resp                xxas_http_resp_obj;
          
        BEGIN
                  
          xxas_block_pkg.delete_block
            (p_menu_id              => {pathParam.menuId}
            ,p_function_id          => {pathParam.functionId}
            ,p_response_xml         => l_xml
            );
          
         l_resp := xxas_http_response
                      .ok(l_xml)
                      .build();
                      
         {response} := l_resp;

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