From 2b94d538054c6af05863f6686235d0befdbcbfc8 Mon Sep 17 00:00:00 2001
From: Petar Hristov <hristov@itc.rwth-aachen.de>
Date: Wed, 23 Nov 2022 11:23:18 +0100
Subject: [PATCH] New: Added ProjectInstitute

---
 .../Implementations/ProjectStructuralData.cs  | 58 ++++++++++---------
 .../ResourceTypeStructuralData.cs             |  4 +-
 src/SQL2Linked/StructuralData.cs              |  6 +-
 3 files changed, 37 insertions(+), 31 deletions(-)

diff --git a/src/SQL2Linked/Implementations/ProjectStructuralData.cs b/src/SQL2Linked/Implementations/ProjectStructuralData.cs
index 6b5a7a6..ab51ee2 100644
--- a/src/SQL2Linked/Implementations/ProjectStructuralData.cs
+++ b/src/SQL2Linked/Implementations/ProjectStructuralData.cs
@@ -14,9 +14,11 @@ namespace SQL2Linked.Implementations
         public readonly Uri vcard = new("http://www.w3.org/2006/vcard/ns#");
         public readonly Uri rdf = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
         public readonly Uri cosc = new("https://purl.org/coscine/");
-        private VisibilityModel VisibilityModel = new VisibilityModel();
-        private ProjectRoleModel ProjectRoleModel = new ProjectRoleModel();
-        private ProjectResourceModel ProjectResourceModel = new ProjectResourceModel();
+
+        private readonly VisibilityModel VisibilityModel = new();
+        private readonly ProjectRoleModel ProjectRoleModel = new();
+        private readonly ProjectResourceModel ProjectResourceModel = new();
+        private readonly ProjectInstituteModel ProjectInstituteModel = new();
 
         // Override to also receive deleted projects
         public override IEnumerable<Project> GetAll()
@@ -34,6 +36,7 @@ namespace SQL2Linked.Implementations
             IEnumerable<Visibility> visibilities = VisibilityModel.GetAll();
             IEnumerable<ProjectRole> projectRoles = ProjectRoleModel.GetAll();
             IEnumerable<ProjectResource> projectResources = ProjectResourceModel.GetAll();
+            IEnumerable<ProjectInstitute> projectInstitutes = ProjectInstituteModel.GetAll();
 
             var graphs = new List<IGraph>();
             var projectUrlHandlePrefix = "https://hdl.handle.net/" + Prefix;
@@ -45,8 +48,10 @@ namespace SQL2Linked.Implementations
                 var projectGraphName = $"{projectUrlPrefix}/{entry.Id}";
                 var projectHandleName = $"{projectUrlHandlePrefix}/{entry.Id}";
 
-                var graph = new Graph();
-                graph.BaseUri = new Uri(projectGraphName);
+                var graph = new Graph
+                {
+                    BaseUri = new Uri(projectGraphName)
+                };
 
                 AssertToGraphUriNode(graph, projectGraphName, rdf + "type", dcat + "Catalog");
                 Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {rdf}type {dcat}Catalog'. ");
@@ -113,36 +118,37 @@ namespace SQL2Linked.Implementations
                 AssertToGraphUriNode(graph, projectGraphName, foaf + "homepage", projectHandleName);
                 Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {foaf}homepage {projectHandleName}'. ");
 
-                foreach (var projectRole in projectRoles)
+                foreach (var projectRole in projectRoles.Where(p => p.ProjectId.Equals(entry.Id)))
                 {
-                    if (entry.Id == projectRole.ProjectId)
-                    {
-                        AssertToGraphUriNode(graph, projectGraphName, vcard + "hasMember", cosc + "users/" + projectRole.UserId);
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {vcard}hasMember {cosc}users/{projectRole.UserId}'. ");
+                    AssertToGraphUriNode(graph, projectGraphName, vcard + "hasMember", cosc + "users/" + projectRole.UserId);
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {vcard}hasMember {cosc}users/{projectRole.UserId}'. ");
 
-                        var blankNode = graph.CreateBlankNode();
+                    var blankNode = graph.CreateBlankNode();
 
-                        AssertToGraphBlankAndUriNode(graph, blankNode, rdf + "type", org + "Membership");
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {rdf}type {org}Membership'. ");
+                    AssertToGraphBlankAndUriNode(graph, blankNode, rdf + "type", org + "Membership");
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {rdf}type {org}Membership'. ");
 
-                        AssertToGraphBlankAndUriNode(graph, blankNode, org + "organization", projectGraphName);
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}organization {projectGraphName}'. ");
+                    AssertToGraphBlankAndUriNode(graph, blankNode, org + "organization", projectGraphName);
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}organization {projectGraphName}'. ");
 
