PlotDirector/PlotLine/Sql/282_WriterEditorialAnnotations.sql

48 lines
5.2 KiB
Transact-SQL

-- Extend existing editorial observations without rewriting existing records or anchors.
SET ANSI_NULLS ON;
SET QUOTED_IDENTIFIER ON;
GO
IF COL_LENGTH('dbo.WriterReviewObservations','Scope') IS NULL ALTER TABLE dbo.WriterReviewObservations ADD Scope varchar(10) NULL,Classification varchar(30) NULL;
GO
IF EXISTS(SELECT 1 FROM sys.check_constraints WHERE name='CK_WriterReview_Status') ALTER TABLE dbo.WriterReviewObservations DROP CONSTRAINT CK_WriterReview_Status;
ALTER TABLE dbo.WriterReviewObservations ADD CONSTRAINT CK_WriterReview_Status CHECK(Status IN('Open','Working','Resolved','Dismissed'));
GO
CREATE OR ALTER PROCEDURE dbo.WriterReview_Observations @Action varchar(12),@UserID int,@BookID int,@ChapterID int,@ID uniqueidentifier=NULL,@Version int=0,
@ManuscriptID uniqueidentifier=NULL,@SnapshotRevision bigint=NULL,@ContentVersion char(64)=NULL,
@StartParagraph uniqueidentifier=NULL,@EndParagraph uniqueidentifier=NULL,@StartOffset int=NULL,@EndOffset int=NULL,@AnchorHash char(64)=NULL,@SceneID int=NULL,
@Observation nvarchar(max)=NULL,@Decision nvarchar(max)=NULL,@Status varchar(16)='Open',@Page int=1,@Scope varchar(10)=NULL,@Classification varchar(30)='Observation'
AS
BEGIN
SET NOCOUNT ON;SET XACT_ABORT ON;
IF NOT EXISTS(SELECT 1 FROM dbo.Books b JOIN dbo.Chapters c ON c.BookID=b.BookID AND c.ChapterID=@ChapterID AND c.IsArchived=0 JOIN dbo.ProjectUserAccess a ON a.ProjectID=b.ProjectID AND a.UserID=@UserID AND a.IsActive=1 WHERE b.BookID=@BookID AND b.IsArchived=0) THROW 51000,'Review scope unavailable',1;
IF @Action='list' BEGIN
IF @Page<1 OR @Page>100000 THROW 51000,'Invalid review page',1;
SELECT * FROM dbo.WriterReviewObservations WHERE UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID ORDER BY CreatedUtc DESC,ID OFFSET ((@Page-1)*50) ROWS FETCH NEXT 51 ROWS ONLY;RETURN;
END;
IF @Action NOT IN('create','update') OR @ID IS NULL OR @Status NOT IN('Open','Working','Resolved','Dismissed') OR LEN(TRIM(COALESCE(@Observation,N'')))=0 OR LEN(@Observation)>10000 OR LEN(COALESCE(@Decision,N''))>10000 THROW 51000,'Invalid observation',1;
IF @Classification NOT IN('Observation','Revision needed','Continuity','Research','Character','Plot','Pacing','Reminder','General note') THROW 51000,'Invalid classification',1;
SET @Scope=COALESCE(NULLIF(@Scope,''),CASE WHEN @StartParagraph IS NULL THEN 'Chapter' ELSE 'Passage' END);
IF @Action='create' AND (@Scope NOT IN('Chapter','Scene','Passage') OR (@Scope='Scene' AND @SceneID IS NULL) OR (@Scope='Passage' AND @StartParagraph IS NULL) OR (@Scope<>'Passage' AND @StartParagraph IS NOT NULL)) THROW 51000,'Invalid annotation scope',1;
IF @Action='create' AND @Scope='Scene' AND NOT EXISTS(SELECT 1 FROM dbo.Scenes WHERE SceneID=@SceneID AND ChapterID=@ChapterID AND IsArchived=0) THROW 51000,'Scene unavailable',1;
BEGIN TRANSACTION;
IF @Action='create'
BEGIN
IF EXISTS(SELECT 1 FROM dbo.WriterReviewObservations WITH(UPDLOCK,HOLDLOCK) WHERE ID=@ID)
BEGIN
IF NOT EXISTS(SELECT 1 FROM dbo.WriterReviewObservations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND Observation=@Observation AND ISNULL(Decision,N'')=ISNULL(@Decision,N'') AND ContentVersion=@ContentVersion AND Status=@Status AND ISNULL(Classification,'Observation')=@Classification AND ISNULL(Scope,CASE WHEN StartParagraph IS NULL THEN 'Chapter' ELSE 'Passage' END)=@Scope AND ISNULL(SceneID,0)=ISNULL(@SceneID,0) AND ISNULL(CONVERT(varchar(36),StartParagraph),'')=ISNULL(CONVERT(varchar(36),@StartParagraph),'') AND ISNULL(CONVERT(varchar(36),EndParagraph),'')=ISNULL(CONVERT(varchar(36),@EndParagraph),'') AND ISNULL(StartOffset,-1)=ISNULL(@StartOffset,-1) AND ISNULL(EndOffset,-1)=ISNULL(@EndOffset,-1)) THROW 51002,'This observation changed; reload before retrying',1;
END ELSE BEGIN
DECLARE @CurrentRevision bigint,@CurrentManuscript uniqueidentifier;
SELECT @CurrentRevision=Revision,@CurrentManuscript=ManuscriptID FROM dbo.BookParagraphManuscripts WITH(UPDLOCK,HOLDLOCK) WHERE BookID=@BookID;
IF ISNULL(@CurrentRevision,-1)<>@SnapshotRevision OR ISNULL(@CurrentManuscript,'00000000-0000-0000-0000-000000000000')<>ISNULL(@ManuscriptID,'00000000-0000-0000-0000-000000000000') THROW 51002,'Accepted manuscript changed; your text has been retained',1;
INSERT dbo.WriterReviewObservations(ID,UserID,BookID,ChapterID,ManuscriptID,SnapshotRevision,ContentVersion,StartParagraph,EndParagraph,StartOffset,EndOffset,AnchorHash,SceneID,Observation,Decision,Status,Scope,Classification)
VALUES(@ID,@UserID,@BookID,@ChapterID,@ManuscriptID,@SnapshotRevision,@ContentVersion,@StartParagraph,@EndParagraph,@StartOffset,@EndOffset,@AnchorHash,@SceneID,@Observation,@Decision,@Status,@Scope,@Classification);
END;
END ELSE BEGIN
UPDATE dbo.WriterReviewObservations SET Observation=@Observation,Decision=@Decision,Status=@Status,Classification=@Classification,Version=Version+1,UpdatedUtc=SYSUTCDATETIME() WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID AND Version=@Version;
IF @@ROWCOUNT<>1 THROW 51002,'Another window changed this observation; your text has been retained',1;
END;
SELECT * FROM dbo.WriterReviewObservations WHERE ID=@ID AND UserID=@UserID AND BookID=@BookID AND ChapterID=@ChapterID;
COMMIT;
END;
GO