Skip to content
Snippets Groups Projects
Select Git revision
  • dc975b1162d244ea75d23738fd5aad8d2771a3a9
  • main default protected
  • gitkeep
  • dev protected
  • Issue/2914-trellisMigrator
  • Issue/2847-reporting
  • Hotfix/2776-workingNewVersion
  • Hotfix/xxxx-correctAssignments
  • Issue/2666-adminCronjobs-theSequal
  • Issue/2666-adminCronjobs
  • Issue/2518-docs
  • Hotfix/xxxx-coscineGraph
  • Issue/2304-virtuosoRoars
  • Fix/v0.1.7-dependencies
  • Hotfix/2212-fixFiles
  • Issue/2222-resourceDateCreated
  • Issue/2221-projectDateCreated
  • Hotfix/xxxx-changeUrls
  • Issue/1321-pidEnquiryOverhaul
  • Issue/1782-structualDataIntegration
  • Issue/2084-migrateResourceStructuralData
  • v0.1.24
  • v0.1.23
  • v0.1.22
  • v0.1.21
  • v0.1.20
  • v0.1.19
  • v0.1.18
  • v0.1.17
  • v0.1.16
  • v0.1.15
  • v0.1.14
  • v0.1.13
  • v0.1.12
  • v0.1.11
  • v0.1.10
  • v0.1.9
  • v0.1.7
  • v0.1.8
  • v0.1.6
  • v0.1.5
41 results

