SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_CREATE_MAPICS_USER_RECORD ( 
	ENVID CHAR(2) , 
	USERNAME VARCHAR(10) , 
	USERROLES VARCHAR(1000) DEFAULT  NULL  , 
	USERDEPT VARCHAR(10) DEFAULT  ''  , 
	USEREMPNO VARCHAR(8) DEFAULT  ''  ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.ESCRTMUSR 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE LIBL VARCHAR ( 1024 ) DEFAULT '' ; 
DECLARE CUR_USR VARCHAR ( 10 ) DEFAULT '' ; 
DECLARE USER_ROLES VARCHAR ( 1000 ) DEFAULT '' ; 
DECLARE RTNCODE CHAR ( 1 ) DEFAULT '' ; 
DECLARE HEXSTAMP CHAR ( 2 ) FOR BIT DATA DEFAULT '' ; 
DECLARE CONTINUE HANDLER FOR SQLEXCEPTION 
BEGIN 
DECLARE V_SQLSTATE CHAR ( 5 ) ; 
DECLARE V_DB2_RETURN_CODE INT ; 
DECLARE V_DB2_MESSAGE_ID CHAR ( 7 ) ; 
DECLARE V_ERROR_MESSAGE CLOB ( 2048 ) ; 
-- Capture diagnostic info 
GET DIAGNOSTICS CONDITION 1 
V_DB2_RETURN_CODE = DB2_RETURNED_SQLCODE , 
V_SQLSTATE = RETURNED_SQLSTATE , 
V_ERROR_MESSAGE = MESSAGE_TEXT , 
V_DB2_MESSAGE_ID = DB2_MESSAGE_ID ; 
RETURN 
( 
VALUES JSON_OBJECT ( 
'success' VALUE 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'SQLState' VALUE V_SQLSTATE , 
'SQLCode' VALUE V_DB2_RETURN_CODE , 
'messageID' VALUE V_DB2_MESSAGE_ID , 
'message' VALUE V_ERROR_MESSAGE , 
'currentTimestamp' VALUE CURRENT_TIMESTAMP 
) 
) ) ; 
END ; 
SET CISTOOLS . LIBL = CISTOOLS . SET_LIBRARY_LIST ( ENVID ) ; 
SET CUR_USR = CURRENT_USER ; 
SET HEXSTAMP = RIGHT ( GENERATE_UNIQUE ( ) , 2 ) ; 
-- Mzf0rep MERGE (this one looks correct): 
MERGE INTO MZF0REP AS TARGET USING ( 
SELECT AUTHORIZATION_NAME AS F0BUCD , 
IFNULL ( TEXT , AUTHORIZATION_NAME ) AS F0H1TX 
FROM USER_INFO 
WHERE AUTHORIZATION_NAME = USERNAME 
) AS SOURCE 
ON TARGET . F0BUCD = SOURCE . F0BUCD 
WHEN MATCHED THEN UPDATE SET F0H1TX = SOURCE . F0H1TX 
WHEN NOT MATCHED THEN INSERT ( F0BUCD , F0H1TX ) VALUES ( 
SOURCE . F0BUCD , SOURCE . F0H1TX ) ; 
MERGE INTO MZGUCPP AS TARGET USING ( 
SELECT * 
FROM TABLE ( 
VALUES USERNAME 
) S ( GUBUCD ) 
) AS SOURCE 
ON TARGET . GUBUCD = SOURCE . GUBUCD 
WHEN MATCHED THEN UPDATE SET ( GUAAHR , GUAMDT , GUACTM , GUAHVN , 
GUAIVN ) = 
( HEXSTAMP , CISTOOLS . TO_CYMD ( ) , ZONED ( CURRENT_TIME ) , CUR_USR , 
'ESCRTMUSR' ) 
WHEN NOT MATCHED THEN INSERT ( GUBUCD , GUAAHR , GUALDT , GUABTM , 
GUAFVN , GUAGVN ) VALUES ( SOURCE . GUBUCD , HEXSTAMP , 
CISTOOLS . TO_CYMD ( ) , ZONED ( CURRENT_TIME ) , CUR_USR , 
'ESCRTMUSR' ) ; 
MERGE INTO MZBRREP AS TARGET USING ( 
SELECT USERNAME AS BRBUCD , 'ACCMAPICS' AS BRSKXX , '' AS BRBTXX , 
HEXSTAMP AS BRAAHR , CISTOOLS . TO_CYMD ( ) AS BRALDT , 
ZONED ( CURRENT_TIME ) AS BRABTM , CURRENT_USER AS BRAFVN , 
'ESCRTMUSR' AS BRAGVN 
FROM SYSIBM . SYSDUMMY1 
) AS SOURCE 
ON TARGET . BRBUCD = SOURCE . BRBUCD 
AND TARGET . BRSKXX = SOURCE . BRSKXX 
WHEN MATCHED THEN UPDATE SET BRBTXX = SOURCE . BRBTXX , BRAAHR = 
SOURCE . BRAAHR , BRALDT = 
SOURCE . BRALDT , BRABTM = 
SOURCE . BRABTM , BRAFVN = 
SOURCE . BRAFVN , BRAGVN = 
SOURCE . BRAGVN 
WHEN NOT MATCHED THEN INSERT ( BRBUCD , BRSKXX , BRBTXX , BRAAHR , 
BRALDT , BRABTM , BRAFVN , BRAGVN ) VALUES ( SOURCE . BRBUCD , 
SOURCE . BRSKXX , SOURCE . BRBTXX , SOURCE . BRAAHR , SOURCE . BRALDT , 
SOURCE . BRABTM , SOURCE . BRAFVN , SOURCE . BRAGVN ) ; 
-- Esuexp MERGE 
MERGE INTO ESUEXP AS TARGET USING ( 
VALUES ( USERNAME , USERDEPT , 'A' , '1' , USEREMPNO ) 
) AS SOURCE ( EXUSER , EXDEPT , EXSTAT , EXFLG1 , EXT08A ) 
ON TARGET . EXUSER = SOURCE . EXUSER 
WHEN MATCHED THEN UPDATE SET EXDEPT = SOURCE . EXDEPT , EXSTAT = 
SOURCE . EXSTAT , EXFLG1 = 
SOURCE . EXFLG1 , EXT08A = 
SOURCE . EXT08A 
WHEN NOT MATCHED THEN INSERT ( EXUSER , EXDEPT , EXSTAT , EXFLG1 , 
EXT08A ) VALUES ( SOURCE . EXUSER , SOURCE . EXDEPT , SOURCE . EXSTAT , 
SOURCE . EXFLG1 , SOURCE . EXT08A ) ; 
IF ( SELECT 
CASE 
WHEN COUNT ( * ) > 0 THEN '1' 
ELSE '0' 
END 
FROM ESUSRP 
WHERE EUUSER = USERNAME ) = '0' THEN 
CALL SAILPOINT . CREATE_ES_USER_RECORD ( 'ADD' , 'U' , USERNAME , CUR_USR 
) ; 
END IF ; 

