/*
Script created by SQL Compare version 5.3.0.44 from Red Gate Software Ltd at 13/10/2008 10:50:12
*/
SET NUMERIC_ROUNDABORT OFF
GO
SET ANSI_PADDING, ANSI_WARNINGS, CONCAT_NULL_YIELDS_NULL, ARITHABORT, QUOTED_IDENTIFIER, ANSI_NULLS ON
GO
SET XACT_ABORT ON
GO
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Notifications]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Notifications]
(
[LogId] [int] NOT NULL,
[PortalId] [int] NOT NULL,
[Sent] [datetime] NULL,
[Template] [nvarchar] (100) NOT NULL,
[UserId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Notifications] on {databaseOwner}[{objectQualifier}DMX_Notifications]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Notifications] ADD CONSTRAINT [PK_{objectQualifier}DMX_Notifications] PRIMARY KEY CLUSTERED  ([LogId], [UserId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateNotification]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateNotification
	@UserId Int,
	@LogId Int,
	@PortalId Int,
	@Sent DateTime,
	@Template NVarChar (100)
AS

UPDATE dbo.{objectQualifier}DMX_Notifications SET
	[PortalId] = @PortalId,
	[Sent] = @Sent,
	[Template] = @Template
WHERE
	[UserId] = @UserId
	AND [LogId] = @LogId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Subscriptions]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Subscriptions]
(
[EntryId] [int] NOT NULL,
[LastAccess] [datetime] NULL,
[UserId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Subscriptions] on {databaseOwner}[{objectQualifier}DMX_Subscriptions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Subscriptions] ADD CONSTRAINT [PK_{objectQualifier}DMX_Subscriptions] PRIMARY KEY CLUSTERED  ([EntryId], [UserId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetSubscription]'
GO

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

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_Subscriptions
WHERE
	[UserId] = @UserId
	AND [EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Addons]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Addons]
(
[AddonKey] [nvarchar] (50) NOT NULL,
[Availability] [nvarchar] (255) NULL,
[Description] [nvarchar] (255) NULL,
[Installed] [datetime] NULL,
[Name] [nvarchar] (50) NULL,
[Controller] [nvarchar] (255) NULL,
[Features] [int] NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Addons] on {databaseOwner}[{objectQualifier}DMX_Addons]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Addons] ADD CONSTRAINT [PK_{objectQualifier}DMX_Addons] PRIMARY KEY CLUSTERED  ([AddonKey])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Attributes]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Attributes]
(
[Addon] [nvarchar] (50) NULL,
[AttributeId] [int] NOT NULL IDENTITY(1, 1),
[ControlToLoad] [nvarchar] (255) NULL,
[EntryTypes] [nvarchar] (255) NULL,
[IsPrivate] [bit] NULL,
[Name] [nvarchar] (50) NOT NULL,
[PortalId] [int] NULL,
[Required] [bit] NULL,
[ResourceFile] [nvarchar] (255) NULL,
[Values] [nvarchar] (2000) NULL,
[ValueType] [nvarchar] (30) NULL,
[ViewOrder] [int] NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Attributes] on {databaseOwner}[{objectQualifier}DMX_Attributes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Attributes] ADD CONSTRAINT [PK_{objectQualifier}DMX_Attributes] PRIMARY KEY CLUSTERED  ([AttributeId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Extensions]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Extensions]
(
[AccessRights] [nvarchar] (255) NULL,
[Addon] [nvarchar] (50) NOT NULL,
[ControlToLoad] [nvarchar] (255) NULL,
[Custom] [nvarchar] (400) NULL,
[DownloadUrl] [nvarchar] (255) NULL,
[EntryTypes] [nvarchar] (255) NULL,
[ExtensionKey] [nvarchar] (50) NOT NULL,
[Icon16] [nvarchar] (100) NULL,
[Icon32] [nvarchar] (100) NULL,
[IsPrivate] [bit] NULL,
[MimeType] [nvarchar] (50) NULL,
[NodePattern] [nvarchar] (50) NULL,
[PortalId] [int] NOT NULL,
[ResourceFile] [nvarchar] (255) NULL,
[SettingsControl] [nvarchar] (250) NULL,
[ViewByDefault] [bit] NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Extensions] on {databaseOwner}[{objectQualifier}DMX_Extensions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Extensions] ADD CONSTRAINT [PK_{objectQualifier}DMX_Extensions] PRIMARY KEY CLUSTERED  ([ExtensionKey], [PortalId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Entries]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Entries]
(
[Author] [nvarchar] (500) NULL,
[CollectionId] [int] NULL,
[Deleted] [bit] NULL,
[Entry] [nvarchar] (255) NOT NULL,
[EntryId] [int] NOT NULL IDENTITY(1, 1),
[EntryType] [nvarchar] (150) NOT NULL,
[FileSize] [bigint] NULL,
[Created] [datetime] NULL,
[IsApproved] [bit] NULL,
[Keywords] [nvarchar] (255) NULL,
[LastModified] [datetime] NULL,
[LastVersionId] [int] NULL,
[LockedBy] [int] NULL,
[LockedUntil] [datetime] NULL,
[LockKey] [uniqueidentifier] NULL,
[MD5Hash] [nvarchar] (50) NULL,
[OriginalFileName] [nvarchar] (255) NULL,
[Owner] [int] NULL,
[PortalId] [int] NULL,
[State] [nvarchar] (50) NULL,
[Version] [int] NULL,
[VersionsToKeep] [int] NULL,
[WorkflowId] [nvarchar] (50) NULL,
[Path] [varchar] (2000) NULL,
[PermissionsOnlyByAdmin] [bit] NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Entries] on {databaseOwner}[{objectQualifier}DMX_Entries]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Entries] ADD CONSTRAINT [PK_{objectQualifier}DMX_Entries] PRIMARY KEY CLUSTERED  ([EntryId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddEntry
	@Author NVarChar(500),
	@CollectionId Int,
	@Created DateTime,
	@Deleted Bit,
	@Entry NVarChar (255),
	@EntryType NVarChar (150),
	@FileSize BIGINT,
	@IsApproved Bit,
	@Keywords NVarChar (255),
	@LastModified DateTime,
	@LastVersionId Int,
	@LockedBy Int,
	@LockedUntil DateTime,
	@LockKey UNIQUEIDENTIFIER,
	@MD5Hash NVarChar(50),
	@OriginalFileName NVarChar (255),
	@Owner Int,
	@Path NVarChar (255),
	@PermissionsOnlyByAdmin Bit,
	@PortalId Int,
	@State NVarChar (50),
	@Version Int,
	@VersionsToKeep Int,
	@WorkflowId NVarChar (50)
AS

INSERT INTO dbo.{objectQualifier}DMX_Entries (
	[Author],
	[CollectionId],
	[Created],
	[Deleted],
	[Entry],
	[EntryType],
	[FileSize],
	[IsApproved],
	[Keywords],
	[LastModified],
	[LastVersionId],
	[LockedBy],
	[LockedUntil],
	[LockKey],
	[MD5Hash],
	[OriginalFileName],
	[Owner],
	[Path],
	[PermissionsOnlyByAdmin],
	[PortalId],
	[State],
	[Version],
	[VersionsToKeep],
	[WorkflowId]
) VALUES (
	@Author,
	@CollectionId,
	@Created,
	@Deleted,
	@Entry,
	@EntryType,
	@FileSize,
	@IsApproved,
	@Keywords,
	@LastModified,
	@LastVersionId,
	@LockedBy,
	@LockedUntil,
	@LockKey,
	@MD5Hash,
	@OriginalFileName,
	@Owner,
	@Path,
	@PermissionsOnlyByAdmin,
	@PortalId,
	@State,
	@Version,
	@VersionsToKeep,
	@WorkflowId
)

select SCOPE_IDENTITY()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Permissions]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Permissions]
(
[Addon] [nvarchar] (50) NULL,
[PermissionId] [int] NOT NULL IDENTITY(1, 1),
[PermissionKey] [nvarchar] (20) NOT NULL,
[PortalId] [int] NULL,
[ResourceFile] [nvarchar] (255) NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Permissions] on {databaseOwner}[{objectQualifier}DMX_Permissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Permissions] ADD CONSTRAINT [PK_{objectQualifier}DMX_Permissions] PRIMARY KEY CLUSTERED  ([PermissionId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes]
(
[EntryType] [nvarchar] (100) NOT NULL,
[PermissionId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_PermissionEntrytypes] on {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ADD CONSTRAINT [PK_{objectQualifier}DMX_PermissionEntrytypes] PRIMARY KEY CLUSTERED  ([EntryType], [PermissionId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_LogTypes]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_LogTypes]
(
[LogTypeKey] [nvarchar] (20) NOT NULL,
[ResourceFile] [nvarchar] (150) NULL,
[AttachFile] [bit] NULL,
[CustomSend] [bit] NULL,
[NotifyApprovers] [nvarchar] (100) NULL,
[NotifyAudit] [bit] NULL,
[NotifyEditors] [nvarchar] (100) NULL,
[NotifyOwner] [nvarchar] (100) NULL,
[NotifySelf] [nvarchar] (100) NULL,
[NotifySubscribers] [nvarchar] (100) NULL,
[PortalId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_LogTypes] on {databaseOwner}[{objectQualifier}DMX_LogTypes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_LogTypes] ADD CONSTRAINT [PK_{objectQualifier}DMX_LogTypes] PRIMARY KEY CLUSTERED  ([LogTypeKey], [PortalId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteLogType]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteLogType
	@PortalId Int,
	@LogTypeKey NVarChar (20)
AS

DELETE FROM dbo.{objectQualifier}DMX_LogTypes
WHERE
	[PortalId] = @PortalId
	AND [LogTypeKey] = @LogTypeKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_SetLastModified]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_SetLastModified
 @PortalId Int,
 @EntryId Int
AS
 DECLARE @CollectionId INT
 SELECT @CollectionId = (SELECT CollectionId FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)
 IF @CollectionId>0
  EXEC dbo.{objectQualifier}DMX_SetLastModified @PortalId, @CollectionId
 UPDATE dbo.{objectQualifier}DMX_Entries
 SET LastModified=GETDATE()
 WHERE EntryId=@EntryId
 AND PortalId=@PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddNotification]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddNotification
	@UserId Int,	
	@LogId Int,
	@PortalId Int,
	@Sent DateTime,
	@Template NVarChar (100)
AS

INSERT INTO dbo.{objectQualifier}DMX_Notifications (
	[UserId],
	[LogId],
	PortalId,
	Sent,
	Template
) VALUES (
	@UserId,
	@LogId,
	@PortalId,
	@Sent,
	@Template
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_ShortTexts]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_ShortTexts]
(
[Locale] [nvarchar] (6) NOT NULL,
[ObjectId] [int] NOT NULL,
[SubTypeKey] [nvarchar] (20) NOT NULL,
[Text] [nvarchar] (255) NOT NULL,
[TypeKey] [nvarchar] (3) NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_ShortTexts] on {databaseOwner}[{objectQualifier}DMX_ShortTexts]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_ShortTexts] ADD CONSTRAINT [PK_{objectQualifier}DMX_ShortTexts] PRIMARY KEY CLUSTERED  ([Locale], [ObjectId], [SubTypeKey], [TypeKey])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_EntryAttributes]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_EntryAttributes]
(
[AttributeId] [int] NOT NULL,
[EntryId] [int] NOT NULL,
[Value] [nvarchar] (2000) NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_EntryAttributes] on {databaseOwner}[{objectQualifier}DMX_EntryAttributes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryAttributes] ADD CONSTRAINT [PK_{objectQualifier}DMX_EntryAttributes] PRIMARY KEY CLUSTERED  ([AttributeId], [EntryId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteAttribute
	@AttributeId Int
AS
BEGIN
 DELETE FROM {objectQualifier}DMX_EntryAttributes WHERE AttributeId=@AttributeId
 DELETE FROM dbo.{objectQualifier}DMX_Attributes WHERE [AttributeId] = @AttributeId
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_LongTexts]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_LongTexts]
(
[Locale] [nvarchar] (6) NOT NULL,
[ObjectId] [int] NOT NULL,
[SubTypeKey] [nvarchar] (20) NOT NULL,
[Text] [nvarchar] (max) NOT NULL,
[TypeKey] [nvarchar] (3) NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_LongTexts] on {databaseOwner}[{objectQualifier}DMX_LongTexts]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_LongTexts] ADD CONSTRAINT [PK_{objectQualifier}DMX_LongTexts] PRIMARY KEY CLUSTERED  ([Locale], [ObjectId], [SubTypeKey], [TypeKey])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntry
	@EntryId Int,
    @PortalId int,
    @Locale NVarChar(10)
