/****** TABLES AND PRIMARY KEYS ******/
if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Addons') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Addons (
 [AddonKey] NVARCHAR (50) NOT NULL,
 [Availability] NVARCHAR (255),
 [Description] NVARCHAR (255),
 [Installed] DATETIME,
 [Name] NVARCHAR (50),
) ON [PRIMARY]
GO

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

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Attributes') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Attributes (
 [Addon] NVARCHAR (50),
 [EntryTypes] NVARCHAR (255),
 [Explanation] NVARCHAR (255),
 [id] INT IDENTITY (1, 1),
 [IsPrivate] BIT,
 [Name] NVARCHAR (50) NOT NULL,
 [PortalId] INT,
 [ValueType] NVARCHAR (30),
) ON [PRIMARY]
GO

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

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Categories') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Categories (
 [id] INT IDENTITY (1, 1),
 [Name] NVARCHAR (100) NOT NULL,
 [ParentId] INT NOT NULL,
 [PortalId] INT NOT NULL,
) ON [PRIMARY]
GO

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

if exists (select * from dbo.sysobjects where id = object_id(N'C_{objectQualifier}DMX_Categories') and OBJECTPROPERTY(id, N'IsConstraint') = 1)

ALTER TABLE {databaseOwner}{objectQualifier}DMX_Categories
  DROP CONSTRAINT C_{objectQualifier}DMX_Categories

GO

ALTER TABLE {databaseOwner}{objectQualifier}DMX_Categories ADD CONSTRAINT
	C_{objectQualifier}DMX_Categories UNIQUE 
	(
	Name,
	PortalId
	) ON [PRIMARY]
GO

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_EntryAttributes') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_EntryAttributes (
 [Attribute] INT,
 [EntryId] INT,
 [id] INT IDENTITY (1, 1),
 [Value] NVARCHAR (255),
) ON [PRIMARY]
GO

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

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Extensions') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_Extensions (
 [AccessRights] NVARCHAR (255),
 [Addon] NVARCHAR (50) NOT NULL,
 [ControlToLoad] NVARCHAR (255),
 [Custom] NVARCHAR (50),
 [ExtensionKey] NVARCHAR (50) NOT NULL,
 [Icon16] NVARCHAR (100),
 [Icon32] NVARCHAR (100),
 [IsPrivate] BIT,
 [MimeType] NVARCHAR (50),
 [Name] NVARCHAR (50),
 [PortalId] INT NOT NULL,
 [ViewByDefault] BIT,
) ON [PRIMARY]
GO

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

if not exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_LogTypes') and OBJECTPROPERTY(id, N'IsTable') = 1)
CREATE TABLE {databaseOwner}{objectQualifier}DMX_LogTypes (
 [LogTypeKey] NVARCHAR (20) NOT NULL,
 [Notification] BIT,
 [Phrase] NVARCHAR (50),
) ON [PRIMARY]
GO

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

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

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


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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAddons
	@AddonKey NVarChar (50)
AS

SELECT
	[AddonKey],
	[Availability],
	[Description],
	[Installed],
	[Name]
FROM
	{databaseOwner}{objectQualifier}DMX_Addons
WHERE
	[AddonKey] = @AddonKey
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddAddons
	@AddonKey NVarChar (50),
	@Availability NVarChar (255),
	@Description NVarChar (255),
	@Installed DateTime,
	@Name NVarChar (50)
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Addons (
	[AddonKey],
	[Availability],
	[Description],
	[Installed],
	[Name]
) VALUES (
	@AddonKey,
	@Availability,
	@Description,
	@Installed,
	@Name
)

GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateAddons
	@AddonKey NVarChar (50),
	@Availability NVarChar (255),
	@Description NVarChar (255),
	@Installed DateTime,
	@Name NVarChar (50)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Addons SET
	[Availability] = @Availability,
	[Description] = @Description,
	[Installed] = @Installed,
	[Name] = @Name
WHERE
	[AddonKey] = @AddonKey
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteAddons
	@AddonKey NVarChar (50)
AS

DELETE FROM {databaseOwner}{objectQualifier}DMX_Addons
WHERE
	[AddonKey] = @AddonKey
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_FindAddonsByName
	@Name NVarChar (50)
AS

SELECT
	[AddonKey],
	[Availability],
	[Description],
	[Installed],
	[Name]
FROM
	{databaseOwner}{objectQualifier}DMX_Addons
WHERE
	[Name] = @Name
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_Attributes_{objectQualifier}DMX_Addons') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Attributes ADD CONSTRAINT
	FK_{objectQualifier}DMX_Attributes_{objectQualifier}DMX_Addons FOREIGN KEY
	(
    Addon
	) REFERENCES {databaseOwner}{objectQualifier}DMX_Addons
	(
    AddonKey
	) ON DELETE NO ACTION