IF USERROLES IS NOT NULL 
AND TRIM ( USERROLES ) <> '' THEN 
SET USER_ROLES = USERROLES ; 
ELSE 
SET USER_ROLES = 'ALUSR' ; 
END IF ; 
FOR SELECT DISTINCT FRAGMENT 
FROM TABLE ( 
SELECT FRAGMENT 
FROM TABLE ( 
CISTOOLS . SPLIT_STRING ( 
REGEXP_REPLACE ( 
CAST ( USER_ROLES AS VARCHAR ( 1000 ) CCSID 37 ) , 
'[\\\[|\]|\{|\}|\"|\,|\s+]' , ' ' ) , ' ' ) 
) 
WHERE FRAGMENT <> '' 
AND FRAGMENT IS NOT NULL 
UNION ALL 
SELECT * 
FROM TABLE ( 
VALUES 'ALUSR' 
) 
) 
DO 
CALL SAILPOINT . ASSIGN_ES_USER_TO_ROLE ( CUR_USR , 'G' , FRAGMENT , 
USERNAME ) ;  -- assign default 
END FOR ; 
RETURN 
COALESCE ( 
JSON_OBJECT ( 
'success' : 'true' , 'data' VALUE ( 
SELECT 
JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'userId' VALUE RTRIM ( 
CAST ( EUUSER AS VARCHAR ( 10 ) CCSID 37 ) ) , 
'roles' VALUE JSON_ARRAY ( ROLESJSON FORMAT JSON ) 
) FORMAT JSON 
) AS FINALJSON 
FROM TABLE ( 
SELECT EUUSER , 
JSON_OBJECTAGG ( RTRIM ( 
CAST ( EUROLE AS VARCHAR ( 10 ) CCSID 37 ) ) 
VALUE RTRIM ( 
CAST ( EUDESC AS VARCHAR ( 50 ) CCSID 37 ) ) ) 
AS ROLESJSON 
FROM ESUSRP 
JOIN ESROLP 
USING ( EUROLE ) 
WHERE EUUSER = USERNAME 
GROUP BY EUUSER 
) ) FORMAT JSON 
) , JSON_OBJECT ( 
'success' : 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'messageID' : 'SQL02000' , 
'message' : 'Requested result not found ' , 
'currentTimestamp' : CURRENT_TIMESTAMP 
) 
) ) ; 
END  ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESCRTMUSR 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESCRTMUSR 
TO PUBLIC ; 

