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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'GetSchool'
    ,p_path               => '/Establish-search'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml'
    );

  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetSchool'
    ,p_path               => '/Establish'
    ,p_description        => 'Gets the value set values by Id'
    ,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
                     ("qualifications"
                     ,XMLForest
                       (p.name   "id"
                       ,p.name "value"
                       ,p.name "label")
                     )
                   )
                 ) "data"
        FROM  (SELECT pe.name name
               FROM   per_establishments pe
               WHERE   UPPER(pe.name) LIKE UPPER('%' || {requestParam.q} || '%')
               
               UNION
               
               select decode(nvl(pea.establishment_id,0),0,pea.establishment, pe.name) name 
               from  per_establishment_attendances pea,per_establishments pe
               WHERE  pea.establishment_id = pe.establishment_id(+) 
                       and UPPER(pea.establishment) LIKE UPPER('%' || {requestParam.q} || '%'))p
        ]'
    );
    
END;
/
COMMIT;
EXIT;
