AS
BEGIN
SET NOCOUNT ON


	   IF OBJECT_ID('tempdb..#sr') IS NOT NULL DROP TABLE #sr
        -- SET + RECORD Area
	   SELECT isc.ID AS IdmsSchemaID,
			isc.SchemaName AS IdmsSchemaName,
			isub.ID AS IdmsSubschemaID,
			isub.SubschemaName AS IdmsSubschemaName,
			SetOrRecord.SetOrRecordID,
			SetOrRecord.SetOrRecordName,
			st.StatementType AS StatementTypeId,
			st.Description AS StatementTypeName,
			sr.ResourceType ,
			rt.Name AS ResourceTypeName,
			 CASE WHEN sr.ResourceType=77  THEN 1
				 WHEN sr.ResourceType=78  THEN 2
				 END AS ResourceTypeGroup1,
			1 AS ResourceTypeGroup2,
			pstmt.PathStr AS StmtPathStr,
			os.StartRow AS StatementStartRow,
			os.StartCol AS StatementStartColumn,
			os.EndRow AS StatementEndRow,
			os.EndCol AS StatementEndColumn,
			p.ProgramID,
			p.ProgramName,
			p.ProgramTypeID,
			pth.PathStr AS ProgramPath,
			occ1.StartRow AS ProgStartRow,
			occ1.StartCol AS ProgStartCol,
			occ1.EndRow AS ProgEndRow,
			occ1.EndCol AS ProgEndCol,
			p.Ancestor AS Ancestor,
			pa.ProgramID AS AncestorID,
			pa.ProgramTypeID AS AncestorTypeID,
			occ2.StartRow AS AncestorStartRow,
			occ2.StartCol AS AncestorStartCol,
			occ2.EndRow AS AncestorEndRow,
			occ2.EndCol AS AncestorEndCol,
			ddc.DDCLElementPath,
			ddc.ID,
			ddc.DDCLElemCategory,
			ddc.DDCLElementName,
			ddc.DDCLElemInfoStartRow,
			ddc.DDCLElemInfoEndRow,
			ddc.DDCLElemInfoStartCol,
			ddc.DDCLElemInfoEndCol,
		     ddc.SetOrRecordRecordInDDCLID
        INTO #sr
	   FROM dbo.StatementReference sr
		   INNER JOIN dbo.ResourceTypes rt ON rt.ResourceID = sr.ResourceType
		   INNER JOIN dbo.OccurrencesStmt os ON sr.OccurID = os.OccurID
		   INNER JOIN (SELECT ProgramTypeID, tp.ProgramID, Ancestor, tpa.AliasName as ProgramName, OccurID FROM dbo.Programs tp INNER JOIN dbo.ProgramAliases tpa ON tpa.ProgramId = tp.ProgramId and tpa.AliasType = 0) p ON p.ProgramTypeID IN (1, 2, 3, 10, 15, 16) AND os.ProgID = p.ProgramID
		   INNER JOIN dbo.Statements st ON sr.StatementType = st.StatementType
		   INNER JOIN
				    (
					   SELECT 78 AS ResourceType, ID AS SetOrRecordID, SetName AS SetOrRecordName,   SubschemaID
					   FROM dbo.IdmsSubschemaSets
					   UNION
					   SELECT 77 AS ResourceType, ID AS SetOrRecordID, RecordName AS SetOrRecordName, SubschemaID
					   FROM dbo.IdmsSubschemaRecords
				    ) SetOrRecord ON sr.ResourceType = SetOrRecord.ResourceType
								 AND sr.ResourceID = SetOrRecord.SetOrRecordID
		   INNER JOIN dbo.IdmsSubschema isub ON SetOrRecord.SubschemaId = isub.ID
		   INNER JOIN dbo.IdmsSchema isc ON isub.SchemaId = isc.ID
		   INNER JOIN dbo.Paths pstmt ON os.PathID = pstmt.PathID
		   INNER JOIN dbo.Occurrences occ1 ON p.OccurID = occ1.OccurID
		   INNER JOIN dbo.Paths pth ON pth.PathID = occ1.PathID
		   LEFT OUTER JOIN ( SELECT DISTINCT p1.ProgramID, OccurId, pa1.AliasName As ProgramName, ProgramTypeID FROM dbo.Programs p1 INNER JOIN dbo.ProgramAliases pa1 ON  pa1.ProgramId = p1.ProgramId) pa ON pa.ProgramName = p.Ancestor AND pa.ProgramTypeID IN (8)
		   LEFT OUTER JOIN dbo.Occurrences occ2 ON pa.OccurID = occ2.OccurID
		   LEFT OUTER JOIN (
						SELECT   ddclepa.PathStr as DDCLElementPath,
							    CASE WHEN ddsTypeid = 3 THEN NULL
								    ELSE  ddcle.id
							    END AS ID,
							    ddcle.ddsTypeid AS DDCLElemCategory,
							    ddcle.Name AS DDCLElementName,
							    ddclesi.StartRow as DDCLElemInfoStartRow,
							    ddclesi.EndRow as DDCLElemInfoEndRow,
							    ddclesi.StartCol as DDCLElemInfoStartCol,
							    ddclesi.EndCol as DDCLElemInfoEndCol,
							    ddcle.id as SetOrRecordRecordInDDCLID
						  FROM  dbo.DDCLElement ddcle
								LEFT OUTER JOIN dbo.DDCLSourceInfo ddclesi on ddclesi.Id = ddcle.SourceInfo
								LEFT OUTER JOIN dbo.Programs ddclep on ddclep.ProgramId = ddclesi.ProgramId
								LEFT OUTER JOIN dbo.Occurrences ddcleo on ddcleo.OccurId = ddclep.OccurId
								LEFT OUTER JOIN dbo.Paths ddclepa on ddclepa.PathId = ddcleo.PathId
						  WHERE ddcle.ddstypeid IN ( 3, 10)
						) ddc ON (
								 (ddc.DDCLElemCategory = 3 AND SetOrRecord.ResourceType = 77 AND SetOrRecord.SetOrRecordName = ddc.DDCLElementName)
								  OR
								 (ddc.DDCLElemCategory = 10 AND SetOrRecord.ResourceType = 78 AND SetOrRecord.SetOrRecordName = ddc.DDCLElementName)
								)
	   WHERE  sr.ResourceType IN (77, 78)
       -- End SET + RECORD Area



	     IF OBJECT_ID('tempdb..#ddcl') IS NOT NULL DROP TABLE #ddcl
	     --- DDCL Area
		   SELECT  --DISTINCT
				    IdmsSchemaID, IdmsSchemaName, IdmsSubschemaID, IdmsSubschemaName,
				    SetOrRecordID, SetOrRecordName, StatementTypeId, StatementTypeName,
				    ResourceType, ResourceTypeName,
				    CASE WHEN ResourceType=3  THEN 1
					    WHEN ResourceType=10 THEN 2
					    END AS ResourceTypeGroup1,
					2 AS ResourceTypeGroup2,
				    StmtPathStr, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn,
				    ProgramId, ProgramName, ProgramTypeID, ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
				    Ancestor, AncestorID, AncestorTypeID, AncestorStartRow, AncestorStartCol, AncestorEndRow, AncestorEndCol,
				    DDCLElementPath, Id, DDCLElemCategory, DDCLElementName, DDCLElemInfoStartRow, DDCLElemInfoEndRow,
				    DDCLElemInfoStartCol, DDCLElemInfoEndCol, SetOrRecordRecordInDDCLID
			 INTO #ddcl
			 FROM(

					   SELECT --f1.ID AS f1ID,
							--f2.ID AS f2ID,
							CASE WHEN ddclv1.ReferenceDdsTypeId = 3 and  (f1.ID IS NOT NULL OR f2.ID IS NOT NULL)   THEN 1
							    ELSE 0
							END AS chk1,
							CASE WHEN ddclv1.ReferenceDdsTypeId = 10 and  (f1.ID IS NOT NULL OR f2.ID IS NOT NULL) THEN 1
								ELSE 0
							 END AS chk2,
							 SchemaElem.Id AS IdmsSchemaID,
							 SchemaElem.Name AS IdmsSchemaName,
							 SubschemaElem.Id AS IdmsSubschemaID,
							 CASE WHEN SubschemaElem.Name is null  THEN 'No Subschema'
								 ELSE SubschemaElem.Name
							  END AS IdmsSubschemaName,
								CAST(NULL AS INT)  AS SetOrRecordID,
								ddclv1.ReferenceName AS SetOrRecordName,
								ddclp1.Id AS StatementTypeId,
								ddclp1.Name AS StatementTypeName,
								ddclv1.ReferenceDdsTypeId as ResourceType,
								rt.Name AS ResourceTypeName,
								Paths.PathStr as StmtPathStr,
								StmtInfo.StartRow AS StatementStartRow,
								StmtInfo.StartCol AS StatementStartColumn,
								StmtInfo.EndRow AS StatementEndRow,
								StmtInfo.EndCol AS StatementEndColumn,
								StmtInfo.ProgramId AS ProgramId,
								p.ProgramName AS ProgramName,
								p.ProgramTypeID AS ProgramTypeID,
								Paths.PathStr AS ProgramPath,

								DDCLElemInfo.StartRow as ProgStartRow,
								DDCLElemInfo.EndRow as ProgEndRow,
								DDCLElemInfo.StartCol as ProgStartCol,
								DDCLElemInfo.EndCol as ProgEndCol,
								
								CAST(NULL AS VARCHAR(250)) AS Ancestor,
								CAST(NULL AS INT) AS AncestorID,
								CAST(NULL AS INT) AS AncestorTypeID,
								CAST(NULL AS INT) AS AncestorStartRow,
								CAST(NULL AS INT) AS AncestorStartCol,
								CAST(NULL AS INT) AS AncestorEndRow,
								CAST(NULL AS INT) AS AncestorEndCol,
								recpa.PathStr as DDCLElementPath,
								ddcle1.Id as Id,
								ddcle1.DdsTypeId as DDCLElemCategory,
								ddcle1.Name AS DDCLElementName,

								RecordInfo.StartRow AS DDCLElemInfoStartRow,
								RecordInfo.EndRow AS DDCLElemInfoEndRow,
								RecordInfo.StartCol AS DDCLElemInfoStartCol,
								RecordInfo.EndCol AS DDCLElemInfoEndCol,

								DDCLElement_1.Id as SetOrRecordRecordInDDCLID			
					   FROM  dbo.DDCLElement ddcle1
						    INNER JOIN  dbo.DDCLValue ddclv1 ON ddclv1.ElementId = ddcle1.Id
												  AND ddclv1.ValueTypeId IN (2,4)
												  AND ddclv1.ReferenceDdsTypeId IN (3,10)
							INNER JOIN dbo.DDCLProperty  ddclp1 ON ddclp1.ID = ddclv1.PropertyId
							INNER JOIN dbo.DDCLSourceInfo AS DDCLElemInfo on ddcle1.SourceInfo = DDCLElemInfo.Id
							INNER JOIN dbo.DDCLSourceInfo AS StmtInfo on ddclp1.SourceInfo = StmtInfo.Id
							INNER JOIN (SELECT OccurId, tpa.ProgramId, tpa.AliasName AS ProgramName, ProgramTypeID FROM dbo.Programs tp INNER JOIN dbo.ProgramAliases tpa ON tp.ProgramId = tpa.ProgramId and tpa.AliasType = 0) p
                                                                on StmtInfo.ProgramId = p.ProgramID
							INNER JOIN dbo.Occurrences on p.OccurID = Occurrences.OccurID
							INNER JOIN dbo.Paths on paths.PathID = Occurrences.PathID
							 LEFT JOIN dbo.DDCLElement AS DDCLElement_1 ON ddclv1.ReferenceName = DDCLElement_1.Name
																AND ddclv1.ReferenceDdsTypeId = DDCLElement_1.DdsTypeId
							 LEFT JOIN dbo.DDCLSourceInfo AS RecordInfo on DDCLElement_1.SourceInfo = RecordInfo.Id
							 LEFT JOIN dbo.Programs recp on recp.ProgramId = RecordInfo.ProgramId
							 LEFT JOIN dbo.Occurrences reco on reco.OccurId = recp.OccurId
							 LEFT JOIN dbo.Paths recpa on recpa.PathID = reco.PathID
							 INNER JOIN dbo.DDCLRelationV on ddcle1.Id=DDCLRelationV.FromElementId
							 INNER JOIN dbo.DDCLElement as SubschemaElem on DDCLRelationV.ToElementId=SubschemaElem.Id
							 LEFT JOIN dbo.DDCLProperty as SubschemaElemProperty on SubschemaElemProperty.ElementId = SubschemaElem.Id and (SubschemaElemProperty.Name = N'SCHEMA')
							 LEFT JOIN dbo.DDCLValue as SubschemaDDCLVal on SubschemaDDCLVal.PropertyId = SubschemaElemProperty.Id and SubschemaDDCLVal.ValueTypeId in (2,4)
							 LEFT JOIN dbo.DDCLElement as SchemaElem ON SchemaElem.DdsTypeId = 6  AND SchemaElem.Name = SubschemaDDCLVal.ReferenceName
							 INNER JOIN dbo.ResourceTypes rt ON rt.ResourceID = ddclv1.ReferenceDdsTypeId
							 --remove false records (called by clause 'SAME AS IN')
							 LEFT join dbo.DDCLValue as parent on parent.Id = ddclv1.ParentValueId
							 -- next joins are implemented for filter purpose
							 LEFT OUTER JOIN (
											 select ssp.Id, ssp.ElementId, rrv.ReferenceName, rrv.ReferenceDdsTypeId
											 from DDCLProperty ssp
												    inner join DDCLValue ssv on ssp.id = ssv.PropertyId and ssv.ValueTypeId = 1 and ssv.Name = 'ALL' and ssv.Value = 'true'
												    inner join DDCLValue srv on ssp.id = srv.PropertyId and srv.ValueTypeId = 2 and srv.ReferenceDdsTypeId = 6
												    inner join DDCLElement se on se.Name = srv.ReferenceName and se.DdsTypeId = 6
												    inner join DDCLValue rrv on rrv.ElementId = se.Id and rrv.ReferenceDdsTypeId IN (3, 10)
											 where  ssp.Name =  'SCHEMA'
										    )f1 ON f1.ElementId = SubschemaElem.Id AND  f1.ReferenceName = ddclv1.ReferenceName AND f1.ReferenceDdsTypeId = ddclv1.ReferenceDdsTypeId
							  LEFT OUTER JOIN (
											  select ssp.Id, ssp.ElementId , ssv.ReferenceName, ssav.Id AS ssavID, ssp.Name , ssv.ReferenceDdsTypeId
											  from DDCLProperty ssp
													   left join DDCLValue ssv on ssp.id = ssv.PropertyId and ssv.ValueTypeId = 2 and ssv.ReferenceDdsTypeId IN (3, 10)
													   left join DDCLValue ssav on ssp.id = ssav.PropertyId  and ssav.ValueTypeId = 1 and ssav.Name = 'all' and ssav.Value = 'true'
											  where ssp.Name  IN ( 'RECORD-MAP' , 'SETS')
										    )f2 ON  f2.ElementId = SubschemaElem.Id
												  AND
												  f2.Name = CASE WHEN ddclv1.ReferenceDdsTypeId = 3 THEN 'RECORD-MAP'
																    ELSE 'SETS'
																    END
												   AND (f2.ReferenceName = ddclv1.ReferenceName or f2.ssavID is not null)	

					   WHERE  ddcle1.DdsTypeId in (11,12,16,27,28,29)
							AND ddcle1.islocal='false'
							AND (
								   (
								    ddclv1.referenceddstypeid=10   AND  (ddcle1.DdsTypeId IN (11, 16) AND ddclp1.name IN ('CHANGES', 'SINGULAR-VIEW') )
	 							    )
								   OR
								    (
										 ddclv1.ReferenceDdsTypeId = 3
										 AND (parent.ComplexValueTypeId IS NULL OR parent.ComplexValueTypeId <> 32)
										 AND (
	 										   (ddcle1.DdsTypeId=11 AND ddclp1.name IN ('CHANGES', 'OUTPUT', 'PROCESSING-STRUCTURE', 'SELECTION', 'SINGULAR-VIEW') )
	 										   OR
	 										   (ddcle1.DdsTypeId=16 AND ddclp1.name IN ('CHANGES', 'PROCESSING-STRUCTURE', 'SELECTION', 'SINGULAR-VIEW') )
	 										   OR
	 										   (ddcle1.DdsTypeId=29 AND ddclp1.name = 'STRUCTURE' )
	 										   OR
	 										   (ddcle1.DdsTypeId in (27,28) AND ddclp1.name IN ('PROCESSING-STRUCTURE', 'VIEW', 'SELECTION') )
	 										   OR
	 										   (ddcle1.DdsTypeId=12 AND ddclp1.name IN ('STRUCTURE', 'RECORDS'))
											 )
									 )
							   )

				    )ddcl
			 WHERE (chk1 =1)
				   OR
				  (chk2=1)
			 -- END DDCL



