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

BEGIN

  xxas_com_sr_pkg.set_rest_service
    (p_service_name       => l_SERVICE
    ,p_description        => 'View available item templates'
    ,p_path               => 'item-templates'
    ,p_consumes           => 'application/xml'
    ,p_produces           => 'application/xml');
    
  xxas_com_sr_pkg.set_rest_method
    (p_service_name       => l_SERVICE
    ,p_method_name        => 'GetItemTemplates'
    ,p_description        => 'Get item templates'
    ,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")
               ,decode(nvl({requestParam.type}, 'flexible'), 'flexible'
                      ,XMLElement
                        ("item"
                        ,XMLForest
                          ('flexible' "type"
                          ,'flexible' "id"
                          ,'line1 (flexible)' "line1"
                          ,'line2' "line2"
                         ,'line3' "line3"
                          ,'#499324' "statusColor"
                          ,'tick' "icon"
                         ,'statusText' "statusText"
                          ,'decorator' "decorator"
                          ,'timelineText' "timelineText"
                          ,'tick' "timelineIcon"
                          ,'http://www.applaudsolutions.com' "href" 
                          )
                       )
                       ,null)
               ,decode(nvl({requestParam.type}, 'worklist'), 'worklist'
                      ,XMLElement
                        ("item"
                        ,XMLForest
                          ('worklist' "type"
                          ,'worklist' "id"
                          ,'sender' "sender"
                          ,'subject (worklist)' "subject"
                          ,'dateSentFormatted' "dateSentFormatted"
                          ,'assertive' "iconColor"
                          ,'timer' "icon"
                          ,'status' "status"
                          ,'http://www.applaudsolutions.com' "href" 
                          )
                        ,XMLElement
                          ("linked"
                          ,XMLElement
                            ("image"
                            ,XMLElement
                               ("self"
                               ,xxas_com_rest_util_pkg.uri_from_path('people/-1/photo'))
                            ,XMLForest
                               ('IN' "initials"
                               ,xxas_per_image_pkg.url(-1, null, 'SMALL') "small"
                               ,xxas_per_image_pkg.url(-1, null, 'MEDIUM') "medium"
                               ,xxas_per_image_pkg.url(-1, null, 'FULL_SIZE') "fullSize"
                               )
                            )
                          )
                        )
                      ,null)
               ,decode(nvl({requestParam.type}, 'people'), 'people'
                     ,XMLElement
                       ("item"
                       ,XMLForest
                         ('people' "type"
                         ,'people' "id"
                         ,'name (people)' "name"
                         ,'title' "title"
                         ,'http://www.applaudsolutions.com' "href"
                         )
                       ,XMLElement
                         ("availability"
                         ,XMLForest
                           ('freeBusy' "freeBusy"
                           ,'#499324' "color"
                           ,'title' "title"
                           ,'description' "description"
                           ,'lastUpdated' "lastUpdated"
                           ,'tick' "icon"
                           )
                         )
                       ,XMLElement
                         ("linked"
                         ,XMLElement
                           ("image"
                           ,XMLElement
                              ("self"
                              ,xxas_com_rest_util_pkg.uri_from_path('people/-1/photo'))
                           ,XMLForest
                              ('IN' "initials"
                              ,xxas_per_image_pkg.url(-1, null, 'SMALL') "small"
                              ,xxas_per_image_pkg.url(-1, null, 'MEDIUM') "medium"
                              ,xxas_per_image_pkg.url(-1, null, 'FULL_SIZE') "fullSize"
                              )
                           )
                         )
                       )
                     ,null)
               ,decode(nvl({requestParam.type}, 'note'), 'note'
                      ,XMLElement
                       ("item"
                       ,XMLForest
                         ('note' "type"
                         ,'note' "id"
                         ,'author' "author"
                         ,'noteType' "noteType"
                         ,'body (note)' "body"
                         ,'timecard' "icon"
                         ,'assertive' "iconColor"
                         ,'status' "status"
                         ,'dateCreatedFormatted' "dateCreatedFormatted"
                         ,'http://www.applaudsolutions.com' "href"
                         )
                       ,XMLElement
                         ("linked"
                         ,XMLElement
                           ("image"
                           ,XMLElement
                              ("self"
                              ,xxas_com_rest_util_pkg.uri_from_path('people/-1/photo'))
                           ,XMLForest
                              ('IN' "initials"
                              ,xxas_per_image_pkg.url(-1, null, 'SMALL') "small"
                              ,xxas_per_image_pkg.url(-1, null, 'MEDIUM') "medium"
                              ,xxas_per_image_pkg.url(-1, null, 'FULL_SIZE') "fullSize"
                              )
                           )
                         )
                       )
                     ,null)
               ,decode(nvl({requestParam.type}, 'vacancies'), 'vacancies'
                      ,XMLElement
                        ("item"
                        ,XMLForest
                          ('vacancies' "type"
                          ,'vacancies' "id"
                          ,'title (vacancies)' "title"
                          ,'distanceText' "distanceText"
                          ,'location' "location"
                          ,'organizationName' "organizationName"
                          ,'postedFormatted' "postedFormatted"
                          ,'closingFormatted' "closingFormatted"
                          ,'refFormatted' "refFormatted"
                          ,'flag' "flag"
                          ,'assertive' "flagColor"
                          ,'hoursFormatted' "hoursFormatted"
                          ,'salaryFormatted' "salaryFormatted"
                          ,'http://www.applaudsolutions.com' "href" 
                          )
                        ,XMLElement
                          ("fields"
                          ,XMLAttributes
                            ('true' AS "json:force-array"
                            ,'http://json.org/' AS "xmlns:json")
                          ,XMLElement
                            ("field"
                            ,XMLForest
                              ('html' "type"
                              ,'label' "label"
                              ,'value' "value"
                              )
                            )
                          ,XMLElement
                            ("field"
                            ,XMLForest
                              ('html' "type"
                              ,'label' "label"
                              ,'value' "value"
                              )
                            )
                          )
                        ,XMLElement
                          ("asideFields"
                          ,XMLAttributes
                            ('true' AS "json:force-array"
                            ,'http://json.org/' AS "xmlns:json")
                          ,XMLElement
                            ("field"
                            ,XMLForest
                              ('html' "type"
                              ,'label' "label"
                              ,'value' "value"
                              )
                            )
                          ,XMLElement
                            ("field"
                            ,XMLForest
                              ('html' "type"
                              ,'label' "label"
                              ,'value' "value"
                              )
                            )
                          )
                        )
                 ,null)
               ,decode(nvl({requestParam.type}, 'goal'), 'goal'
                      ,XMLElement
                       ("item"
                       ,XMLForest
                         ('goal' "type"
                         ,'goal' "id"
                         ,'title (goal)' "title"
                         ,'http://www.applaudsolutions.com' "href" 
                         ,XMLForest
                           ('NS' "id"
                           ,'label' "label"
                           ,'positive' "positive") "status"
                         ,XMLForest
                           ('50' "value"
                           ,'label' "label") "weighting"
                         )
                       ,(SELECT XMLElement
                                 ("statuses"
                                 ,XMLAttributes
                                   ('true' AS "json:force-array"
                                   ,'http://json.org/' AS "xmlns:json")
                                 ,XMLAgg
                                   (XMLElement
                                     ("statuses"
                                     ,XMLForest
                                       (hrl.lookup_code AS "id"
                                       ,hrl.meaning AS "label"
                                       ,nvl(hrl.description, 'positive') AS "color")
                                     )
                                   )
                                 )
                        FROM  hr_lookups hrl
                        WHERE hrl.lookup_type = 'XXAS_GOAL_STATUS'
                        AND   hrl.enabled_flag = 'Y'
                        AND   trunc(sysdate ) BETWEEN
                              nvl(hrl.start_date_active, trunc(sysdate))
                        AND   nvl(hrl.end_date_active, trunc(sysdate))) "linked"
                       ,XMLElement
                         ("self"
                         ,XMLForest
                           (xxas_com_rest_util_pkg.uri_from_path('goals/-1') "href")
                         ) "links"
                       )
                     ,null)
               ) "data"
      FROM   dual
      WHERE  xxas_wf_role_util_pkg.user_has_role
              ('UMX|XXAS_FUNCTIONAL_CONFIG') = 'Y'
        ]'
    );

    
END;
/
COMMIT;
EXIT;
