PlotDirector/PlotLine/Services/ProjectBackupGraphValidation.cs

125 lines
18 KiB
C#

using System.Collections;
using PlotLine.Models;
namespace PlotLine.Services;
// Explicit persisted relationships for the current serialized entity sets, not inferred author names.
internal static class ProjectBackupGraphValidation
{
private sealed record Link(string Column, string TargetCollection, string TargetColumn);
private sealed record Entity(string Collection, string? Identity, params Link[] Links);
private static readonly Entity[] Entities = [
new("StoryIntelligenceRuns","StoryIntelligenceRunID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("StoryIntelligenceChapterResults","ChapterResultID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("StoryIntelligenceSceneResults","SceneResultID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ChapterResultID","StoryIntelligenceChapterResults","ChapterResultID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID"),new Link("SceneID","Scenes","SceneID")),
new("StoryIntelligenceImportCommits","StoryIntelligenceImportCommitID",new Link("StoryIntelligenceRunID","StoryIntelligenceRuns","StoryIntelligenceRunID"),new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ChapterID","Chapters","ChapterID")),
new("CoreImportBasicLocationReviews","CoreImportBasicLocationReviewID",new Link("BookID","Books","BookID"),new Link("ProjectID","Project","ProjectID"),new Link("BuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID")),
new("CoreImportBasicLocationCandidates","CoreImportBasicLocationCandidateID",new Link("ExistingLocationID","Locations","LocationID"),new Link("ParentLocationID","Locations","LocationID"),new Link("ResultLocationID","Locations","LocationID"),new Link("CoreImportBasicLocationReviewID","CoreImportBasicLocationReviews","CoreImportBasicLocationReviewID"),new Link("SameAsLocationID","Locations","LocationID")),
new("CoreImportBasicLocationEvidence","CoreImportBasicLocationEvidenceID",new Link("CoreImportBasicLocationCandidateID","CoreImportBasicLocationCandidates","CoreImportBasicLocationCandidateID"),new Link("SceneID","Scenes","SceneID"),new Link("ChapterID","Chapters","ChapterID")),
new("SceneAssetSuggestions","SceneAssetSuggestionID",new Link("SceneID","Scenes","SceneID"),new Link("AssetID","StoryAssets","StoryAssetID")),
new("SceneCharacterSuggestions","SceneCharacterSuggestionID",new Link("CharacterID","Characters","CharacterID"),new Link("SceneID","Scenes","SceneID")),
new("SceneLocationSuggestions","SceneLocationSuggestionID",new Link("LocationID","Locations","LocationID"),new Link("SceneID","Scenes","SceneID")),
new("SceneTextStatistics",null,new Link("SceneID","Scenes","SceneID")),
new("OnboardingManuscriptBuilds","OnboardingManuscriptBuildID",new Link("ProjectID","Project","ProjectID"),new Link("BookID","Books","BookID"),new Link("ManuscriptDocumentID","ManuscriptDocuments","ManuscriptDocumentID")),
new("OnboardingManuscriptBuildChapterMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("ChapterID","Chapters","ChapterID")),
new("OnboardingManuscriptBuildSceneMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("SceneID","Scenes","SceneID")),
new("OnboardingManuscriptBuildCharacterMap",null,new Link("OnboardingManuscriptBuildID","OnboardingManuscriptBuilds","OnboardingManuscriptBuildID"),new Link("CharacterID","Characters","CharacterID")),
new("CharacterImages","CharacterImageID",new Link("CharacterID","Characters","CharacterID"),new Link("ProjectID","Project","ProjectID")),
new("WordCompanionSceneAssets","WordCompanionSceneAssetID",new Link("SceneID","Scenes","SceneID"),new Link("StoryAssetID","StoryAssets","StoryAssetID")),
new("WordCompanionSceneLocations","WordCompanionSceneLocationID",new Link("SceneID","Scenes","SceneID"),new Link("LocationID","Locations","LocationID")),
new("AssetAliases", "AssetAliasID", new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("AssetCustodyCharacters", "AssetCustodyEventCharacterID", new Link("AssetCustodyEventID", "AssetCustodyEvents", "AssetCustodyEventID"), new Link("CharacterID", "Characters", "CharacterID")),
new("AssetCustodyEvents", "AssetCustodyEventID", new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("AssetDependencies", "AssetDependencyID", new Link("SourceAssetID", "StoryAssets", "StoryAssetID"), new Link("SourceRequiredStateID", "AssetStates", "AssetStateID"), new Link("TargetAssetID", "StoryAssets", "StoryAssetID"), new Link("TargetBlockedStateID", "AssetStates", "AssetStateID")),
new("AssetEvents", "AssetEventID", new Link("FromStateID", "AssetStates", "AssetStateID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID"), new Link("ToStateID", "AssetStates", "AssetStateID")),
new("AssetStates", "AssetStateID", new Link("ProjectID", "Project", "ProjectID")),
new("AuthorPlotSeeds", "AuthorPlotSeedID", new Link("BookID", "Books", "BookID"), new Link("MatchedPlotLineID", "PlotLines", "PlotLineID"), new Link("ProjectID", "Project", "ProjectID")),
new("Books", "BookID", new Link("ProjectID", "Project", "ProjectID")),
new("ChapterGoals", "ChapterGoalID", new Link("ChapterID", "Chapters", "ChapterID")),
new("Chapters", "ChapterID", new Link("BookID", "Books", "BookID"), new Link("ManuscriptDocumentID", "ManuscriptDocuments", "ManuscriptDocumentID"), new Link("POVCharacterID", "Characters", "CharacterID")),
new("CharacterAliases", "CharacterAliasID", new Link("CharacterID", "Characters", "CharacterID")),
new("CharacterAttributeEvents", "CharacterAttributeEventID", new Link("CharacterAttributeTypeID", "CharacterAttributeTypes", "CharacterAttributeTypeID"), new Link("CharacterID", "Characters", "CharacterID"), new Link("SceneID", "Scenes", "SceneID")),
new("CharacterAttributeTypes", "CharacterAttributeTypeID", new Link("ProjectID", "Project", "ProjectID")),
new("CharacterKnowledge", "CharacterKnowledgeID", new Link("CharacterID", "Characters", "CharacterID"), new Link("PlotThreadID", "PlotThreads", "PlotThreadID"), new Link("SceneID", "Scenes", "SceneID"), new Link("SourceEventID", "ThreadEvents", "ThreadEventID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("CharacterRelationships", "CharacterRelationshipID", new Link("CharacterAID", "Characters", "CharacterID"), new Link("CharacterBID", "Characters", "CharacterID"), new Link("EndSceneID", "Scenes", "SceneID"), new Link("InitialBookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID"), new Link("StartSceneID", "Scenes", "SceneID")),
new("Characters", "CharacterID", new Link("AvatarSourceCharacterImageID","CharacterImages","CharacterImageID"), new Link("AgeReferenceSceneID", "Scenes", "SceneID"), new Link("ProjectID", "Project", "ProjectID")),
new("ContinuityWarningAcknowledgements", "ContinuityWarningAcknowledgementID", new Link("AssetID", "StoryAssets", "StoryAssetID"), new Link("CharacterID", "Characters", "CharacterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID")),
new("ContinuityWarnings", "ContinuityWarningID", new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID"), new Link("ValidationRunID", "ValidationRuns", "ValidationRunID")),
new("FloorPlanBlocks", "FloorPlanBlockID", new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("LocationID", "Locations", "LocationID")),
new("FloorPlanFloors", "FloorPlanFloorID", new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID")),
new("FloorPlanTransitions", "FloorPlanTransitionID", new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("FromLocationID", "Locations", "LocationID"), new Link("ToLocationID", "Locations", "LocationID")),
new("FloorPlans", "FloorPlanID", new Link("LocationID", "Locations", "LocationID"), new Link("ProjectID", "Project", "ProjectID")),
new("GenreMetricPresetMetrics", null, new Link("GenreMetricPresetID", "GenreMetricPresets", "GenreMetricPresetID"), new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID")),
new("GenreMetricPresets", "GenreMetricPresetID"),
new("LocationAliases", "LocationAliasID", new Link("LocationID", "Locations", "LocationID")),
new("LocationRelationships", "LocationRelationshipID", new Link("FromLocationID", "Locations", "LocationID"), new Link("ToLocationID", "Locations", "LocationID")),
new("Locations", "LocationID", new Link("ParentLocationID", "Locations", "LocationID"), new Link("ProjectID", "Project", "ProjectID")),
new("ManuscriptDocuments", "ManuscriptDocumentID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("ManuscriptStructuralChapterSources", null, new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID")),
new("MapFeatures", null, new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID"), new Link("MapID", "StoryMaps", "MapID")),
new("MapJourneyNotes", null, new Link("CharacterID", "Characters", "CharacterID"), new Link("FromSceneID", "Scenes", "SceneID"), new Link("MapID", "StoryMaps", "MapID"), new Link("ToSceneID", "Scenes", "SceneID")),
new("MapReferenceImages", null, new Link("ProjectID", "Project", "ProjectID")),
new("MapRelationships", null, new Link("MapID", "MapFeatures", "MapID"), new Link("MapID", "StoryMaps", "MapID"), new Link("SourceID", "MapFeatures", "FeatureID"), new Link("TargetID", "MapFeatures", "FeatureID")),
new("PlotEventTargetPlotLines", null, new Link("PlotEventID", "ThreadEvents", "ThreadEventID"), new Link("PlotLineID", "PlotLines", "PlotLineID")),
new("PlotLines", "PlotLineID", new Link("BookID", "Books", "BookID"), new Link("EmergesFromPlotLineID", "PlotLines", "PlotLineID"), new Link("ParentPlotLineID", "PlotLines", "PlotLineID"), new Link("ProjectID", "Project", "ProjectID")),
new("PlotThreads", "PlotThreadID", new Link("ActualResolutionSceneID", "Scenes", "SceneID"), new Link("IntroducedSceneID", "Scenes", "SceneID"), new Link("PlannedResolutionSceneID", "Scenes", "SceneID"), new Link("PlotLineID", "PlotLines", "PlotLineID")),
new("TimelineMetricSettings", null, new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID"), new Link("ProjectID", "Project", "ProjectID")),
new("TimelineSettings", "ProjectTimelineSettingsID", new Link("ProjectID", "Project", "ProjectID")),
new("ProjectAccess", "ProjectUserAccessID", new Link("ProjectID", "Project", "ProjectID")),
new("RelationshipEvents", "RelationshipEventID", new Link("CharacterAID", "Characters", "CharacterID"), new Link("CharacterBID", "Characters", "CharacterID"), new Link("CharacterRelationshipID", "CharacterRelationships", "CharacterRelationshipID"), new Link("SceneID", "Scenes", "SceneID")),
new("ScenarioSceneOrder", "ScenarioSceneOrderID", new Link("ProposedChapterID", "Chapters", "ChapterID"), new Link("ScenarioID", "Scenarios", "ScenarioID"), new Link("SceneID", "Scenes", "SceneID")),
new("ScenarioValidationRuns", "ScenarioValidationRunID", new Link("ScenarioID", "Scenarios", "ScenarioID")),
new("ScenarioWarnings", "ScenarioWarningID", new Link("ScenarioID", "Scenarios", "ScenarioID"), new Link("ScenarioValidationRunID", "ScenarioValidationRuns", "ScenarioValidationRunID"), new Link("SceneID", "Scenes", "SceneID")),
new("Scenarios", "ScenarioID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("SceneAssetLocations", "SceneAssetLocationID", new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("SceneAttachments", "SceneAttachmentID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneCharacters", "SceneCharacterID", new Link("CharacterID", "Characters", "CharacterID"), new Link("EntryLocationID", "Locations", "LocationID"), new Link("ExitLocationID", "Locations", "LocationID"), new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneChecklistItems", "SceneChecklistItemID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneDependencies", "SceneDependencyID", new Link("SourceSceneID", "Scenes", "SceneID"), new Link("TargetSceneID", "Scenes", "SceneID")),
new("SceneFloorPlanOccupancy", "SceneFloorPlanOccupancyID", new Link("CharacterID", "Characters", "CharacterID"), new Link("FloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("LocationID", "Locations", "LocationID"), new Link("SceneID", "Scenes", "SceneID"), new Link("StoryAssetID", "StoryAssets", "StoryAssetID")),
new("SceneManuscriptSources", null, new Link("BookID", "Books", "BookID"), new Link("ChapterID", "Chapters", "ChapterID"), new Link("ProjectID", "Project", "ProjectID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneMetricTypes", "MetricTypeID", new Link("ProjectID", "Project", "ProjectID")),
new("SceneMetricValues", null, new Link("MetricTypeID", "SceneMetricTypes", "MetricTypeID"), new Link("SceneID", "Scenes", "SceneID")),
new("SceneNotes", "SceneNoteID", new Link("SceneID", "Scenes", "SceneID")),
new("SceneOutcomes", "SceneOutcomeID", new Link("SceneID", "Scenes", "SceneID")),
new("ScenePurposes", null, new Link("SceneID", "Scenes", "SceneID")),
new("SceneWorkflow", "SceneWorkflowID", new Link("SceneID", "Scenes", "SceneID")),
new("Scenes", "SceneID", new Link("ChapterID", "Chapters", "ChapterID"), new Link("FloorPlanID", "FloorPlans", "FloorPlanID"), new Link("InitialFloorPlanFloorID", "FloorPlanFloors", "FloorPlanFloorID"), new Link("ManuscriptDocumentID", "ManuscriptDocuments", "ManuscriptDocumentID"), new Link("POVCharacterID", "Characters", "CharacterID"), new Link("PrimaryLocationID", "Locations", "LocationID")),
new("StoryAssets", "StoryAssetID", new Link("CurrentLocationID", "Locations", "LocationID"), new Link("CurrentStateID", "AssetStates", "AssetStateID"), new Link("ProjectID", "Project", "ProjectID")),
new("StoryMaps", "MapID", new Link("ParentMapID", "StoryMaps", "MapID"), new Link("ProjectID", "Project", "ProjectID")),
new("ThreadEvents", "ThreadEventID", new Link("PlotLineID", "PlotLines", "PlotLineID"), new Link("PlotThreadID", "PlotThreads", "PlotThreadID"), new Link("SceneID", "Scenes", "SceneID"), new Link("TargetPlotLineID", "PlotLines", "PlotLineID")),
new("TimelineViewPresets", "TimelineViewPresetID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("UserFiles", "UserFileID", new Link("ProjectID", "Project", "ProjectID")),
new("ValidationRuns", "ValidationRunID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("WritingPlans", "WritingPlanID", new Link("BookID", "Books", "BookID"), new Link("ProjectID", "Project", "ProjectID")),
new("WritingScheduleItems", "WritingScheduleItemID", new Link("SceneID", "Scenes", "SceneID"), new Link("WritingPlanID", "WritingPlans", "WritingPlanID"))
];
public static string? Validate(ProjectBackupExport backup)
{
if(backup.Project is null || backup.Project.ProjectID<=0) return "Project backup contains an invalid source project identifier.";
var rows=Entities.ToDictionary(entity=>entity.Collection,entity=>((IEnumerable)typeof(ProjectBackupData).GetProperty(entity.Collection)!.GetValue(backup.Data)!).Cast<object>().ToArray());
object? Value(object row,string column)=>row.GetType().GetProperty(column)?.GetValue(row);
foreach(var entity in Entities)
{
if(entity.Identity is not null)
{
var ids=rows[entity.Collection].Select(row=>Value(row,entity.Identity)).ToArray();
if(ids.Any(id=>id is null || id is int integer && integer<=0 || id is long wide && wide<=0) || ids.Distinct().Count()!=ids.Length)
return $"Project backup contains duplicate or missing {entity.Collection} identifiers.";
}
foreach(var link in entity.Links)
{
// These older formats did not carry document rows; copied links are detached on import.
if(link.TargetCollection=="CharacterImages" && backup.FormatVersion!="1.3")continue;
if(link.TargetCollection=="ManuscriptDocuments" && backup.FormatVersion is "1.0" or "1.1") continue;
var allowed=link.TargetCollection=="Project" ? new HashSet<object?> { backup.Project.ProjectID }
: rows[link.TargetCollection].Select(row=>Value(row,link.TargetColumn)).Where(value=>value is not null).ToHashSet();
if(rows[entity.Collection].Any(row=>Value(row,link.Column) is object value && !allowed.Contains(value)))
return $"Project backup {entity.Collection}.{link.Column} references a missing or foreign {link.TargetCollection} record.";
}
}
if(backup.Data.StoryIntelligenceRuns.Count>0 && backup.Data.Scenes.Any(scene=>scene.ImportRunID.HasValue && !backup.Data.StoryIntelligenceRuns.Any(run=>run.StoryIntelligenceRunID==scene.ImportRunID)))
return "Project backup scene analysis provenance references a missing or foreign run.";
return ProjectBackupTimelineState.Validate(backup);
}
}