From 92f89078cd9c20f484c3e29c7544cc36885b0817 Mon Sep 17 00:00:00 2001
From: Benedikt Heinrichs <heinrichs@itc.rwth-aachen.de>
Date: Mon, 22 Jan 2024 12:13:52 +0100
Subject: [PATCH] Fix: Working new version

---
 .../Implementations/ProjectStructuralData.cs  |  2 +-
 .../Implementations/ResourceStructuralData.cs | 32 +++++++++++++++++--
 src/SQL2Linked/SQL2Linked.csproj              |  4 +--
 src/SQL2Linked/Utils/UriHelper.cs             | 24 ++++++++++++++
 4 files changed, 56 insertions(+), 6 deletions(-)

diff --git a/src/SQL2Linked/Implementations/ProjectStructuralData.cs b/src/SQL2Linked/Implementations/ProjectStructuralData.cs
index b50b0b1..6242f4b 100644
--- a/src/SQL2Linked/Implementations/ProjectStructuralData.cs
+++ b/src/SQL2Linked/Implementations/ProjectStructuralData.cs
@@ -33,7 +33,7 @@ public class ProjectStructuralData : StructuralData<ProjectAdminDto>
     public override async Task<IEnumerable<IGraph>> ConvertToLinkedDataAsync(IEnumerable<ProjectAdminDto> entries)
     {
         var graphs = new List<IGraph>();
-        var coscineHandlePrefix = UriHelper.TryCombineUri(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
+        var coscineHandlePrefix = UriHelper.TryCombinePath(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
             ?? throw new Exception("Could not combine handle prefix with PID prefix");
 
         var coscineGraph = new Graph
diff --git a/src/SQL2Linked/Implementations/ResourceStructuralData.cs b/src/SQL2Linked/Implementations/ResourceStructuralData.cs
index 67eb96c..cb16834 100644
--- a/src/SQL2Linked/Implementations/ResourceStructuralData.cs
+++ b/src/SQL2Linked/Implementations/ResourceStructuralData.cs
@@ -3,6 +3,7 @@ using Coscine.ApiClient.Core.Model;
 using Newtonsoft.Json;
 using SQL2Linked.Utils;
 using VDS.RDF;
+using VDS.RDF.Parsing;
 
 namespace SQL2Linked.Implementations;
 
@@ -34,7 +35,7 @@ public class ResourceStructuralData : StructuralData<ResourceAdminDto>
     public override async Task<IEnumerable<IGraph>> ConvertToLinkedDataAsync(IEnumerable<ResourceAdminDto> entries)
     {
         var graphs = new List<IGraph>();
-        var coscineHandlePrefix = UriHelper.TryCombineUri(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
+        var coscineHandlePrefix = UriHelper.TryCombinePath(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
             ?? throw new Exception("Could not combine handle prefix with PID prefix");
 
         foreach (var entry in entries)
@@ -120,7 +121,32 @@ public class ResourceStructuralData : StructuralData<ResourceAdminDto>
             Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.FoafHomepage} {resourceGraphName}'. ");
 
             AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.CoscineTermsResourceDeleted, entry.Deleted.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean"));
-            Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceDeleted} {entry.Deleted}'. ");
+            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceDeleted} {entry.Deleted}'. ");
+
+            // Reinstate the catalog assignments
+            var oldResourceGraphResponse = await _adminApi.GetMetadataGraphAsync(
+                resourceGraphName.AbsoluteUri,
+                RdfFormat.TextTurtle
+            );
+            if (oldResourceGraphResponse is not null)
+            {
+                var oldResourceGraph = new Graph();
+                var ttlparser = new TurtleParser();
+                ttlparser.Load(oldResourceGraph, new StringReader(oldResourceGraphResponse.Data.Content));
+
+                foreach (var result in oldResourceGraph.GetTriplesWithPredicate(RdfUris.DcatCatalog))
+                {
+                    if (result.Object.NodeType == NodeType.Uri)
+                    { 
+                        var catalogedUri = result.Object as IUriNode;
+                        if (catalogedUri is not null)
+                        {
+                            AssertToGraphUriNode(graph, resourceGraphName, RdfUris.DcatCatalog, catalogedUri.Uri);
+                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcatCatalog} {catalogedUri.Uri}'. ");
+                        }
+                    }
+                }
+            }
 
             foreach (var projectResource in entry.ProjectResources)
             {
@@ -151,7 +177,7 @@ public class ResourceStructuralData : StructuralData<ResourceAdminDto>
             if (entry.DateCreated is not null && entry.DateCreated.HasValue)
             {
                 AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsCreated, entry.DateCreated.Value.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
-                Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsCreated} {entry.DateCreated}'. ");
+                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsCreated} {entry.DateCreated}'. ");
             }
 
             graphs.Add(graph);
diff --git a/src/SQL2Linked/SQL2Linked.csproj b/src/SQL2Linked/SQL2Linked.csproj
index a473097..b9b1ce6 100644
--- a/src/SQL2Linked/SQL2Linked.csproj
+++ b/src/SQL2Linked/SQL2Linked.csproj
@@ -1,4 +1,4 @@
-<Project Sdk="Microsoft.NET.Sdk">
+<Project Sdk="Microsoft.NET.Sdk">
 
 	<PropertyGroup>
 		<OutputType>Exe</OutputType>
@@ -16,7 +16,7 @@
 	</PropertyGroup>
 
 	<ItemGroup>
-		<PackageReference Include="Coscine.ApiClient" Version="1.3.0" />
+		<PackageReference Include="Coscine.ApiClient" Version="1.3.1" />
 		<PackageReference Include="dotNetRdf" Version="3.1.1" />
 		<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />
 		<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.1" />
diff --git a/src/SQL2Linked/Utils/UriHelper.cs b/src/SQL2Linked/Utils/UriHelper.cs
index e0e8f06..af5e504 100644
--- a/src/SQL2Linked/Utils/UriHelper.cs
+++ b/src/SQL2Linked/Utils/UriHelper.cs
@@ -5,6 +5,30 @@
 /// </summary>
 public static class UriHelper
 {
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="baseUri"></param>
+    /// <param name="relativePath"></param>
+    /// <returns></returns>
+    public static Uri? TryCombinePath(Uri baseUri, string relativePath)
+    {
+        Uri.TryCreate(baseUri, relativePath + "/", out Uri? result);
+        return result;
+    }
+
+    /// <summary>
+    /// 
+    /// </summary>
+    /// <param name="baseUri"></param>
+    /// <param name="relativePath"></param>
+    /// <returns></returns>
+    public static Uri? TryCombinePath(Uri baseUri, Guid relativePath)
+    {
+        Uri.TryCreate(baseUri, relativePath.ToString() + "/", out Uri? result);
+        return result;
+    }
+
     /// <summary>
     /// 
     /// </summary>
-- 
GitLab