-                        AssertToGraphBlankAndUriNode(graph, blankNode, org + "role", cosc + "roles/" + projectRole.RoleId);
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}role {cosc}roles/{projectRole.RoleId}'. ");
+                    AssertToGraphBlankAndUriNode(graph, blankNode, org + "role", cosc + "roles/" + projectRole.RoleId);
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}role {cosc}roles/{projectRole.RoleId}'. ");
 
-                        AssertToGraphBlankAndUriNode(graph, blankNode, org + "member", cosc + "users/" + projectRole.UserId);
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}member {cosc}users/{projectRole.UserId}'. ");
-                    }
+                    AssertToGraphBlankAndUriNode(graph, blankNode, org + "member", cosc + "users/" + projectRole.UserId);
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}member {cosc}users/{projectRole.UserId}'. ");
                 }
 
-                foreach (var projectResource in projectResources)
+                foreach (var projectResource in projectResources.Where(p => p.ProjectId.Equals(entry.Id)))
                 {
-                    if (entry.Id == projectResource.ProjectId)
-                    {
-                        AssertToGraphUriNode(graph, projectGraphName, dcat + "catalog", $"{resourceUrlPrefix}/{projectResource.ResourceId}");
-                        Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcat}catalog {resourceUrlPrefix}/{projectResource.ResourceId}'. ");
-                    }
+                    AssertToGraphUriNode(graph, projectGraphName, dcat + "catalog", $"{resourceUrlPrefix}/{projectResource.ResourceId}");
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcat}catalog {resourceUrlPrefix}/{projectResource.ResourceId}'. ");
+                }
+
+                foreach (var projectInstitute in projectInstitutes.Where(p => p.ProjectId.Equals(entry.Id)))
+                {
+                    var blankNode = graph.CreateBlankNode();
+                    AssertToGraphBlankAndUriNode(graph, blankNode, org + "organization", projectGraphName);
+                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}organization {projectGraphName}'. ");
                 }
 
                 if (entry.Creator is not null)
diff --git a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs
index 06040e1..be7ca6e 100644
--- a/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs
+++ b/src/SQL2Linked/Implementations/ResourceTypeStructuralData.cs
@@ -12,7 +12,7 @@ namespace SQL2Linked.Implementations
         public readonly Uri dcterms = new("http://purl.org/dc/terms/");
         public override IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<ResourceType> entries)
         {
-       
+
             var graphs = new List<IGraph>();
 
             foreach (var entry in entries)
@@ -53,5 +53,5 @@ namespace SQL2Linked.Implementations
             return graphs;
         }
     }
-    
+
 }
\ No newline at end of file
diff --git a/src/SQL2Linked/StructuralData.cs b/src/SQL2Linked/StructuralData.cs
index 11df7b5..0e077a6 100644
--- a/src/SQL2Linked/StructuralData.cs
+++ b/src/SQL2Linked/StructuralData.cs
@@ -1,7 +1,7 @@
-using Polly;
-using Coscine.Configuration;
+using Coscine.Configuration;
 using Coscine.Database.Models;
 using Coscine.Metadata;
+using Polly;
 using VDS.RDF;
 
 namespace SQL2Linked
@@ -64,7 +64,7 @@ namespace SQL2Linked
                     WrapRequest(() => RdfStoreConnector.AddGraph(graph));
                     Console.WriteLine($" - Graph {graph.BaseUri} added successfully");
                     Console.WriteLine();
-                } 
+                }
                 catch (Exception e)
                 {
                     Console.Error.WriteLine($"Error on ({graph.BaseUri}):");
-- 
GitLab