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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'Find Wf responses'
    ,p_path               => '/wfresponses'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindResponses'
    ,p_description        => 'Find Wf responses matching the given criteria'
    ,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
                     ("role"
                     ,XMLForest
                       (p.lookup_code as "id"
                       ,p.meaning      as "label"
                       ,p.meaning      as "value"
                       )
                     )
                   )
                 )"data"
            FROM   (  SELECT wl.lookup_code,wl.meaning FROM
                      WF_MESSAGE_ATTRIBUTES_VL MsgAttribsEO
                             ,WF_lookups wl
                      WHERE  MsgAttribsEO.MESSAGE_TYPE = substr({requestParam.type},1,instr({requestParam.type},',')-1)
                      AND    MsgAttribsEO.MESSAGE_NAME   = substr({requestParam.type},instr({requestParam.type},',')+1)
                      AND    MsgAttribsEO.SUBTYPE        = 'RESPOND'
                      AND    MsgAttribsEO.TYPE   IN ('LOOKUP')
                      AND    wl.lookup_type = MsgAttribsEO.format
                      UNION
                      SELECT 'CLOSE' lookup_code, 'CLOSE' meaning FROM
                      DUAL where not exists(select 1 from 
                                            WF_MESSAGE_ATTRIBUTES_VL MsgAttribsEO
                                                   ,WF_lookups wl
                                            WHERE  MsgAttribsEO.MESSAGE_TYPE = substr({requestParam.type},1,instr({requestParam.type},',')-1)
                                            AND    MsgAttribsEO.MESSAGE_NAME   = substr({requestParam.type},instr({requestParam.type},',')+1)
                                            AND    MsgAttribsEO.SUBTYPE        = 'RESPOND'
                                            AND    MsgAttribsEO.TYPE   IN ('LOOKUP')
                                            AND    wl.lookup_type = MsgAttribsEO.format
                                            )
                      
                   )p
        ]'
    );
    
     xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindResponsesByPathParam'
    ,p_description        => 'Find Wf responses matching the given criteria'
    ,p_methods            => 'GET'
    ,p_path               => '/{type}'
    ,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
                     ("role"
                     ,XMLForest
                       (p.lookup_code as "id"
                       ,p.meaning      as "label"
                       ,p.meaning      as "value"
                       )
                     )
                   )
                 )"data"
            FROM   (  SELECT wl.lookup_code,wl.meaning FROM
                      WF_MESSAGE_ATTRIBUTES_VL MsgAttribsEO
                             ,WF_lookups wl
                      WHERE  MsgAttribsEO.MESSAGE_TYPE = substr({pathParam.type},1,instr({pathParam.type},',')-1)
                      AND    MsgAttribsEO.MESSAGE_NAME   = substr({pathParam.type},instr({pathParam.type},',')+1)
                      AND    MsgAttribsEO.SUBTYPE        = 'RESPOND'
                      AND    MsgAttribsEO.TYPE   IN ('LOOKUP')
                      AND    wl.lookup_type = MsgAttribsEO.format
                      UNION
                      SELECT 'CLOSE' lookup_code, 'CLOSE' meaning FROM
                      DUAL where not exists(select 1 from 
                                            WF_MESSAGE_ATTRIBUTES_VL MsgAttribsEO
                                                   ,WF_lookups wl
                                            WHERE  MsgAttribsEO.MESSAGE_TYPE = substr({pathParam.type},1,instr({pathParam.type},',')-1)
                                            AND    MsgAttribsEO.MESSAGE_NAME   = substr({pathParam.type},instr({pathParam.type},',')+1)
                                            AND    MsgAttribsEO.SUBTYPE        = 'RESPOND'
                                            AND    MsgAttribsEO.TYPE   IN ('LOOKUP')
                                            AND    wl.lookup_type = MsgAttribsEO.format
                                            )
                      
                   )p
        ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindResponseTypes'
    ,p_description        => 'Find Response Types'
    ,p_methods            => 'GET'
    ,p_path               => '/respond-types/{itemtype}/{msgname}'
    ,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
                     ("role"
                     ,XMLForest
                       (p.code as "id"
                       ,p.meaning      as "label"
                       ,p.meaning      as "value"
                       )
                     )
                   )
                 )"data"
            FROM   (  SELECT 'REASSIGN' code 
                            ,'Reassign' meaning
                      FROM   dual
                      UNION
                      SELECT 'RESPOND' code 
                            ,'Automatic Respond' meaning
                      FROM   dual
                      WHERE  substr({pathParam.msgname},2) IS NOT NULL
                      UNION
                      SELECT 'NOOP' code 
                            ,'Deliver notifications to me regardless of any general rules' meaning
                      FROM   dual
                      WHERE  substr({pathParam.itemtype},2) &lt;&gt; '*'                      
                   )p
        ]'
    );
    
    xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'FindResponsesByNotificationId'
    ,p_description        => 'Find Response Types'
    ,p_methods            => 'GET'
    ,p_path               => '/by-notification/{notificationId}'
    ,p_remove_root_ele    => true
    ,p_sql                =>
      q'[
            select xxas_com_worklist_pkg.get_notific_responses({pathParam.notificationId},{requestParam.formulaName}) "data" from dual
        ]'
    );


END;
/
COMMIT;
EXIT;
