Skip to content
Snippets Groups Projects
Select Git revision
  • 55f0274d1a4f77b316a91779df4cf908aa153ab6
  • 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
  • Benedikt Heinrichs's avatar
    Benedikt Heinrichs authored and Sandra Westerhoff committed
    92f89078
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    ResourceStructuralData.cs 11.79 KiB
    using Coscine.ApiClient;
    using Coscine.ApiClient.Core.Model;
    using Newtonsoft.Json;
    using SQL2Linked.Utils;
    using VDS.RDF;
    using VDS.RDF.Parsing;
    
    namespace SQL2Linked.Implementations;
    
    /// <summary>
    /// Class responsible for converting resource data into linked data graphs.
    /// It retrieves resource information from the API and then transforms this data into a series of RDF graphs,
    /// making use of predefined URIs and RDF constructs.
    /// </summary>
    public class ResourceStructuralData : StructuralData<ResourceAdminDto>
    {
        /// <summary>
        /// Asynchronously retrieves all resource data, including deleted resources.
        /// </summary>
        /// <returns>A <see cref="Task"/> that represents the asynchronous operation and returns a collection of <see cref="ResourceAdminDto"/>.</returns>
        /// <remarks>This override allows for the inclusion of deleted resources.</remarks>
        public override async Task<IEnumerable<ResourceAdminDto>> GetAll()
        {
            return await RequestUtil.WrapPagedRequest<ResourceAdminDtoPagedResponse, ResourceAdminDto>(
                (currentPage) => _adminApi.GetAllResourcesAsync(includeDeleted: true, pageNumber: currentPage, pageSize: 250)
            );
        }
    
        /// <summary>
        /// Converts a collection of resource data entries into a set of RDF graphs.
        /// Each resource is transformed into a graph, with RDF triples representing various properties of the resource.
        /// </summary>
        /// <param name="entries">A collection of <see cref="ResourceAdminDto"/> instances representing resource data.</param>
        /// <returns>A collection of <see cref="IGraph"/> instances, each representing an RDF graph of a resource.</returns>
        public override async Task<IEnumerable<IGraph>> ConvertToLinkedDataAsync(IEnumerable<ResourceAdminDto> entries)
        {
            var graphs = new List<IGraph>();
            var coscineHandlePrefix = UriHelper.TryCombinePath(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
                ?? throw new Exception("Could not combine handle prefix with PID prefix");
    
            foreach (var entry in entries)
            {
                var resourceGraphName = UriHelper.TryCombineUri(RdfUris.CoscineResources, entry.Id)
                    ?? throw new Exception("Could not combine resources prefix with resource ID");
                var resourceHandleName = UriHelper.TryCombineUri(coscineHandlePrefix, entry.Id);
    
                var graph = new Graph
                {
                    BaseUri = resourceGraphName
                };
    
                AssertToGraphUriNode(graph, resourceGraphName, RdfUris.A, RdfUris.DcatCatalogClass);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.A} {RdfUris.DcatCatalogClass}'. ");
    
                AssertToGraphUriNode(graph, resourceGraphName, RdfUris.A, RdfUris.PimStorageClass);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.A} {RdfUris.PimStorageClass}'. ");
    
                AssertToGraphUriNode(graph, resourceGraphName, RdfUris.DcatService, UriHelper.TryCombineUri(RdfUris.CoscineResourceTypes, entry.Type.Id));
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcatService} {UriHelper.TryCombineUri(RdfUris.CoscineResourceTypes, entry.Type.Id)}'. ");
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsTitle, entry.Name);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsTitle} {entry.Name}'. ");
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsAlternative, entry.DisplayName);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsAlternative} {entry.DisplayName}'. ");
    
                if (entry.Visibility.DisplayName.Contains("Public"))
                {
                    AssertToGraphUriNode(graph, resourceGraphName, RdfUris.CoscineTermsResourceVisibility, RdfUris.CoscineTermsVisibilityPublic);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceVisibility} {RdfUris.CoscineTermsVisibilityPublic}'. ");
                }
                else if (entry.Visibility.DisplayName.Contains("Project Members"))
                {
                    AssertToGraphUriNode(graph, resourceGraphName, RdfUris.CoscineTermsResourceVisibility, RdfUris.CoscineTermsVisibilityProjectMember);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceVisibility} {RdfUris.CoscineTermsVisibilityProjectMember}'. ");
                }
    
                if (entry.License is not null)
                {
                    AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsLicense, entry.License.DisplayName);
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsLicense} {entry.License.DisplayName}'. ");
                }
    
                if (entry.Keywords.Count > 0)
                {
                    foreach (var keyword in entry.Keywords)
                    {
                        AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsSubject, keyword);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsSubject} {keyword}'. ");
                    }
                }
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsRights, entry.UsageRights);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsRights} {entry.UsageRights}'. ");
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsDescription, entry.Description);
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsDescription} {entry.Description}'. ");
    
                // Skipping broken resources
                if (string.IsNullOrWhiteSpace(entry.ApplicationProfile.Uri))
                {
                    continue;
                }
    
                AssertToGraphUriNode(graph, resourceGraphName, RdfUris.DcTermsConformsTo, new Uri(entry.ApplicationProfile.Uri));
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsConformsTo} {entry.ApplicationProfile}'. ");
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.CoscineTermsResourceFixedValues, JsonConvert.SerializeObject(entry.FixedValues));
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceFixedValues} {JsonConvert.SerializeObject(entry.FixedValues)}'. ");
    
                if (entry.Creator is not null)
                {
                    AssertToGraphUriNode(graph, resourceGraphName, RdfUris.DcTermsCreator, UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Creator.Id));
                    Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsCreator} {UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Creator.Id)}'. ");
                }
    
                AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.CoscineTermsResourceArchived, entry.Archived.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean"));
                Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.CoscineTermsResourceArchived} {entry.Archived}'. ");
    
                AssertToGraphUriNode(graph, resourceGraphName, RdfUris.FoafHomepage, resourceHandleName);
                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 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)
                {
                    if (entry.Id == projectResource.ResourceId)
                    {
                        var blankNode = graph.CreateBlankNode();
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.A, RdfUris.AclAuthorizationClass);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.A} {RdfUris.AclAuthorizationClass}'. ");
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclAgentGroup, UriHelper.TryCombineUri(RdfUris.CoscineProjects, projectResource.ProjectId));
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclAgentGroup} {UriHelper.TryCombineUri(RdfUris.CoscineProjects, projectResource.ProjectId)}'. ");
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclAccessTo, resourceGraphName);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclAccessTo} {resourceGraphName}'. ");
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclDefault, resourceGraphName);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclDefault} {resourceGraphName}'. ");
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclMode, RdfUris.AclReadClass);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclMode} {RdfUris.AclReadClass}'. ");
    
                        AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclMode, RdfUris.AclWriteClass);
                        Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclMode} {RdfUris.AclWriteClass}'. ");
                    }
                }
    
                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 resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsCreated} {entry.DateCreated}'. ");
                }
    
                graphs.Add(graph);
            }
    
            return await Task.FromResult(graphs);
        }
    }