SET PATH *LIBL ; 

CREATE OR REPLACE FUNCTION CISTOOLS.GET_XA_DOMAIN_CLASS_INFO ( 
	OBJECT_CLASS VARCHAR(256) ) 
	RETURNS TABLE ( 
	FIELD_NAME VARCHAR(128) , 
	DESCRIPTION VARCHAR(128) , 
	FIELD_TYPE VARCHAR(20) , 
	FIELD_LENGTH DECFLOAT(16) , 
	USE_IN_CREATE VARCHAR(10) , 
	USE_IN_MAINTAIN VARCHAR(10) , 
	NULLABLE VARCHAR(10) , 
	KEY_POSITION VARCHAR(10) , 
	ACTIONS_XML XML , 
	TITLE_XML XML , 
	OPTIONS_XML XML , 
	RELATIONSHIP_XML XML , 
	TEXT_INFO_XML XML , 
	EXTENDED_PROPERTIES_XML XML )   
	LANGUAGE SQL 
	SPECIFIC CISTOOLS.GET_XA_DOMAIN_CLASS_INFO 
	NOT DETERMINISTIC 
	READS SQL DATA 
	CALLED ON NULL INPUT 
	SET OPTION  ALWBLK = *ALLREAD , 
	ALWCPYDTA = *OPTIMIZE , 
	COMMIT = *NONE , 
	DECRESULT = (31, 31, 00) , 
	DYNDFTCOL = *NO , 
	DYNUSRPRF = *OWNER , 
	SRTSEQ = *HEX   
	BEGIN 
