if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_LogTypes') and OBJECTPROPERTY(id, N'IsTable') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_LogTypes 
DROP COLUMN [Notification]
GO

if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_LogTypes') and OBJECTPROPERTY(id, N'IsTable') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_LogTypes 
DROP COLUMN [Phrase]
GO


if exists (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_LogTypes') and OBJECTPROPERTY(id, N'IsTable') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_LogTypes 
ADD [ResourceFile] NVARCHAR (150)
GO



IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_GetLogTypes') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogTypes
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogTypes
	@LogTypeKey NVarChar (20)
AS

SELECT
	[LogTypeKey],
	[ResourceFile]
FROM
	{databaseOwner}{objectQualifier}DMX_LogTypes
WHERE
	[LogTypeKey] = @LogTypeKey
GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_AddLogTypes') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_AddLogTypes
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddLogTypes
	@LogTypeKey NVarChar (20),
	@ResourceFile NVarChar (150)
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_LogTypes (
	[LogTypeKey],
	[ResourceFile]
) VALUES (
	@LogTypeKey,
	@ResourceFile
)

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_UpdateLogTypes') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateLogTypes
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateLogTypes
	@LogTypeKey NVarChar (20),
	@ResourceFile NVarChar (150)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_LogTypes SET
	[ResourceFile] = @ResourceFile
WHERE
	[LogTypeKey] = @LogTypeKey
GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_DeleteLogTypes') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteLogTypes
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteLogTypes
	@LogTypeKey NVarChar (20)
AS

DELETE FROM {databaseOwner}{objectQualifier}DMX_LogTypes
WHERE
	[LogTypeKey] = @LogTypeKey
GO



IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_GetLogForEntry') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogForEntry
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogForEntry
  @LastVersionId Int
AS

SELECT {objectQualifier}DMX_Log.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {objectQualifier}DMX_Entries.Version, {objectQualifier}DMX_LogTypes.ResourceFile
FROM {objectQualifier}Users INNER JOIN ({objectQualifier}DMX_LogTypes INNER JOIN ({objectQualifier}DMX_Entries INNER JOIN {objectQualifier}DMX_Log ON {objectQualifier}DMX_Entries.EntryId = {objectQualifier}DMX_Log.EntId) ON {objectQualifier}DMX_LogTypes.LogTypeKey = {objectQualifier}DMX_Log.Operation) ON {objectQualifier}Users.UserID = {objectQualifier}DMX_Log.UserId
WHERE ((({objectQualifier}DMX_Entries.LastVersionId)=@LastVersionId))
ORDER BY {objectQualifier}DMX_Log.Datime DESC

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{databaseOwner}{objectQualifier}DMX_GetLogForItem') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogForItem
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogForItem
  @EntryId Int
AS

SELECT {objectQualifier}DMX_Log.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {objectQualifier}DMX_Entries.Version, {objectQualifier}DMX_LogTypes.ResourceFile
FROM {objectQualifier}Users INNER JOIN ({objectQualifier}DMX_LogTypes INNER JOIN ({objectQualifier}DMX_Entries INNER JOIN {objectQualifier}DMX_Log ON {objectQualifier}DMX_Entries.EntryId = {objectQualifier}DMX_Log.EntId) ON {objectQualifier}DMX_LogTypes.LogTypeKey = {objectQualifier}DMX_Log.Operation) ON {objectQualifier}Users.UserID = {objectQualifier}DMX_Log.UserId
WHERE ((({objectQualifier}DMX_Entries.EntryId)=@EntryId))
ORDER BY {objectQualifier}DMX_Log.Datime DESC

GO

