/****** TABLES AND PRIMARY KEYS ******/
if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Entries') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Entries (
 [Author] NVARCHAR (50),
 [CollectionId] INT,
 [DateSubmitted] DATETIME,
 [Deleted] BIT,
 [Description] NVARCHAR (150) NOT NULL,
 [EditRoles] NVARCHAR (255),
 [Entry] NVARCHAR (255) NOT NULL,
 [EntryId] INT IDENTITY (1, 1),
 [EntryType] NVARCHAR (150) NOT NULL,
 [FileSize] INT,
 [IsApproved] BIT,
 [IsLocked] BIT,
 [Keywords] NVARCHAR (255),
 [LastVersionId] INT,
 [LockedBy] INT,
 [OriginalFileName] NVARCHAR (255),
 [PortalId] INT,
 [Remarks] NTEXT,
 [UserId] INT,
 [Version] INT,
 [ViewRoles] NVARCHAR (255),
) ON [PRIMARY]
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'PK_{objectQualifier}DMX_Entries') and OBJECTPROPERTY(id, N'IsPrimaryKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Entries ADD CONSTRAINT
	PK_{objectQualifier}DMX_Entries PRIMARY KEY CLUSTERED 
	(
	EntryId
	) ON [PRIMARY]
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Log') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Log (
 [Datime] DATETIME,
 [EntId] INT,
 [id] INT IDENTITY (1, 1),
 [Operation] INT,
 [UserId] INT,
) ON [PRIMARY]
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'PK_{objectQualifier}DMX_Log') and OBJECTPROPERTY(id, N'IsPrimaryKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Log ADD CONSTRAINT
	PK_{objectQualifier}DMX_Log PRIMARY KEY CLUSTERED 
	(
	id
	) ON [PRIMARY]
GO


/****** PROCEDURES ******/

if not exists (select * from dbo.sysobjects where id = object_id(N'FK_{objectQualifier}DMX_Entries_{objectQualifier}Portals') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Entries ADD CONSTRAINT
	FK_{objectQualifier}DMX_Entries_{objectQualifier}Portals FOREIGN KEY
	(
    PortalId
	) REFERENCES {databaseOwner}{objectQualifier}Portals
	(
    PortalID
	) ON DELETE CASCADE

GO

if not exists (select * from dbo.sysobjects where id = object_id(N'FK_{objectQualifier}DMX_Entries_{objectQualifier}Users') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Entries ADD CONSTRAINT
	FK_{objectQualifier}DMX_Entries_{objectQualifier}Users FOREIGN KEY
	(
    UserId
	) REFERENCES {databaseOwner}{objectQualifier}Users
	(
    UserID
	) ON DELETE NO ACTION

GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetEntries    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetEntries') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetEntries
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_AddEntries    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_AddEntries') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_AddEntries
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_UpdateEntries    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_UpdateEntries') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_UpdateEntries
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_DeleteEntries    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_DeleteEntries') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_DeleteEntries
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetEntriesByPortals    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetEntriesByPortals') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetEntriesByPortals
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetEntriesByUsers    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetEntriesByUsers') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetEntriesByUsers
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntries
	@EntryId Int,
        @PortalId int
AS

SELECT
	[Author],
	[CollectionId],
	[DateSubmitted],
	[Deleted],
	[Description],
	[EditRoles],
	[Entry],
	[EntryId],
	[EntryType],
	[FileSize],
	[IsApproved],
	[IsLocked],
	[Keywords],
	[LastVersionId],
	[LockedBy],
	[OriginalFileName],
	[PortalId],
	[Remarks],
	[UserId],
	[Version],
	[ViewRoles]
FROM
	{databaseOwner}{objectQualifier}DMX_Entries
WHERE
	[EntryId] = @EntryId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddEntries
	@Author NVarChar (50),
	@CollectionId Int,
	@DateSubmitted DateTime,
	@Deleted Bit,
	@Description NVarChar (150),
	@EditRoles NVarChar (255),
	@Entry NVarChar (255),
	@EntryType NVarChar (150),
	@FileSize Int,
	@IsApproved Bit,
	@IsLocked Bit,
	@Keywords NVarChar (255),
	@LastVersionId Int,
	@LockedBy Int,
	@OriginalFileName NVarChar (255),
	@PortalId Int,
	@Remarks NText,
	@UserId Int,
	@Version Int,
	@ViewRoles NVarChar (255)
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Entries (
	[Author],
	[CollectionId],
	[DateSubmitted],
	[Deleted],
	[Description],
	[EditRoles],
	[Entry],
	[EntryType],
	[FileSize],
	[IsApproved],
	[IsLocked],
	[Keywords],
	[LastVersionId],
	[LockedBy],
	[OriginalFileName],
	[PortalId],
	[Remarks],
	[UserId],
	[Version],
	[ViewRoles]
) VALUES (
	@Author,
	@CollectionId,
	@DateSubmitted,
	@Deleted,
	@Description,
	@EditRoles,
	@Entry,
	@EntryType,
	@FileSize,
	@IsApproved,
	@IsLocked,
	@Keywords,
	@LastVersionId,
	@LockedBy,
	@OriginalFileName,
	@PortalId,
	@Remarks,
	@UserId,
	@Version,
	@ViewRoles
)

select SCOPE_IDENTITY()
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateEntries
	@EntryId Int,
	@Author NVarChar (50),
	@CollectionId Int,
	@DateSubmitted DateTime,
	@Deleted Bit,
	@Description NVarChar (150),
	@EditRoles NVarChar (255),
	@Entry NVarChar (255),
	@EntryType NVarChar (150),
	@FileSize Int,
	@IsApproved Bit,
	@IsLocked Bit,
	@Keywords NVarChar (255),
	@LastVersionId Int,
	@LockedBy Int,
	@OriginalFileName NVarChar (255),
	@PortalId Int,
	@Remarks NText,
	@UserId Int,
	@Version Int,
	@ViewRoles NVarChar (255)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Entries SET
	[Author] = @Author,
	[CollectionId] = @CollectionId,
	[DateSubmitted] = @DateSubmitted,
	[Deleted] = @Deleted,
	[Description] = @Description,
	[EditRoles] = @EditRoles,
	[Entry] = @Entry,
	[EntryType] = @EntryType,
	[FileSize] = @FileSize,
	[IsApproved] = @IsApproved,
	[IsLocked] = @IsLocked,
	[Keywords] = @Keywords,
	[LastVersionId] = @LastVersionId,
	[LockedBy] = @LockedBy,
	[OriginalFileName] = @OriginalFileName,
	[PortalId] = @PortalId,
	[Remarks] = @Remarks,
	[UserId] = @UserId,
	[Version] = @Version,
	[ViewRoles] = @ViewRoles
WHERE
	[EntryId] = @EntryId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

DELETE FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE
	[EntryId] = @EntryId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntriesByPortals
	@PortalId Int,
        @IncludeDeleted BIT
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Entries.Author,
	{databaseOwner}{objectQualifier}DMX_Entries.CollectionId,
	{databaseOwner}{objectQualifier}DMX_Entries.DateSubmitted,
	{databaseOwner}{objectQualifier}DMX_Entries.Deleted,
	{databaseOwner}{objectQualifier}DMX_Entries.Description,
	{databaseOwner}{objectQualifier}DMX_Entries.EditRoles,
	{databaseOwner}{objectQualifier}DMX_Entries.Entry,
	{databaseOwner}{objectQualifier}DMX_Entries.EntryId,
	{databaseOwner}{objectQualifier}DMX_Entries.EntryType,
	{databaseOwner}{objectQualifier}DMX_Entries.FileSize,
	{databaseOwner}{objectQualifier}DMX_Entries.IsApproved,
	{databaseOwner}{objectQualifier}DMX_Entries.IsLocked,
	{databaseOwner}{objectQualifier}DMX_Entries.Keywords,
	{databaseOwner}{objectQualifier}DMX_Entries.LastVersionId,
	{databaseOwner}{objectQualifier}DMX_Entries.LockedBy,
	{databaseOwner}{objectQualifier}DMX_Entries.OriginalFileName,
	{databaseOwner}{objectQualifier}DMX_Entries.PortalId,
	{databaseOwner}{objectQualifier}DMX_Entries.Remarks,
	{databaseOwner}{objectQualifier}DMX_Entries.UserId,
	{databaseOwner}{objectQualifier}DMX_Entries.Version,
	{databaseOwner}{objectQualifier}DMX_Entries.ViewRoles, 
	{databaseOwner}{objectQualifier}Portals.Description

FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Entries.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Entries.PortalId = @PortalId
	AND	((NOT {databaseOwner}{objectQualifier}DMX_Entries.Deleted = 1) OR (@IncludeDeleted = 1))
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntriesByUsers
	@UserId Int,
        @IncludeDeleted BIT
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Entries.Author,
	{databaseOwner}{objectQualifier}DMX_Entries.CollectionId,
	{databaseOwner}{objectQualifier}DMX_Entries.DateSubmitted,
	{databaseOwner}{objectQualifier}DMX_Entries.Deleted,
	{databaseOwner}{objectQualifier}DMX_Entries.Description,
	{databaseOwner}{objectQualifier}DMX_Entries.EditRoles,
	{databaseOwner}{objectQualifier}DMX_Entries.Entry,
	{databaseOwner}{objectQualifier}DMX_Entries.EntryId,
	{databaseOwner}{objectQualifier}DMX_Entries.EntryType,
	{databaseOwner}{objectQualifier}DMX_Entries.FileSize,
	{databaseOwner}{objectQualifier}DMX_Entries.IsApproved,
	{databaseOwner}{objectQualifier}DMX_Entries.IsLocked,
	{databaseOwner}{objectQualifier}DMX_Entries.Keywords,
	{databaseOwner}{objectQualifier}DMX_Entries.LastVersionId,
	{databaseOwner}{objectQualifier}DMX_Entries.LockedBy,
	{databaseOwner}{objectQualifier}DMX_Entries.OriginalFileName,
	{databaseOwner}{objectQualifier}DMX_Entries.PortalId,
	{databaseOwner}{objectQualifier}DMX_Entries.Remarks,
	{databaseOwner}{objectQualifier}DMX_Entries.UserId,
	{databaseOwner}{objectQualifier}DMX_Entries.Version,
	{databaseOwner}{objectQualifier}DMX_Entries.ViewRoles, 
	{databaseOwner}{objectQualifier}Portals.Description

FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Entries.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Entries.UserId = @UserId
	AND	((NOT {databaseOwner}{objectQualifier}DMX_Entries.Deleted = 1) OR (@IncludeDeleted = 1))
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO




if not exists (select * from dbo.sysobjects where id = object_id(N'FK_{objectQualifier}DMX_Log_{objectQualifier}DMX_Entries') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Log ADD CONSTRAINT
	FK_{objectQualifier}DMX_Log_{objectQualifier}DMX_Entries FOREIGN KEY
	(
    EntId
	) REFERENCES {databaseOwner}{objectQualifier}DMX_Entries
	(
    EntryId
	) ON DELETE CASCADE

GO

if not exists (select * from dbo.sysobjects where id = object_id(N'FK_{objectQualifier}DMX_Log_{objectQualifier}Users') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Log ADD CONSTRAINT
	FK_{objectQualifier}DMX_Log_{objectQualifier}Users FOREIGN KEY
	(
    UserId
	) REFERENCES {databaseOwner}{objectQualifier}Users
	(
    UserID
	) ON DELETE NO ACTION

GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetLog    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetLog') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetLog
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_AddLog    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_AddLog') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_AddLog
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_UpdateLog    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_UpdateLog') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_UpdateLog
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_DeleteLog    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_DeleteLog') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_DeleteLog
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetLogByEntries    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetLogByEntries') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetLogByEntries
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetLogByPortals    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetLogByPortals') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetLogByPortals
GO

/****** Object:  Stored Procedure {databaseOwner}{objectQualifier}DMX_GetLogByUsers    Script Date: 14 July 2004 ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetLogByUsers') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
drop procedure {databaseOwner}{objectQualifier}DMX_GetLogByUsers
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLog
	@id Int
AS

SELECT
	[Datime],
	[EntId],
	[id],
	[Operation],
	[UserId]
FROM
	{databaseOwner}{objectQualifier}DMX_Log
WHERE
	[id] = @id
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddLog
	@Datime DateTime,
	@EntId Int,
	@Operation Int,
	@UserId Int
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Log (
	[Datime],
	[EntId],
	[Operation],
	[UserId]
) VALUES (
	@Datime,
	@EntId,
	@Operation,
	@UserId
)

select SCOPE_IDENTITY()
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateLog
	@id Int,
	@Datime DateTime,
	@EntId Int,
	@Operation Int,
	@UserId Int
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Log SET
	[Datime] = @Datime,
	[EntId] = @EntId,
	[Operation] = @Operation,
	[UserId] = @UserId
WHERE
	[id] = @id
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteLog
	@id Int
AS

DELETE FROM {databaseOwner}{objectQualifier}DMX_Log
WHERE
	[id] = @id
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO
SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogByEntries
	@EntId Int
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Log.Datime,
	{databaseOwner}{objectQualifier}DMX_Log.EntId,
	{databaseOwner}{objectQualifier}DMX_Log.id,
	{databaseOwner}{objectQualifier}DMX_Log.Operation,
	{databaseOwner}{objectQualifier}DMX_Log.UserId, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description

FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Log.EntId
WHERE
	{databaseOwner}{objectQualifier}DMX_Log.EntId = @EntId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogByPortals
	@PortalId Int
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Log.Datime,
	{databaseOwner}{objectQualifier}DMX_Log.EntId,
	{databaseOwner}{objectQualifier}DMX_Log.id,
	{databaseOwner}{objectQualifier}DMX_Log.Operation,
	{databaseOwner}{objectQualifier}DMX_Log.UserId
FROM
	({databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Log.EntId)
WHERE
	{databaseOwner}{objectQualifier}DMX_Entries.PortalId = @PortalId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogByUsers
	@UserId Int
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Log.Datime,
	{databaseOwner}{objectQualifier}DMX_Log.EntId,
	{databaseOwner}{objectQualifier}DMX_Log.id,
	{databaseOwner}{objectQualifier}DMX_Log.Operation,
	{databaseOwner}{objectQualifier}DMX_Log.UserId, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description

FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Log.EntId
WHERE
	{databaseOwner}{objectQualifier}DMX_Log.UserId = @UserId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO




/****** OTHER PROCEDURES ******/
CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_CleanupLog
  @ThresholdDate DateTime, @PortalId Int
AS

DELETE 
FROM {databaseOwner}{objectQualifier}DMX_Log
WHERE (({objectQualifier}DMX_Log.Datime < @ThresholdDate) AND (EntId IN (
SELECT EntryId FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE {databaseOwner}{objectQualifier}DMX_Entries.PortalId = @PortalId) ))

GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAllPortalCollections
  @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.Description, {databaseOwner}{objectQualifier}DMX_Entries.EntryId
FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE ((({objectQualifier}DMX_Entries.EntryType) LIKE 'Collection') AND ((NOT {databaseOwner}{objectQualifier}DMX_Entries.Deleted = 1)) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Entries.Description

GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetCollectionContents
  @ParentId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}Users ON {databaseOwner}{objectQualifier}DMX_Entries.UserId={objectQualifier}Users.UserID
WHERE ((({objectQualifier}DMX_Entries.CollectionId)=@ParentId) And (({objectQualifier}DMX_Entries.EntryId)=({objectQualifier}DMX_Entries.LastVersionId)))

GO

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

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

GO

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetVersionList
  @LastVersionId Int, @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}Users ON {databaseOwner}{objectQualifier}DMX_Entries.UserId={objectQualifier}Users.UserID
WHERE ((({objectQualifier}DMX_Entries.LastVersionId)=@LastVersionId) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Entries.DateSubmitted

GO