AS

SELECT
	e.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale='')) Title,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale='')) Remarks
FROM
	dbo.{objectQualifier}DMX_Entries e
WHERE
	[EntryId] = @EntryId
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Log]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Log]
(
[Action] [nvarchar] (20) NOT NULL,
[Datime] [datetime] NOT NULL,
[EntId] [int] NOT NULL,
[id] [int] NOT NULL IDENTITY(1, 1),
[UserId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Log] on {databaseOwner}[{objectQualifier}DMX_Log]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Log] ADD CONSTRAINT [PK_{objectQualifier}DMX_Log] PRIMARY KEY CLUSTERED  ([id])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateLog]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateLog
	@id Int,
	@Action NVarChar (20),
	@Datime DateTime,
	@EntId Int,
	@UserId Int
AS

UPDATE dbo.{objectQualifier}DMX_Log SET
	[Action] = @Action,
	[Datime] = @Datime,
	[EntId] = @EntId,
	[UserId] = @UserId
WHERE
	[id] = @id

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetParentChain]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetParentChain
 @PortalId Int,
 @EntryId Int,
 @Locale NVarChar(10)
AS
 DECLARE @CollectionId INT
 SELECT @CollectionId = (SELECT CollectionId FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)
 IF @CollectionId>0
  EXEC dbo.{objectQualifier}DMX_GetParentChain @PortalId, @CollectionId, @Locale
 SELECT e.*,
  ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale='')) Title
  FROM dbo.{objectQualifier}DMX_Entries e WHERE [EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_EntryCategories]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_EntryCategories]
(
[CategoryId] [int] NOT NULL,
[EntryId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_EntryCategories] on {databaseOwner}[{objectQualifier}DMX_EntryCategories]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryCategories] ADD CONSTRAINT [PK_{objectQualifier}DMX_EntryCategories] PRIMARY KEY CLUSTERED  ([CategoryId], [EntryId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_MoveEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_MoveEntry
 @PortalId Int,
 @EntryId Int,
 @NewCollectionId INT
AS
 UPDATE dbo.{objectQualifier}DMX_Entries
  SET CollectionId=@NewCollectionId
 WHERE
  LastVersionId=@EntryId
  AND PortalId=@PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_EntryPermissions]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_EntryPermissions]
(
[AllowAccess] [bit] NULL,
[EntryId] [int] NOT NULL,
[Expires] [datetime] NULL,
[PermissionId] [int] NOT NULL,
[PortalId] [int] NOT NULL,
[RoleId] [int] NOT NULL,
[UserId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_EntryPermissions] on {databaseOwner}[{objectQualifier}DMX_EntryPermissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryPermissions] ADD CONSTRAINT [PK_{objectQualifier}DMX_EntryPermissions] PRIMARY KEY CLUSTERED  ([EntryId], [PermissionId], [RoleId], [UserId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_MoveAllEntries]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_MoveAllEntries
 @PortalId Int,
 @OldCollectionId Int,
 @NewCollectionId Int
AS

UPDATE dbo.{objectQualifier}DMX_Entries SET
 [CollectionId] = @NewCollectionId
WHERE
 [CollectionId] = @OldCollectionId
 AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateAttribute
	@AttributeId Int,
	@Addon NVarChar (50),
	@ControlToLoad NVarChar (255),
	@EntryTypes NVarChar (255),
	@IsPrivate Bit,
	@Name NVarChar (50),
	@PortalId Int,
	@Required Bit,
	@ResourceFile NVarChar (255),
	@Values NVarChar (2000),
	@ValueType NVarChar (30),
	@ViewOrder Int
AS

UPDATE dbo.{objectQualifier}DMX_Attributes SET
	[Addon] = @Addon,
	[ControlToLoad] = @ControlToLoad,
	[EntryTypes] = @EntryTypes,
	[IsPrivate] = @IsPrivate,
	[Name] = @Name,
	[PortalId] = @PortalId,
	[Required] = @Required,
	[ResourceFile] = @ResourceFile,
	[Values] = @Values,
	[ValueType] = @ValueType,
	[ViewOrder] = @ViewOrder
WHERE
	[AttributeId] = @AttributeId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddPermission
	@Addon NVarChar (50),
	@PermissionKey NVarChar (20),
	@PortalId Int,
	@ResourceFile NVarChar (255)
AS

INSERT INTO dbo.{objectQualifier}DMX_Permissions (
	[Addon],
	[PermissionKey],
	[PortalId],
	[ResourceFile]
) VALUES (

	@Addon,
	@PermissionKey,
	@PortalId,
	@ResourceFile
)

select SCOPE_IDENTITY()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateLogType]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateLogType
	@PortalId Int,
	@LogTypeKey NVarChar (20),
	@AttachFile Bit,
	@CustomSend Bit,
	@NotifyApprovers NVarChar (100),
	@NotifyAudit BIT,
	@NotifyEditors NVarChar (100),
	@NotifyOwner NVarChar (100),
	@NotifySelf NVarChar (100),
	@NotifySubscribers NVarChar (100),
	@ResourceFile NVarChar (150)
AS

UPDATE dbo.{objectQualifier}DMX_LogTypes SET
	[AttachFile] = @AttachFile,
	[CustomSend] = @CustomSend,
	[NotifyApprovers] = @NotifyApprovers,
	[NotifyAudit] = @NotifyAudit,
	[NotifyEditors] = @NotifyEditors,
	[NotifyOwner] = @NotifyOwner,
	[NotifySelf] = @NotifySelf,
	[NotifySubscribers] = @NotifySubscribers,
	[ResourceFile] = @ResourceFile
WHERE
	[PortalId] = @PortalId
	AND [LogTypeKey] = @LogTypeKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetAttributesByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetAttributesByPortal
	@PortalId Int
AS

SELECT
	a.*, 
	ad.Description AS AddonsDescription
FROM
	dbo.{objectQualifier}DMX_Addons ad INNER JOIN dbo.{objectQualifier}DMX_Attributes a ON ad.AddonKey = a.Addon
WHERE
	a.PortalId = @PortalId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddSubscription]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddSubscription
	@UserId Int,	
	@EntryId Int,
	@LastAccess DateTime
AS

INSERT INTO dbo.{objectQualifier}DMX_Subscriptions (
	[UserId],
	[EntryId],
	LastAccess
) VALUES (
	@UserId,
	@EntryId,
	@LastAccess
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_PermissionPermissions]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_PermissionPermissions]
(
[PermissionId] [int] NOT NULL,
[RoleId] [int] NOT NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_PermissionPermissions] on {databaseOwner}[{objectQualifier}DMX_PermissionPermissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] ADD CONSTRAINT [PK_{objectQualifier}DMX_PermissionPermissions] PRIMARY KEY CLUSTERED  ([PermissionId], [RoleId])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionPermission
	@PermissionId Int,
	@RoleId Int
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_PermissionPermissions
WHERE
	[PermissionId] = @PermissionId
	AND [RoleId] = @RoleId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UnifySetPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UnifySetPermission
 @PortalId INT,
 @UnifyingUserId INT,
 @CollectionId INT,
 @PermissionKey NVARCHAR(20),
 @RoleId INT,
 @UserId INT
AS

BEGIN
 DECLARE @Id INT
 SELECT @Id = MIN(e.EntryId)
  FROM dbo.{objectQualifier}DMX_Entries e
  INNER JOIN
   (SELECT DISTINCT ep.EntryId FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Permissions pt ON pt.PermissionId=ep.PermissionId
   LEFT JOIN dbo.{objectQualifier}UserRoles r ON r.RoleId=ep.RoleId WHERE pt.PermissionKey='EDIT' AND (ep.UserId=@UnifyingUserId OR r.UserId=@UnifyingUserId OR ep.RoleId=-1)) perm
  ON perm.EntryId=e.EntryId
  WHERE e.CollectionId=@CollectionId
 DECLARE @PermissionId INT
 SELECT @PermissionId = PermissionId FROM dbo.{objectQualifier}DMX_Permissions WHERE PermissionKey=@PermissionKey AND PortalId=@PortalId
 DECLARE @EntryType NVARCHAR(100)
 WHILE @Id IS NOT NULL
 BEGIN
  SELECT @EntryType = EntryType FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@Id
  IF (SELECT COUNT(*) FROM dbo.{objectQualifier}DMX_PermissionEntrytypes WHERE PermissionId=@PermissionId AND @EntryType LIKE EntryType+'%') = 1
  BEGIN
   IF NOT EXISTS(SELECT * FROM dbo.{objectQualifier}DMX_EntryPermissions 
    WHERE [EntryId]=@Id AND [PermissionId]=@PermissionId AND [RoleId]=@RoleId AND [UserId]=@UserId AND [AllowAccess]=1 AND [PortalId]=@PortalId)
   INSERT INTO dbo.{objectQualifier}DMX_EntryPermissions ([EntryId], [PermissionId], [RoleId], [UserId], [AllowAccess], [Expires], [PortalId])
    VALUES (@Id, @PermissionId, @RoleId, @UserId, 1, NULL, @PortalId)
  END
  EXEC dbo.{objectQualifier}DMX_UnifySetPermission @PortalId, @UnifyingUserId, @Id, @PermissionKey, @RoleId, @UserId
  SELECT @Id = MIN(e.EntryId)
   FROM dbo.{objectQualifier}DMX_Entries e
   INNER JOIN
    (SELECT DISTINCT ep.EntryId FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Permissions pt ON pt.PermissionId=ep.PermissionId
    LEFT JOIN dbo.{objectQualifier}UserRoles r ON r.RoleId=ep.RoleId WHERE pt.PermissionKey='EDIT' AND (ep.UserId=@UnifyingUserId OR r.UserId=@UnifyingUserId OR ep.RoleId=-1)) perm 
   ON perm.EntryId=e.EntryId
   WHERE e.CollectionId=@CollectionId
   AND e.EntryId>@Id
 END
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateEntryAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateEntryAttribute
	@EntryId Int,
	@AttributeId Int,
	@Value NVarChar (2000)
