USE DD_ECM

CREATE TABLE [dbo].[TBMT_FILTERS](
	[GUID] [int] IDENTITY(1,1) NOT NULL,
	[SQL_COMMAND] [nvarchar](500) NOT NULL,
	[ITEM_ID] [int] NOT NULL,
	[TABLE_ID] [int] NOT NULL,
	[ACTIVE] [bit] NOT NULL,
	[COMMENT] [nvarchar](100) NULL,
	[ADDED_WHO] [nvarchar](50) NOT NULL,
	[ADDED_WHEN] [datetime] NULL,
	[CHANGED_WHO] [nvarchar](50) NULL,
	[CHANGED_WHEN] [datetime] NULL,
 CONSTRAINT [PK__TBMT_FILTERS] PRIMARY KEY CLUSTERED 
(
	[GUID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO

ALTER TABLE [dbo].[TBMT_FILTERS] ADD  CONSTRAINT [DF_TBMT_FILTERS_ACTIVE]  DEFAULT ((1)) FOR [ACTIVE]
GO

ALTER TABLE [dbo].[TBMT_FILTERS] ADD  CONSTRAINT [DF_TBMT_FILTERS_ADDED_WHO]  DEFAULT (N'DEFAULT') FOR [ADDED_WHO]
GO

ALTER TABLE [dbo].[TBMT_FILTERS] ADD  CONSTRAINT [DF_TBMT_FILTERS_ADDED_WHEN]  DEFAULT (getdate()) FOR [ADDED_WHEN]
GO

CREATE   VIEW [dbo].[VWMT_FILTERS]
AS
SELECT   
TFILTERS.*,
TTABLES.NAME TABLE_NAME,
TITEMS.XML_NAME ITEM_NAME
FROM            
	dbo.TBMT_FILTERS TFILTERS 
	INNER JOIN TBMT_TABLES AS TTABLES ON TFILTERS.TABLE_ID = TTABLES.GUID
	INNER JOIN TBMT_TEMPLATE_ITEMS AS TITEMS ON TFILTERS.ITEM_ID = TITEMS.GUID
GO

EXEC sp_rename 'TBMT_TEMPLATE_ITEMS.XML_TABLE_ID', 'TABLE_ID', 'COLUMN'
GO

EXEC sp_rename 'TBMT_TEMPLATE_ITEMS.XML_TYPE_ID', 'TYPE_ID', 'COLUMN'
GO

EXEC sp_rename 'TBMT_TEMPLATE_ITEMS.XML_NAME', 'NAME', 'COLUMN'
GO

ALTER   VIEW [dbo].[VWMT_ITEMS]
AS
	SELECT DISTINCT
		[TBTEMPLATES].[NAME] [TEMPLATE_NAME]
		,[TBTABLES].[NAME] AS [TABLE_NAME]
		,[TBITEMS].[NAME] AS [ITEM_NAME]		
		,[TBTYPES].[NAME] AS [DATA_TYPE]
		,[TBTABLES].[IS_HEAD] AS [IS_HEAD]
		,[TBITEMS].[IS_READ_ONLY]
		,[TBITEMS].[IS_VISIBLE]
        ,[TBITEMS].[IS_REQUIRED]
		,[TBITEMS].[IS_VIRTUAL]
		,[TBITEMS].[ORDER_KEY]
		,[TBITEMS].[PREFER_EXTERNAL]
		,[TBFUNCTIONS].[GUID] AS [FUNCTION_ID]
		,[TBFUNCTIONS].[NAME] AS [FUNCTION_NAME]
		,[TBITEMS].[FUNCTION_PARAMETERS]
	FROM
		[dbo].[TBMT_TEMPLATE_ITEMS] [TBITEMS]
		INNER JOIN [dbo].[TBMT_TABLES] [TBTABLES] ON [TBITEMS].[TABLE_ID] = [TBTABLES].[GUID]
		INNER JOIN [dbo].[TBMT_TYPES] [TBTYPES] ON [TBITEMS].[TYPE_ID] = [TBTYPES].[GUID]
		INNER JOIN [dbo].[TBMT_TEMPLATES] [TBTEMPLATES] ON [TBTABLES].[TEMPLATE_ID] = [TBTEMPLATES].[GUID]
		LEFT OUTER JOIN [dbo].[TBMT_FUNCTIONS] [TBFUNCTIONS] ON [TBITEMS].[FUNCTION_ID] = [TBFUNCTIONS].[GUID]
		
GO

ALTER VIEW [dbo].[VWMT_MAPPING]
AS
SELECT        
TMAPPINGCONFIG.ORDER_KEY, 

TSOURCE.NAME AS SOURCE_NAME, 
TSOURCE_ITEMS.NAME SOURCE_ITEM,
TMAPPINGCONFIG.SOURCE_REGEX,

TDESTINATION.NAME AS DESTINATION_NAME, 
TDESTINATION_ITEMS.NAME AS DESTINATION_ITEM, 
TMAPPINGCONFIG.DESTINATION_VALUE

FROM            
	dbo.TBMT_MAPPING_CONFIG TMAPPINGCONFIG 
	INNER JOIN TBMT_MAPPING_ENTITIES AS TSOURCE ON TMAPPINGCONFIG.SOURCE_ENTITY_ID = TSOURCE.GUID
    INNER JOIN TBMT_MAPPING_ENTITIES AS TDESTINATION ON TMAPPINGCONFIG.DESTINATION_ENTITY_ID = TDESTINATION.GUID
	LEFT OUTER JOIN TBMT_TEMPLATE_ITEMS AS TSOURCE_ITEMS ON TSOURCE.ITEM_ID = TSOURCE_ITEMS.GUID
	LEFT OUTER JOIN TBMT_TEMPLATE_ITEMS AS TDESTINATION_ITEMS ON TDESTINATION.ITEM_ID = TDESTINATION_ITEMS.GUID
GO

ALTER   VIEW [dbo].[VWMT_FILTERS]
AS
SELECT   
TFILTERS.*,
TTABLES.NAME TABLE_NAME,
TITEMS.NAME ITEM_NAME
FROM            
	dbo.TBMT_FILTERS TFILTERS 
	INNER JOIN TBMT_TABLES AS TTABLES ON TFILTERS.TABLE_ID = TTABLES.GUID
	INNER JOIN TBMT_TEMPLATE_ITEMS AS TITEMS ON TFILTERS.ITEM_ID = TITEMS.GUID
GO