DECLARE SL_RESP XML ; 
-- Build SystemLink request; 
SET SL_REQ = STRIP_SPACES ( '<?xml version=''1.0'' ?>
<!DOCTYPE System-Link SYSTEM ''SystemLinkRequest.dtd''>
<System-Link>
  <Login userId=''&%username%&'' password=''&%password%&'' maxIdle=''600''
         properties=''com.mapics.cas.domain.EnvironmentId=&%env%&,
                      com.mapics.cas.domain.SystemName=&%hostname%&,
                      com.mapics.cas.user.LanguageId=en''/>
  <Request sessionHandle=''*current'' workHandle=''*new''
           broker=''EJB'' maxIdle=''600''>
      <ListDomainClassInfo name=''GetFields_1'' classMnemonic=''&%domainclass%&'' displayProperties=''keySeparated''
           showActions=''true'' showTitles=''true'' showOptions=''true''
           showRelationships=''true'' showTextInfo=''true'' showExtendedPropertyInfo=''true'' />
  </Request>
  <Logout sessionHandle=''*current''/>
</System-Link>' ) ; 
-- URL encode the request 
SET SL_REQ = SL_URL || 'SystemLinkRequest=' || URL_ENCODE ( SL_REQ ) ; 
SET SL_REQ = SL_REQ || '&username=' || 
URL_ENCODE ( TRIM ( SUBSTR ( SL_CREDS , 1 , 10 ) ) ) ; 
SET SL_REQ = SL_REQ || '&password=' || 
URL_ENCODE ( TRIM ( SUBSTR ( SL_CREDS , 11 , 10 ) ) ) ; 
SET SL_REQ = SL_REQ || '&env=' || URL_ENCODE ( MAPICS_ENV ) ; 
SET SL_REQ = SL_REQ || '&hostname=' || URL_ENCODE ( TRIM ( HOST_NAME ) ) ; 
SET SL_REQ = SL_REQ || '&domainclass=' || URL_ENCODE ( OBJECT_CLASS ) ; 
-- Call SystemLink 
SET SL_RESP = XMLPARSE ( DOCUMENT QSYS2 . HTTP_GET ( SL_REQ ) ) ; 
RETURN 
( 
SELECT K . FIELD_NAME , K . DESCRIPTION , K . FIELD_TYPE , 
K . FIELD_LENGTH , K . USE_IN_CREATE , 
K . USE_IN_UPDATE AS USE_IN_MAINTAIN , K . NULLABLE , 
CAST ( ROW_NUMBER ( ) OVER ( 
) AS VARCHAR ( 10 ) ) AS KEY_POSITION , 
-- Attach full XML blocks 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/ActionsAllowed' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS ACTIONS_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/FormTitle' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS TITLE_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/CopyOptionInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS OPTIONS_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/RelationshipInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS RELATIONSHIP_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/TextRelationshipInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS TEXT_INFO_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/PropertyExtendedInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS EXTENDED_PROPERTIES_XML 
FROM ( 
VALUES SL_RESP 
) AS D ( XMLDOC ) 
LEFT JOIN 
XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/KeyInfo/PropertyInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS FIELD_NAME VARCHAR ( 128 ) 
PATH '@path' , DESCRIPTION VARCHAR ( 128 ) PATH 'text()' , 
FIELD_TYPE VARCHAR ( 20 ) PATH '@format' , FIELD_LENGTH DECFLOAT ( 
16 ) PATH '@length' , USE_IN_CREATE VARCHAR ( 10 ) PATH 
'@canCreate' , USE_IN_UPDATE VARCHAR ( 10 ) PATH '@canUpdate' , 
NULLABLE VARCHAR ( 10 ) PATH '@isNullAllowed' ) AS K 
ON 1 = 1 
UNION ALL 
SELECT P . FIELD_NAME , P . DESCRIPTION , P . FIELD_TYPE , 
P . FIELD_LENGTH , P . USE_IN_CREATE , 
P . USE_IN_UPDATE AS USE_IN_MAINTAIN , P . NULLABLE , 
CAST ( NULL AS VARCHAR ( 10 ) ) AS KEY_POSITION , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/ActionsAllowed' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS ACTIONS_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/FormTitle' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS TITLE_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/CopyOptionInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS OPTIONS_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/RelationshipInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS RELATIONSHIP_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/TextRelationshipInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS TEXT_INFO_XML , 
( 
SELECT * 
FROM TABLE ( 
SELECT X 
FROM XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/PropertyExtendedInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS X XML PATH '.' ) 
) 
LIMIT 1 ) AS EXTENDED_PROPERTIES_XML 
FROM ( 
VALUES SL_RESP 
) AS D ( XMLDOC ) 
LEFT JOIN 
XMLTABLE ( 
'$doc/System-Link/Response/ListDomainClassInfoResponse/DomainClassInfo/PropertyInfo' 
PASSING D . XMLDOC AS "doc" COLUMNS FIELD_NAME VARCHAR ( 128 ) 
PATH '@path' , DESCRIPTION VARCHAR ( 128 ) PATH 'text()' , 
FIELD_TYPE VARCHAR ( 20 ) PATH '@format' , FIELD_LENGTH DECFLOAT ( 
16 ) PATH '@length' , USE_IN_CREATE VARCHAR ( 10 ) PATH 
'@canCreate' , USE_IN_UPDATE VARCHAR ( 10 ) PATH '@canUpdate' , 
NULLABLE VARCHAR ( 10 ) PATH '@isNullAllowed' ) AS P 
ON 1 = 1 ) ; 
END  ; 

COMMENT ON PARAMETER SPECIFIC FUNCTION CISTOOLS.GET_XA_DOMAIN_CLASS_INFO 
( OBJECT_CLASS IS 'Object Class' ) ; 

LABEL ON SPECIFIC FUNCTION CISTOOLS.GET_XA_DOMAIN_CLASS_INFO 
	IS 'Get Xa Domain Class Info' ; 

GRANT ALTER , EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_XA_DOMAIN_CLASS_INFO 
TO AMAPICS WITH GRANT OPTION ; 

GRANT EXECUTE   
ON SPECIFIC FUNCTION CISTOOLS.GET_XA_DOMAIN_CLASS_INFO 
TO PUBLIC ; 