AS

UPDATE dbo.{objectQualifier}DMX_EntryAttributes SET
	[Value] = @Value
WHERE
	[EntryId] = @EntryId
	AND [AttributeId] = @AttributeId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_Categories]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_Categories]
(
[id] [int] NOT NULL IDENTITY(1, 1),
[ParentId] [int] NOT NULL,
[PortalId] [int] NOT NULL,
[ViewOrder] [int] NULL
)

GO
PRINT N'Creating primary key [PK_{objectQualifier}DMX_Categories] on {databaseOwner}[{objectQualifier}DMX_Categories]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Categories] ADD CONSTRAINT [PK_{objectQualifier}DMX_Categories] PRIMARY KEY CLUSTERED  ([id])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddCategory

	@ParentId Int,
	@PortalId Int,
	@ViewOrder Int
AS

INSERT INTO dbo.{objectQualifier}DMX_Categories (
	[ParentId],
	[PortalId],
	[ViewOrder]
) VALUES (
	@ParentId,
	@PortalId,
	@ViewOrder
)

select SCOPE_IDENTITY()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_FindAddonByName]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_FindAddonByName
	@Name NVarChar (50)
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_Addons
WHERE
	[Name] = @Name

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateLongText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateLongText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6),
	@Text NVarChar(MAX)
AS

UPDATE dbo.{objectQualifier}DMX_LongTexts SET
	[Text] = @Text
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddEntryPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddEntryPermission
	@EntryId Int,	
	@PermissionId Int,	
	@RoleId Int,	
	@UserId Int,
	@AllowAccess Bit,
	@Expires DateTime,
	@PortalId Int
AS

INSERT INTO dbo.{objectQualifier}DMX_EntryPermissions (
	[EntryId],
	[PermissionId],
	[RoleId],
	[UserId],
	AllowAccess,
	Expires,
	PortalId
) VALUES (
	@EntryId,
	@PermissionId,
	@RoleId,
	@UserId,
	@AllowAccess,
	@Expires,
	@PortalId
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeletePermissionPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeletePermissionPermission
	@PermissionId Int,
	@RoleId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_PermissionPermissions
WHERE
	[PermissionId] = @PermissionId
	AND [RoleId] = @RoleId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntriesByUser]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntriesByUser
 @Owner Int,
 @IncludeDeleted BIT
AS

SELECT
	e.*
FROM
	dbo.{objectQualifier}DMX_Entries e
WHERE
	e.Owner = @Owner
	AND	((NOT e.Deleted = 1) OR (@IncludeDeleted = 1))


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteNotification]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteNotification
	@UserId Int,
	@LogId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Notifications
WHERE
	[UserId] = @UserId
	AND [LogId] = @LogId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddEntryAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddEntryAttribute
	@EntryId Int,	
	@AttributeId Int,
	@Value NVarChar (2000)
AS

INSERT INTO dbo.{objectQualifier}DMX_EntryAttributes (
	[EntryId],
	[AttributeId],
	Value
) VALUES (
	@EntryId,
	@AttributeId,
	@Value
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntriesByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntriesByPortal
 @PortalId Int,
 @IncludeDeleted BIT,
 @Locale NVarChar(10)
AS

SELECT
	e.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale='')) Title,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale='')) Remarks
FROM
	dbo.{objectQualifier}DMX_Entries e
WHERE
	e.PortalId = @PortalId
	AND	((NOT e.Deleted = 1) OR (@IncludeDeleted = 1))


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddAttribute

	@Addon NVarChar (50),
	@ControlToLoad NVarChar (255),
	@EntryTypes NVarChar (255),
	@IsPrivate Bit,
	@Name NVarChar (50),
	@PortalId Int,
	@Required Bit,
	@ResourceFile NVarChar (255),
	@Values NVarChar (2000),
	@ValueType NVarChar (30),
	@ViewOrder Int
AS

INSERT INTO dbo.{objectQualifier}DMX_Attributes (
	[Addon],
	[ControlToLoad],
	[EntryTypes],
	[IsPrivate],
	[Name],
	[PortalId],
	[Required],
	[ResourceFile],
	[Values],
	[ValueType],
	[ViewOrder]
) VALUES (
	@Addon,
	@ControlToLoad,
	@EntryTypes,
	@IsPrivate,
	@Name,
	@PortalId,
	@Required,
	@ResourceFile,
	@Values,
	@ValueType,
	@ViewOrder
)

select SCOPE_IDENTITY()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryAttribute
	@EntryId Int,
	@AttributeId Int
AS

SELECT
	ea.[EntryId],
	ea.[Value],
	a.*
FROM
	dbo.{objectQualifier}DMX_EntryAttributes ea
	INNER JOIN dbo.{objectQualifier}DMX_Attributes a ON ea.AttributeId=a.AttributeId
WHERE
	ea.[EntryId] = @EntryId
	AND ea.[AttributeId] = @AttributeId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetAttributesByAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetAttributesByAddon
	@Addon NVarChar (50)
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Attributes a
WHERE
 a.Addon = @Addon


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetApprovedEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetApprovedEntry
	@LastVersionId Int,
    @PortalId int,
    @Locale NVarChar(10)
AS

SELECT
	e.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale='')) Title,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale='')) Remarks
FROM
	dbo.{objectQualifier}DMX_Entries e
WHERE
	[LastVersionId] = @LastVersionId
	AND e.Version=(SELECT MAX(Version) FROM dbo.{objectQualifier}DMX_Entries WHERE LastVersionId=e.LastVersionId AND IsApproved=1)
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetAttribute
	@AttributeId Int,
    @PortalId int
AS

SELECT
	*
FROM
	dbo.{objectQualifier}DMX_Attributes
WHERE
	[AttributeId] = @AttributeId
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeletePermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeletePermission
	@PermissionId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Permissions
WHERE
	[PermissionId] = @PermissionId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryAttributesByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryAttributesByEntry
	@EntryId Int
AS

SELECT
	ea.[EntryId],
	ea.[Value],
	a.*
FROM
	dbo.{objectQualifier}DMX_EntryAttributes ea
	INNER JOIN dbo.{objectQualifier}DMX_Attributes a ON ea.AttributeId=a.AttributeId
WHERE
	ea.EntryId = @EntryId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_IsEntryPermissionByAdmin]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_IsEntryPermissionByAdmin
 @PortalId Int,
 @EntryId Int
AS
 IF EXISTS(SELECT PermissionsOnlyByAdmin FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PermissionsOnlyByAdmin=1)
  SELECT 1
 ELSE
  DECLARE @Path VARCHAR(2000)
  SELECT @Path = ';'+(SELECT Path FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)+';'
  IF EXISTS(SELECT PermissionsOnlyByAdmin FROM dbo.{objectQualifier}DMX_Entries WHERE PermissionsOnlyByAdmin=1 AND @Path LIKE '%;'+CAST(EntryId AS VARCHAR(10))+';%')
   SELECT 1
  ELSE
   SELECT 0

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteLog]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteLog
	@id Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Log
WHERE
	[id] = @id

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetExtension]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetExtension
	@PortalId Int,
	@ExtensionKey NVarChar (50)
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Extensions
WHERE
	[PortalId] = @PortalId
	AND [ExtensionKey] = @ExtensionKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteEntryAttributesByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteEntryAttributesByEntry
	@EntryId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_EntryAttributes
WHERE EntryID=@EntryID


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddAddon
	@AddonKey NVarChar (50),
	@Availability NVarChar (255),
	@Controller NVarChar (255),
	@Description NVarChar (255),
	@Features Int,
	@Installed DateTime,
	@Name NVarChar (50)
AS

INSERT INTO dbo.{objectQualifier}DMX_Addons (
	[AddonKey],
	[Availability],
	[Controller],
	[Description],
	[Features],
	[Installed],
	[Name]
) VALUES (
	@AddonKey,
	@Availability,
	@Controller,
	@Description,
	@Features,
	@Installed,
	@Name
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLongText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLongText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6)
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_LongTexts
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryAttributesByAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryAttributesByAttribute
	@AttributeId Int
AS

SELECT
	ea.[EntryId],
	ea.[Value],
	a.*
FROM
	dbo.{objectQualifier}DMX_EntryAttributes ea
	INNER JOIN dbo.{objectQualifier}DMX_Attributes a ON ea.AttributeId=a.AttributeId
WHERE
	ea.AttributeId = @AttributeId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddEntryCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddEntryCategory
	@EntryId Int,	
	@CategoryId Int

AS

INSERT INTO dbo.{objectQualifier}DMX_EntryCategories (
	[EntryId],
	[CategoryId]
) VALUES (
	@EntryId,
	@CategoryId
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryCategoriesByCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryCategoriesByCategory
	@CategoryId Int
AS

SELECT
	*
FROM
	dbo.{objectQualifier}DMX_EntryCategories
WHERE
	CategoryId = @CategoryId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteCategory
	@id Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Categories
WHERE
	[id] = @id

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_RenameEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_RenameEntry
 @PortalId Int,
 @EntryId Int,
 @Locale NVarChar(10),
 @NewName nvarchar(255)
AS
 IF EXISTS(SELECT EntryId FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)
  BEGIN
   DELETE FROM dbo.{objectQualifier}DMX_ShortTexts WHERE 
    ObjectId=@EntryId
    AND TypeKey='ENT'
    AND SubTypeKey='TITLE'
    AND Locale=@Locale
   INSERT INTO dbo.{objectQualifier}DMX_ShortTexts 
    (ObjectId, TypeKey, SubTypeKey, Locale, Text)
    VALUES
    (@EntryId, 'ENT', 'TITLE', @Locale, @NewName)
  END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionEntrytypesByPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionEntrytypesByPermission
	@PermissionId Int
AS

SELECT
 * 
FROM
 dbo.{objectQualifier}DMX_PermissionEntrytypes
WHERE
 PermissionId = @PermissionId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogType]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogType
	@PortalId Int,
	@LogTypeKey NVarChar (20)
AS

SELECT
 lt.*
FROM
 dbo.{objectQualifier}DMX_LogTypes lt
WHERE
 [PortalId] = @PortalId
 AND [LogTypeKey] = @LogTypeKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateShortText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateShortText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6),
	@Text NVarChar (255)
AS

UPDATE dbo.{objectQualifier}DMX_ShortTexts SET
	[Text] = @Text
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionsByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionsByPortal
	@PortalId Int
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_Permissions
WHERE
 PortalId = @PortalId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}vw_DMX_ActiveUserRoles]'
GO

