SET ANSI_NULLS ON; SET QUOTED_IDENTIFIER ON; GO CREATE OR ALTER PROCEDURE dbo.CharacterWorkspace_ChangeRelationship @CharacterID int,@UserID int,@RelationshipID int,@Version varchar(64),@Action nvarchar(24),@Confirmed bit=0,@EventID int=NULL,@Notes nvarchar(4000)=NULL,@StateID int=NULL,@Intensity int=NULL,@Awareness bit=0,@TypeID int=NULL,@ReverseDirection bit=0,@Reciprocal bit=NULL,@Permanent bit=NULL,@ReaderKnows bit=NULL AS BEGIN SET NOCOUNT ON;SET XACT_ABORT ON; BEGIN TRY BEGIN TRANSACTION; DECLARE @project int,@allowed bit=0; SELECT @project=c.ProjectID FROM dbo.Characters c JOIN dbo.Projects p WITH(HOLDLOCK) ON p.ProjectID=c.ProjectID JOIN dbo.ProjectUserAccess a WITH(HOLDLOCK) ON a.ProjectID=c.ProjectID AND a.UserID=@UserID AND a.IsActive=1 AND a.AccessRole IN(N'Owner',N'Collaborator') WHERE c.CharacterID=@CharacterID AND c.IsArchived=0 AND p.IsArchived=0; IF @project IS NULL THROW 51020,'Editing unavailable',1; DECLARE @current varchar(64); SELECT @current=CONVERT(varchar(64),HASHBYTES('SHA2_256',CONCAT((SELECT r.* FOR JSON PATH,WITHOUT_ARRAY_WRAPPER),(SELECT e.RelationshipEventID,e.UpdatedDate,e.SceneID,e.RelationshipStateID,e.Description FROM dbo.RelationshipEvents e WITH(UPDLOCK,HOLDLOCK) WHERE e.CharacterRelationshipID=r.CharacterRelationshipID ORDER BY e.RelationshipEventID FOR JSON PATH))),2) FROM dbo.CharacterRelationships r WITH(UPDLOCK,HOLDLOCK) WHERE r.CharacterRelationshipID=@RelationshipID AND r.ProjectID=@project AND r.IsArchived=0 AND (r.CharacterAID=@CharacterID OR r.CharacterBID=@CharacterID); IF @current IS NULL THROW 51020,'Relationship unavailable',1; IF @Version IS NULL OR @current<>@Version THROW 51021,'This relationship or its history changed. Cancel and reopen to review current values.',1; IF @Action=N'archive' BEGIN IF @Confirmed<>1 THROW 51022,'Confirm removal of this underlying relationship.',1; EXEC dbo.Relationship_Archive @CharacterRelationshipID=@RelationshipID; END ELSE IF @Action=N'definition' BEGIN IF @Intensity IS NOT NULL AND @Intensity NOT BETWEEN 1 AND 10 THROW 51022,'Intensity must be 1 to 10.',1; IF @StateID IS NOT NULL AND NOT EXISTS(SELECT 1 FROM dbo.RelationshipStates WHERE RelationshipStateID=@StateID) THROW 51022,'Choose a recorded state',1; DECLARE @a int,@b int,@type int,@savedPermanent bit,@start int,@end int,@known bit,@initial bit,@book int,@reader bit,@state int,@strength int,@savedReciprocal bit; SELECT @a=CharacterAID,@b=CharacterBID,@type=RelationshipTypeID,@savedPermanent=IsPermanent,@start=StartSceneID,@end=EndSceneID,@known=IsKnownToReader,@initial=IsInitialRelationship,@book=InitialBookID,@reader=ReaderInitiallyKnows,@state=InitialRelationshipStateID,@strength=InitialIntensity,@savedReciprocal=IsReciprocal FROM dbo.CharacterRelationships WHERE CharacterRelationshipID=@RelationshipID; DECLARE @oldCategory int=(SELECT RelationshipCategoryID FROM dbo.RelationshipTypes WHERE RelationshipTypeID=@type),@newCategory int,@categoryName nvarchar(100); SET @TypeID=COALESCE(@TypeID,@type);SET @Reciprocal=COALESCE(@Reciprocal,@savedReciprocal); SELECT @newCategory=t.RelationshipCategoryID,@categoryName=c.CategoryName FROM dbo.RelationshipTypes t JOIN dbo.RelationshipCategories c ON c.RelationshipCategoryID=t.RelationshipCategoryID WHERE t.RelationshipTypeID=@TypeID AND (t.IsActive=1 OR t.RelationshipTypeID=@type); IF @newCategory IS NULL THROW 51022,'Choose a supported canonical relationship type.',1; IF @newCategory<>@oldCategory THROW 51022,'Changing category requires the full Relationships Editor; no history or family semantics were changed.',1; IF (@TypeID<>@type OR @ReverseDirection=1 OR @Reciprocal<>@savedReciprocal) AND EXISTS(SELECT 1 FROM dbo.RelationshipEvents WITH(UPDLOCK,HOLDLOCK) WHERE CharacterRelationshipID=@RelationshipID) THROW 51022,'This definition has recorded history. Use the full Relationships Editor for structural changes; notes and initial properties remain editable here.',1; IF @categoryName=N'Family' AND @Reciprocal<>@savedReciprocal THROW 51022,'Family reciprocity is structural. Use the full Relationships Editor to review the exact family semantics.',1; SET @type=@TypeID;SET @savedReciprocal=@Reciprocal;SET @savedPermanent=COALESCE(@Permanent,@savedPermanent);SET @known=COALESCE(@ReaderKnows,@known); IF @ReverseDirection=1 BEGIN DECLARE @swap int=@a;SET @a=@b;SET @b=@swap;END; IF @initial=1 BEGIN SET @state=@StateID;SET @strength=@Intensity;SET @reader=@Awareness;END; EXEC dbo.Relationship_Save @CharacterRelationshipID=@RelationshipID,@ProjectID=@project,@CharacterAID=@a,@CharacterBID=@b,@RelationshipTypeID=@type,@IsPermanent=@savedPermanent,@StartSceneID=@start,@EndSceneID=@end,@IsKnownToReader=@known,@Notes=@Notes,@IsInitialRelationship=@initial,@InitialBookID=@book,@ReaderInitiallyKnows=@reader,@InitialRelationshipStateID=@state,@InitialIntensity=@strength,@IsReciprocal=@savedReciprocal; END ELSE IF @Action IN(N'event-edit',N'event-remove') BEGIN IF NOT EXISTS(SELECT 1 FROM dbo.RelationshipEvents WITH(UPDLOCK,HOLDLOCK) WHERE RelationshipEventID=@EventID AND CharacterRelationshipID=@RelationshipID) THROW 51020,'Event unavailable',1; IF EXISTS(SELECT 1 FROM dbo.RelationshipEvents WHERE RelationshipEventID=@EventID AND SourceReviewKey IS NOT NULL) OR EXISTS(SELECT 1 FROM dbo.ManuscriptEvidenceIndex WITH(UPDLOCK,HOLDLOCK) WHERE EventKind=N'Relationship' AND EventID=@EventID) OR EXISTS(SELECT 1 FROM dbo.CharacterKnowledge WITH(UPDLOCK,HOLDLOCK) WHERE SourceEventID=@EventID) THROW 51022,'This event has source/review dependencies. Use its source or review workflow; no history was deleted.',1; IF @Action=N'event-remove' BEGIN IF @Confirmed<>1 THROW 51022,'Confirm removal of this one event.',1; EXEC dbo.RelationshipEvent_Delete @RelationshipEventID=@EventID; END ELSE BEGIN IF @StateID IS NULL OR NOT EXISTS(SELECT 1 FROM dbo.RelationshipStates WHERE RelationshipStateID=@StateID) THROW 51022,'Choose a recorded state.',1; IF @Intensity IS NOT NULL AND @Intensity NOT BETWEEN 1 AND 10 THROW 51022,'Intensity must be 1 to 10.',1; DECLARE @scene int=(SELECT SceneID FROM dbo.RelationshipEvents WHERE RelationshipEventID=@EventID); EXEC dbo.RelationshipEvent_Save @RelationshipEventID=@EventID,@CharacterRelationshipID=@RelationshipID,@SceneID=@scene,@RelationshipStateID=@StateID,@Intensity=@Intensity,@IsKnownToOtherCharacter=@Awareness,@Description=@Notes; END; END ELSE THROW 51022,'Unsupported operation.',1; COMMIT; END TRY BEGIN CATCH IF XACT_STATE()<>0 ROLLBACK;THROW;END CATCH; END; GO