--------------------------------------------------------
--  DDL for Function FNC_IDM_GROUPS
--------------------------------------------------------

  CREATE OR REPLACE EDITIONABLE FUNCTION "ENVX_OPER"."FNC_IDM_GROUPS" (
   pi_group_list IN CLOB
) RETURN CLOB AS
   v_input_string VARCHAR2(4000) := pi_group_list;
   v_output_json  CLOB := '';
   v_domain_table sys.odcivarchar2list;
   v_json_obj     json;
   v_domain_name  VARCHAR2(100) := '';
   v_domain_aux   VARCHAR2(100) := '';
   v_group_name   VARCHAR2(100) := '';
   v_element      NUMBER := 1;
BEGIN
   IF pi_group_list IS NULL THEN
      RETURN NULL;
   ELSE
      
  -- Parse comma-separated string into a table
      v_domain_table := sys.odcivarchar2list();
      v_domain_table.extend(regexp_count(
                                        v_input_string,
                                        ','
                            ) + 1);
      v_output_json  := '{';
      FOR i IN 1..v_domain_table.count LOOP
         v_domain_table(i) := regexp_substr(
                                           v_input_string,
                                           '[^,]+',
                                           1,
                                           i
                              );
         v_domain_name     := replace(
                                 nvl(
                                    regexp_substr(
                                                 v_domain_table(i),
                                                 '(.+)(\/)',
                                                 1,
                                                 1
                                    ),
                                    'others'
                                 ),
                                 '/',
                                 ''
                          );
         v_group_name      := regexp_substr(
                                      v_domain_table(i),
                                      '(.+\/)?(.+)',
                                      1,
                                      1,
                                      NULL,
                                      2
                         );

         IF v_domain_aux IS NULL THEN
            v_output_json := v_output_json
                             || '"'
                             || v_domain_name
                             || '":[';
            v_domain_aux  := v_domain_name;
         ELSIF v_domain_aux != v_domain_name THEN
            v_output_json := v_output_json
                             || '],"'
                             || v_domain_name
                             || '":[';
            v_domain_aux  := v_domain_name;
            v_element     := 1;
         END IF;

         v_output_json     := v_output_json
                          || (
            CASE
               WHEN v_element != 1 THEN
                  ','
               ELSE ''
            END
         )
                          || '"'
                          || v_group_name
                          || '"';
         v_element         := v_element + 1;
      END LOOP;
      v_output_json  := v_output_json || ']}';
      RETURN v_output_json;
   END IF;
   
EXCEPTION
   WHEN OTHERS THEN
      RETURN NULL;
END;

/