CREATE VIEW dbo.{objectQualifier}vw_DMX_ActiveUserRoles
AS
SELECT ur.*
FROM dbo.{objectQualifier}UserRoles ur
WHERE 
 ISNULL(ur.EffectiveDate, DATEADD(day, -1, GETDATE())) < GETDATE()
 AND ISNULL(ur.ExpiryDate, DATEADD(day, 1, GETDATE())) > GETDATE()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_FindEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_FindEntry
 @PortalId Int,
 @SearchText nvarchar(255),
 @Locale nvarchar(10)
AS
SELECT
 e.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale='')) Title,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_LongTexts WHERE TypeKey='ENT' AND SubTypeKey='REMARKS' AND ObjectId=e.EntryId AND Locale='')) Remarks
FROM
 dbo.DMX_Entries e
WHERE
 PortalId=@PortalId AND
 (OriginalFilename LIKE '%'+@SearchText+'%' OR
 Keywords LIKE '%'+@SearchText+'%' OR
 EXISTS (SELECT * FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=e.EntryId AND Locale=@Locale AND Text LIKE '%'+@SearchText+'%'))

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteSubscription]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteSubscription
	@UserId Int,
	@EntryId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Subscriptions
WHERE
	[UserId] = @UserId
	AND [EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteAddon
	@AddonKey NVarChar (50)
AS

DELETE FROM dbo.{objectQualifier}DMX_Addons
WHERE
	[AddonKey] = @AddonKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeletePermissionEntrytype]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeletePermissionEntrytype
	@PermissionId Int,
	@EntryType NVarChar (100)
AS

DELETE FROM dbo.{objectQualifier}DMX_PermissionEntrytypes
WHERE
	[PermissionId] = @PermissionId
	AND [EntryType] = @EntryType

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteLongText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteLongText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6)
AS

DELETE FROM dbo.{objectQualifier}DMX_LongTexts
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetCategory
	@id Int,
	@PortalId Int, 
    @Locale NVarChar(10)
AS

SELECT
	c.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='CAT' AND SubTypeKey='NAME' AND ObjectId=c.id AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='CAT' AND SubTypeKey='NAME' AND ObjectId=c.id AND Locale='')) Name
FROM
	dbo.{objectQualifier}DMX_Categories c
WHERE
	c.PortalId = @PortalId
	AND c.id = @id


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryCategoriesByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryCategoriesByEntry
	@EntryId Int
AS

SELECT
	*
FROM
	dbo.{objectQualifier}DMX_EntryCategories
WHERE
	EntryId = @EntryId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionEntrytype]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionEntrytype
	@PermissionId Int,
	@EntryType NVarChar (100)
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_PermissionEntrytypes
WHERE
	[PermissionId] = @PermissionId
	AND [EntryType] = @EntryType

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateSubscription]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateSubscription
	@UserId Int,
	@EntryId Int,
	@LastAccess DateTime
AS

UPDATE dbo.{objectQualifier}DMX_Subscriptions SET
	[LastAccess] = @LastAccess
WHERE
	[UserId] = @UserId
	AND [EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetExtensionsByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetExtensionsByPortal
	@PortalId Int
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Extensions
WHERE
 PortalId = @PortalId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdatePermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdatePermission
	@PermissionId Int,
	@Addon NVarChar (50),
	@PermissionKey NVarChar (20),
	@PortalId Int,
	@ResourceFile NVarChar (255)
AS

UPDATE dbo.{objectQualifier}DMX_Permissions SET
	[Addon] = @Addon,
	[PermissionKey] = @PermissionKey,
	[PortalId] = @PortalId,
	[ResourceFile] = @ResourceFile
WHERE
	[PermissionId] = @PermissionId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_FindAttributeByName]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_FindAttributeByName
	@Name NVarChar (50),
    @PortalId int
AS

SELECT
	*
FROM
	dbo.{objectQualifier}DMX_Attributes
WHERE
	[Name] = @Name
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteEntryCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteEntryCategory
	@EntryId Int,
	@CategoryId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_EntryCategories
WHERE
	[EntryId] = @EntryId
	AND [CategoryId] = @CategoryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdatePaths]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdatePaths
 @PortalID INT,
 @EntryId INT = NULL
AS

BEGIN

-- NULLIFY what is 'dirty' 
IF @EntryId is NULL 
 BEGIN
  UPDATE dbo.{objectQualifier}DMX_Entries SET [Path]=NULL WHERE PortalID = @PortalID 
 END 
ELSE 
 BEGIN
  DECLARE @OldPath VARCHAR(2000)
  SELECT @OldPath = (SELECT [Path] FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)
  UPDATE dbo.{objectQualifier}DMX_Entries 
   SET [Path]=NULL 
  WHERE [Path] LIKE @OldPath + '%' 
   AND PortalID = @PortalID 
 END

-- If we have any root items with NULL we set the [Path] for these 
UPDATE dbo.{objectQualifier}DMX_Entries 
 SET [Path] = '0;' + Cast(EntryId as NVARCHAR(32)) + ';' 
WHERE
 CollectionId=0
 And PortalId=@PortalId
 AND [Path] IS NULL

-- As long as we find items that are nullified but whose parents are not NULL, we set the path for them
WHILE EXISTS 
 (SELECT 'X' 
  FROM dbo.{objectQualifier}DMX_Entries 
  WHERE 
   PortalId=@PortalId
   AND [Path] IS NULL
   AND EXISTS (SELECT 'X' 
               FROM dbo.{objectQualifier}DMX_Entries e1 
               WHERE e1.[Path] IS NOT NULL 
                AND e1.EntryID=dbo.{objectQualifier}DMX_Entries.CollectionID))
 BEGIN
  UPDATE dbo.{objectQualifier}DMX_Entries
   SET [Path] = (SELECT e2.[Path] + CAST(dbo.{objectQualifier}DMX_Entries.EntryId AS NVARCHAR(32)) + ';' 
                 FROM dbo.{objectQualifier}DMX_Entries e2 
                 WHERE e2.EntryID=dbo.{objectQualifier}DMX_Entries.CollectionID)
  WHERE 
   dbo.{objectQualifier}DMX_Entries.PortalId=@PortalId
   AND [Path] IS NULL
   AND EXISTS (SELECT 'X' 
               FROM dbo.{objectQualifier}DMX_Entries e3 
               WHERE e3.[Path] IS NOT NULL 
                AND e3.EntryID=dbo.{objectQualifier}DMX_Entries.CollectionID)
 END;

END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryCategory
	@EntryId Int,
	@CategoryId Int
AS

SELECT
	*
FROM
	dbo.{objectQualifier}DMX_EntryCategories
WHERE
	[EntryId] = @EntryId
	AND [CategoryId] = @CategoryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetAddon
	@AddonKey NVarChar (50)
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_Addons
WHERE
	[AddonKey] = @AddonKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateExtension]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateExtension
	@PortalId Int,
	@ExtensionKey NVarChar (50),
	@AccessRights NVarChar (255),
	@Addon NVarChar (50),
	@ControlToLoad NVarChar (255),
	@Custom NVarChar (400),
	@DownloadUrl NVarChar (255),
	@EntryTypes NVarChar (255),
	@Icon16 NVarChar (100),
	@Icon32 NVarChar (100),
	@IsPrivate Bit,
	@MimeType NVarChar (50),
	@NodePattern NVarChar (50),
	@ResourceFile NVarChar (255),
	@SettingsControl NVarChar (250),
	@ViewByDefault Bit
AS

UPDATE dbo.{objectQualifier}DMX_Extensions SET
	[AccessRights] = @AccessRights,
	[Addon] = @Addon,
	[ControlToLoad] = @ControlToLoad,
	[Custom] = @Custom,
	[DownloadUrl] = @DownloadUrl,
	[EntryTypes] = @EntryTypes,
	[Icon16] = @Icon16,
	[Icon32] = @Icon32,
	[IsPrivate] = @IsPrivate,
	[MimeType] = @MimeType,
	[NodePattern] = @NodePattern,
	[ResourceFile] = @ResourceFile,
	[SettingsControl] = @SettingsControl,
	[ViewByDefault] = @ViewByDefault
WHERE
	[PortalId] = @PortalId
	AND [ExtensionKey] = @ExtensionKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogTypes]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogTypes
 @PortalId Int
AS

SELECT
 lt.*
FROM
 dbo.{objectQualifier}DMX_LogTypes lt
WHERE
 [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateEntry
	@EntryId Int,
	@Author NVarChar(500),
	@CollectionId Int,
	@Created DateTime,
	@Deleted Bit,
	@Entry NVarChar (255),
	@EntryType NVarChar (150),
	@FileSize BIGINT,
	@IsApproved Bit,
	@Keywords NVarChar (255),
	@LastModified DateTime,
	@LastVersionId Int,
	@LockedBy Int,
	@LockedUntil DateTime,
	@LockKey UNIQUEIDENTIFIER,
	@MD5Hash NVarChar(50),
	@OriginalFileName NVarChar (255),
	@Owner Int,
	@Path NVarChar (255),
	@PermissionsOnlyByAdmin Bit,
	@PortalId Int,
	@State NVarChar (50),
	@Version Int,
	@VersionsToKeep Int,
	@WorkflowId NVarChar (50)
AS

UPDATE dbo.{objectQualifier}DMX_Entries SET
	[Author] = @Author,
	[CollectionId] = @CollectionId,
	[Created] = @Created,
	[Deleted] = @Deleted,
	[Entry] = @Entry,
	[EntryType] = @EntryType,
	[FileSize] = @FileSize,
	[IsApproved] = @IsApproved,
	[Keywords] = @Keywords,
	[LastModified] = @LastModified,
	[LastVersionId] = @LastVersionId,
	[LockedBy] = @LockedBy,
	[LockedUntil] = @LockedUntil,
	[LockKey] = @LockKey,
	[MD5Hash] = @MD5Hash,
	[OriginalFileName] = @OriginalFileName,
	[Owner] = @Owner,
	[Path] = @Path,
	[PermissionsOnlyByAdmin] = @PermissionsOnlyByAdmin,
	[PortalId] = @PortalId,
	[State] = @State,
	[Version] = @Version,
	[VersionsToKeep] = @VersionsToKeep,
	[WorkflowId] = @WorkflowId
WHERE
	[EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddPermissionEntrytype]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddPermissionEntrytype
 @PermissionId Int,	
 @EntryType NVarChar (100)
AS

INSERT INTO dbo.{objectQualifier}DMX_PermissionEntrytypes (
	[PermissionId],
	[EntryType]
) VALUES (
	@PermissionId,
	@EntryType
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetAddonsWithFeature]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetAddonsWithFeature
	@Feature INT
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Addons
WHERE
 ISNULL([Features], 0) & @Feature = @Feature

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateCategory]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateCategory
	@id Int,
	@ParentId Int,
	@PortalId Int,
	@ViewOrder Int
AS

UPDATE dbo.{objectQualifier}DMX_Categories SET
	[ParentId] = @ParentId,
	[PortalId] = @PortalId,
	[ViewOrder] = @ViewOrder
WHERE
	[id] = @id

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddPermissionPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddPermissionPermission
	@PermissionId Int,	
	@RoleId Int