ResourceStructuralData.cs

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ResourceStructuralData.cs 10.51 KiB
    using Coscine.Database.DataModel;
    using Coscine.Database.Models;
    using Coscine.Database.Util;
    using VDS.RDF;
    using VDS.RDF.Query;
    
    namespace SQL2Linked.Implementations
    {
        public class ResourceStructuralData : StructuralData<Resource, ResourceModel>
        {
            public readonly Uri org = new("http://www.w3.org/ns/org#");
            public readonly Uri dcat = new("http://www.w3.org/ns/dcat#");
            public readonly Uri dcterms = new("http://purl.org/dc/terms/");
            public readonly Uri acl = new("http://www.w3.org/ns/auth/acl#");
            public readonly Uri foaf = new("http://xmlns.com/foaf/0.1/");
            public readonly Uri pim = new("http://www.w3.org/ns/pim/space#");
            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 ProjectResourceModel ProjectResourceModel = new ProjectResourceModel();
            private LicenseModel LicenseModel = new LicenseModel();
    
            // Override to also receive deleted resources
            public override IEnumerable<Resource> GetAll()
            {
                return DatabaseConnection.ConnectToDatabase((db) =>
                {
                    return
                        (from tableEntry in Model.GetITableFromDatabase(db)
                         select tableEntry).ToList();
                });
            }
    
            public override IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<Resource> entries)
            {
                IEnumerable<Visibility> visibilities = VisibilityModel.GetAll();
                IEnumerable<ProjectResource> projectResources = ProjectResourceModel.GetAll();
                IEnumerable<License> licenses = LicenseModel.GetAll();
    
                var graphs = new List<IGraph>();
                string resourceUrlPrefix = "https://hdl.handle.net/" + Prefix;
    
                foreach (var entry in entries)
                {
                    var resourceGraphName = $"{resourceUrlPrefix}/{entry.Id}";
    
                    var graph = new Graph();
                    graph.BaseUri = new Uri(resourceGraphName);
    
                    AssertToGraphUriNode(graph, resourceGraphName, rdf + "type", dcat + "Catalog");
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {rdf}type {dcat}Catalog'. ");
    
                    AssertToGraphUriNode(graph, resourceGraphName, rdf + "type", pim + "Storage");
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {rdf}type {pim}Storage'. ");
    
                    AssertToGraphUriNode(graph, resourceGraphName, dcat + "service", cosc + $"resourcetype/{entry.TypeId}");
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcat}service {cosc}resourcetype/{entry.TypeId}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "title", entry.ResourceName);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}title {entry.ResourceName}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "alternative", entry.DisplayName);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}alternative {entry.DisplayName}'. ");
    
                    foreach (var visibility in visibilities)
                    {
                        if (entry.VisibilityId == visibility.Id && visibility.DisplayName.Contains("Public"))
                        {
                            AssertToGraphUriNode(graph, resourceGraphName, cosc + "terms/resource#visibility", cosc + $"terms/visibility#public");
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#visibility {cosc}terms/visibility#public'. ");
                            break;
                        }
                        else if (entry.VisibilityId == visibility.Id && visibility.DisplayName.Contains("Project Members"))
                        {
                            AssertToGraphUriNode(graph, resourceGraphName, cosc + "terms/resource#visibility", cosc + $"terms/visibility#projectMember");
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#visibility {cosc}terms/visibility#projectMember'. ");
                            break;
                        }
                    }
    
                    foreach (var license in licenses)
                    {
                        if (entry.LicenseId == license.Id)
                        {
                            AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "license", license.DisplayName);
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}license {license.DisplayName}'. ");
                            break;
                        }
                    }
    
                    if (!string.IsNullOrWhiteSpace(entry.Keywords))
                    {
                        var listKeywords = entry.Keywords.Split(';').ToList();
                        foreach (var keyword in listKeywords)
                        {
                            AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "subject", keyword);
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}subject {keyword}'. ");
                        }
                    }
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "rights", entry.UsageRights);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}rights {entry.UsageRights}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "description", entry.Description);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}description {entry.Description}'. ");
    
                    // Skipping broken resources
                    if (string.IsNullOrWhiteSpace(entry.ApplicationProfile))
                    {
                        continue;
                    }
    
                    AssertToGraphUriNode(graph, resourceGraphName, dcterms + "conformsTo", entry.ApplicationProfile);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}conformsTo {entry.ApplicationProfile}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, cosc + "terms/resource#fixedValues", entry.FixedValues);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#fixedValues {entry.FixedValues}'. ");
    
                    AssertToGraphUriNode(graph, resourceGraphName, dcterms + "creator", cosc + $"users/{entry.Creator}");
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}creator {cosc}users/{entry.Creator}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, cosc + "terms/resource#archived", entry.Archived.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean"));
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#archived {entry.Archived}'. ");
    
                    AssertToGraphUriNode(graph, resourceGraphName, foaf + "homepage", resourceGraphName);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {foaf}homepage {resourceGraphName}'. ");
    
                    AssertToGraphLiteralNode(graph, resourceGraphName, cosc + "terms/resource#deleted", entry.Deleted.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean"));
                    Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#deleted {entry.Deleted}'. ");
    
                    SparqlParameterizedString cmdString = new SparqlParameterizedString
                    {
                        CommandText = "SELECT DISTINCT ?g " +
                                        "WHERE {" +
                                            "GRAPH ?g { ?s ?p ?o } . " +
                                            "FILTER(STRSTARTS(STR(?s), \"" + resourceGraphName + "@\"))" +
                                         "}"
                    };
    
                    var resultSet = QueryEndpoint.QueryWithResultSet(cmdString.ToString());
    
                    foreach (var result in resultSet)
                    {
                        AssertToGraphUriNode(graph, resourceGraphName, dcat + "catalog", result[0].ToString());
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcat}catalog {result[0].ToString()}'. ");
                    }
    
                    foreach (var projectResource in projectResources)
                    {
                        if (entry.Id == projectResource.ResourceId)
                        {
                            var blankNode = graph.CreateBlankNode();
    
                            AssertToGraphBlankAndUriNode(graph, blankNode, rdf + "type", acl + "Authorization");
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {rdf}type {acl}Authorization'. ");
    
                            AssertToGraphBlankAndUriNode(graph, blankNode, acl + "agentGroup", $"{resourceUrlPrefix}/{projectResource.ProjectId}");
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}agentGroup {resourceUrlPrefix}/{projectResource.ProjectId}'. ");
    
                            AssertToGraphBlankAndUriNode(graph, blankNode, acl + "accessTo", resourceGraphName);
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}accessTo {resourceGraphName}'. ");
    
                            AssertToGraphBlankAndUriNode(graph, blankNode, acl + "default", resourceGraphName);
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}default {resourceGraphName}'. ");
    
                            AssertToGraphBlankAndUriNode(graph, blankNode, acl + "mode", acl + "Control");
                            Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}accessTo {acl}Control'. ");
                        }
                    }
    
                    graphs.Add(graph);
                }
    
                return graphs;
            }
        }
    }