Skip to content
Snippets Groups Projects
Select Git revision
  • 30f3afc7dc494c7f542af663786099c74b50b1b4
  • 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

RoleStructuralData.cs

  • Petar Hristov's avatar
    Petar Hristov authored and L. Ellenbeck committed
    d2b59b64
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    RoleStructuralData.cs 3.67 KiB
    using Coscine.ApiClient;
    using Coscine.ApiClient.Core.Api;
    using Coscine.ApiClient.Core.Model;
    using SQL2Linked.Utils;
    using VDS.RDF;
    using VDS.RDF.Parsing;
    
    namespace SQL2Linked.Implementations;
    
    /// <summary>
    /// Class responsible for converting role data into linked data graphs.
    /// It retrieves role 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 RoleStructuralData : StructuralData<RoleDto>
    {
        /// <summary>
        /// Asynchronously retrieves all role data.
        /// </summary>
        /// <returns>A <see cref="Task"/> that represents the asynchronous operation and returns a collection of <see cref="RoleDto"/>.</returns>
        public override async Task<IEnumerable<RoleDto>> GetAll()
        {
            var roleApi = new RoleApi(_apiConfiguration);
            return await RequestUtil.WrapPagedRequest<RoleDtoPagedResponse, RoleDto>(
                (currentPage) => roleApi.GetRolesAsync(pageNumber: currentPage)
            );
        }
    
        /// <summary>
        /// Converts a collection of role data entries into a set of RDF graphs.
        /// Each role is transformed into a graph, with RDF triples representing various properties of the role.
        /// </summary>
        /// <param name="entries">A collection of <see cref="RoleDto"/> instances representing role data.</param>
        /// <returns>A collection of <see cref="IGraph"/> instances, each representing an RDF graph of a role.</returns>
        public override async Task<IEnumerable<IGraph>> ConvertToLinkedDataAsync(IEnumerable<RoleDto> entries)
        {
            var graphs = new List<IGraph>();
    
            foreach (var entry in entries)
            {
                var roleGraphName = UriHelper.TryCombineUri(RdfUris.CoscineRoles, entry.Id)
                    ?? throw new Exception("Could not combine role prefix with role ID");
                var response = await _adminApi.GetMetadataGraphAsync(roleGraphName.AbsoluteUri, RdfFormat.TextTurtle);
    
                var graph = new Graph()
                {
                    BaseUri = roleGraphName
                };
    
                graph.LoadFromString(response.Data.Content, new TurtleParser());
    
                // check if a triple with a org:role already exists in the role graph
                var getTriplesOrgRole = graph.GetTriplesWithObject(RdfUris.OrgRoleClass);
    
                if (!getTriplesOrgRole.Any())
                {
                    AssertToGraphUriNode(graph, roleGraphName, RdfUris.A, RdfUris.OrgRoleClass);
                    Console.WriteLine($"For role '{entry.DisplayName}' will migrate triple '{graph.BaseUri} {RdfUris.A} {RdfUris.OrgRoleClass}'. ");
                }
                else
                {
                    Console.WriteLine($"For role '{entry.DisplayName}' will NOT migrate triple '{graph.BaseUri} {RdfUris.A} {RdfUris.OrgRoleClass}'. ");
                }
    
                // check if a triple with dcterms:title '{entry.DisplayName}' already exists in the role graph
                var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(RdfUris.DcTermsTitle);
    
                if (!getTriplesDctermsTitle.Any())
                {
                    AssertToGraphLiteralNode(graph, roleGraphName, RdfUris.DcTermsTitle, entry.DisplayName);
                    Console.WriteLine($"For role '{entry.DisplayName}' will migrate triple '{graph.BaseUri} {RdfUris.DcTermsTitle} {entry.DisplayName}'. ");
                }
                else
                {
                    Console.WriteLine($"For role '{entry.DisplayName}' will NOT migrate triple '{graph.BaseUri} {RdfUris.DcTermsTitle} {entry.DisplayName}'. ");
                }
                if (!getTriplesOrgRole.Any() || !getTriplesDctermsTitle.Any())
                {
                    graphs.Add(graph);
                }
            }
            return graphs;
        }
    }