AS

INSERT INTO dbo.{objectQualifier}DMX_PermissionPermissions (
	[PermissionId],
	[RoleId]
) VALUES (
	@PermissionId,
	@RoleId
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetSubscriptionsByUser]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetSubscriptionsByUser
	@UserId Int
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Subscriptions
WHERE
 UserId = @UserId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddExtension]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddExtension
	@PortalId Int,	
	@ExtensionKey NVarChar (50),
	@AccessRights NVarChar (255),
	@Addon NVarChar (50),
	@ControlToLoad NVarChar (255),
	@Custom NVarChar (400),
	@DownloadUrl NVarChar (255),
	@EntryTypes NVarChar (255),
	@Icon16 NVarChar (100),
	@Icon32 NVarChar (100),
	@IsPrivate Bit,
	@MimeType NVarChar (50),
	@NodePattern NVarChar (50),
	@ResourceFile NVarChar (255),
	@SettingsControl NVarChar (250),
	@ViewByDefault Bit
AS

INSERT INTO dbo.{objectQualifier}DMX_Extensions (
	[PortalId],
	[ExtensionKey],
	AccessRights,
	Addon,
	ControlToLoad,
	Custom,
	DownloadUrl,
	EntryTypes,
	Icon16,
	Icon32,
	IsPrivate,
	MimeType,
	NodePattern,
	ResourceFile,
	SettingsControl,
	ViewByDefault
) VALUES (
	@PortalId,
	@ExtensionKey,
	@AccessRights,
	@Addon,
	@ControlToLoad,
	@Custom,
	@DownloadUrl,
	@EntryTypes,
	@Icon16,
	@Icon32,
	@IsPrivate,
	@MimeType,
	@NodePattern,
	@ResourceFile,
	@SettingsControl,
	@ViewByDefault
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetShortTexts]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetShortTexts
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int
AS

SELECT
	[Locale],
	[ObjectId],
	[SubTypeKey],
	[Text],
	[TypeKey]
FROM
	dbo.{objectQualifier}DMX_ShortTexts
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddLongText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddLongText
	@TypeKey NVarChar (3),	
	@SubTypeKey NVarChar (20),	
	@ObjectId Int,	
	@Locale NVarChar (6),
	@Text NVarChar(MAX)
AS

INSERT INTO dbo.{objectQualifier}DMX_LongTexts (
	[TypeKey],
	[SubTypeKey],
	[ObjectId],
	[Locale],
	Text
) VALUES (
	@TypeKey,
	@SubTypeKey,
	@ObjectId,
	@Locale,
	@Text
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteEntryAttribute]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteEntryAttribute
	@EntryId Int,
	@AttributeId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_EntryAttributes
WHERE
	[EntryId] = @EntryId
	AND [AttributeId] = @AttributeId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermission
	@PermissionId Int,
        @PortalId int
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_Permissions
WHERE
	[PermissionId] = @PermissionId
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetNotification]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetNotification
 @UserId Int,
 @LogId Int,
 @PortalId int
AS

SELECT
	[LogId],
	[PortalId],
	[Sent],
	[Template],
	[UserId]
FROM
	dbo.{objectQualifier}DMX_Notifications
WHERE
	[UserId] = @UserId
	AND [LogId] = @LogId
	AND [PortalId] = @PortalId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteEntry
	@EntryId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_Entries
WHERE
	[EntryId] = @EntryId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddShortText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddShortText
	@TypeKey NVarChar (3),	
	@SubTypeKey NVarChar (20),	
	@ObjectId Int,	
	@Locale NVarChar (6),
	@Text NVarChar (255)
AS

INSERT INTO dbo.{objectQualifier}DMX_ShortTexts (
	[TypeKey],
	[SubTypeKey],
	[ObjectId],
	[Locale],
	Text
) VALUES (
	@TypeKey,
	@SubTypeKey,
	@ObjectId,
	@Locale,
	@Text
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetExtensionsByAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetExtensionsByAddon
	@Addon NVarChar (50)
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_Extensions
WHERE
 Addon = @Addon


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMXDAV_Numbers]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMXDAV_Numbers]
(
[Number] [int] NOT NULL
)

GO
PRINT N'Creating primary key [Index_Numbers] on {databaseOwner}[{objectQualifier}DMXDAV_Numbers]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMXDAV_Numbers] ADD CONSTRAINT [Index_Numbers] PRIMARY KEY CLUSTERED  ([Number])
GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMXDAV_URLEncode]'
GO

CREATE FUNCTION dbo.{objectQualifier}DMXDAV_URLEncode(@String VARCHAR(4000))
RETURNS VARCHAR(8000)
AS
BEGIN
 DECLARE @URLEncodedString VARCHAR(8000)
 SELECT @URLEncodedString=''
 SELECT @URLEncodedString=@URLEncodedString + 
  CASE WHEN theChar LIKE '[A-Za-z0-9()''*-._!/]'
   THEN theChar
   ELSE '%' + substring ('0123456789ABCDEF',(ASCII(theChar) / 16)+1,1) +  SUBSTRING('0123456789ABCDEF',(ASCII(theChar) % 16)+1,1)
  END
  FROM (SELECT [theChar]=SUBSTRING(@string,number,1)
   FROM dbo.{objectQualifier}DMXDAV_Numbers WHERE Number <= LEN(@String)) CHARACTERARRAY
 RETURN @URLEncodedString
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetShortText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetShortText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6)
AS

SELECT
 *
FROM
	dbo.{objectQualifier}DMX_ShortTexts
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetUnsentNotifications]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetUnsentNotifications
 @PortalId int,
 @UserId Int
AS

SELECT
 n.*
FROM
 dbo.{objectQualifier}DMX_Notifications n
WHERE
 n.[UserId] = @UserId
 AND n.[PortalId] = @PortalId
 AND n.[Sent] IS NULL


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddLog]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddLog
	@Action NVARCHAR (20),
	@Datime DateTime,
	@EntId Int,
	@UserId Int
AS

INSERT INTO dbo.{objectQualifier}DMX_Log (
	[Action],
	[Datime],
	[EntId],
	[UserId]
) VALUES (
	@Action,
	@Datime,
	@EntId,
	@UserId
)

select SCOPE_IDENTITY()

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteExtension]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteExtension
	@PortalId Int,
	@ExtensionKey NVarChar (50)
AS

DELETE FROM dbo.{objectQualifier}DMX_Extensions
WHERE
	[PortalId] = @PortalId
	AND [ExtensionKey] = @ExtensionKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_AddLogType]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_AddLogType
	@PortalId Int,	
	@LogTypeKey NVarChar (20),
	@AttachFile Bit,
	@CustomSend Bit,
	@NotifyApprovers NVarChar (100),
	@NotifyAudit BIT,
	@NotifyEditors NVarChar (100),
	@NotifyOwner NVarChar (100),
	@NotifySelf NVarChar (100),
	@NotifySubscribers NVarChar (100),
	@ResourceFile NVarChar (150)
AS

INSERT INTO dbo.{objectQualifier}DMX_LogTypes (
	[PortalId],
	[LogTypeKey],
	AttachFile,
	CustomSend,
	NotifyApprovers,
	NotifyAudit,
	NotifyEditors,
	NotifyOwner,
	NotifySelf,
	NotifySubscribers,
	ResourceFile
) VALUES (
	@PortalId,
	@LogTypeKey,
	@AttachFile,
	@CustomSend,
	@NotifyApprovers,
	@NotifyAudit,
	@NotifyEditors,
	@NotifyOwner,
	@NotifySelf,
	@NotifySubscribers,
	@ResourceFile
)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionPermissionsByRole]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionPermissionsByRole
	@RoleId Int
AS

SELECT
 *
FROM
 dbo.{objectQualifier}DMX_PermissionPermissions
WHERE
 RoleId = @RoleId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteShortText]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteShortText
	@TypeKey NVarChar (3),
	@SubTypeKey NVarChar (20),
	@ObjectId Int,
	@Locale NVarChar (6)
AS

DELETE FROM dbo.{objectQualifier}DMX_ShortTexts
WHERE
	[TypeKey] = @TypeKey
	AND [SubTypeKey] = @SubTypeKey
	AND [ObjectId] = @ObjectId
	AND [Locale] = @Locale

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_DeleteEntryPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_DeleteEntryPermission
	@EntryId Int,
	@PermissionId Int,
	@RoleId Int,
	@UserId Int
AS

DELETE FROM dbo.{objectQualifier}DMX_EntryPermissions
WHERE
	[EntryId] = @EntryId
	AND [PermissionId] = @PermissionId
	AND [RoleId] = @RoleId
	AND [UserId] = @UserId

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMXDAV_GetExtension]'
GO

CREATE FUNCTION dbo.{objectQualifier}DMXDAV_GetExtension
 (@Filename NVARCHAR(200))
RETURNS NVARCHAR(10)
AS
BEGIN
 IF @Filename IS NOT NULL
  IF @Filename<>''
   DECLARE @POS INT
   SELECT @POS = LEN(@Filename)-1
   DECLARE @CHR NVARCHAR(1)
   SELECT @CHR = SUBSTRING(@Filename,@POS,1)
   WHILE @CHR<>'.' AND @POS>1
   BEGIN
    SELECT @POS = @POS-1
    SELECT @CHR = SUBSTRING(@Filename,@POS,1)
   END
   IF @POS > 1
    RETURN SUBSTRING(@Filename,@POS,LEN(@Filename)-@POS+1)
 RETURN ''
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UpdateAddon]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UpdateAddon
	@AddonKey NVarChar (50),
	@Availability NVarChar (255),
	@Controller NVarChar (255),
	@Description NVarChar (255),
	@Features Int,
	@Installed DateTime,
	@Name NVarChar (50)
AS

UPDATE dbo.{objectQualifier}DMX_Addons SET
	[Availability] = @Availability,
	[Controller] = @Controller,
	[Description] = @Description,
	[Features] = @Features,
	[Installed] = @Installed,
	[Name] = @Name
WHERE
	[AddonKey] = @AddonKey

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetUserPermissions]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetUserPermissions
	@EntryId Int,
	@PortalId Int,
	@UserId Int
AS

SELECT DISTINCT p.PermissionKey
FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Permissions p ON ep.PermissionId=p.PermissionId
LEFT JOIN dbo.{objectQualifier}vw_DMX_ActiveUserRoles ur ON ep.RoleId=ur.RoleID
LEFT JOIN dbo.{objectQualifier}Users u ON u.UserId=@UserId
WHERE ep.EntryId=@EntryId
AND ep.PortalId=@PortalId
AND p.PortalId=@PortalId
AND (ep.RoleID=-1 OR ur.UserId=@UserId OR ep.UserId=@UserId OR u.IsSuperUser=1)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMXDAV_GetPath]'
GO

CREATE FUNCTION dbo.{objectQualifier}DMXDAV_GetPath
 (@EntryId INT, @PortalId INT)