IF OBJECT_ID('dbo.cacheEZViewer_Usage_IDMSX') IS NOT NULL  DROP TABLE dbo.cacheEZViewer_Usage_IDMSX
SELECT IdmsSchemaID, IdmsSchemaName, IdmsSubschemaID, IdmsSubschemaName,
       SetOrRecordID, SetOrRecordName, StatementTypeId, StatementTypeName,
	  ResourceType, ResourceTypeName, ResourceTypeGroup1, ResourceTypeGroup2,
	  StmtPathStr, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn,
	  ProgramId, ProgramName, ProgramTypeID, ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
	  Ancestor, AncestorID, AncestorTypeID, AncestorStartRow, AncestorStartCol, AncestorEndRow, AncestorEndCol,
	  DDCLElementPath, Id, DDCLElemCategory, DDCLElementName, DDCLElemInfoStartRow, DDCLElemInfoEndRow, DDCLElemInfoStartCol, DDCLElemInfoEndCol, SetOrRecordRecordInDDCLID
INTO dbo.cacheEZViewer_Usage_IDMSX
FROM
    (
		SELECT
		     IdmsSchemaID, IdmsSchemaName, IdmsSubschemaID, IdmsSubschemaName,
			SetOrRecordID, SetOrRecordName, StatementTypeId, StatementTypeName,
			ResourceType, ResourceTypeName, ResourceTypeGroup1, ResourceTypeGroup2,
			StmtPathStr, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn,
			ProgramId, ProgramName, ProgramTypeID, ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
			Ancestor, AncestorID, AncestorTypeID, AncestorStartRow, AncestorStartCol, AncestorEndRow, AncestorEndCol,
			DDCLElementPath, Id, DDCLElemCategory, DDCLElementName, DDCLElemInfoStartRow, DDCLElemInfoEndRow, DDCLElemInfoStartCol, DDCLElemInfoEndCol, SetOrRecordRecordInDDCLID
		FROM #sr
		
		UNION

          SELECT
		     IdmsSchemaID, IdmsSchemaName, IdmsSubschemaID, IdmsSubschemaName,
			SetOrRecordID, SetOrRecordName, StatementTypeId, StatementTypeName,
			ResourceType, ResourceTypeName, ResourceTypeGroup1, ResourceTypeGroup2,
			StmtPathStr, StatementStartRow, StatementStartColumn, StatementEndRow, StatementEndColumn,
			ProgramId, ProgramName, ProgramTypeID, ProgramPath, ProgStartRow, ProgStartCol, ProgEndRow, ProgEndCol,
			Ancestor, AncestorID, AncestorTypeID, AncestorStartRow, AncestorStartCol, AncestorEndRow, AncestorEndCol,
			DDCLElementPath, Id, DDCLElemCategory, DDCLElementName, DDCLElemInfoStartRow, DDCLElemInfoEndRow, DDCLElemInfoStartCol, DDCLElemInfoEndCol, SetOrRecordRecordInDDCLID
		FROM #ddcl
	)x


END