USE DD_ECM

EXEC sp_rename 'TBEDI_XML_TYPES', 'TBMT_TYPES';
EXEC sp_rename 'TBEDI_XML_CONFIG', 'TBMT_CONFIG';
EXEC sp_rename 'TBEDI_XML_FUNCTIONS', 'TBMT_FUNCTIONS';
EXEC sp_rename 'TBEDI_XML_MANDATORS', 'TBMT_MANDATORS';
EXEC sp_rename 'TBEDI_XML_MAPPING_CONFIG', 'TBMT_MAPPING_CONFIG';
EXEC sp_rename 'TBEDI_XML_MAPPING_ENTITY', 'TBMT_MAPPING_ENTITIES';
EXEC sp_rename 'TBEDI_XML_TABLES', 'TBMT_TABLES';
EXEC sp_rename 'TBEDI_XML_TEMPLATE_ITEMS', 'TBMT_TEMPLATE_ITEMS';
EXEC sp_rename 'TBEDI_XML_TEMPLATES', 'TBMT_TEMPLATES';
GO

DROP VIEW [dbo].[VWEDI_XML_ITEMS]
GO

CREATE VIEW [dbo].[VWMT_ITEMS]
AS
	SELECT DISTINCT
		[TBTEMPLATES].[NAME] [TEMPLATE_NAME]
		,[TBTABLES].[NAME] AS [XML_TABLE]
		,[TBITEMS].[XML_NAME] AS [XML_ITEM]		
		,[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]
		,[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].[XML_TABLE_ID] = [TBTABLES].[GUID]
		INNER JOIN [dbo].[TBMT_TYPES] [TBTYPES] ON [TBITEMS].[XML_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

DROP VIEW [dbo].[VWEDI_XML_MAPPING]
GO

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

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

		TDESTINATION.NAME AS DESTINATION_NAME, 
		TDESTINATION_ITEMS.XML_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

---

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_FUNCTIONS_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_FUNCTIONS_AFTER_UPDATE] ON [dbo].[TBMT_FUNCTIONS]
FOR UPDATE
AS
  UPDATE [TBMT_FUNCTIONS] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_FUNCTIONS].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_FUNCTIONS] ENABLE TRIGGER [TBMT_FUNCTIONS_AFTER_UPDATE]
GO

----

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_MANDATORS_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_MANDATORS_AFTER_UPDATE] ON [dbo].[TBMT_MANDATORS]
FOR UPDATE
AS
  UPDATE [TBMT_MANDATORS] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_MANDATORS].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_MANDATORS] ENABLE TRIGGER [TBMT_MANDATORS_AFTER_UPDATE]
GO

---

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_MAPPING_CONFIG_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBTM_MAPPING_CONFIG_AFTER_UPDATE] ON [dbo].[TBMT_MAPPING_CONFIG]
FOR UPDATE
AS
  UPDATE [TBMT_MAPPING_CONFIG] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_MAPPING_CONFIG].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_MAPPING_CONFIG] ENABLE TRIGGER [TBTM_MAPPING_CONFIG_AFTER_UPDATE]
GO

--

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_TABLES_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_TABLES_AFTER_UPDATE] ON [dbo].[TBMT_TABLES]
FOR UPDATE
AS
  UPDATE [TBMT_TABLES] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_TABLES].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_TABLES] ENABLE TRIGGER [TBMT_TABLES_AFTER_UPDATE]
GO

---

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_ITEMS_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_ITEMS_AFTER_UPDATE] ON [dbo].[TBMT_TEMPLATE_ITEMS]
FOR UPDATE
AS
  UPDATE [TBMT_TEMPLATE_ITEMS] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_TEMPLATE_ITEMS].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_TEMPLATE_ITEMS] ENABLE TRIGGER [TBMT_ITEMS_AFTER_UPDATE]
GO

---

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_TEMPLATES_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_TEMPLATES_AFTER_UPDATE] ON [dbo].[TBMT_TEMPLATES]
FOR UPDATE
AS
  UPDATE [TBMT_TEMPLATES] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_TEMPLATES].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_TEMPLATES] ENABLE TRIGGER [TBMT_TEMPLATES_AFTER_UPDATE]
GO

---

DROP TRIGGER IF EXISTS [dbo].[TBEDI_XML_TYPES_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_TYPES_AFTER_UPDATE] ON [dbo].[TBMT_TYPES]
FOR UPDATE
AS
  UPDATE [TBMT_TYPES] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_TYPES].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_TYPES] ENABLE TRIGGER [TBMT_TYPES_AFTER_UPDATE]
GO

---

CREATE OR ALTER TRIGGER [dbo].[TBMT_CONFIG_AFTER_UPDATE] ON [dbo].[TBMT_CONFIG]
FOR UPDATE
AS
  UPDATE [TBMT_CONFIG] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_CONFIG].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_CONFIG] ENABLE TRIGGER [TBMT_CONFIG_AFTER_UPDATE]
GO

---

DROP TRIGGER [dbo].[TBEDI_XML_MAPPING_ENTITY_AFTER_UPDATE]
GO

CREATE OR ALTER TRIGGER [dbo].[TBMT_MAPPING_ENTITIES_AFTER_UPDATE] ON [dbo].[TBMT_MAPPING_ENTITIES]
FOR UPDATE
AS
  UPDATE [TBMT_MAPPING_ENTITIES] SET CHANGED_WHEN = GETDATE() FROM INSERTED WHERE [TBMT_MAPPING_ENTITIES].GUID = INSERTED.GUID
GO

ALTER TABLE [dbo].[TBMT_MAPPING_ENTITIES] ENABLE TRIGGER [TBMT_MAPPING_ENTITIES_AFTER_UPDATE]
GO

---

INSERT INTO TBMT_FUNCTIONS (NAME, DESCRIPTION, ACTIVE, ADDED_WHO) VALUES ('FIELD', 'Gets a value from another field + subfield', 1, 'UPDATE-002')