RETURNS NVARCHAR(4000)
AS
BEGIN
 DECLARE @res NVARCHAR(4000)
 IF EXISTS (SELECT EntryId FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)
 BEGIN
  DECLARE @collection INT
  SELECT @collection = CollectionId FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId
  DECLARE @name NVARCHAR(2000)
  SELECT @name = [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=@EntryId AND Locale=''
  DECLARE @orig NVARCHAR(300)
  SELECT @orig = OriginalFilename FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId AND LEFT(EntryType,4)='File'
  SELECT @res = dbo.{objectQualifier}DMXDAV_GetPath(@collection, @PortalId)+'/'+@name+dbo.{objectQualifier}DMXDAV_GetExtension(@orig)
 END
ELSE
 SELECT @res = ''
RETURN @res
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogsByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogsByPortal
	@PortalId Int
AS

SELECT
 l.*,
 lt.*,
 u.*
FROM
 dbo.{objectQualifier}DMX_Log l
 INNER JOIN dbo.{objectQualifier}DMX_Entries e ON l.EntId=e.EntryId
 INNER JOIN dbo.{objectQualifier}DMX_LogTypes lt ON l.Action=lt.LogTypeKey AND lt.PortalId=e.PortalId
 INNER JOIN dbo.{objectQualifier}Users u ON u.UserId=l.UserId
WHERE
 e.PortalId = @PortalId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetEntryPermissionsByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetEntryPermissionsByEntry
	@EntryId Int
AS

SELECT
	ep.*,
	p.[Addon],
	p.[PermissionKey],
	p.[ResourceFile],
	r.[RoleName],
	u.[UserName],
	u.[DisplayName]
FROM
	dbo.{objectQualifier}DMX_EntryPermissions ep
	INNER JOIN dbo.{objectQualifier}DMX_Permissions p ON ep.PermissionId = p.PermissionId
	LEFT JOIN dbo.{objectQualifier}Roles r ON ep.RoleId = r.RoleId
	LEFT JOIN dbo.{objectQualifier}Users u ON ep.UserId = u.UserId
WHERE
	ep.EntryId = @EntryId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogsByUser]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogsByUser
	@UserId Int
AS

SELECT
 l.*,
 lt.*,
 u.*
FROM
 dbo.{objectQualifier}DMX_Log l
 INNER JOIN dbo.{objectQualifier}DMX_Entries e ON l.EntId=e.EntryId
 INNER JOIN dbo.{objectQualifier}DMX_LogTypes lt ON l.Action=lt.LogTypeKey AND lt.PortalId=e.PortalId
 INNER JOIN dbo.{objectQualifier}Users u ON u.UserId=l.UserId
WHERE
 l.UserId = @UserId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_RemoveDeadPermissions]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_RemoveDeadPermissions
AS
DELETE FROM
 dbo.{objectQualifier}DMX_EntryPermissions
WHERE
 (NOT EXISTS(SELECT * FROM dbo.{objectQualifier}Roles r WHERE r.RoleId=dbo.{objectQualifier}DMX_EntryPermissions.RoleId) AND RoleId>-1 AND UserId=-10)
OR
 (NOT EXISTS(SELECT * FROM dbo.{objectQualifier}Users s WHERE s.UserId=dbo.{objectQualifier}DMX_EntryPermissions.UserId) AND UserId>-1 AND RoleId=-1)

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetCategoriesByPortal]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetCategoriesByPortal
	@PortalId Int, 
    @Locale NVarChar(10)
AS

SELECT
	c.*,
 ISNULL((SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='CAT' AND SubTypeKey='NAME' AND ObjectId=c.id AND Locale=@Locale),
  (SELECT [Text] FROM dbo.{objectQualifier}DMX_ShortTexts WHERE TypeKey='CAT' AND SubTypeKey='NAME' AND ObjectId=c.id AND Locale='')) Name
FROM
	dbo.{objectQualifier}DMX_Categories c
WHERE
	c.PortalId = @PortalId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLog]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLog
 @id Int
AS

SELECT
 l.*,
 lt.*,
 u.*
FROM
 dbo.{objectQualifier}DMX_Log l
 INNER JOIN dbo.{objectQualifier}DMX_Entries e ON l.EntId=e.EntryId
 INNER JOIN dbo.{objectQualifier}DMX_LogTypes lt ON l.Action=lt.LogTypeKey AND lt.PortalId=e.PortalId
 INNER JOIN dbo.{objectQualifier}Users u ON u.UserId=l.UserId
WHERE
 l.[id] = @id

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetPermissionPermissionsByPermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetPermissionPermissionsByPermission
	@PermissionId Int
AS

SELECT
 pp.*,
 r.Description AS RolesDescription
FROM
 dbo.{objectQualifier}Roles r INNER JOIN dbo.{objectQualifier}DMX_PermissionPermissions pp 
  ON r.RoleID = pp.RoleId
WHERE
 pp.PermissionId = @PermissionId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogByTime]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogByTime
 @PortalId Int,
 @StartTime DATETIME,
 @EndTime DATETIME
AS

SELECT
 l.*,
 lt.*,
 u.*
FROM
 dbo.{objectQualifier}DMX_Log l
 INNER JOIN dbo.{objectQualifier}DMX_Entries e ON l.EntId=e.EntryId
 INNER JOIN dbo.{objectQualifier}DMX_LogTypes lt ON l.Action=lt.LogTypeKey AND lt.PortalId=e.PortalId
 INNER JOIN dbo.{objectQualifier}Users u ON u.UserId=l.UserId
WHERE
 e.PortalId = @PortalId
 AND l.Datime >= @StartTime
 AND l.Datime < @EndTime


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UnifyPermissions]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UnifyPermissions
 @PortalId Int,
 @EntryId Int,
 @UserId INT
AS
DECLARE @BasePath VARCHAR(2000)
SELECT @BasePath = (SELECT [Path] FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)
IF EXISTS (select [name] from tempdb..sysobjects where [name] LIKE '#ToChange%')
 DROP TABLE #ToChange;
CREATE TABLE #ToChange 
 (EntryId INT, [Path] VARCHAR(2000), EntryType nvarchar(150));
INSERT INTO #ToChange
 (EntryId, [Path], EntryType)
SELECT e.EntryId, e.[Path], e.EntryType FROM dbo.{objectQualifier}DMX_Entries e INNER JOIN
 (SELECT DISTINCT ep.EntryId FROM dbo.{objectQualifier}DMX_EntryPermissions ep 
   INNER JOIN dbo.{objectQualifier}DMX_Permissions pt ON pt.PermissionId=ep.PermissionId
   LEFT JOIN dbo.{objectQualifier}vw_DMX_ActiveUserRoles r ON r.RoleId=ep.RoleId 
   WHERE pt.PermissionKey='EDIT' AND (ep.UserId=@UserId OR r.UserId=@UserId OR ep.RoleId=-1)) perm ON perm.EntryId=e.EntryId
 WHERE PortalId=@PortalId AND [Path] LIKE @BasePath+'_%';
IF EXISTS (select [name] from tempdb..sysobjects where [name] LIKE '#NewPerms%')
 DROP TABLE #NewPerms;
CREATE TABLE #NewPerms (
	[AllowAccess] [bit] NULL,
	[EntryId] [int] NOT NULL,
	[Expires] [datetime] NULL,
	[PermissionId] [int] NOT NULL,
	[PortalId] [int] NOT NULL,
	[RoleId] [int] NOT NULL,
	[UserId] [int] NOT NULL,
    [EntryType] nvarchar(150))
INSERT INTO #NewPerms
 (AllowAccess, EntryId, Expires, PermissionId, PortalId, RoleId, UserId, EntryType)
SELECT ep.AllowAccess, ep.EntryId, ep.Expires, ep.PermissionId, ep.PortalId, ep.RoleId, ep.UserId, pet.EntryType
 FROM dbo.{objectQualifier}DMX_EntryPermissions ep
 INNER JOIN dbo.{objectQualifier}DMX_PermissionEntryTypes pet ON pet.PermissionId=ep.PermissionId
 WHERE ep.EntryId=@EntryId
DELETE FROM dbo.{objectQualifier}DMX_EntryPermissions
 WHERE EntryId IN (SELECT EntryId FROM #ToChange);
INSERT INTO dbo.{objectQualifier}DMX_EntryPermissions
 (AllowAccess, EntryId, Expires, PermissionId, PortalId, RoleId, UserId)
 SELECT ep.AllowAccess, tc.EntryId, ep.Expires, ep.PermissionId, ep.PortalId, ep.RoleId, ep.UserId
  FROM #NewPerms ep
  INNER JOIN #ToChange tc ON tc.EntryType LIKE ep.EntryType+'%';
DROP TABLE #NewPerms;
DROP TABLE #ToChange;

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetLogsByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetLogsByEntry
	@EntId Int
AS

SELECT
 l.*,
 u.DisplayName
FROM
 dbo.{objectQualifier}Users u INNER JOIN dbo.{objectQualifier}DMX_Log l ON u.UserID = l.UserId
WHERE
 l.EntId = @EntId
ORDER BY l.Datime DESC
 

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_ResetPermissions]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_ResetPermissions
 @PortalId Int,
 @EntryId Int
AS
DECLARE @AdminRoleId INT
SELECT @AdminRoleId = (SELECT AdministratorRoleId FROM dbo.{objectQualifier}Portals WHERE PortalId=@PortalId);
DECLARE @BasePath VARCHAR(2000)
SELECT @BasePath = (SELECT [Path] FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId);
DELETE dbo.{objectQualifier}DMX_EntryPermissions
 FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Entries e ON e.EntryId=ep.EntryId
 WHERE e.PortalId=@PortalId AND e.[Path] LIKE @BasePath+'%';
INSERT INTO dbo.{objectQualifier}DMX_EntryPermissions
 (AllowAccess, EntryId, Expires, PermissionId, PortalId, RoleId, UserId)
 SELECT 1, e.EntryId, NULL, p.PermissionId, 0, @AdminRoleId, -10
 FROM dbo.{objectQualifier}DMX_Entries e, dbo.{objectQualifier}DMX_Permissions p
 WHERE 
  e.PortalId=@PortalId AND e.[Path] LIKE @BasePath+'%' AND (NOT e.EntryType LIKE 'Collection%')
  AND (p.PermissionKey = 'EDIT' OR p.PermissionKey = 'VIEW');
INSERT INTO dbo.{objectQualifier}DMX_EntryPermissions
 (AllowAccess, EntryId, Expires, PermissionId, PortalId, RoleId, UserId)
 SELECT 1, e.EntryId, NULL, p.PermissionId, 0, @AdminRoleId, -10
 FROM dbo.{objectQualifier}DMX_Entries e, dbo.{objectQualifier}DMX_Permissions p
 WHERE 
  e.PortalId=@PortalId AND e.[Path] LIKE @BasePath+'%' AND e.EntryType LIKE 'Collection%'
  AND (p.PermissionKey = 'EDIT' OR p.PermissionKey = 'VIEW'  OR p.PermissionKey = 'ADD');

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_UnifyRemovePermission]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_UnifyRemovePermission
 @PortalId INT,
 @UnifyingUserId INT,
 @CollectionId INT,
 @PermissionKey NVARCHAR(20)
AS