GO

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

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAttributes
	@id Int,
        @PortalId int
AS

SELECT
	[Addon],
	[EntryTypes],
	[Explanation],
	[id],
	[IsPrivate],
	[Name],
	[PortalId],
	[ValueType]
FROM
	{databaseOwner}{objectQualifier}DMX_Attributes
WHERE
	[id] = @id
	AND [PortalId] = @PortalId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddAttributes
	@Addon NVarChar (50),
	@EntryTypes NVarChar (255),
	@Explanation NVarChar (255),
	@IsPrivate Bit,
	@Name NVarChar (50),
	@PortalId Int,
	@ValueType NVarChar (30)
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Attributes (
	[Addon],
	[EntryTypes],
	[Explanation],
	[IsPrivate],
	[Name],
	[PortalId],
	[ValueType]
) VALUES (
	@Addon,
	@EntryTypes,
	@Explanation,
	@IsPrivate,
	@Name,
	@PortalId,
	@ValueType
)

select SCOPE_IDENTITY()
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateAttributes
	@id Int,
	@Addon NVarChar (50),
	@EntryTypes NVarChar (255),
	@Explanation NVarChar (255),
	@IsPrivate Bit,
	@Name NVarChar (50),
	@PortalId Int,
	@ValueType NVarChar (30)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Attributes SET
	[Addon] = @Addon,
	[EntryTypes] = @EntryTypes,
	[Explanation] = @Explanation,
	[IsPrivate] = @IsPrivate,
	[Name] = @Name,
	[PortalId] = @PortalId,
	[ValueType] = @ValueType
WHERE
	[id] = @id
GO

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

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

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

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

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_FindAttributesByName
	@Name NVarChar (50),
        @PortalId int
AS

SELECT
	[Addon],
	[EntryTypes],
	[Explanation],
	[id],
	[IsPrivate],
	[Name],
	[PortalId],
	[ValueType]
FROM
	{databaseOwner}{objectQualifier}DMX_Attributes
WHERE
	[Name] = @Name
	AND [PortalId] = @PortalId
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAttributesByAddons
	@Addon NVarChar (50)
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Attributes.Addon,
	{databaseOwner}{objectQualifier}DMX_Attributes.EntryTypes,
	{databaseOwner}{objectQualifier}DMX_Attributes.Explanation,
	{databaseOwner}{objectQualifier}DMX_Attributes.id,
	{databaseOwner}{objectQualifier}DMX_Attributes.IsPrivate,
	{databaseOwner}{objectQualifier}DMX_Attributes.Name,
	{databaseOwner}{objectQualifier}DMX_Attributes.PortalId,
	{databaseOwner}{objectQualifier}DMX_Attributes.ValueType, 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Attributes.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Attributes.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.Addon = @Addon
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Attributes.Addon,
	{databaseOwner}{objectQualifier}DMX_Attributes.EntryTypes,
	{databaseOwner}{objectQualifier}DMX_Attributes.Explanation,
	{databaseOwner}{objectQualifier}DMX_Attributes.id,
	{databaseOwner}{objectQualifier}DMX_Attributes.IsPrivate,
	{databaseOwner}{objectQualifier}DMX_Attributes.Name,
	{databaseOwner}{objectQualifier}DMX_Attributes.PortalId,
	{databaseOwner}{objectQualifier}DMX_Attributes.ValueType, 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Attributes.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Attributes.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.PortalId = @PortalId
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_Categories_{objectQualifier}Portals') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Categories ADD CONSTRAINT
	FK_{objectQualifier}DMX_Categories_{objectQualifier}Portals FOREIGN KEY
	(
    PortalId
	) REFERENCES {databaseOwner}{objectQualifier}Portals
	(
    PortalID
	) ON DELETE CASCADE

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetCategories
	@id Int,
        @PortalId int
AS

SELECT
	[id],
	[Name],
	[ParentId],
	[PortalId]
FROM
	{databaseOwner}{objectQualifier}DMX_Categories
WHERE
	[id] = @id
	AND [PortalId] = @PortalId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddCategories
	@Name NVarChar (100),
	@ParentId Int,
	@PortalId Int
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Categories (
	[Name],
	[ParentId],
	[PortalId]
) VALUES (
	@Name,
	@ParentId,
	@PortalId
)

select SCOPE_IDENTITY()
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateCategories
	@id Int,
	@Name NVarChar (100),
	@ParentId Int,
	@PortalId Int
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Categories SET
	[Name] = @Name,
	[ParentId] = @ParentId,
	[PortalId] = @PortalId
WHERE
	[id] = @id
GO

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

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

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

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

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_FindCategoriesByName
	@Name NVarChar (100),
        @PortalId int
AS

SELECT
	[id],
	[Name],
	[ParentId],
	[PortalId]
