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

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

BEGIN

-- NULLIFY what is 'dirty' 
IF @EntryId is NULL 
 BEGIN
  UPDATE {databaseOwner}{objectQualifier}DMX_Entries SET [Path]=NULL WHERE PortalID = @PortalID 
 END 
ELSE 
 BEGIN
  DECLARE @OldPath VARCHAR(2000)
  SELECT @OldPath = (SELECT [Path] FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId)
  UPDATE {databaseOwner}{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 {databaseOwner}{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 {databaseOwner}{objectQualifier}DMX_Entries 
  WHERE 
   PortalId=@PortalId
   AND [Path] IS NULL
   AND EXISTS (SELECT 'X' 
               FROM {databaseOwner}{objectQualifier}DMX_Entries e1 
               WHERE e1.[Path] IS NOT NULL 
                AND e1.EntryID={databaseOwner}{objectQualifier}DMX_Entries.CollectionID))
 BEGIN
  UPDATE {databaseOwner}{objectQualifier}DMX_Entries
   SET [Path] = (SELECT e2.[Path] + CAST({databaseOwner}{objectQualifier}DMX_Entries.EntryId AS NVARCHAR(32)) + ';' 
                 FROM {databaseOwner}{objectQualifier}DMX_Entries e2 
                 WHERE e2.EntryID={databaseOwner}{objectQualifier}DMX_Entries.CollectionID)
  WHERE 
   {databaseOwner}{objectQualifier}DMX_Entries.PortalId=@PortalId
   AND [Path] IS NULL
   AND EXISTS (SELECT 'X' 
               FROM {databaseOwner}{objectQualifier}DMX_Entries e3 
               WHERE e3.[Path] IS NOT NULL 
                AND e3.EntryID={databaseOwner}{objectQualifier}DMX_Entries.CollectionID)
 END;

END
GO


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

CREATE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath
ON {databaseOwner}{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 {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
   EXEC {databaseOwner}{objectQualifier}DMX_UpdatePaths @PortalId, @EntryId;
   EXEC {databaseOwner}{objectQualifier}DMX_SetLastModified @PortalId, @EntryId;
   ENABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
  END
 ELSE
  BEGIN
	  DISABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{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 {databaseOwner}{objectQualifier}DMX_UpdatePaths @PortalId, @EntryId;
  	  EXEC {databaseOwner}{objectQualifier}DMX_SetLastModified @PortalId, @EntryId;
    END;
    CLOSE cursorX;
    DEALLOCATE cursorX;
 	  ENABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
   END;
  END
  
GO



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

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

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

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

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

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

DROP FUNCTION {databaseOwner}{objectQualifier}DMXDAV_GetPath
GO

CREATE FUNCTION {databaseOwner}{objectQualifier}DMXDAV_GetPath
 (@EntryId INT, @PortalId INT)
RETURNS NVARCHAR(4000)
AS
BEGIN
 DECLARE @res NVARCHAR(4000)
 IF EXISTS (SELECT EntryId FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)
 BEGIN
  DECLARE @collection INT
  SELECT @collection = CollectionId FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId
  DECLARE @name NVARCHAR(2000)
  SELECT @name = [Text] FROM {databaseOwner}{objectQualifier}DMX_ShortTexts WHERE TypeKey='ENT' AND SubTypeKey='TITLE' AND ObjectId=@EntryId AND Locale=''
  DECLARE @orig NVARCHAR(300)
  SELECT @orig = OriginalFilename FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId
  SELECT @res = {databaseOwner}{objectQualifier}DMXDAV_GetPath(@collection, @PortalId)+'/'+@name+{databaseOwner}{objectQualifier}DMXDAV_GetExtension(@orig)
 END
ELSE
 SELECT @res = ''
RETURN @res
END
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

UPDATE {databaseOwner}[{objectQualifier}DMX_Extensions]
SET [ControlToLoad]='Bring2mind/DMX/ViewCollection.ascx'
WHERE [ControlToLoad]='Bring2mind/DMX/ViewDefault.ascx'
GO

UPDATE {databaseOwner}{objectQualifier}DMX_Categories
SET ViewOrder=1
WHERE ViewOrder IS NULL
GO

DISABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
UPDATE {databaseOwner}{objectQualifier}DMX_Entries
SET Path = NULL
WHERE NOT Path LIKE '0;%';
ENABLE TRIGGER {databaseOwner}{objectQualifier}DMX_EntryUpdatePath ON {databaseOwner}{objectQualifier}DMX_Entries;
GO