BEGIN
 DECLARE @Id INT
 SELECT @Id = MIN(e.EntryId)
  FROM dbo.{objectQualifier}DMX_Entries e
  INNER JOIN
   (SELECT DISTINCT ep.EntryId FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Permissions pt ON pt.PermissionId=ep.PermissionId
   LEFT JOIN dbo.{objectQualifier}UserRoles r ON r.RoleId=ep.RoleId WHERE pt.PermissionKey='EDIT' AND (ep.UserId=@UnifyingUserId OR r.UserId=@UnifyingUserId OR ep.RoleId=-1)) perm
  ON perm.EntryId=e.EntryId
  WHERE e.CollectionId=@CollectionId
 DECLARE @PermissionId INT
 SELECT @PermissionId = PermissionId FROM dbo.{objectQualifier}DMX_Permissions WHERE PermissionKey=@PermissionKey AND PortalId=@PortalId
 DECLARE @AdminRoleId INT
 SELECT @AdminRoleId=AdministratorRoleId FROM dbo.{objectQualifier}Portals WHERE PortalId=@PortalId
 WHILE @Id IS NOT NULL
 BEGIN
  DELETE FROM dbo.{objectQualifier}DMX_EntryPermissions
  FROM dbo.{objectQualifier}DMX_EntryPermissions ep
   WHERE ep.EntryId=@Id 
    AND ep.PermissionId=@PermissionId
    AND NOT (ep.RoleId=@AdminRoleId AND (@PermissionKey='VIEW' OR @PermissionKey='EDIT' OR @PermissionKey='ADD'))
    AND (@PermissionKey<>'EDIT' OR (SELECT COUNT(*) FROM dbo.{objectQualifier}UserRoles r WHERE r.UserId=@UnifyingUserId AND r.RoleId=ep.RoleId)=0)
    AND (@PermissionKey<>'EDIT' OR ep.UserId <> @UnifyingUserId)
  EXEC dbo.{objectQualifier}DMX_UnifyRemovePermission @PortalId, @UnifyingUserId, @Id, @PermissionKey
  SELECT @Id = MIN(e.EntryId)
   FROM dbo.{objectQualifier}DMX_Entries e
   INNER JOIN
    (SELECT DISTINCT ep.EntryId FROM dbo.{objectQualifier}DMX_EntryPermissions ep INNER JOIN dbo.{objectQualifier}DMX_Permissions pt ON pt.PermissionId=ep.PermissionId
    LEFT JOIN dbo.{objectQualifier}UserRoles r ON r.RoleId=ep.RoleId WHERE pt.PermissionKey='EDIT' AND (ep.UserId=@UnifyingUserId OR r.UserId=@UnifyingUserId OR ep.RoleId=-1)) perm 
   ON perm.EntryId=e.EntryId
   WHERE e.CollectionId=@CollectionId
   AND e.EntryId>@Id
 END
END

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetSubscriptionsByEntry]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetSubscriptionsByEntry
	@EntryId Int
AS

SELECT
 s.*,
 u.DisplayName
FROM
 dbo.{objectQualifier}Users u INNER JOIN dbo.{objectQualifier}DMX_Subscriptions s 
  ON u.UserID = s.UserId
WHERE
 s.EntryId = @EntryId


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_GetUsersToNotify]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_GetUsersToNotify
 @PortalId int
AS

SELECT 
 u.*
FROM
 dbo.{objectQualifier}Users u
WHERE
 EXISTS (SELECT COUNT(*) FROM dbo.{objectQualifier}DMX_Notifications n WHERE n.[UserId] = u.UserId AND n.[PortalId] = @PortalId AND n.[Sent] IS NULL)


GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_SearchResults]'
GO
CREATE TABLE {databaseOwner}[{objectQualifier}DMX_SearchResults]
(
[SearchId] [nvarchar] (50) NOT NULL,
[EntryId] [int] NOT NULL,
[Rank] [real] NULL
)

GO
PRINT N'Creating {databaseOwner}[{objectQualifier}DMX_SetDeleteRecursive]'
GO

CREATE PROCEDURE dbo.{objectQualifier}DMX_SetDeleteRecursive
 @PortalId Int,
 @EntryId Int,
 @Delete BIT
