/****** TABLES AND PRIMARY KEYS ******/
if exists (select * from dbo.sysobjects where id = object_id(N'{objectQualifier}DMX_Extensions') and OBJECTPROPERTY(id, N'IsTable') = 1)
ALTER TABLE {databaseOwner}{objectQualifier}DMX_Extensions
ADD [SettingsControl] NVARCHAR (250)

GO


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

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

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


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

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

	@AccessRights NVarChar (255),

	@ControlToLoad NVarChar (255),

	@Custom NVarChar (50),

	@EntryTypes NVarChar (255),

	@Icon16 NVarChar (100),

	@Icon32 NVarChar (100),

	@IsPrivate Bit,

	@MimeType NVarChar (50),

	@Name NVarChar (50),

	@SettingsControl NVarChar (250),

	@ViewByDefault Bit
AS

INSERT INTO {databaseOwner}{objectQualifier}DMX_Extensions (
	[PortalId],
	[Addon],
	[ExtensionKey],

	AccessRights,

	ControlToLoad,

	Custom,

	EntryTypes,

	Icon16,

	Icon32,

	IsPrivate,

	MimeType,

	Name,

	SettingsControl,

	ViewByDefault
) VALUES (
	@PortalId,
	@Addon,
	@ExtensionKey,

	@AccessRights,

	@ControlToLoad,

	@Custom,

	@EntryTypes,

	@Icon16,

	@Icon32,

	@IsPrivate,

	@MimeType,

	@Name,

	@SettingsControl,

	@ViewByDefault
)

GO


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

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

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

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

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

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


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

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

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

GO




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

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

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

GO




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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_GetEntriesByCollectionId
  @CollectionId Int
AS

SELECT {databaseOwner}{objectQualifier}DMX_Entries.*
FROM {databaseOwner}{objectQualifier}DMX_Entries
WHERE ((({objectQualifier}DMX_Entries.CollectionId)=@CollectionId))

GO
