IF EXISTS (select * from dbo.sysobjects where id = object_id(N'{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
   DECLARE cursorX CURSOR FOR
   SELECT INSERTED.EntryId, INSERTED.PortalID FROM INSERTED INNER JOIN DELETED
    ON INSERTED.EntryId= DELETED.EntryId
    WHERE INSERTED.CollectionId <> DELETED.EntryId;
   BEGIN
    OPEN cursorX
    FETCH cursorX
    INTO @EntryId, @PortalId
    WHILE @@fetch_status=0
    BEGIN
     FETCH cursorX
     INTO @EntryId, @PortalId;
      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;
    CLOSE cursorX;
    DEALLOCATE cursorX;
   END;
  END
  
GO

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

CREATE PROCEDURE {databaseOwner}{objectQualifier}DMX_SoftDeleteEntry
 @PortalId Int,
 @EntryId Int,
 @Delete Bit
AS

BEGIN
 DECLARE @LastVersionId INT
 SELECT @LastVersionId=(SELECT LastVersionId FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)
 DECLARE @Path VARCHAR(2000)
 SELECT @Path = (SELECT [Path] FROM {databaseOwner}{objectQualifier}DMX_Entries WHERE EntryId=@EntryId AND PortalId=@PortalId)
 IF @LastVersionId=@EntryId
  IF @Delete=1
   UPDATE {databaseOwner}{objectQualifier}DMX_Entries
    SET Deleted=@Delete
    WHERE Path LIKE @Path + '%' AND PortalId=@PortalId;
  ELSE
   UPDATE {databaseOwner}{objectQualifier}DMX_Entries
    SET Deleted=@Delete
    WHERE LastVersionId=@LastVersionId AND PortalId=@PortalId;
 ELSE
  UPDATE {databaseOwner}{objectQualifier}DMX_Entries
   SET Deleted=@Delete
   WHERE EntryId=@EntryId AND PortalId=@PortalId;
END
GO

UPDATE {databaseOwner}{objectQualifier}DMX_Attributes
SET IsPrivate=1
WHERE Addon='urn:schemas-microsoft-com:' AND
 (Name='Win32CreationTime'
  OR Name='Win32LastAccessTime'
  OR Name='Win32LastModifiedTime'
  OR Name='Win32FileAttributes')
GO

