SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; GO CREATE OR ALTER PROCEDURE dbo.NarrativeFocus_Read @UserID int,@ProjectID int,@BookID int AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Projects p ON p.ProjectID=b.ProjectID JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.ProjectID=@ProjectID AND b.IsArchived=0 AND p.IsArchived=0) THROW 51000,'Narrative focus is unavailable in this book.',1; BEGIN TRANSACTION; IF NOT EXISTS(SELECT 1 FROM dbo.NarrativeFocus WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND BookID=@BookID) BEGIN INSERT dbo.NarrativeFocus(UserID,BookID,Version) VALUES(@UserID,@BookID,0); END; -- Reads retain stale references. Coordinate reports invalidity without silently redirecting. DELETE h FROM dbo.NarrativeFocusHistory h WHERE h.UserID=@UserID AND h.BookID=@BookID AND (NOT EXISTS(SELECT 1 FROM dbo.Chapters c WHERE c.ChapterID=h.ChapterID AND c.BookID=@BookID AND c.IsArchived=0) OR(h.SceneID IS NOT NULL AND NOT EXISTS(SELECT 1 FROM dbo.Scenes s WHERE s.SceneID=h.SceneID AND s.ChapterID=h.ChapterID AND s.IsArchived=0))); COMMIT; SELECT @ProjectID ProjectID,BookID,ChapterID,SceneID,ParagraphID,ManuscriptID,OriginWindowID,Version FROM dbo.NarrativeFocus WHERE UserID=@UserID AND BookID=@BookID; SELECT c.ChapterID,c.ChapterTitle,c.ChapterNumber,n.SceneID,n.SceneTitle,n.SceneNumber,n.BookSceneIndex FROM dbo.Chapters c LEFT JOIN dbo.NarrativeSceneOrder n ON n.ChapterID=c.ChapterID WHERE c.BookID=@BookID AND c.IsArchived=0 ORDER BY c.SortOrder,c.ChapterNumber,CASE WHEN n.SceneID IS NULL THEN 1 ELSE 0 END,n.BookSceneIndex,c.ChapterID; SELECT TOP(20) h.ChapterID,h.SceneID,h.Sequence FROM dbo.NarrativeFocusHistory h WHERE h.UserID=@UserID AND h.BookID=@BookID ORDER BY h.Sequence DESC; END; GO CREATE OR ALTER PROCEDURE dbo.NarrativeFocus_Set @UserID int,@ProjectID int,@BookID int,@ChapterID int=NULL,@SceneID int=NULL,@ExpectedVersion bigint,@Direction int=0 AS BEGIN SET NOCOUNT ON; SET XACT_ABORT ON; IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Projects p ON p.ProjectID=b.ProjectID JOIN dbo.ProjectUserAccess a ON a.ProjectID=p.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.ProjectID=@ProjectID AND b.IsArchived=0 AND p.IsArchived=0) THROW 51000,'Narrative focus is unavailable in this book.',1; IF @Direction NOT IN(-1,0,1) THROW 51000,'Invalid narrative direction.',1; BEGIN TRANSACTION; DECLARE @version bigint,@oldChapter int,@oldScene int; SELECT @version=Version,@oldChapter=ChapterID,@oldScene=SceneID FROM dbo.NarrativeFocus WITH(UPDLOCK,HOLDLOCK) WHERE UserID=@UserID AND BookID=@BookID; IF @version IS NULL OR @version<>@ExpectedVersion THROW 51001,'Current focus changed in another window. Review it and try again.',1; IF @Direction<>0 BEGIN DECLARE @index bigint=(SELECT BookSceneIndex FROM dbo.NarrativeSceneOrder WHERE BookID=@BookID AND SceneID=@oldScene AND ChapterID=@oldChapter); SET @ChapterID=NULL;SET @SceneID=NULL; IF @index IS NOT NULL SELECT @ChapterID=ChapterID,@SceneID=SceneID FROM dbo.NarrativeSceneOrder WHERE BookID=@BookID AND BookSceneIndex=@index+@Direction; ELSE IF @Direction=1 SELECT TOP(1) @ChapterID=n.ChapterID,@SceneID=n.SceneID FROM dbo.NarrativeSceneOrder n LEFT JOIN dbo.Chapters currentChapter ON currentChapter.ChapterID=@oldChapter AND currentChapter.BookID=@BookID AND currentChapter.IsArchived=0 WHERE n.BookID=@BookID AND(currentChapter.ChapterID IS NULL OR n.ChapterID=currentChapter.ChapterID OR (NOT EXISTS(SELECT 1 FROM dbo.NarrativeSceneOrder present WHERE present.BookID=@BookID AND present.ChapterID=currentChapter.ChapterID) AND(n.ChapterSortOrder>currentChapter.SortOrder OR(n.ChapterSortOrder=currentChapter.SortOrder AND n.ChapterNumber>currentChapter.ChapterNumber)))) ORDER BY n.BookSceneIndex; ELSE SELECT TOP(1) @ChapterID=n.ChapterID,@SceneID=n.SceneID FROM dbo.NarrativeSceneOrder n LEFT JOIN dbo.Chapters currentChapter ON currentChapter.ChapterID=@oldChapter AND currentChapter.BookID=@BookID AND currentChapter.IsArchived=0 WHERE n.BookID=@BookID AND(currentChapter.ChapterID IS NULL OR n.ChapterID=currentChapter.ChapterID OR (NOT EXISTS(SELECT 1 FROM dbo.NarrativeSceneOrder present WHERE present.BookID=@BookID AND present.ChapterID=currentChapter.ChapterID) AND(n.ChapterSortOrder@ChapterID) THROW 51000,'Scene is unavailable in this book and chapter.',1; SET @ChapterID=@actualChapter; END; IF @ChapterID IS NOT NULL AND NOT EXISTS(SELECT 1 FROM dbo.Chapters WHERE ChapterID=@ChapterID AND BookID=@BookID AND IsArchived=0) THROW 51000,'Chapter is unavailable in this book.',1; IF ISNULL(@ChapterID,0)<>ISNULL(@oldChapter,0) OR ISNULL(@SceneID,0)<>ISNULL(@oldScene,0) BEGIN UPDATE dbo.NarrativeFocus SET ChapterID=@ChapterID,SceneID=@SceneID,ParagraphID=NULL,OriginWindowID=NULL,Version=Version+1 WHERE UserID=@UserID AND BookID=@BookID; DELETE dbo.NarrativeFocusHistory WHERE UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND ISNULL(SceneID,0)=ISNULL(@SceneID,0); IF @ChapterID IS NOT NULL INSERT dbo.NarrativeFocusHistory VALUES(@UserID,@BookID,@version+1,@ChapterID,@SceneID); DELETE h FROM dbo.NarrativeFocusHistory h WHERE UserID=@UserID AND BookID=@BookID AND Sequence NOT IN (SELECT TOP(20) Sequence FROM dbo.NarrativeFocusHistory WHERE UserID=@UserID AND BookID=@BookID ORDER BY Sequence DESC); END; COMMIT; END; GO