From acdadb2279b131d1f9979d6134d019e31239a128 Mon Sep 17 00:00:00 2001 From: Matt Lyons Date: Sun, 24 Sep 2023 19:32:57 -0500 Subject: [PATCH] PR updates --- .../JsonUtils/ProjectDataScopeConverter.cs | 2 +- c-sharp/JsonUtils/ProjectMetadataConverter.cs | 4 +- c-sharp/Projects/LocalProjects.cs | 45 ++++++++++--------- .../Projects/ParatextProjectDataProvider.cs | 5 --- .../ParatextProjectDataProviderFactory.cs | 2 +- .../ParatextProjectStorageInterpreter.cs | 10 ++--- c-sharp/Projects/ProjectDataProvider.cs | 14 +----- c-sharp/Projects/ProjectDataScope.cs | 2 +- c-sharp/Projects/ProjectDetails.cs | 16 ++++--- c-sharp/Projects/ProjectMetadata.cs | 15 +++++-- .../RawDirectoryProjectStreamManager.cs | 4 +- src/main/main.ts | 2 +- 12 files changed, 59 insertions(+), 62 deletions(-) diff --git a/c-sharp/JsonUtils/ProjectDataScopeConverter.cs b/c-sharp/JsonUtils/ProjectDataScopeConverter.cs index e965eb818a..a026d64bc0 100644 --- a/c-sharp/JsonUtils/ProjectDataScopeConverter.cs +++ b/c-sharp/JsonUtils/ProjectDataScopeConverter.cs @@ -22,7 +22,7 @@ out string errorMessage JObject parsedArgs = JObject.Parse(jsonString); dataScope = new ProjectDataScope() { - ProjectID = Guid.Parse(Get(parsedArgs, PROJECT_ID)!), + ProjectID = Get(parsedArgs, PROJECT_ID), ProjectName = Get(parsedArgs, PROJECT_NAME), ExtensionName = Get(parsedArgs, EXTENSION_NAME), DataType = Get(parsedArgs, DATA_TYPE), diff --git a/c-sharp/JsonUtils/ProjectMetadataConverter.cs b/c-sharp/JsonUtils/ProjectMetadataConverter.cs index a13e8caf7f..3bebb2d518 100644 --- a/c-sharp/JsonUtils/ProjectMetadataConverter.cs +++ b/c-sharp/JsonUtils/ProjectMetadataConverter.cs @@ -19,7 +19,7 @@ out string errorMessage try { JObject parsedArgs = JObject.Parse(jsonString); - Guid id = Guid.Parse(Get(parsedArgs, ID)); + string id = Get(parsedArgs, ID); string name = Get(parsedArgs, NAME); string projectStorageType = Get(parsedArgs, STORAGE_TYPE); string projectType = Get(parsedArgs, PROJECT_TYPE); @@ -51,7 +51,7 @@ public static string ToJsonString(ProjectMetadata projectMetadata) { return new JObject { - [ID] = projectMetadata.ID.ToString(), + [ID] = projectMetadata.ID, [NAME] = projectMetadata.Name, [STORAGE_TYPE] = projectMetadata.ProjectStorageType, [PROJECT_TYPE] = projectMetadata.ProjectType diff --git a/c-sharp/Projects/LocalProjects.cs b/c-sharp/Projects/LocalProjects.cs index 0aea6003f3..f3497a2462 100644 --- a/c-sharp/Projects/LocalProjects.cs +++ b/c-sharp/Projects/LocalProjects.cs @@ -11,8 +11,8 @@ namespace Paranext.DataProvider.Projects; /// internal static partial class LocalProjects { - private static readonly ConcurrentDictionary s_projectDetailsMap = new(); - private static readonly ConcurrentDictionary s_paratextProjectMap = new(); + private static readonly ConcurrentDictionary s_projectDetailsMap = + new(); // All project directories are subdirectories of this private static readonly string s_projectRootFolder; @@ -39,7 +39,7 @@ static LocalProjects() public static void Initialize() { - if (!s_paratextProjectMap.IsEmpty || !s_projectDetailsMap.IsEmpty) + if (!s_projectDetailsMap.IsEmpty) return; if (!Directory.Exists(s_projectRootFolder)) @@ -72,14 +72,14 @@ public static IList GetAllProjectDetails() return s_projectDetailsMap.Values.ToList(); } - public static ProjectDetails GetProjectDetails(Guid projectId) + public static ProjectDetails GetProjectDetails(string projectId) { - return s_projectDetailsMap[projectId]; + return s_projectDetailsMap[projectId.ToUpperInvariant()]; } - public static ScrText GetParatextProject(Guid projectId) + public static ScrText GetParatextProject(string projectId) { - return s_paratextProjectMap[projectId]; + return ScrTextCollection.GetById(HexId.FromStr(projectId)); } public static void SaveProjectMetadata(ProjectMetadata metadata, bool overwrite = false) @@ -105,7 +105,7 @@ public static void SaveProjectMetadata(ProjectMetadata metadata, bool overwrite AddProjectToMaps(new ProjectDetails(metadata, projectHomeDir)); } - public static void LoadProject(string projectName, Guid projectID) + public static void LoadProject(string projectName, string projectID) { var dir = GetProjectDir(projectName, projectID); var projectMetadata = @@ -113,26 +113,27 @@ public static void LoadProject(string projectName, Guid projectID) AddProjectToMaps(new ProjectDetails(projectMetadata, dir)); } - private static string GetProjectDir(string projectName, Guid projectID) + private static string GetProjectDir(string projectName, string projectID) { return Path.Join(s_projectRootFolder, $"{projectName}_{projectID}"); } private static void AddProjectToMaps(ProjectDetails projectDetails) { - ProjectName projectName = - new( - Path.Join( - projectDetails.Directory, - PROJECT_SUBDIRECTORY, - PARATEXT_DATA_SUBDIRECTORY - ) - ); + var projectPath = Path.Join( + projectDetails.HomeDirectory, + PROJECT_SUBDIRECTORY, + PARATEXT_DATA_SUBDIRECTORY + ); + ProjectName projectName = new(projectPath); var id = projectDetails.Metadata.ID; - if (s_projectDetailsMap.ContainsKey(id) || s_paratextProjectMap.ContainsKey(id)) - Console.WriteLine($"Replacing Paratext project in map: {id}"); + Console.WriteLine( + s_projectDetailsMap.ContainsKey(id) + ? $"Replacing Paratext project in map: {id} = {projectPath}" + : $"Adding Paratext project in map: {id} = {projectPath}" + ); s_projectDetailsMap[id] = projectDetails; - s_paratextProjectMap[id] = new ScrText(projectName, RegistrationInfo.DefaultUser); + ScrTextCollection.Add(new ScrText(projectName, RegistrationInfo.DefaultUser)); } /// @@ -191,8 +192,8 @@ out string errorMessage var matches = ProjectDirectoryRegex().Matches(finalDirectory); if ( (matches.Count != 1) - || Guid.Parse(matches[0].Groups["id"].Value) != metadata!.ID - || matches[0].Groups["name"].Value != metadata.Name + || (metadata == null) + || !metadata.Contains(matches[0].Groups["id"].Value, matches[0].Groups["name"].Value) ) { errorMessage = $"Project directory does not match its metadata: {projectHomeDir}"; diff --git a/c-sharp/Projects/ParatextProjectDataProvider.cs b/c-sharp/Projects/ParatextProjectDataProvider.cs index 385a218ea4..3cdba7bdd8 100644 --- a/c-sharp/Projects/ParatextProjectDataProvider.cs +++ b/c-sharp/Projects/ParatextProjectDataProvider.cs @@ -37,11 +37,6 @@ protected override ResponseToRequest SetExtensionData(ProjectDataScope dataScope return _paratextPsi.SetExtensionData(dataScope, data); } - protected override ResponseToRequest SubscribeExtensionData(string jsonString) - { - return ResponseToRequest.Failed("Subscribing to extension data is not supported"); - } - private ResponseToRequest Get(string dataType, string dataQualifier) { ProjectDataScope scope = diff --git a/c-sharp/Projects/ParatextProjectDataProviderFactory.cs b/c-sharp/Projects/ParatextProjectDataProviderFactory.cs index 64fd5ddba1..575a574ee2 100644 --- a/c-sharp/Projects/ParatextProjectDataProviderFactory.cs +++ b/c-sharp/Projects/ParatextProjectDataProviderFactory.cs @@ -36,7 +36,7 @@ string projectStorageInterpreterId ProjectDetails details; try { - details = LocalProjects.GetProjectDetails(Guid.Parse(projectID)); + details = LocalProjects.GetProjectDetails(projectID); } catch (Exception) { diff --git a/c-sharp/Projects/ParatextProjectStorageInterpreter.cs b/c-sharp/Projects/ParatextProjectStorageInterpreter.cs index 11868a85dc..6c46c0ac89 100644 --- a/c-sharp/Projects/ParatextProjectStorageInterpreter.cs +++ b/c-sharp/Projects/ParatextProjectStorageInterpreter.cs @@ -44,7 +44,7 @@ public override ResponseToRequest CreateProject(ProjectDataScope scope) ProjectMetadata metadata = new( - scope.ProjectID.Value, + scope.ProjectID, scope.ProjectName, ProjectStorageType.ParatextFolders, ProjectType.Paratext @@ -68,7 +68,7 @@ public override ResponseToRequest GetProjectSettings(ProjectDataScope scope) if (scope.ProjectID == null) return ResponseToRequest.Failed("Must provide a project ID"); - var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value); + var scrText = LocalProjects.GetParatextProject(scope.ProjectID); return ResponseToRequest.Succeeded(JsonConvert.SerializeObject(scrText.Settings)); } @@ -84,7 +84,7 @@ public override ResponseToRequest GetProjectData(ProjectDataScope scope) // Not making it mandatory that all calls provide a VerseRef since there might be some types that don't use it VerseRefConverter.TryCreateVerseRef(scope.DataQualifier, out var verseRef, out var error); - var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value); + var scrText = LocalProjects.GetParatextProject(scope.ProjectID); return scope.DataType.ToUpperInvariant() switch { Book @@ -115,7 +115,7 @@ public override ResponseToRequest SetProjectData(ProjectDataScope scope, string // Not making it mandatory that all calls provide a VerseRef since there might be some types that don't use it VerseRefConverter.TryCreateVerseRef(scope.DataQualifier, out var verseRef, out var error); - var scrText = LocalProjects.GetParatextProject(scope.ProjectID.Value); + var scrText = LocalProjects.GetParatextProject(scope.ProjectID); switch (scope.DataType.ToUpperInvariant()) { case Chapter: @@ -178,7 +178,7 @@ public override ResponseToRequest SetExtensionData(ProjectDataScope scope, strin if (scope.ProjectID == null) return null; - var projectDetails = LocalProjects.GetProjectDetails(scope.ProjectID.Value); + var projectDetails = LocalProjects.GetProjectDetails(scope.ProjectID); RawDirectoryProjectStreamManager extensionStreamManager = new(projectDetails); return extensionStreamManager.GetDataStream( $"extensions/{scope.ExtensionName}/{scope.DataQualifier}", diff --git a/c-sharp/Projects/ProjectDataProvider.cs b/c-sharp/Projects/ProjectDataProvider.cs index 319417aa1c..ef72888fc4 100644 --- a/c-sharp/Projects/ProjectDataProvider.cs +++ b/c-sharp/Projects/ProjectDataProvider.cs @@ -7,8 +7,8 @@ namespace Paranext.DataProvider.Projects; /// /// Manages reading/writing to a project -/// Subclasses are expected to define and add get/set/subscribe methods to the Getters/Setters/Subscribers -/// Subclasses are also expected to use a ProjectStorageInterpreter for implementing all get/set/subscribe methods as appropriate +/// Subclasses are expected to define and add get/set methods to the Getters/Setters +/// Subclasses are also expected to use a ProjectStorageInterpreter for implementing all get/set methods as appropriate /// ProjectDataProviders are meant to be created/owned by ProjectDataProviderFactory instances /// internal abstract class ProjectDataProvider : NetworkObjects.DataProvider @@ -19,14 +19,12 @@ protected ProjectDataProvider(string name, PapiClient papiClient, ProjectDetails ProjectDetails = projectDetails; Getters.Add("getExtensionData", GetExtensionData); Setters.Add("setExtensionData", SetExtensionData); - Subscribers.Add("subscribeExtensionData", SubscribeExtensionData); } protected ProjectDetails ProjectDetails { get; } protected Dictionary> Getters { get; } = new(); protected Dictionary> Setters { get; } = new(); - protected Dictionary> Subscribers { get; } = new(); protected ProjectDataScope ExtractDataScope(string jsonString) { @@ -51,9 +49,6 @@ protected override ResponseToRequest HandleRequest(string functionName, JsonArra { try { - if (functionName.StartsWith("subscribe")) - return Subscribers[functionName](args[0]!.ToJsonString()); - if (functionName.StartsWith("get")) return Getters[functionName](args[0]!.ToJsonString()); @@ -102,9 +97,4 @@ private ResponseToRequest SetExtensionData(string jsonScope, string jsonData) /// Set an extension's data in a project identified by . /// protected abstract ResponseToRequest SetExtensionData(ProjectDataScope scope, string jsonData); - - /// - /// Subscribe to changes in an extension's data in a project - /// - protected abstract ResponseToRequest SubscribeExtensionData(string jsonString); } diff --git a/c-sharp/Projects/ProjectDataScope.cs b/c-sharp/Projects/ProjectDataScope.cs index c5eaef61de..311e117381 100644 --- a/c-sharp/Projects/ProjectDataScope.cs +++ b/c-sharp/Projects/ProjectDataScope.cs @@ -8,7 +8,7 @@ internal class ProjectDataScope /// /// Project ID /// - public Guid? ProjectID { get; set; } + public string? ProjectID { get; set; } /// /// Project short name diff --git a/c-sharp/Projects/ProjectDetails.cs b/c-sharp/Projects/ProjectDetails.cs index 93576336bf..c49f67183d 100644 --- a/c-sharp/Projects/ProjectDetails.cs +++ b/c-sharp/Projects/ProjectDetails.cs @@ -5,14 +5,18 @@ namespace Paranext.DataProvider.Projects; /// internal class ProjectDetails { - public ProjectDetails(ProjectMetadata metadata, string directory) + public ProjectDetails(ProjectMetadata metadata, string homeDirectory) { Metadata = metadata; - Directory = directory; + HomeDirectory = homeDirectory; } public ProjectMetadata Metadata { get; } - public string Directory { get; } + + /// + /// Directory where everything we know about a project is stored + /// + public string HomeDirectory { get; } public override bool Equals(object? obj) { @@ -22,16 +26,16 @@ public override bool Equals(object? obj) protected bool Equals(ProjectDetails other) { - return Metadata.Equals(other.Metadata) && Directory == other.Directory; + return Metadata.Equals(other.Metadata) && HomeDirectory == other.HomeDirectory; } public override int GetHashCode() { - return HashCode.Combine(Metadata, Directory); + return HashCode.Combine(Metadata, HomeDirectory); } public override string ToString() { - return $"[{Directory}] = {Metadata}"; + return $"[{HomeDirectory}] = {Metadata}"; } } diff --git a/c-sharp/Projects/ProjectMetadata.cs b/c-sharp/Projects/ProjectMetadata.cs index e3694efadd..d37a4a73e6 100644 --- a/c-sharp/Projects/ProjectMetadata.cs +++ b/c-sharp/Projects/ProjectMetadata.cs @@ -5,18 +5,18 @@ namespace Paranext.DataProvider.Projects; /// public class ProjectMetadata { - public ProjectMetadata(Guid id, string name, string projectStorageType, string projectType) + public ProjectMetadata(string id, string name, string projectStorageType, string projectType) { - ID = id; + ID = id.ToUpperInvariant(); Name = name; ProjectStorageType = projectStorageType; ProjectType = projectType; } /// - /// ID of the project (must be unique) + /// ID of the project (must be unique and case insensitive) /// - public Guid ID { get; } + public string ID { get; } /// /// Short name of the project (not necessarily unique) @@ -33,6 +33,13 @@ public ProjectMetadata(Guid id, string name, string projectStorageType, string p /// public string ProjectType { get; } + public bool Contains(string id, string name, bool nameIsCaseSensitive = false) + { + return ID.Equals(id, StringComparison.InvariantCultureIgnoreCase) && nameIsCaseSensitive + ? Name.Equals(name) + : Name.Equals(name, StringComparison.InvariantCultureIgnoreCase); + } + public override bool Equals(object? obj) { ProjectMetadata? that = obj as ProjectMetadata; diff --git a/c-sharp/Projects/RawDirectoryProjectStreamManager.cs b/c-sharp/Projects/RawDirectoryProjectStreamManager.cs index 723724d052..b231533690 100644 --- a/c-sharp/Projects/RawDirectoryProjectStreamManager.cs +++ b/c-sharp/Projects/RawDirectoryProjectStreamManager.cs @@ -15,12 +15,12 @@ internal class RawDirectoryProjectStreamManager : IProjectStreamManager public RawDirectoryProjectStreamManager(ProjectDetails projectDetails) { _projectDetails = projectDetails; - _writableRootDir = Path.Join(projectDetails.Directory, "project"); + _writableRootDir = Path.Join(projectDetails.HomeDirectory, "project"); } public void Initialize() { - if (!Directory.Exists(_projectDetails.Directory)) + if (!Directory.Exists(_projectDetails.HomeDirectory)) throw new Exception( $"Project contents missing for {_projectDetails.Metadata.Name} ({_projectDetails.Metadata.ID})" ); diff --git a/src/main/main.ts b/src/main/main.ts index c6de9a8349..88b57743e5 100644 --- a/src/main/main.ts +++ b/src/main/main.ts @@ -323,7 +323,7 @@ async function main() { // Test a .NET data provider setTimeout(async () => { const paratextPdp = await getProjectDataProvider( - '499234e1-9965-452b-8175-138330893b6a', + 'b4c501ad2538989d6fb723518e92408406e232d3', 'ParatextStandard', 'paratextFolders', );