SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION SAILPOINT.ES_GET_MAPICS_USER_INFO ( 
	ENVIRONMENT CHAR(2) , 
	NAME_OR_ID VARCHAR(60) DEFAULT  NULL  ) 
	RETURNS CLOB(2147483647)   
	LANGUAGE SQL 
	SPECIFIC SAILPOINT.ESRTVMUSRI 
	NOT DETERMINISTIC 
	MODIFIES SQL DATA 
	CALLED ON NULL INPUT 
	NOT FENCED 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DBGVIEW = *SOURCE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
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 ( ENVIRONMENT ) ; 
RETURN 
COALESCE ( 
JSON_OBJECT ( 
'success' : 'true' , 'data' VALUE ( 
SELECT JSON_ARRAYAGG ( 
JSON_OBJECT ( 
'envId' VALUE CISTOOLS . MAPICS_ENV , 
'userId' VALUE TRIM ( 
CAST ( F0BUCD AS VARCHAR ( 10 ) CCSID 37 ) ) , 
'userName' VALUE TRIM ( 
CAST ( F0H1TX AS VARCHAR ( 60 ) CCSID 37 ) ) , 
'operatorId' VALUE TRIM ( 
CAST ( GUAAHY AS VARCHAR ( 5 ) CCSID 37 ) ) , 
'userType' VALUE TRIM ( CAST ( 
CASE EXTYPE 
WHEN ' ' THEN 'General User' 
WHEN 'A' THEN 'Administrator' 
WHEN 'S' THEN 'System/Application User' 
WHEN 'G' THEN 'Generic User' 
ELSE '(Unknown)' 
END AS VARCHAR ( 50 ) CCSID 37 ) ) , 
'userDept' VALUE JSON_OBJECT ( 
TRIM ( CAST ( EXDEPT AS VARCHAR ( 10 ) CCSID 37 ) ) 
VALUE TRIM ( 
CAST ( EUDESC AS VARCHAR ( 60 ) CCSID 37 ) ) 
ABSENT ON NULL 
) , 'userEmpNo' VALUE TRIM ( 
CAST ( EXT08A AS VARCHAR ( 10 ) CCSID 37 ) ) , 
'status' VALUE TRIM ( CAST ( 
CASE EXSTAT 
WHEN 'A' THEN 'Active' 
WHEN ' ' THEN 'Unknown' 
WHEN 'D' THEN 'Disabled' 
END AS VARCHAR ( 15 ) CCSID 37 ) ) , 
'startMenu' VALUE TRIM ( 
CAST ( GUADMW AS VARCHAR ( 15 ) CCSID 37 ) ) , 
'roles' VALUE JSON_ARRAY ( SELECT JSON_OBJECT ( 
TRIM ( CAST ( EUROLE AS VARCHAR ( 20 ) CCSID 37 ) ) 
VALUE TRIM ( 
CAST ( EUDESC AS VARCHAR ( 60 ) CCSID 37 ) ) 
) 
FROM ESUSRP 
JOIN ESROLP 
USING ( EUROLE ) 
WHERE EUUSER = MZ . F0BUCD FORMAT JSON ) , 
'isGroupId' VALUE 
CASE MZ . F0UGRP 
WHEN 1 THEN 'true' 
WHEN 0 THEN 'false' 
END FORMAT JSON 
) 
) 
FROM MZF0REP MZ 
JOIN ESUEXP 
LEFT JOIN ESDPTP 
ON EXDEPT = EUDEPT 
ON MZ . F0BUCD = EXUSER 
LEFT JOIN MZGUCPS0 
ON GUBUCD = MZ . F0BUCD 
WHERE EXTYPE <> 'R' 
AND ( LOCATE ( 
UPPER ( TRIM ( IFNULL ( NAME_OR_ID , MZ . F0BUCD ) ) ) , 
UPPER ( MZ . F0BUCD ) ) > 0 
OR LOCATE ( 
UPPER ( TRIM ( IFNULL ( NAME_OR_ID , MZ . F0BUCD ) ) ) , 
UPPER ( MZ . F0H1TX ) ) > 0 ) ) FORMAT JSON 
) , JSON_OBJECT ( 
'success' VALUE 'false' , 'SQLInfo' VALUE JSON_OBJECT ( 
'messageID' VALUE 'SQL02000' , 
'message' VALUE 'Requested resource not found ' , 
'currentTimestamp' : CURRENT_TIMESTAMP 
) 
) ) ; 
END  ; 

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

GRANT EXECUTE   
ON SPECIFIC FUNCTION SAILPOINT.ESRTVMUSRI 
TO PUBLIC ; 

