LANGUAGE SQL
sp:BEGIN   

                        -- clean-up temporary tables / session tables
						BEGIN
							DECLARE CONTINUE HANDLER FOR SQLSTATE '42704'
							BEGIN END;
							
							COMMIT;
							DROP TABLE SESSION.p;

						END;




                       -- declare temp table
                       DECLARE GLOBAL TEMPORARY TABLE session.p
		               (
							ProgramID INTEGER ,
							ProgramName VARCHAR(256) ,
							Ancestor VARCHAR(256) ,
							ProgramTypeID INTEGER ,
							ProgramTypeName VARGRAPHIC(50) ,
							ProgStartRow INTEGER ,
							ProgStartCol INTEGER ,
							ProgEndRow INTEGER ,
							ProgEndCol INTEGER ,
							PathID INTEGER
		                )   CCSID UNICODE
			                ON COMMIT PRESERVE ROWS
							   NOT LOGGED;
					         
	              
                      

                       -- fill in temp table
                         INSERT INTO session.p
                         SELECT  prg.ProgramID,
								 prg.ProgramName,
								 prg.Ancestor,
								 prg.ProgramTypeID,
								 pt.ProgramType AS ProgramTypeName,
								 occ.StartRow AS ProgStartRow,
								 occ.StartCol AS ProgStartCol,
								 occ.EndRow AS ProgEndRow,
								 occ.EndCol AS ProgEndCol,
								 occ.PathID--pth.PathStr
							FROM   (
										SELECT p1.ProgramID,
											   p2.AliasName AS ProgramName,
											   p1.Ancestor,
											   p1.ProgramTypeID,
											   p1.OccurID
										FROM Programs p1
											 INNER JOIN ProgramAliases p2 ON p1.ProgramID = p2.ProgramID
																				 AND p2.AliasType = 0
									) prg 
							    INNER JOIN ProgramTypes pt ON pt.ProgramTypeID = prg.ProgramTypeID
							    INNER JOIN Occurrences occ ON prg.OccurID = occ.OccurID
							    --INNER JOIN Paths pth ON occ.PathID = pth.PathID
						         ;
                                 COMMIT;


				            BEGIN
						      COMMIT; 
				              TRUNCATE TABLE cacheEZViewer_Usage_TPMSX ;
				            END;

                            -- fill in cache table
                            LOCK TABLE  cacheEZViewer_Usage_TPMSX IN EXCLUSIVE MODE ;-- lock table
							INSERT INTO cacheEZViewer_Usage_TPMSX
							SELECT TPMSXScreenID, TPMSXScreenName,
								   StatementTypeID, StatementTypeName, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn, pth1.PathStr AS StmtPathStr,
								   ID, ParentName, Ancestor, TypeID, TypeDescription, pth2.PathStr AS Path, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
								   pth3.PathStr AS ScreenPath, ScreenStartRow, ScreenStartCol, ScreenEndRow, ScreenEndCol
							FROM (
									  SELECT
										  res.ResourceID AS TPMSXScreenID,
										  res.Name AS TPMSXScreenName,
										  st.StatementType AS StatementTypeID,
										  st.Description AS StatementTypeName,
										  os.StartRow AS StatementStartRow,
										  os.StartCol AS StatementStartColumn,
										  os.EndRow AS StatementEndRow,
										  os.EndCol AS StatementEndColumn,
										  os.PathID AS StmtPathID,
										  p1.ProgramID AS ID,
										  p1.ProgramName AS ParentName,
										  p1.Ancestor AS Ancestor,
										  p1.ProgramTypeID AS TypeID,
										  p1.ProgramTypeName as TypeDescription,
										  p1.PathID AS PathID,
										  p1.ProgStartRow,
										  p1.ProgStartCol,
										  p1.ProgEndRow,
										  p1.ProgEndCol,
										  p2.PathID AS ScreenPathID,
										  ElementSourceInfo.StartRow AS ScreenStartRow,
										  ElementSourceInfo.StartCol AS ScreenStartCol,
										  ElementSourceInfo.EndRow AS ScreenEndRow,
										  ElementSourceInfo.EndCol AS ScreenEndCol
									  FROM session.p p1
										  INNER JOIN OccurrencesStmt os ON p1.ProgramID = os.ProgID
										  INNER JOIN StatementReference sr ON sr.OccurID = os.OccurID
										  --INNER JOIN Paths AS PathsStmt ON os.PathID = PathsStmt.PathID 
										  INNER JOIN Statements st ON sr.StatementType = st.StatementType 
										  INNER JOIN Resources res ON sr.ResourceID = res.ResourceID
										  LEFT OUTER JOIN DDCLElement de ON de.DDsTypeId = 14 AND UPPER(de.Name) = UPPER(res.Name) --screen 
										  LEFT OUTER JOIN DDCLSourceInfo AS ElementSourceInfo ON de.SourceInfo=ElementSourceInfo.Id 
										  LEFT OUTER JOIN session.p p2 ON ElementSourceInfo.ProgramId=p2.ProgramID 	
									  WHERE sr.ResourceType = 101
										  AND p1.ProgramTypeID = 1 
										  AND os.StatementType IN (1838, 1839)
										  AND p1.PathID IS NOT NULL 
										  AND os.PathID IS NOT NULL 
							
							
									  UNION
									  SELECT
										  -1 AS TPMSXScreenID,
										  'NA' AS TPMSXScreenName,
										  st.StatementType AS StatementTypeID,
										  st.Description AS StatementTypeName,
										  os.StartRow AS StatementStartRow,
										  os.StartCol AS StatementStartColumn,
										  os.EndRow AS StatementEndRow,
										  os.EndCol AS StatementEndColumn,
										  os.PathID AS StmtPathID,
										  p1.ProgramID AS ID,
										  p1.ProgramName AS ParentName,
										  p1.Ancestor AS Ancestor,
										  p1.ProgramTypeID AS TypeID,
										  p1.ProgramTypeName as TypeDescription,
										  p1.PathID AS PathID,
										  p1.ProgStartRow,
										  p1.ProgStartCol,
										  p1.ProgEndRow,
										  p1.ProgEndCol,
										  CAST(NULL AS INTEGER) AS ScreenPathID,
										  CAST(NULL AS INTEGER) AS ScreenStartRow,
										  CAST(NULL AS INTEGER) AS ScreenStartCol,
										  CAST(NULL AS INTEGER) AS ScreenEndRow,
										  CAST(NULL AS INTEGER) AS ScreenEndCol
									  FROM session.p p1
										  INNER JOIN OccurrencesStmt os ON os.ProgID = p1.ProgramID
										  --INNER JOIN Paths AS PathsStmt ON os.PathID = PathsStmt.PathID
										  INNER JOIN Statements st ON os.StatementType = st.StatementType
									  WHERE p1.ProgramTypeID = 1
									    AND os.StatementType IN (1838, 1839)
							            AND os.OccurID NOT IN
															  (
											     			   SELECT OccurID
											     			   FROM   StatementReference
											     			   WHERE  ResourceType = 101
															  )
							            AND p1.PathID IS NOT NULL 
							            AND os.PathID IS NOT NULL 
							
							
									  --add ddcl components DIALOGUE and EXCHANGE that relate to the screen
									  UNION
									  SELECT 
										  de.Id AS TPMSXScreenID,
										  dv.ReferenceName AS TPMSXScreenName,
										  CAST(NULL AS INTEGER) AS StatementTypeID,
										  'DISPLAY-STRUCTURE' AS StatementTypeName,
										  CallSourceInfo.StartRow AS StatementStartRow,
										  CallSourceInfo.StartCol AS StatementStartColumn,
										  CallSourceInfo.EndRow AS StatementEndRow,
										  CallSourceInfo.EndCol AS StatementEndColumn,
										  p3.PathID AS StmtPathID,
										  dv.ElementId AS ID,
										  DDCLElementParent.Name AS ParentName,
										  CAST(NULL AS VARCHAR(256)) AS Ancestor,
										  DDCLElementParent.DdsTypeId AS TypeID,
										  ddt.Description as TypeDescription,
										  p2.PathID AS PathID,
										  ParentSourceInfo.StartRow AS ProgStartRow,
										  ParentSourceInfo.StartCol AS ProgStartCol,
										  ParentSourceInfo.EndRow AS ProgEndRow,
										  ParentSourceInfo.EndCol AS ProgEndCol,
										  p1.PathID AS ScreenPathID,
										  ElementSourceInfo.StartRow AS ScreenStartRow,
										  ElementSourceInfo.StartCol AS ScreenStartCol,
										  ElementSourceInfo.EndRow AS ScreenEndRow,
										  ElementSourceInfo.EndCol AS ScreenEndCol
									  FROM DDCLElement de
										  RIGHT OUTER JOIN DDCLValue dv ON de.DdsTypeId=dv.ReferenceDdsTypeId AND UPPER(de.Name) = UPPER(dv.ReferenceName)  
										  INNER JOIN DDCLProperty dp ON dv.PropertyId=dp.Id 
										  INNER JOIN DDCLElement AS DDCLElementParent ON dv.ElementId=DDCLElementParent.Id 
										  INNER JOIN DDCLDdsType ddt ON DDCLElementParent.DdsTypeId = ddt.id
										  LEFT OUTER JOIN DDCLSourceInfo AS ElementSourceInfo ON de.SourceInfo=ElementSourceInfo.Id 
										  LEFT OUTER JOIN session.p p1 ON ElementSourceInfo.ProgramId=p1.ProgramID 
										  LEFT OUTER JOIN DDCLSourceInfo AS ParentSourceInfo ON DDCLElementParent.SourceInfo= ParentSourceInfo.Id 
										  LEFT OUTER JOIN session.p p2 ON ParentSourceInfo.ProgramId = p2.ProgramID
										  INNER JOIN DDCLSourceInfo AS CallSourceInfo ON dv.SourceInfo=CallSourceInfo.Id 
										  INNER JOIN session.p p3 ON CallSourceInfo.ProgramId = p3.ProgramID 
									  WHERE dv.ReferenceDdsTypeId = 14 
									      AND DDCLElementParent.DdsTypeId IN (16, 26)
										  AND UPPER(dp.Name) ='DISPLAY-STRUCTURE' 
										  AND p3.PathID IS NOT NULL
									  
									  
									  
									  
									  UNION
									  SELECT 
										  DDCLElement_SCREEN.Id AS TPMSXScreenID,
										  DDCLElement_SCREEN.Name AS TPMSXScreenName,
										  CAST(NULL AS INTEGER) AS StatementTypeID,
										  'DISPLAY-STRUCTURE' AS StatementTypeName,
										  CallSourceInfo.StartRow AS StatementStartRow,
										  CallSourceInfo.StartCol AS StatementStartColumn,
										  CallSourceInfo.EndRow AS StatementEndRow,
										  CallSourceInfo.EndCol AS StatementEndColumn,
										  p3.PathID AS StmtPathID,
										  DDCLElementParent.Id AS ID,
										  DDCLElementParent.Name AS ParentName,
										  CAST(NULL AS VARCHAR(256)) AS Ancestor,
										  DDCLElementParent.DdsTypeId AS TypeID,
										  ddt.Description as TypeDescription,
										  p2.PathID AS PathID,
										  ParentSourceInfo.StartRow AS ProgStartRow,
										  ParentSourceInfo.StartCol AS ProgStartCol,
										  ParentSourceInfo.EndRow AS ProgEndRow,
										  ParentSourceInfo.EndCol AS ProgEndCol,
										  p1.PathID AS ScreenPathID,
										  ElementSourceInfo.StartRow AS ScreenStartRow,
										  ElementSourceInfo.StartCol AS ScreenStartCol,
										  ElementSourceInfo.EndRow AS ScreenEndRow,
										  ElementSourceInfo.EndCol AS ScreenEndCol
									  FROM DDCLElement AS DDCLElement_SCREEN 
										  INNER JOIN DDCLValue AS DDCLValue_1 ON DDCLElement_SCREEN.Id = DDCLValue_1.ElementId 
										  INNER JOIN DDCLProperty AS DDCLProperty_1 ON DDCLValue_1.PropertyId=DDCLProperty_1.Id 
										  INNER JOIN DDCLValue AS DDCLValue_2 ON DDCLValue_1.ReferenceDdsTypeId=DDCLValue_2.ReferenceDdsTypeId
										                                         AND UPPER(DDCLValue_1.ReferenceName) = UPPER(DDCLValue_2.ReferenceName) 
										  INNER JOIN DDCLProperty AS DDCLProperty_2 ON DDCLValue_2.PropertyId=DDCLProperty_2.Id 
										  INNER JOIN DDCLElement AS DDCLElementParent ON DDCLValue_2.ElementId=DDCLElementParent.Id 
										  INNER JOIN DDCLDdsType ddt ON DDCLElementParent.DdsTypeId = ddt.id
										  INNER JOIN DDCLSourceInfo AS ElementSourceInfo ON DDCLElement_SCREEN.SourceInfo=ElementSourceInfo.Id 
										  INNER JOIN session.p p1 ON ElementSourceInfo.ProgramId=p1.ProgramID  
										  LEFT OUTER JOIN DDCLSourceInfo AS ParentSourceInfo ON DDCLElementParent.SourceInfo = ParentSourceInfo.Id 
										  LEFT OUTER JOIN session.p p2 ON ParentSourceInfo.ProgramId=p2.ProgramID
										  INNER JOIN DDCLSourceInfo AS CallSourceInfo ON DDCLValue_1.SourceInfo=CallSourceInfo.Id 
										  INNER JOIN session.p p3 ON CallSourceInfo.ProgramId=p3.ProgramID  
									  WHERE DDCLElement_SCREEN.DdsTypeId=14 
										  AND DDCLValue_1.ReferenceDdsTypeId=15 
										  AND DDCLValue_2.ReferenceDdsTypeId=15
										  AND DDCLElementParent.DdsTypeId IN (16, 26)
										  AND UPPER(DDCLProperty_1.Name)='DISPLAY-STRUCTURE' 
										  AND UPPER(DDCLProperty_2.Name)='DISPLAY-STRUCTURE' 
							              AND p1.PathID IS NOT NULL 
										  AND p3.PathID IS NOT NULL 
								)s
								LEFT OUTER JOIN Paths pth1 ON s.StmtPathID = pth1.PathID
								LEFT OUTER JOIN Paths pth2 ON s.PathID = pth2.PathID
								LEFT OUTER JOIN Paths pth3 ON s.ScreenPathID = pth3.PathID;

								COMMIT;    
END