AS
 DECLARE @Path VARCHAR(2000)
 SET @Path = (SELECT [Path] FROM dbo.{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)+'%';
 DISABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;
 UPDATE dbo.{objectQualifier}DMX_Entries
  SET Deleted=@Delete
 WHERE
  PortalId=@PortalId
  AND (LastVersionId=@EntryId
  OR [Path] LIKE @Path);
 ENABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;

GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_EntryPermissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryPermissions] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_EntryPermissions_{objectQualifier}DMX_Entries] FOREIGN KEY ([EntryId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Entries] ([EntryId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Notifications]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Notifications] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Notifications_{objectQualifier}DMX_Log] FOREIGN KEY ([LogId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Log] ([id]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Attributes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Attributes] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Attributes_{objectQualifier}DMX_Addons] FOREIGN KEY ([Addon]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Addons] ([AddonKey]) ON DELETE CASCADE NOT FOR REPLICATION,
CONSTRAINT [FK_{objectQualifier}DMX_Attributes_{objectQualifier}Portals] FOREIGN KEY ([PortalId]) REFERENCES {databaseOwner}[{objectQualifier}Portals] ([PortalID]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Permissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Permissions] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Permissions_{objectQualifier}Portals] FOREIGN KEY ([PortalId]) REFERENCES {databaseOwner}[{objectQualifier}Portals] ([PortalID]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_PermissionPermissions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_PermissionPermissions_{objectQualifier}DMX_Permissions] FOREIGN KEY ([PermissionId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Permissions] ([PermissionId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_EntryAttributes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryAttributes] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_EntryAttributes_{objectQualifier}DMX_Attributes] FOREIGN KEY ([AttributeId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Attributes] ([AttributeId]) NOT FOR REPLICATION,
CONSTRAINT [FK_{objectQualifier}DMX_EntryAttributes_{objectQualifier}DMX_Entries] FOREIGN KEY ([EntryId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Entries] ([EntryId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Extensions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Extensions] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Extensions_{objectQualifier}DMX_Addons] FOREIGN KEY ([Addon]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Addons] ([AddonKey]) ON DELETE CASCADE NOT FOR REPLICATION,
CONSTRAINT [FK_{objectQualifier}DMX_Extensions_{objectQualifier}Portals] FOREIGN KEY ([PortalId]) REFERENCES {databaseOwner}[{objectQualifier}Portals] ([PortalID]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_PermissionEntrytypes_{objectQualifier}DMX_Permissions] FOREIGN KEY ([PermissionId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Permissions] ([PermissionId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Log]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Log] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Log_{objectQualifier}DMX_Entries] FOREIGN KEY ([EntId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Entries] ([EntryId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Entries]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Entries] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Entries_{objectQualifier}Portals] FOREIGN KEY ([PortalId]) REFERENCES {databaseOwner}[{objectQualifier}Portals] ([PortalID]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Categories]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Categories] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Categories_{objectQualifier}Portals] FOREIGN KEY ([PortalId]) REFERENCES {databaseOwner}[{objectQualifier}Portals] ([PortalID]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_Subscriptions]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_Subscriptions] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_Subscriptions_{objectQualifier}DMX_Entries] FOREIGN KEY ([EntryId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Entries] ([EntryId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Adding foreign keys to {databaseOwner}[{objectQualifier}DMX_EntryCategories]'
GO
ALTER TABLE {databaseOwner}[{objectQualifier}DMX_EntryCategories] WITH NOCHECK ADD
CONSTRAINT [FK_{objectQualifier}DMX_EntryCategories_{objectQualifier}DMX_Entries] FOREIGN KEY ([EntryId]) REFERENCES {databaseOwner}[{objectQualifier}DMX_Entries] ([EntryId]) ON DELETE CASCADE NOT FOR REPLICATION
GO
PRINT N'Creating trigger {databaseOwner}[{objectQualifier}DMX_EntryUpdatePath] on {databaseOwner}[{objectQualifier}DMX_Entries]'
GO

CREATE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath
ON dbo.{objectQualifier}DMX_Entries
AFTER INSERT, UPDATE
AS

 DECLARE @EntryId INT
 DECLARE @PortalId INT
 DECLARE @RowCount INT
 SELECT @RowCount=(SELECT COUNT(*) FROM INSERTED)

 IF @RowCount=1
  BEGIN
   SELECT @EntryId = (SELECT EntryId FROM INSERTED);
   SELECT @PortalId = (SELECT PortalId FROM INSERTED);
   DISABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;
   EXEC dbo.{objectQualifier}DMX_UpdatePaths @PortalId, @EntryId;
   EXEC dbo.{objectQualifier}DMX_SetLastModified @PortalId, @EntryId;
   ENABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;
  END
 ELSE
  BEGIN
	  DISABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;
   DECLARE cursorX CURSOR FOR
   SELECT EntryId, PortalID FROM INSERTED
   BEGIN
    OPEN cursorX
    FETCH cursorX
    INTO @EntryId, @PortalId
    WHILE @@fetch_status=0
    BEGIN
     FETCH cursorX
     INTO @EntryId, @PortalId;
  	  EXEC dbo.{objectQualifier}DMX_UpdatePaths @PortalId, @EntryId;
  	  EXEC dbo.{objectQualifier}DMX_SetLastModified @PortalId, @EntryId;
    END;
    CLOSE cursorX;
    DEALLOCATE cursorX;
 	  ENABLE TRIGGER dbo.{objectQualifier}DMX_EntryUpdatePath ON dbo.{objectQualifier}DMX_Entries;
   END;
  END
  

GO

/*
DATA
*/
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Addons] ([AddonKey], [Availability], [Description], [Installed], [Name])
VALUES ('Core', 'All', 'Document Exchange Core', GETDATE(), 'Core')
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Add', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, NULL, NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Approve', p.PortalId, NULL, 0, 0, N'CORE_APPROVER_APPROVE', 1, N'CORE_EDITOR_APPROVE', NULL, NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Delete', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, N'CORE_SUBSCRIBER', NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Download', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, NULL, NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Edit', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, NULL, NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Import', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, NULL, NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Lock', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, NULL, NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'MailMe', p.PortalId, NULL, 1, 1, NULL, 1, NULL, NULL, N'CORE_MAILME', NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Move', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, N'CORE_SUBSCRIBER', NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Rollback', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, NULL, NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Subscribe', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, N'CORE_SUBSCRIBER', NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Undelete', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, NULL, NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Unlock', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, NULL, NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Unsubscribe', p.PortalId, NULL, 0, 0, NULL, 1, NULL, NULL, N'CORE_SUBSCRIBER', NULL FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_LogTypes] ([LogTypeKey], [PortalId], [ResourceFile], [AttachFile], [CustomSend], [NotifyApprovers], [NotifyAudit], [NotifyEditors], [NotifyOwner], [NotifySelf], [NotifySubscribers]) SELECT N'Upload', p.PortalId, NULL, 0, 0, N'CORE_APPROVE', 1, NULL, NULL, NULL, N'CORE_SUBSCRIBER' FROM {databaseOwner}[{objectQualifier}Portals] p
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Collection', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', NULL, NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/Collection.Gif', N'DesktopModules/Bring2mind/DMX/images/Collection.Big.Gif', 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Command.Download', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/Download.aspx', NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Command.Edit', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/EditEntry.ascx', NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Command.New', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/EditEntry.ascx', NULL, NULL, NULL, NULL, NULL, 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Command.ViewCollection', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', NULL, NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/Collection.Gif', N'DesktopModules/Bring2mind/DMX/images/Collection.Big.Gif', 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Contents.Categories', p.PortalId, N'-1;', N'Core', N'Bring2mind.DNN.Modules.DMX.Business.CategoriesController, Bring2mind.DNN.Modules.DMX.Core', N'1', NULL, NULL, NULL, NULL, 0, NULL, N'cat(\d+)', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Contents.Collections', p.PortalId, N'-1;', N'Core', N'Bring2mind.DNN.Modules.DMX.Business.EntriesController, Bring2mind.DNN.Modules.DMX.Core', N'0', NULL, NULL, NULL, NULL, 0, NULL, N'ent(\d+)', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Contents.NonApprovedItems', p.PortalId, N'-1;', N'Core', N'Bring2mind.DNN.Modules.DMX.Framework.ApprovalController, Bring2mind.DNN.Modules.DMX.Core', N'3', NULL, NULL, NULL, NULL, 0, NULL, N'nonapproveditems|nonapprovedowneditems', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Contents.SearchResults', p.PortalId, N'-1;', N'Core', N'Bring2mind.DNN.Modules.DMX.Services.Search.SearchController, Bring2mind.DNN.Modules.DMX.Core', N'2', NULL, NULL, NULL, NULL, 0, NULL, N'searchresults', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Contents.Subscriptions', p.PortalId, N'-1;', N'Core', N'Bring2mind.DNN.Modules.DMX.Business.SubscriptionsController, Bring2mind.DNN.Modules.DMX.Core', N'4', NULL, NULL, NULL, NULL, 0, NULL, N'subscriptions', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'DetailsPage.Details', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/Controls/Details.ascx', N'0', NULL, N'File;Collection;Hyperlink;', NULL, NULL, 0, NULL, N'ent(\d+)', N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, NULL  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Adobe.Acrobat', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'pdf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Adobe.Acrobat.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Adobe.Acrobat.Big.Gif', 0, N'application/pdf', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Adobe.InDesign', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'indd', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Adobe.InDesign.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Adobe.InDesign.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Adobe.Photoshop', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'psd', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Adobe.Photoshop.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Adobe.Photoshop.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Autocad.Dwg', p.PortalId, N'-1;', N'Core', N'bring2mind/dmx/viewcollection.ascx', N'dwg', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Dwg.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Dwg.Big.Gif', 0, N'application/acad', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Avi', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'avi', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Avi.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Avi.Big.Gif', 0, N'video/x-msvideo', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Bmp', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'bmp', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Bmp.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Bmp.Big.Gif', 0, N'image/bmp', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Dxf', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'dxf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Dxf.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Dxf.Big.Gif', 0, N'application/dxf', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Emf', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'emf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Emf.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Emf.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Eps', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'eps', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Eps.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Eps.Big.Gif', 0, N'application/postscript', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Gif', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'gif', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Gif.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Gif.Big.Gif', 0, N'image/gif', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Htm', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'htm;html', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Htm.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Htm.Big.Gif', 0, N'text/html', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Jpg', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'jpg;jpeg', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Jpg.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Jpg.Big.Gif', 0, N'image/jpeg', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.Excel', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'xls', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Excel.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Excel.Big.Gif', 0, N'application/vnd.ms-excel', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.ExcelX', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'xlsx', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.ExcelX.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.ExcelX.Big.Gif', 0, N'application/vnd.ms-excel', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.Infopath', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'xsn', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Infopath.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Infopath.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.Powerpoint', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'ppt', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Powerpoint.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Powerpoint.Big.Gif', 0, N'application/powerpoint', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.PowerpointX', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'pptx', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.PowerpointX.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.PowerpointX.Big.Gif', 0, N'application/powerpoint', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.Word', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'doc', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Word.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.Word.Big.Gif', 0, N'application/msword', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Microsoft.WordX', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'docx', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.WordX.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Microsoft.WordX.Big.Gif', 0, N'application/msword', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Mpeg', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'mpg;mpeg;mpe', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Mpeg.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Mpeg.Big.Gif', 0, N'video/mpeg', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Png', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'png', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Png.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Png.Big.Gif', 0, N'image/png', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Quicktime', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'mov', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Quicktime.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Quicktime.Big.Gif', 0, N'video/quicktime', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Rar', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'rar', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Rar.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Rar.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Rtf', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'rtf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Rtf.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Rtf.Big.Gif', 0, N'text/richtext', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Svg', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'svg', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Svg.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Svg.Big.Gif', 0, N'application/svg', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Targa', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'tga', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Targa.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Targa.Big.Gif', 0, N'image/targa', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Text', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'txt', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Text.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Text.Big.Gif', 0, N'text/plain', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Tiff', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'tif;tiff', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Tiff.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Tiff.Big.Gif', 0, N'image/tiff', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Ttf', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'ttf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Ttf.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Ttf.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Unknown', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', NULL, NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Unknown.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Unknown.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Wav', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'wav', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Wav.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Wav.Big.Gif', 0, N'audio/x-wav', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Wmf', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'wmf', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Wmf.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Wmf.Big.Gif', 0, N'application/octet-stream', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Wma', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'wma', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Wma.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Wma.Big.Gif', 0, N'audio/x-ms-wma', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Wmv', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'wmv', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Wmv.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Wmv.Big.Gif', 0, N'video/x-ms-wmv', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 1  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Xml', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'xml', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Xml.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Xml.Big.Gif', 0, N'text/xml', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'File.Zip', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', N'zip', NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/File.Zip.Gif', N'DesktopModules/Bring2mind/DMX/images/File.Zip.Big.Gif', 0, N'application/zip', NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Extensions] ([ExtensionKey], [PortalId], [AccessRights], [Addon], [ControlToLoad], [Custom], [DownloadUrl], [EntryTypes], [Icon16], [Icon32], [IsPrivate], [MimeType], [NodePattern], [ResourceFile], [SettingsControl], [ViewByDefault]) SELECT N'Hyperlink', p.PortalId, N'-1;', N'Core', N'Bring2mind/DMX/ViewCollection.ascx', NULL, NULL, NULL, N'DesktopModules/Bring2mind/DMX/images/Hyperlink.Gif', N'DesktopModules/Bring2mind/DMX/images/Hyperlink.Big.Gif', 0, NULL, NULL, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources', NULL, 0  FROM {databaseOwner}[{objectQualifier}Portals] p
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_Permissions] ([Addon], [PermissionKey], [PortalId], [ResourceFile]) SELECT N'Core', N'VIEW', p.PortalId, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Permissions] ([Addon], [PermissionKey], [PortalId], [ResourceFile]) SELECT N'Core', N'EDIT', p.PortalId, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Permissions] ([Addon], [PermissionKey], [PortalId], [ResourceFile]) SELECT N'Core', N'ADD', p.PortalId, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources' FROM {databaseOwner}[{objectQualifier}Portals] p
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_Permissions] ([Addon], [PermissionKey], [PortalId], [ResourceFile]) SELECT N'Core', N'APPROVE', p.PortalId, N'DesktopModules/Bring2mind/DMX/App_LocalResources/SharedResources' FROM {databaseOwner}[{objectQualifier}Portals] p
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'File' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='VIEW'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Collection' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='VIEW'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Hyperlink' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='VIEW'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'File' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='EDIT'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Collection' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='EDIT'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Hyperlink' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='EDIT'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Collection' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='ADD'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'File' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='APPROVE'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Collection' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='APPROVE'
GO
INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionEntrytypes] ([PermissionId], [EntryType]) SELECT [PermissionId], N'Hyperlink' FROM {databaseOwner}[{objectQualifier}DMX_Permissions] WHERE [PermissionKey]='APPROVE'
GO


INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] ([PermissionId], [RoleId]) 
SELECT dmxp.PermissionId, p.RegisteredRoleId FROM {databaseOwner}{objectQualifier}Portals p INNER JOIN {databaseOwner}{objectQualifier}DMX_Permissions dmxp ON dmxp.PortalId=p.PortalId
WHERE dmxp.PermissionKey='VIEW'
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] ([PermissionId], [RoleId]) 
SELECT dmxp.PermissionId, p.RegisteredRoleId FROM {databaseOwner}{objectQualifier}Portals p INNER JOIN {databaseOwner}{objectQualifier}DMX_Permissions dmxp ON dmxp.PortalId=p.PortalId
WHERE dmxp.PermissionKey='EDIT'
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] ([PermissionId], [RoleId]) 
SELECT dmxp.PermissionId, p.AdministratorRoleId FROM {databaseOwner}{objectQualifier}Portals p INNER JOIN {databaseOwner}{objectQualifier}DMX_Permissions dmxp ON dmxp.PortalId=p.PortalId
WHERE dmxp.PermissionKey='ADD'
GO

INSERT INTO {databaseOwner}[{objectQualifier}DMX_PermissionPermissions] ([PermissionId], [RoleId]) 
SELECT dmxp.PermissionId, p.AdministratorRoleId FROM {databaseOwner}{objectQualifier}Portals p INNER JOIN {databaseOwner}{objectQualifier}DMX_Permissions dmxp ON dmxp.PortalId=p.PortalId
WHERE dmxp.PermissionKey='APPROVE'
GO

BEGIN
 DECLARE @II INT
 SELECT @II=10000
 SET NOCOUNT ON
 WHILE (@II>0)
  BEGIN
   INSERT INTO {databaseOwner}{objectQualifier}DMXDAV_Numbers (Number) SELECT @II
   SELECT @II=@II-1
  END
END
GO


INSERT INTO {databaseOwner}[{objectQualifier}Schedule] ([TypeFullName], [TimeLapse], [TimeLapseMeasurement], [RetryTimeLapse], [RetryTimeLapseMeasurement], [RetainHistoryNum], [AttachToEvent], [CatchUpEnabled], [Enabled], [ObjectDependencies], [Servers]) VALUES ('Bring2mind.DNN.Modules.DMX.Services.Schedule.Maintenance, BRING2MIND.DNN.MODULES.DMX.CORE', 12, 'h', 2, 'h', 10, '', 0, 1, '', NULL)
GO
INSERT INTO {databaseOwner}[{objectQualifier}Schedule] ([TypeFullName], [TimeLapse], [TimeLapseMeasurement], [RetryTimeLapse], [RetryTimeLapseMeasurement], [RetainHistoryNum], [AttachToEvent], [CatchUpEnabled], [Enabled], [ObjectDependencies], [Servers]) VALUES ('Bring2mind.DNN.Modules.DMX.Services.Schedule.ScheduledTask, BRING2MIND.DNN.MODULES.DMX.CORE', 5, 'm', 2, 'm', 10, '', 0, 1, '', NULL)
GO