FROM
	{databaseOwner}{objectQualifier}DMX_Categories
WHERE
	[Name] = @Name
	AND [PortalId] = @PortalId
GO

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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Categories.id,
	{databaseOwner}{objectQualifier}DMX_Categories.Name,
	{databaseOwner}{objectQualifier}DMX_Categories.ParentId,
	{databaseOwner}{objectQualifier}DMX_Categories.PortalId, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Categories ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Categories.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Categories.PortalId = @PortalId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON
GO
SET ANSI_NULLS OFF 
GO

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

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

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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddEntries
	@Author NVarChar (50),
	@Categories NVarChar (255),
	@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),
	@LastModified DateTime,
	@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],
	[Categories],
	[CollectionId],
	[DateSubmitted],
	[Deleted],
	[Description],
	[EditRoles],
	[Entry],
	[EntryType],
	[FileSize],
	[IsApproved],
	[IsLocked],
	[Keywords],
	[LastModified],
	[LastVersionId],
	[LockedBy],
	[OriginalFileName],
	[PortalId],
	[Remarks],
	[UserId],
	[Version],
	[ViewRoles]
) VALUES (
	@Author,
	@Categories,
	@CollectionId,
	@DateSubmitted,
	@Deleted,
	@Description,
	@EditRoles,
	@Entry,
	@EntryType,
	@FileSize,
	@IsApproved,
	@IsLocked,
	@Keywords,
	@LastModified,
	@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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateEntries
	@EntryId Int,
	@Author NVarChar (50),
	@Categories NVarChar (255),
	@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),
	@LastModified DateTime,
	@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,
	[Categories] = @Categories,
	[CollectionId] = @CollectionId,
	[DateSubmitted] = @DateSubmitted,
	[Deleted] = @Deleted,
	[Description] = @Description,
	[EditRoles] = @EditRoles,
	[Entry] = @Entry,
	[EntryType] = @EntryType,
	[FileSize] = @FileSize,
	[IsApproved] = @IsApproved,
	[IsLocked] = @IsLocked,
	[Keywords] = @Keywords,
	[LastModified] = @LastModified,
	[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

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

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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Entries.Author,
	{databaseOwner}{objectQualifier}DMX_Entries.Categories,
	{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.LastModified,
	{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 AS PortalsDescription
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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Entries.Author,
	{databaseOwner}{objectQualifier}DMX_Entries.Categories,
	{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.LastModified,
	{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 AS PortalsDescription
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

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

GO

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

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	[Attribute],
	[EntryId],
	[id],
	[Value]
FROM
	{databaseOwner}{objectQualifier}DMX_EntryAttributes
WHERE
	[id] = @id
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddEntryAttributes
	@Attribute Int,
	@EntryId Int,
	@Value NVarChar (255)
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_EntryAttributes (
	[Attribute],
	[EntryId],
	[Value]
) VALUES (
	@Attribute,
	@EntryId,
	@Value
)

select SCOPE_IDENTITY()
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateEntryAttributes
	@id Int,
	@Attribute Int,
	@EntryId Int,
	@Value NVarChar (255)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_EntryAttributes SET
	[Attribute] = @Attribute,
	[EntryId] = @EntryId,
	[Value] = @Value
WHERE
	[id] = @id
GO

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

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

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

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

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntryAttributesByAttributes
	@Attribute Int
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Attributes.Name AS AttributesName, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute = @Attribute
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntryAttributesByAddons
	@Addon NVarChar (50)
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN (({databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute)) ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.Addon = @Addon
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN (({databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute)) ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.PortalId = @PortalId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Attributes.Name AS AttributesName, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId = @EntryId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Attributes.Name AS AttributesName
FROM
	{databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN (({databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId)) ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute
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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.id,
	{databaseOwner}{objectQualifier}DMX_EntryAttributes.Value, 
	{databaseOwner}{objectQualifier}DMX_Attributes.Name AS AttributesName
FROM
	{databaseOwner}{objectQualifier}DMX_Attributes INNER JOIN (({databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_EntryAttributes.EntryId)) ON {databaseOwner}{objectQualifier}DMX_Attributes.id = {databaseOwner}{objectQualifier}DMX_EntryAttributes.Attribute
WHERE
	{databaseOwner}{objectQualifier}DMX_Entries.UserId = @UserId
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_Extensions_{objectQualifier}DMX_Addons') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Extensions ADD CONSTRAINT
	FK_{objectQualifier}DMX_Extensions_{objectQualifier}DMX_Addons FOREIGN KEY
	(
    Addon
	) REFERENCES {databaseOwner}{objectQualifier}DMX_Addons
	(
    AddonKey
	) ON DELETE CASCADE

GO

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

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetExtensions
	@PortalId Int, 	@Addon NVarChar (50), 	@ExtensionKey NVarChar (50)
AS

SELECT
	[AccessRights],
	[Addon],
	[ControlToLoad],
	[Custom],
	[ExtensionKey],
	[Icon16],
	[Icon32],
	[IsPrivate],
	[MimeType],
	[Name],
	[PortalId],
	[ViewByDefault]
FROM
	{databaseOwner}{objectQualifier}DMX_Extensions
WHERE
	[PortalId] = @PortalId
	AND [Addon] = @Addon
	AND [ExtensionKey] = @ExtensionKey
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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


CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddExtensions
	@PortalId Int,
	@Addon NVarChar (50),
	@ExtensionKey NVarChar (50),
	@AccessRights NVarChar (255),
	@ControlToLoad NVarChar (255),
	@Custom NVarChar (50),
	@Icon16 NVarChar (100),
	@Icon32 NVarChar (100),
	@IsPrivate Bit,
	@MimeType NVarChar (50),
	@Name NVarChar (50),
	@ViewByDefault Bit
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Extensions (
	PortalId,
	Addon,
	ExtensionKey,
	AccessRights,
	ControlToLoad,
	Custom,
	Icon16,
	Icon32,
	IsPrivate,
	MimeType,
	Name,
	ViewByDefault
) VALUES (
	@PortalId,
	@Addon,
	@ExtensionKey,
	@AccessRights,
	@ControlToLoad,
	@Custom,
	@Icon16,
	@Icon32,
	@IsPrivate,
	@MimeType,
	@Name,
	@ViewByDefault
)

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateExtensions
	@PortalId Int,
	@Addon NVarChar (50),
	@ExtensionKey NVarChar (50),
	@AccessRights NVarChar (255),
	@ControlToLoad NVarChar (255),
	@Custom NVarChar (50),
	@Icon16 NVarChar (100),
	@Icon32 NVarChar (100),
	@IsPrivate Bit,
	@MimeType NVarChar (50),
	@Name NVarChar (50),
	@ViewByDefault Bit
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Extensions SET
	[AccessRights] = @AccessRights,
	[ControlToLoad] = @ControlToLoad,
	[Custom] = @Custom,
	[Icon16] = @Icon16,
	[Icon32] = @Icon32,
	[IsPrivate] = @IsPrivate,
	[MimeType] = @MimeType,
	[Name] = @Name,
	[ViewByDefault] = @ViewByDefault
WHERE
	[PortalId] = @PortalId
	AND [Addon] = @Addon
	AND [ExtensionKey] = @ExtensionKey
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteExtensions
	@PortalId Int,
	@Addon NVarChar (50),
	@ExtensionKey NVarChar (50)
AS

DELETE FROM {databaseOwner}{objectQualifier}DMX_Extensions
WHERE
	[PortalId] = @PortalId
	AND [Addon] = @Addon
	AND [ExtensionKey] = @ExtensionKey
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_FindExtensionsByName
	@Name NVarChar (50),
        @PortalId int
AS

SELECT
	[AccessRights],
	[Addon],
	[ControlToLoad],
	[Custom],
	[ExtensionKey],
	[Icon16],
	[Icon32],
	[IsPrivate],
	[MimeType],
	[Name],
	[PortalId],
	[ViewByDefault]
FROM
	{databaseOwner}{objectQualifier}DMX_Extensions
WHERE
	[Name] = @Name
	AND [PortalId] = @PortalId
GO

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetExtensionsByAddons
	@Addon NVarChar (50)
AS

SELECT
	{databaseOwner}{objectQualifier}DMX_Extensions.AccessRights,
	{databaseOwner}{objectQualifier}DMX_Extensions.Addon,
	{databaseOwner}{objectQualifier}DMX_Extensions.ControlToLoad,
	{databaseOwner}{objectQualifier}DMX_Extensions.Custom,
	{databaseOwner}{objectQualifier}DMX_Extensions.ExtensionKey,
	{databaseOwner}{objectQualifier}DMX_Extensions.Icon16,
	{databaseOwner}{objectQualifier}DMX_Extensions.Icon32,
	{databaseOwner}{objectQualifier}DMX_Extensions.IsPrivate,
	{databaseOwner}{objectQualifier}DMX_Extensions.MimeType,
	{databaseOwner}{objectQualifier}DMX_Extensions.Name,
	{databaseOwner}{objectQualifier}DMX_Extensions.PortalId,
	{databaseOwner}{objectQualifier}DMX_Extensions.ViewByDefault, 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Extensions.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Extensions.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Extensions.Addon = @Addon
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Extensions.AccessRights,
	{databaseOwner}{objectQualifier}DMX_Extensions.Addon,
	{databaseOwner}{objectQualifier}DMX_Extensions.ControlToLoad,
	{databaseOwner}{objectQualifier}DMX_Extensions.Custom,
	{databaseOwner}{objectQualifier}DMX_Extensions.ExtensionKey,
	{databaseOwner}{objectQualifier}DMX_Extensions.Icon16,
	{databaseOwner}{objectQualifier}DMX_Extensions.Icon32,
	{databaseOwner}{objectQualifier}DMX_Extensions.IsPrivate,
	{databaseOwner}{objectQualifier}DMX_Extensions.MimeType,
	{databaseOwner}{objectQualifier}DMX_Extensions.Name,
	{databaseOwner}{objectQualifier}DMX_Extensions.PortalId,
	{databaseOwner}{objectQualifier}DMX_Extensions.ViewByDefault, 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Extensions.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Extensions.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Extensions.PortalId = @PortalId
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

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddLog
	@Datime DateTime,
	@EntId Int,
	@Operation NVarChar (20),
	@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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateLog
	@id Int,
	@Datime DateTime,
	@EntId Int,
	@Operation NVarChar (20),
	@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

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

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

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

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 AS EntriesDescription
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

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

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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetLogByLogTypes
	@Operation NVarChar (20)
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 AS EntriesDescription
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.Operation = @Operation
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO


SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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 AS EntriesDescription
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


SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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],
	[Notification],
	[Phrase]
FROM
	{databaseOwner}{objectQualifier}DMX_LogTypes
WHERE
	[LogTypeKey] = @LogTypeKey
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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),
	@Notification Bit,
	@Phrase NVarChar (50)
AS

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

GO

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

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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),
	@Notification Bit,
	@Phrase NVarChar (50)
AS

UPDATE {databaseOwner}{objectQualifier}DMX_LogTypes SET
	[Notification] = @Notification,
	[Phrase] = @Phrase
WHERE
	[LogTypeKey] = @LogTypeKey
GO

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

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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

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_Subscriptions_{objectQualifier}DMX_Entries') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Subscriptions ADD CONSTRAINT
	FK_{objectQualifier}DMX_Subscriptions_{objectQualifier}DMX_Entries FOREIGN KEY
	(
    EntryId
	) 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_Subscriptions_{objectQualifier}Users') and OBJECTPROPERTY(id, N'IsForeignKey') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Subscriptions ADD CONSTRAINT
	FK_{objectQualifier}DMX_Subscriptions_{objectQualifier}Users FOREIGN KEY
	(
    UserId
	) REFERENCES {databaseOwner}{objectQualifier}Users
	(
    UserID
	) ON DELETE CASCADE

GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	[EntryId],
	[id],
	[LastAccess],
	[UserId]
FROM
	{databaseOwner}{objectQualifier}DMX_Subscriptions
WHERE
	[id] = @id
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddSubscriptions
	@EntryId Int,
	@LastAccess DateTime,
	@UserId Int
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Subscriptions (
	[EntryId],
	[LastAccess],
	[UserId]
) VALUES (
	@EntryId,
	@LastAccess,
	@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

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateSubscriptions
	@id Int,
	@EntryId Int,
	@LastAccess DateTime,
	@UserId Int
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Subscriptions SET
	[EntryId] = @EntryId,
	[LastAccess] = @LastAccess,
	[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

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

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

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

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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.id,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.LastAccess,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.UserId, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Subscriptions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId = @EntryId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO

SET QUOTED_IDENTIFIER ON 
GO
SET ANSI_NULLS OFF 
GO

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.id,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.LastAccess,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.UserId
FROM
	({databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Subscriptions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId)
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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.id,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.LastAccess,
	{databaseOwner}{objectQualifier}DMX_Subscriptions.UserId, 
	{databaseOwner}{objectQualifier}DMX_Entries.Description AS EntriesDescription
FROM
	{databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Subscriptions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Subscriptions.EntryId
WHERE
	{databaseOwner}{objectQualifier}DMX_Subscriptions.UserId = @UserId
GO

SET QUOTED_IDENTIFIER OFF 
GO
SET ANSI_NULLS ON 
GO



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

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 = (
SELECT EntryId FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE {databaseOwner}{objectQualifier}DMX_Entries.PortalId = @PortalId) ))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_DeleteAnAttribute
  @EntryId Int, @AttributeId Int
AS

DELETE
FROM {databaseOwner}{objectQualifier}DMX_EntryAttributes
WHERE ((({objectQualifier}DMX_EntryAttributes.EntryId)=@EntryId) AND (({objectQualifier}DMX_EntryAttributes.Attribute)=@AttributeId))

GO

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

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

SELECT {databaseOwner}{objectQualifier}DMX_Entries.Description, {databaseOwner}{objectQualifier}DMX_Entries.EntryId, {databaseOwner}{objectQualifier}DMX_Entries.CollectionId, {databaseOwner}{objectQualifier}DMX_Entries.EditRoles, {databaseOwner}{objectQualifier}DMX_Entries.ViewRoles, {databaseOwner}{objectQualifier}DMX_Extensions.Name, {databaseOwner}{objectQualifier}DMX_Extensions.Icon16
FROM {databaseOwner}{objectQualifier}DMX_Entries LEFT JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryType={objectQualifier}DMX_Extensions.ExtensionKey
WHERE (((LEFT({objectQualifier}DMX_Entries.EntryType,10)='Collection') Or (LEFT({objectQualifier}DMX_Entries.EntryType,17)='Custom.Collection')) And (({objectQualifier}DMX_Entries.Deleted)=0) And (({objectQualifier}DMX_Entries.PortalId)=@PortalId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Entries.Description

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAttributesByAddonAndPortal
  @AddonKey NVarChar (255), @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Attributes.*
FROM {databaseOwner}{objectQualifier}DMX_Attributes
WHERE ((({objectQualifier}DMX_Attributes.Addon)=@AddonKey) AND (({objectQualifier}DMX_Attributes.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetAttributeValue
  @EntryId Int, @Attribute Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_EntryAttributes.*
FROM {databaseOwner}{objectQualifier}DMX_EntryAttributes
WHERE ((({objectQualifier}DMX_EntryAttributes.EntryId)=@EntryId) AND (({objectQualifier}DMX_EntryAttributes.Attribute)=@Attribute))

GO

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

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

SELECT {databaseOwner}{objectQualifier}DMX_Entries.Description, {databaseOwner}{objectQualifier}DMX_Entries.CollectionId AS CollectionId0, Entries_1.Description AS Description1, Entries_1.CollectionId AS CollectionId1, Entries_2.Description AS Description2, Entries_2.CollectionId AS CollectionId2, Entries_3.Description AS Description3, Entries_3.CollectionId AS CollectionId3, Entries_4.Description AS Description4, Entries_4.CollectionId AS CollectionId4
FROM ((({objectQualifier}DMX_Entries LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_1 ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Entries_1.EntryId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_2 ON Entries_1.CollectionId=Entries_2.EntryId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_3 ON Entries_2.CollectionId=Entries_3.EntryId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_4 ON Entries_3.CollectionId=Entries_4.EntryId
WHERE ((({objectQualifier}DMX_Entries.EntryId)=@EntryId) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetCategoryBreadCrumbs
  @CategoryId Int, @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Categories.Name AS Name0, {databaseOwner}{objectQualifier}DMX_Categories.ParentId AS ParentId0, Categories_1.Name AS Name1, Categories_1.ParentId AS ParentId1, Categories_2.Name AS Name2, Categories_2.ParentId AS ParentId2, Categories_3.Name AS Name3, Categories_3.ParentId AS ParentId3, Categories_4.Name AS Name4, Categories_4.ParentId AS ParentId4
FROM ((({objectQualifier}DMX_Categories LEFT JOIN {databaseOwner}{objectQualifier}DMX_Categories AS Categories_1 ON {databaseOwner}{objectQualifier}DMX_Categories.ParentId = Categories_1.id) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Categories AS Categories_2 ON Categories_1.ParentId = Categories_2.id) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Categories AS Categories_3 ON Categories_2.ParentId = Categories_3.id) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Categories AS Categories_4 ON Categories_3.ParentId = Categories_4.id
WHERE ((({objectQualifier}DMX_Categories.id)=@CategoryId) AND (({objectQualifier}DMX_Categories.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetCategoryContents
  @CategoryId Int, @PortalId Int, @UserId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {databaseOwner}{objectQualifier}DMX_Extensions.Name, {databaseOwner}{objectQualifier}DMX_Extensions.Icon16, Entries_1.Description AS CollectionName, UserSubs.id, UserSubs.LastAccess
FROM ((({objectQualifier}Users INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries ON {databaseOwner}{objectQualifier}Users.UserID={objectQualifier}DMX_Entries.UserId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryType={objectQualifier}DMX_Extensions.ExtensionKey) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_1 ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Entries_1.EntryId) LEFT JOIN (SELECT * FROM {databaseOwner}{objectQualifier}DMX_Subscriptions WHERE {databaseOwner}{objectQualifier}DMX_Subscriptions.UserId=@UserId) AS UserSubs ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId=UserSubs.EntryId
WHERE ((({objectQualifier}DMX_Entries.EntryId)=({objectQualifier}DMX_Entries.LastVersionId))
And (';' + {databaseOwner}{objectQualifier}DMX_Entries.Categories + ';' LIKE '%;' + LTRIM(RTRIM(STR(@CategoryId))) + ';%')
AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetCollectionContents
  @ParentId Int, @PortalId Int, @UserId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {databaseOwner}{objectQualifier}DMX_Extensions.Name, {databaseOwner}{objectQualifier}DMX_Extensions.Icon16, UserSubs.id, UserSubs.LastAccess
FROM (({objectQualifier}Users INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries ON {databaseOwner}{objectQualifier}Users.UserID = {databaseOwner}{objectQualifier}DMX_Entries.UserId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryType = {databaseOwner}{objectQualifier}DMX_Extensions.ExtensionKey) LEFT JOIN (SELECT * FROM {databaseOwner}{objectQualifier}DMX_Subscriptions WHERE {databaseOwner}{objectQualifier}DMX_Subscriptions.UserId=@UserId) AS UserSubs ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = UserSubs.EntryId
WHERE ((({objectQualifier}DMX_Entries.EntryId)=([{objectQualifier}DMX_Entries].[LastVersionId])) 
AND (({objectQualifier}DMX_Entries.CollectionId)=@ParentId) 
AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetDeletedItems
  @ThresholdDate DateTime, @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.EntryId, {databaseOwner}{objectQualifier}DMX_Entries.Entry, Max({objectQualifier}DMX_Log.Datime) AS MaxOfDatime
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId={objectQualifier}DMX_Log.EntId
GROUP BY {databaseOwner}{objectQualifier}DMX_Entries.EntryId, {databaseOwner}{objectQualifier}DMX_Entries.Entry, {databaseOwner}{objectQualifier}DMX_Entries.Deleted, {databaseOwner}{objectQualifier}DMX_Entries.PortalId
HAVING (((Max({objectQualifier}DMX_Log.Datime))<@ThresholdDate) AND (({objectQualifier}DMX_Entries.Deleted)=1) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntriesByEntryType
  @EntryType NVarChar (255), @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*
FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE ((({objectQualifier}DMX_Entries.EntryType)=@EntryType) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntryAttributeByEntryAndAttribute
  @EntryId Int, @AttributeId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_EntryAttributes.*
FROM {databaseOwner}{objectQualifier}DMX_EntryAttributes
WHERE ((({objectQualifier}DMX_EntryAttributes.EntryId)=@EntryId) AND (({objectQualifier}DMX_EntryAttributes.Attribute)=@AttributeId))

GO

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

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

SELECT {databaseOwner}{objectQualifier}DMX_Attributes.id, {databaseOwner}{objectQualifier}DMX_Attributes.Name, {databaseOwner}{objectQualifier}DMX_Attributes.Addon, {databaseOwner}{objectQualifier}DMX_Attributes.ValueType, {databaseOwner}{objectQualifier}DMX_Attributes.EntryTypes, {databaseOwner}{objectQualifier}DMX_Attributes.Explanation, {databaseOwner}{objectQualifier}DMX_Attributes.IsPrivate, {databaseOwner}{objectQualifier}DMX_EntryAttributes.id AS EntryAttributeId, {databaseOwner}{objectQualifier}DMX_EntryAttributes.Value
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN ({objectQualifier}DMX_Attributes INNER JOIN {databaseOwner}{objectQualifier}DMX_EntryAttributes ON {databaseOwner}{objectQualifier}DMX_Attributes.id={objectQualifier}DMX_EntryAttributes.Attribute) ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId={objectQualifier}DMX_EntryAttributes.EntryId
WHERE ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) AND (({objectQualifier}DMX_Entries.EntryId)=@EntryId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetExtensionsByAddonAndPortal
  @AddonKey NVarChar (255), @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Extensions.*
FROM {databaseOwner}{objectQualifier}DMX_Extensions
WHERE ((({objectQualifier}DMX_Extensions.Addon)=@AddonKey) AND (({objectQualifier}DMX_Extensions.PortalId)=@PortalId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetExtensionsByKey
  @PortalId Int, @ExtensionKey NVarChar (255)
AS

SELECT {databaseOwner}{objectQualifier}DMX_Extensions.*
FROM {databaseOwner}{objectQualifier}DMX_Extensions
WHERE ((({objectQualifier}DMX_Extensions.PortalId)=@PortalId) AND (({objectQualifier}DMX_Extensions.ExtensionKey)=@ExtensionKey))

GO

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

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

SELECT TOP 50 {databaseOwner}{objectQualifier}DMX_Entries.*, Collection.Description AS CollectionName
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Collection ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Collection.EntryId
WHERE ((({objectQualifier}DMX_Entries.PortalId)=@PortalId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Entries.DateSubmitted DESC

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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 {databaseOwner}{objectQualifier}DMX_Log.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {databaseOwner}{objectQualifier}DMX_Entries.Version, {databaseOwner}{objectQualifier}DMX_LogTypes.Phrase
FROM {databaseOwner}{objectQualifier}Users INNER JOIN ({objectQualifier}DMX_LogTypes INNER JOIN ({objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Log.EntId) ON {databaseOwner}{objectQualifier}DMX_LogTypes.LogTypeKey = {databaseOwner}{objectQualifier}DMX_Log.Operation) ON {databaseOwner}{objectQualifier}Users.UserID = {databaseOwner}{objectQualifier}DMX_Log.UserId
WHERE ((({objectQualifier}DMX_Entries.LastVersionId)=@LastVersionId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Log.Datime DESC

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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 {databaseOwner}{objectQualifier}DMX_Log.*, LTrim(RTrim({objectQualifier}Users.FirstName+' '+{objectQualifier}Users.LastName)) AS FullName, {databaseOwner}{objectQualifier}DMX_Entries.Version, {databaseOwner}{objectQualifier}DMX_LogTypes.Phrase
FROM {databaseOwner}{objectQualifier}Users INNER JOIN ({objectQualifier}DMX_LogTypes INNER JOIN ({objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Log ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId = {databaseOwner}{objectQualifier}DMX_Log.EntId) ON {databaseOwner}{objectQualifier}DMX_LogTypes.LogTypeKey = {databaseOwner}{objectQualifier}DMX_Log.Operation) ON {databaseOwner}{objectQualifier}Users.UserID = {databaseOwner}{objectQualifier}DMX_Log.UserId
WHERE ((({objectQualifier}DMX_Entries.EntryId)=@EntryId))
ORDER BY {databaseOwner}{objectQualifier}DMX_Log.Datime DESC

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetSubscription
  @UserId Int, @EntryId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Subscriptions.*
FROM {databaseOwner}{objectQualifier}DMX_Subscriptions
WHERE ((({objectQualifier}DMX_Subscriptions.UserId)=@UserId) AND (({objectQualifier}DMX_Subscriptions.EntryId)=@EntryId))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetUserSubscriptions
  @UserId Int, @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Subscriptions.id, {databaseOwner}{objectQualifier}DMX_Subscriptions.LastAccess, {databaseOwner}{objectQualifier}DMX_Entries.*, {databaseOwner}{objectQualifier}DMX_Extensions.Name, {databaseOwner}{objectQualifier}DMX_Extensions.Icon16, Entries_1.Description AS CollectionName
FROM (({objectQualifier}DMX_Entries LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_1 ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Entries_1.EntryId) INNER JOIN {databaseOwner}{objectQualifier}DMX_Subscriptions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId={objectQualifier}DMX_Subscriptions.EntryId) LEFT JOIN {databaseOwner}{objectQualifier}DMX_Extensions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryType={objectQualifier}DMX_Extensions.ExtensionKey
WHERE ((({objectQualifier}DMX_Subscriptions.UserId)=@UserId) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_GetVersionList') and OBJECTPROPERTY(id, N'IsProcedure') = 1)
DROP PROCEDURE {databaseOwner}{objectQualifier}DMX_GetVersionList
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 DESC

GO

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

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

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, Entries_1.Description AS ParentName
FROM {databaseOwner}{objectQualifier}DMX_Entries LEFT JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Entries_1 ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Entries_1.EntryId
WHERE ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) And (LEFT({objectQualifier}DMX_Entries.EntryType,4)='File'))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_QuickSearch
  @SearchText NVarChar (255), @PortalId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*, Collection.Description AS CollectionName
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Entries AS Collection ON {databaseOwner}{objectQualifier}DMX_Entries.CollectionId=Collection.EntryId
WHERE ((({objectQualifier}DMX_Entries.Description) Like @SearchText) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId)) OR ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) AND (({objectQualifier}DMX_Entries.Remarks) Like @SearchText)) OR ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) AND (({objectQualifier}DMX_Entries.Author) Like @SearchText)) OR ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) AND (({objectQualifier}DMX_Entries.Keywords) Like @SearchText)) OR ((({objectQualifier}DMX_Entries.PortalId)=@PortalId) AND (({objectQualifier}DMX_Entries.OriginalFileName) Like @SearchText))

GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_RelinkSubscriptions
  @NewEntryId Int, @OldEntryId Int, @PortalId Int
AS

UPDATE {databaseOwner}{objectQualifier}DMX_Subscriptions
SET EntryId = @NewEntryId
FROM {databaseOwner}{objectQualifier}DMX_Entries INNER JOIN {databaseOwner}{objectQualifier}DMX_Subscriptions ON {databaseOwner}{objectQualifier}DMX_Entries.EntryId={objectQualifier}DMX_Subscriptions.EntryId 
WHERE ((({objectQualifier}DMX_Subscriptions.EntryId)=@OldEntryId) AND (({objectQualifier}DMX_Entries.PortalId)=@PortalId))

GO

