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

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

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

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

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


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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_AddAttribute

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

INSERT INTO {databaseOwner}{objectQualifier}DMX_Attributes (

	[Addon],
	[EntryTypes],
	[IsPrivate],
	[Name],
	[PortalId],
	[Required],
	[ResourceFile],
	[Values],
	[ValueType],
	[ViewOrder]
) VALUES (

	@Addon,
	@EntryTypes,
	@IsPrivate,
	@Name,
	@PortalId,
	@Required,
	@ResourceFile,
	@Values,
	@ValueType,
	@ViewOrder
)

select SCOPE_IDENTITY()
GO


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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_UpdateAttribute
	@id Int,
	@Addon NVarChar (50),
	@EntryTypes NVarChar (255),
	@IsPrivate Bit,
	@Name NVarChar (50),
	@PortalId Int,
	@Required Bit,
	@ResourceFile NVarChar (255),
	@Values NVarChar (255),
	@ValueType NVarChar (30),
	@ViewOrder Int
AS

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

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

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

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

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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Attributes.[Addon],
	{databaseOwner}{objectQualifier}DMX_Attributes.[EntryTypes],
	{databaseOwner}{objectQualifier}DMX_Attributes.[id],
	{databaseOwner}{objectQualifier}DMX_Attributes.[IsPrivate],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Name],
	{databaseOwner}{objectQualifier}DMX_Attributes.[PortalId],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Required],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ResourceFile],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Values],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ValueType],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ViewOrder], 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Attributes.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Attributes.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.Addon = @Addon

GO




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

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

SELECT
	{databaseOwner}{objectQualifier}DMX_Attributes.[Addon],
	{databaseOwner}{objectQualifier}DMX_Attributes.[EntryTypes],
	{databaseOwner}{objectQualifier}DMX_Attributes.[id],
	{databaseOwner}{objectQualifier}DMX_Attributes.[IsPrivate],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Name],
	{databaseOwner}{objectQualifier}DMX_Attributes.[PortalId],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Required],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ResourceFile],
	{databaseOwner}{objectQualifier}DMX_Attributes.[Values],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ValueType],
	{databaseOwner}{objectQualifier}DMX_Attributes.[ViewOrder], 
	{databaseOwner}{objectQualifier}DMX_Addons.Description AS AddonsDescription, 
	{databaseOwner}{objectQualifier}Portals.Description AS PortalsDescription
FROM
	{databaseOwner}{objectQualifier}Portals INNER JOIN {databaseOwner}{objectQualifier}DMX_Addons INNER JOIN {databaseOwner}{objectQualifier}DMX_Attributes ON {databaseOwner}{objectQualifier}DMX_Addons.AddonKey = {databaseOwner}{objectQualifier}DMX_Attributes.Addon ON {databaseOwner}{objectQualifier}Portals.PortalID = {databaseOwner}{objectQualifier}DMX_Attributes.PortalId
WHERE
	{databaseOwner}{objectQualifier}DMX_Attributes.PortalId = @PortalId

GO

