Skip to content
Snippets Groups Projects
Commit a091f3f0 authored by Petar Hristov's avatar Petar Hristov :speech_balloon:
Browse files

WIP: Migrated to use RdfUris

parent 6195ff90
No related branches found
No related tags found
2 merge requests!29Fix: Working new version,!27BREAKING: Migrated SQL2Linked to the APIv2 infrastructure
Pipeline #1189945 failed
using Coscine.ApiClient;
using Coscine.ApiClient.Core.Model;
using Microsoft.Extensions.Configuration;
using SQL2Linked.Utils;
using VDS.RDF;
namespace SQL2Linked.Implementations;
public class ProjectStructuralData(IConfiguration configuration) : StructuralData<ProjectAdminDto>
/// <summary>
/// Class responsible for converting project data into linked data graphs.
/// It retrieves project 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 ProjectStructuralData : StructuralData<ProjectAdminDto>
{
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 foaf = new("http://xmlns.com/foaf/0.1/");
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/");
// Override to also receive deleted projects
/// <summary>
/// Asynchronously retrieves all project data, including deleted projects.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the asynchronous operation and returns a collection of <see cref="ProjectAdminDto"/>.</returns>
/// <remarks>This override allows for the inclusion of deleted projects.</remarks>
public override async Task<IEnumerable<ProjectAdminDto>> GetAll()
{
return await RequestUtil.WrapPagedRequest<ProjectAdminDtoPagedResponse, ProjectAdminDto>(
(currentPage) => _adminApi.GetAllProjectsAsync(includeDeleted: true, pageNumber: currentPage, pageSize: 150));
(currentPage) => _adminApi.GetAllProjectsAsync(includeDeleted: true, pageNumber: currentPage, pageSize: 250)
);
}
/// <summary>
/// Converts a collection of project data entries into a set of RDF graphs.
/// Each project is transformed into a graph, with RDF triples representing various properties of the project.
/// </summary>
/// <param name="entries">A collection of <see cref="ProjectAdminDto"/> instances representing project data.</param>
/// <returns>A collection of <see cref="IGraph"/> instances, each representing an RDF graph of a project.</returns>
public override IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<ProjectAdminDto> entries)
{
var graphs = new List<IGraph>();
var projectUrlHandlePrefix = "https://hdl.handle.net/" + _pidConfiguration?.Prefix;
var projectUrlPrefix = "https://purl.org/coscine/projects";
var resourceUrlPrefix = "https://purl.org/coscine/resources";
var coscineHandlePrefix = UriHelper.TryCombineUri(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
?? throw new Exception("Could not combine handle prefix with PID prefix");
var coscineGraph = new Graph
{
BaseUri = cosc
BaseUri = RdfUris.CoscinePrefix
};
AssertToGraphUriNode(coscineGraph, cosc.AbsoluteUri, dcat + "catalog", projectUrlPrefix + "/");
AssertToGraphUriNode(coscineGraph, cosc.AbsoluteUri, dcat + "catalog", resourceUrlPrefix + "/");
AssertToGraphUriNode(coscineGraph, RdfUris.CoscinePrefix, RdfUris.DcatCatalog, RdfUris.CoscineProjects);
AssertToGraphUriNode(coscineGraph, RdfUris.CoscinePrefix, RdfUris.DcatCatalog, RdfUris.CoscineResources);
graphs.Add(coscineGraph);
foreach (var entry in entries)
{
var projectGraphName = $"{projectUrlPrefix}/{entry.Id}";
var projectHandleName = $"{projectUrlHandlePrefix}/{entry.Id}";
var projectGraphName = UriHelper.TryCombineUri(RdfUris.CoscineProjects, entry.Id)
?? throw new Exception("Could not combine projects prefix with project ID");
var projectHandleName = UriHelper.TryCombineUri(coscineHandlePrefix, entry.Id);
var graph = new Graph
{
BaseUri = new Uri(projectGraphName)
BaseUri = projectGraphName
};
AssertToGraphUriNode(graph, projectGraphName, rdf + "type", dcat + "Catalog");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {rdf}type {dcat}Catalog'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.A, RdfUris.DcatCatalogClass);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.A} {RdfUris.DcatCatalogClass}'. ");
AssertToGraphUriNode(graph, projectGraphName, rdf + "type", org + "OrganizationalCollaboration");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {rdf}type {org}OrganizationalCollaboration'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.A, RdfUris.OrgOrganizationalCollaborationClass);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.A} {RdfUris.OrgOrganizationalCollaborationClass}'. ");
AssertToGraphUriNode(graph, projectGraphName, rdf + "type", vcard + "Group");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {rdf}type {vcard}Group'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.A, RdfUris.VcardGroupClass);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.A} {RdfUris.VcardGroupClass}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "title", entry.Name);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}title {entry.Name}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsTitle, entry.Name);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsTitle} {entry.Name}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "description", entry.Description);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}description {entry.Description}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsDescription, entry.Description);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsDescription} {entry.Description}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, dcat + "startDate", entry.StartDate.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcat}startDate {entry.StartDate}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsStartDate, entry.StartDate.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsStartDate} {entry.StartDate}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, dcat + "endDate", entry.EndDate.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcat}endDate {entry.EndDate}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsEndDate, entry.EndDate.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsEndDate} {entry.EndDate}'. ");
if (entry.Keywords.Count > 0)
{
foreach (var keyword in entry.Keywords)
{
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "subject", keyword);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}subject {keyword}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsSubject, keyword);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsSubject} {keyword}'. ");
}
}
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "alternative", entry.DisplayName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}alternative {entry.DisplayName}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsAlternative, entry.DisplayName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsAlternative} {entry.DisplayName}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "rightsHolder", entry.PrincipleInvestigators);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}rightsHolder {entry.PrincipleInvestigators}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsRightsHolder, entry.PrincipleInvestigators);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsRightsHolder} {entry.PrincipleInvestigators}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, "https://schema.org/funding", entry.GrantId);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} https://schema.org/funding {entry.GrantId}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.SchemaFunding, entry.GrantId);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.SchemaFunding} {entry.GrantId}'. ");
if (entry.Visibility.DisplayName.Contains("Public"))
{
AssertToGraphUriNode(graph, projectGraphName, cosc + "terms/project#visibility", cosc + $"terms/visibility#public");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {cosc}terms/project#visibility {cosc}terms/visibility#public'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.CoscineTermsProjectVisibility, RdfUris.CoscineTermsVisibilityPublic);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.CoscineTermsProjectVisibility} {RdfUris.CoscineTermsVisibilityPublic}'. ");
}
else if (entry.Visibility.DisplayName.Contains("Project Members"))
{
AssertToGraphUriNode(graph, projectGraphName, cosc + "terms/project#visibility", cosc + $"terms/visibility#projectMember");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {cosc}terms/project#visibility {cosc}terms/visibility#projectMember'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.CoscineTermsProjectVisibility, RdfUris.CoscineTermsVisibilityProjectMember);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.CoscineTermsProjectVisibility} {RdfUris.CoscineTermsVisibilityProjectMember}'. ");
}
AssertToGraphLiteralNode(graph, projectGraphName, cosc + "terms/project#deleted", entry.Deleted.ToString().ToLower(), new Uri("http://www.w3.org/2001/XMLSchema#boolean"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {cosc}terms/project#deleted {entry.Deleted}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.CoscineTermsProjectDeleted, entry.Deleted.ToString().ToLower(), RdfUris.XsdBoolean);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.CoscineTermsProjectDeleted} {entry.Deleted}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, cosc + "terms/project#slug", entry.Slug);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {cosc}terms/project#slug {entry.Slug}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.CoscineTermsProjectSlug, entry.Slug);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.CoscineTermsProjectSlug} {entry.Slug}'. ");
AssertToGraphUriNode(graph, projectGraphName, foaf + "homepage", projectHandleName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {foaf}homepage {projectHandleName}'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.FoafHomepage, projectHandleName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.FoafHomepage} {projectHandleName}'. ");
foreach (var projectRole in entry.ProjectRoles)
{
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, RdfUris.VcardHasMember, UriHelper.TryCombineUri(RdfUris.CoscineUsers, projectRole.UserId));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.VcardHasMember} {UriHelper.TryCombineUri(RdfUris.CoscineUsers, projectRole.UserId)}'. ");
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, RdfUris.A, RdfUris.OrgMembershipClass);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.A} {RdfUris.OrgMembershipClass}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, org + "organization", projectGraphName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {org}organization {projectGraphName}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.OrgOrganization, projectGraphName);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.OrgOrganization} {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, RdfUris.OrgRole, UriHelper.TryCombineUri(RdfUris.CoscineRoles, projectRole.RoleId));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.OrgRole} {UriHelper.TryCombineUri(RdfUris.CoscineRoles, 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, RdfUris.OrgMember, UriHelper.TryCombineUri(RdfUris.CoscineUsers, projectRole.UserId));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.OrgMember} {UriHelper.TryCombineUri(RdfUris.CoscineUsers, projectRole.UserId)}'. ");
}
foreach (var projectResource in entry.ProjectResources)
{
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, RdfUris.DcatCatalog, UriHelper.TryCombineUri(RdfUris.CoscineResources, projectResource.ResourceId));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcatCatalog} {UriHelper.TryCombineUri(RdfUris.CoscineResources, projectResource.ResourceId)}'. ");
}
foreach (var projectInstitute in entry.Organizations)
{
AssertToGraphUriNode(graph, projectGraphName, org + "organization", projectInstitute.Uri);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {org}organization {projectInstitute.Uri}'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.OrgOrganization, new Uri(projectInstitute.Uri));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.OrgOrganization} {projectInstitute.Uri}'. ");
}
if (entry.Creator is not null)
{
AssertToGraphUriNode(graph, projectGraphName, dcterms + "creator", cosc + $"users/{entry.Creator}");
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}creator {cosc}users/{entry.Creator}'. ");
AssertToGraphUriNode(graph, projectGraphName, RdfUris.DcTermsCreator, UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Creator.Id));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsCreator} {UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Creator.Id)}'. ");
}
if (entry.CreationDate is not null && entry.CreationDate.HasValue)
{
AssertToGraphLiteralNode(graph, projectGraphName, dcterms + "created", entry.CreationDate.Value.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {dcterms}created {entry.CreationDate}'. ");
AssertToGraphLiteralNode(graph, projectGraphName, RdfUris.DcTermsCreated, entry.CreationDate.Value.ToString(), RdfUris.XsdDateTime);
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{projectGraphName} {RdfUris.DcTermsCreated} {entry.CreationDate}'. ");
}
graphs.Add(graph);
......
......
using Coscine.ApiClient;
using Coscine.ApiClient.Core.Model;
using Microsoft.Extensions.Configuration;
using Newtonsoft.Json;
using SQL2Linked.Utils;
using VDS.RDF;
namespace SQL2Linked.Implementations;
public class ResourceStructuralData(IConfiguration configuration) : StructuralData<ResourceAdminDto>
/// <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>
{
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/");
// Override to also receive deleted resources
/// <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: 150));
(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 IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<ResourceAdminDto> entries)
{
var graphs = new List<IGraph>();
var resourceUrlHandlePrefix = "https://hdl.handle.net/" + _pidConfiguration?.Prefix;
var projectUrlPrefix = "https://purl.org/coscine/projects";
var resourceUrlPrefix = "https://purl.org/coscine/resources";
var coscineHandlePrefix = UriHelper.TryCombineUri(RdfUris.HandlePrefix, _pidConfiguration.Prefix)
?? throw new Exception("Could not combine handle prefix with PID prefix");
foreach (var entry in entries)
{
var resourceGraphName = $"{resourceUrlPrefix}/{entry.Id}";
var resourceHandleName = $"{resourceUrlHandlePrefix}/{entry.Id}";
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();
graph.BaseUri = new Uri(resourceGraphName);
var graph = new Graph
{
BaseUri = 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, RdfUris.A, RdfUris.DcatCatalogClass);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.A} {RdfUris.DcatCatalogClass}'. ");
AssertToGraphUriNode(graph, resourceGraphName, rdf + "type", pim + "Storage");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {rdf}type {pim}Storage'. ");
AssertToGraphUriNode(graph, resourceGraphName, RdfUris.A, RdfUris.PimStorageClass);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.A} {RdfUris.PimStorageClass}'. ");
AssertToGraphUriNode(graph, resourceGraphName, dcat + "service", cosc + $"resourcetypes/{entry.Type.Id}");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcat}service {cosc}resourcetypes/{entry.Type.Id}'. ");
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, dcterms + "title", entry.Name);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}title {entry.Name}'. ");
AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsTitle, entry.Name);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsTitle} {entry.Name}'. ");
AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "alternative", entry.DisplayName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}alternative {entry.DisplayName}'. ");
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, 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'. ");
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, 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'. ");
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, dcterms + "license", entry.License.DisplayName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}license {entry.License.DisplayName}'. ");
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, dcterms + "subject", keyword);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}subject {keyword}'. ");
AssertToGraphLiteralNode(graph, resourceGraphName, RdfUris.DcTermsSubject, keyword);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsSubject} {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, RdfUris.DcTermsRights, entry.UsageRights);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsRights} {entry.UsageRights}'. ");
AssertToGraphLiteralNode(graph, resourceGraphName, dcterms + "description", entry.Description);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}description {entry.Description}'. ");
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))
......@@ -92,26 +101,26 @@ public class ResourceStructuralData(IConfiguration configuration) : StructuralDa
continue;
}
AssertToGraphUriNode(graph, resourceGraphName, dcterms + "conformsTo", entry.ApplicationProfile.Uri);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}conformsTo {entry.ApplicationProfile}'. ");
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, cosc + "terms/resource#fixedValues", JsonConvert.SerializeObject(entry.FixedValues));
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {cosc}terms/resource#fixedValues {JsonConvert.SerializeObject(entry.FixedValues)}'. ");
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, dcterms + "creator", cosc + $"users/{entry.Creator}");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}creator {cosc}users/{entry.Creator}'. ");
AssertToGraphUriNode(graph, resourceGraphName, RdfUris.DcTermsCreator, UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Creator));
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.DcTermsCreator} {UriHelper.TryCombineUri(RdfUris.CoscineUsers, 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}'. ");
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, foaf + "homepage", resourceHandleName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {foaf}homepage {resourceGraphName}'. ");
AssertToGraphUriNode(graph, resourceGraphName, RdfUris.FoafHomepage, resourceHandleName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{resourceGraphName} {RdfUris.FoafHomepage} {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}'. ");
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}'. ");
foreach (var projectResource in entry.ProjectResources)
{
......@@ -119,30 +128,30 @@ public class ResourceStructuralData(IConfiguration configuration) : StructuralDa
{
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, RdfUris.A, RdfUris.AclAuthorizationClass);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.A} {RdfUris.AclAuthorizationClass}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, acl + "agentGroup", $"{projectUrlPrefix}/{projectResource.ProjectId}");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}agentGroup {projectUrlPrefix}/{projectResource.ProjectId}'. ");
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, acl + "accessTo", resourceGraphName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}accessTo {resourceGraphName}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclAccessTo, resourceGraphName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclAccessTo} {resourceGraphName}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, acl + "default", resourceGraphName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}default {resourceGraphName}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclDefault, resourceGraphName);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclDefault} {resourceGraphName}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, acl + "mode", acl + "Read");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}accessTo {acl}Read'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, RdfUris.AclMode, RdfUris.AclReadClass);
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {RdfUris.AclMode} {RdfUris.AclReadClass}'. ");
AssertToGraphBlankAndUriNode(graph, blankNode, acl + "mode", acl + "Write");
Console.WriteLine($"For resource '{entry.DisplayName}' will migrate triple '{blankNode} {acl}accessTo {acl}Write'. ");
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, dcterms + "created", entry.DateCreated.Value.ToString(), new Uri("http://www.w3.org/2001/XMLSchema#dateTime"));
Console.WriteLine($"For project '{entry.DisplayName}' will migrate triple '{resourceGraphName} {dcterms}created {entry.DateCreated}'. ");
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}'. ");
}
graphs.Add(graph);
......
......
using Coscine.ApiClient.Core.Api;
using Coscine.ApiClient.Core.Model;
using Microsoft.Extensions.Configuration;
using SQL2Linked.Utils;
using VDS.RDF;
using VDS.RDF.Parsing;
namespace SQL2Linked.Implementations;
public class ResourceTypeStructuralData(IConfiguration configuration) : StructuralData<ResourceTypeInformationDto>
/// <summary>
/// Class responsible for converting resource type data into linked data graphs.
/// It retrieves resource type 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 ResourceTypeStructuralData : StructuralData<ResourceTypeInformationDto>
{
public readonly string ResourceTypeUrlPrefix = "https://purl.org/coscine/resourcetypes";
public readonly Uri rdf = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
public readonly Uri dcat = new("http://www.w3.org/ns/dcat#");
public readonly Uri dcterms = new("http://purl.org/dc/terms/");
// Override to only transform users which have accepted the TOS
/// <summary>
/// Asynchronously retrieves all resource type data.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the asynchronous operation and returns a collection of <see cref="ResourceTypeInformationDto"/>.</returns>
public override async Task<IEnumerable<ResourceTypeInformationDto>> GetAll()
{
var resourceTypeApi = new ResourceTypeApi();
......@@ -22,58 +24,67 @@ public class ResourceTypeStructuralData(IConfiguration configuration) : Structur
return resourceTypeInformationsResponse.Data;
}
/// <summary>
/// Converts a collection of resource type data entries into a set of RDF graphs.
/// Each resource type is transformed into a graph, with RDF triples representing various properties of the resource type.
/// </summary>
/// <param name="entries">A collection of <see cref="ResourceTypeInformationDto"/> instances representing resource type data.</param>
/// <returns>A collection of <see cref="IGraph"/> instances, each representing an RDF graph of a resource type.</returns>
public override IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<ResourceTypeInformationDto> entries)
{
var graphs = new List<IGraph>();
foreach (var entry in entries)
{
var resourceTypeGraphName = $"{ResourceTypeUrlPrefix}/{entry.Id}";
var response = _adminApi.GetMetadataGraph(resourceTypeGraphName, RdfFormat.TextTurtle);
var resourceTypeGraphName = UriHelper.TryCombineUri(RdfUris.CoscineResourceTypes, entry.Id)
?? throw new Exception("Could not combine resource types prefix with resource type ID");
var response = _adminApi.GetMetadataGraph(resourceTypeGraphName.AbsoluteUri, RdfFormat.TextTurtle);
var graph = new Graph()
{
BaseUri = new Uri(resourceTypeGraphName)
BaseUri = resourceTypeGraphName
};
graph.LoadFromString(response.Data.Content, new TurtleParser());
// check if a triple with a dcat:DataService already exists in the resourcetype graph
var getTriplesDcatDataService = graph.GetTriplesWithObject(new Uri(dcat + "DataService"));
var getTriplesDcatDataService = graph.GetTriplesWithObject(RdfUris.DcatDataServiceClass);
if (!getTriplesDcatDataService.Any())
{
AssertToGraphUriNode(graph, resourceTypeGraphName, rdf + "type", dcat + "DataService");
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {rdf}type {dcat}DataService'. ");
AssertToGraphUriNode(graph, resourceTypeGraphName, RdfUris.A, RdfUris.DcatDataServiceClass);
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {RdfUris.A} {RdfUris.DcatDataServiceClass}'. ");
}
else
{
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {rdf}type {dcat}DataService'. ");
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {RdfUris.A} {RdfUris.DcatDataServiceClass}'. ");
}
// check if a triple with dcterms:title '{entry.DisplayName}' already exists in the role graph
var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(new Uri(dcterms + "title"));
var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(RdfUris.DcTermsTitle);
if (!getTriplesDctermsTitle.Any())
{
AssertToGraphLiteralNode(graph, resourceTypeGraphName, dcterms + "title", entry.SpecificType);
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {dcterms}title {entry.SpecificType}'. ");
AssertToGraphLiteralNode(graph, resourceTypeGraphName, RdfUris.DcTermsTitle, entry.SpecificType);
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {RdfUris.DcTermsTitle} {entry.SpecificType}'. ");
}
else
{
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {dcterms}title {entry.SpecificType}'. ");
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {RdfUris.DcTermsTitle} {entry.SpecificType}'. ");
}
// check if a triple with dcterms:title '{entry.DisplayName}' already exists in the role graph
var getTriplesDctermsAlternative = graph.GetTriplesWithPredicate(new Uri(dcterms + "alternative"));
var getTriplesDctermsAlternative = graph.GetTriplesWithPredicate(RdfUris.DcTermsAlternative);
if (!getTriplesDctermsAlternative.Any())
{
AssertToGraphLiteralNode(graph, resourceTypeGraphName, dcterms + "alternative", entry.GeneralType);
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {dcterms}alternative {entry.GeneralType}'. ");
AssertToGraphLiteralNode(graph, resourceTypeGraphName, RdfUris.DcTermsAlternative, entry.GeneralType);
Console.WriteLine($"For resource type '{entry.SpecificType}' will migrate triple '{graph.BaseUri} {RdfUris.DcTermsAlternative} {entry.GeneralType}'. ");
}
else
{
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {dcterms}alternative {entry.GeneralType}'. ");
Console.WriteLine($"For resource type '{entry.SpecificType}' will NOT migrate triple '{graph.BaseUri} {RdfUris.DcTermsAlternative} {entry.GeneralType}'. ");
}
if (!getTriplesDcatDataService.Any() || !getTriplesDctermsTitle.Any())
......
......
using Coscine.ApiClient;
using Coscine.ApiClient.Core.Api;
using Coscine.ApiClient.Core.Model;
using Microsoft.Extensions.Configuration;
using SQL2Linked.Utils;
using VDS.RDF;
using VDS.RDF.Parsing;
namespace SQL2Linked.Implementations;
public class RoleStructuralData(IConfiguration configuration) : StructuralData<RoleDto>
/// <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>
{
public readonly string RoleUrlPrefix = "https://purl.org/coscine/roles";
public readonly Uri cosc = new("https://purl.org/coscine/");
public readonly Uri rdf = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
public readonly Uri org = new("http://www.w3.org/ns/org#");
public readonly Uri dcterms = new("http://purl.org/dc/terms/");
// Override to only transform users which have accepted the TOS
/// <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();
return await RequestUtil.WrapPagedRequest<RoleDtoPagedResponse, RoleDto>(
(currentPage) => roleApi.GetRolesAsync(pageNumber: currentPage));
(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 IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<RoleDto> entries)
{
var graphs = new List<IGraph>();
foreach (var entry in entries)
{
var roleGraphName = $"{RoleUrlPrefix}/{entry.Id}";
var response = _adminApi.GetMetadataGraph(roleGraphName, RdfFormat.TextTurtle);
var roleGraphName = UriHelper.TryCombineUri(RdfUris.CoscineRoles, entry.Id)
?? throw new Exception("Could not combine role prefix with role ID");
var response = _adminApi.GetMetadataGraph(roleGraphName.AbsoluteUri, RdfFormat.TextTurtle);
var graph = new Graph()
{
BaseUri = new Uri(roleGraphName)
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(new Uri(org + "Role"));
var getTriplesOrgRole = graph.GetTriplesWithObject(RdfUris.OrgRoleClass);
if (!getTriplesOrgRole.Any())
{
AssertToGraphUriNode(graph, roleGraphName, rdf + "type", org + "Role");
Console.WriteLine($"For role '{entry.DisplayName}' will migrate triple '{graph.BaseUri} {rdf}type {org}Role'. ");
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} {rdf}type {org}Role'. ");
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(new Uri(dcterms + "title"));
var getTriplesDctermsTitle = graph.GetTriplesWithPredicate(RdfUris.DcTermsTitle);
if (!getTriplesDctermsTitle.Any())
{
AssertToGraphLiteralNode(graph, roleGraphName, dcterms + "title", entry.DisplayName);
Console.WriteLine($"For role '{entry.DisplayName}' will migrate triple '{graph.BaseUri} {dcterms}title {entry.DisplayName}'. ");
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} {dcterms}title {entry.DisplayName}'. ");
Console.WriteLine($"For role '{entry.DisplayName}' will NOT migrate triple '{graph.BaseUri} {RdfUris.DcTermsTitle} {entry.DisplayName}'. ");
}
if (!getTriplesOrgRole.Any() || !getTriplesDctermsTitle.Any())
{
......
......
using Coscine.ApiClient;
using Coscine.ApiClient.Core.Model;
using Microsoft.Extensions.Configuration;
using SQL2Linked.Utils;
using VDS.RDF;
using VDS.RDF.Parsing;
namespace SQL2Linked.Implementations;
public class UserStructuralData(IConfiguration configuration) : StructuralData<UserDto>
/// <summary>
/// Class responsible for converting user data into linked data graphs.
/// It retrieves user 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 UserStructuralData : StructuralData<UserDto>
{
public readonly string UserUrlPrefix = "https://purl.org/coscine/users";
public readonly Uri rdf = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
public readonly Uri foaf = new("http://xmlns.com/foaf/0.1/");
// Override to only transform users which have accepted the TOS
/// <summary>
/// Asynchronously retrieves all user data only for users who have accepted the ToS.
/// </summary>
/// <returns>A <see cref="Task"/> that represents the asynchronous operation and returns a collection of <see cref="UserDto"/>.</returns>
/// <remarks>This override allows for the retrieval of users who have accepted the ToS.</remarks>
public override async Task<IEnumerable<UserDto>> GetAll()
{
return await RequestUtil.WrapPagedRequest<UserDtoPagedResponse, UserDto>(
(currentPage) => _adminApi.GetAllUsersAsync(tosAccepted: true, pageNumber: currentPage, pageSize: 150));
(currentPage) => _adminApi.GetAllUsersAsync(tosAccepted: true, pageNumber: currentPage, pageSize: 250)
);
}
/// <summary>
/// Converts a collection of user data entries into a set of RDF graphs.
/// Each user is transformed into a graph, with RDF triples representing various properties of the user.
/// </summary>
/// <param name="entries">A collection of <see cref="UserDto"/> instances representing user data.</param>
/// <returns>A collection of <see cref="IGraph"/> instances, each representing an RDF graph of a user.</returns>
public override IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<UserDto> entries)
{
var graphs = new List<IGraph>();
foreach (var entry in entries)
{
var userGraphName = $"{UserUrlPrefix}/{entry.Id}";
var response = _adminApi.GetMetadataGraph(userGraphName, RdfFormat.TextTurtle);
var userGraphName = UriHelper.TryCombineUri(RdfUris.CoscineUsers, entry.Id)
?? throw new Exception("Could not combine users prefix with user ID");
var response = _adminApi.GetMetadataGraph(userGraphName.AbsoluteUri, RdfFormat.TextTurtle);
var graph = new Graph()
{
BaseUri = new Uri(userGraphName)
BaseUri = userGraphName
};
graph.LoadFromString(response.Data.Content, new TurtleParser());
// check if a triple with a foaf:Person already exists in the user graph
var getTriples = graph.GetTriplesWithObject(new Uri(foaf + "Person"));
var getTriples = graph.GetTriplesWithObject(RdfUris.FoafPersonClass);
// check if the current display name is already applied in the user graph
var getTriplesName = graph.GetTriplesWithPredicate(new Uri(foaf + "name"));
var getTriplesName = graph.GetTriplesWithPredicate(RdfUris.FoafName);
if (!getTriples.Any() || !getTriplesName.Any((triple) => (triple.Object as ILiteralNode)?.Value == entry.DisplayName))
{
AssertToGraphUriNode(graph, userGraphName, rdf + "type", foaf + "Person");
AssertToGraphUriNode(graph, userGraphName, RdfUris.A, RdfUris.FoafPersonClass);
Console.WriteLine($"For user '{entry.DisplayName}' will migrate triple '{userGraphName} {RdfUris.A} {RdfUris.FoafPersonClass}'. ");
graph.Retract(getTriplesName);
AssertToGraphLiteralNode(graph, userGraphName, foaf + "name", entry.DisplayName);
AssertToGraphLiteralNode(graph, userGraphName, RdfUris.FoafName, entry.DisplayName);
graphs.Add(graph);
......
......
namespace Coscine.KpiGenerator.Models.ConfigurationModels;
namespace SQL2Linked.Models.ConfigurationModels;
/// <summary>
/// Represents the configuration settings used in the application.
......
......
using Coscine.KpiGenerator.Models.ConfigurationModels;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using SQL2Linked.Implementations;
using SQL2Linked.Models.ConfigurationModels;
using Winton.Extensions.Configuration.Consul;
namespace SQL2Linked;
......
......
......@@ -23,8 +23,4 @@
<PackageReference Include="NLog" Version="5.2.7" />
</ItemGroup>
<ItemGroup>
<Folder Include="Utils\" />
</ItemGroup>
</Project>
......@@ -10,7 +10,7 @@ namespace SQL2Linked;
public abstract class StructuralData<S>
{
public static string AdminToken { get; set; } = "";
private readonly string _adminToken;
protected readonly AdminApi _adminApi;
protected readonly PidConfiguration _pidConfiguration; // Comes from the API Client, not from the application's own configuration
......@@ -26,11 +26,11 @@ public abstract class StructuralData<S>
// Generate an admin token for the API Client
var jwtConfiguration = ApiConfigurationUtil.RetrieveJwtConfiguration();
AdminToken = ApiConfigurationUtil.GenerateAdminToken(jwtConfiguration);
_adminToken = ApiConfigurationUtil.GenerateAdminToken(jwtConfiguration);
_adminApi = new AdminApi(new Configuration()
{
ApiKeyPrefix = { { "Authorization", "Bearer" } },
ApiKey = { { "Authorization", AdminToken } },
ApiKey = { { "Authorization", _adminToken } },
});
}
......@@ -104,43 +104,43 @@ public abstract class StructuralData<S>
}
}
public void AssertToGraphUriNode(IGraph graph, string graphSubject, string graphPredicate, string? graphObject)
public void AssertToGraphUriNode(IGraph graph, Uri graphSubject, Uri graphPredicate, Uri? graphObject)
{
if (graphObject != null)
{
graph.Assert(
new Triple(
graph.CreateUriNode(new Uri(graphSubject)),
graph.CreateUriNode(new Uri(graphPredicate)),
graph.CreateUriNode(new Uri(graphObject))
graph.CreateUriNode(graphSubject),
graph.CreateUriNode(graphPredicate),
graph.CreateUriNode(graphObject)
)
);
}
}
public void AssertToGraphLiteralNode(IGraph graph, string graphSubject, string graphPredicate, string? graphObject, Uri? objectType = null)
public void AssertToGraphLiteralNode(IGraph graph, Uri graphSubject, Uri graphPredicate, string? graphObject, Uri? objectType = null)
{
if (graphObject != null)
{
graph.Assert(
new Triple(
graph.CreateUriNode(new Uri(graphSubject)),
graph.CreateUriNode(new Uri(graphPredicate)),
graph.CreateUriNode(graphSubject),
graph.CreateUriNode(graphPredicate),
objectType is not null ? graph.CreateLiteralNode(graphObject, objectType) : graph.CreateLiteralNode(graphObject)
)
);
}
}
public void AssertToGraphBlankAndUriNode(IGraph graph, IBlankNode graphSubject, string graphPredicate, string? graphObject)
public void AssertToGraphBlankAndUriNode(IGraph graph, IBlankNode graphSubject, Uri graphPredicate, Uri? graphObject)
{
if (graphObject != null)
{
graph.Assert(
new Triple(
graphSubject,
graph.CreateUriNode(new Uri(graphPredicate)),
graph.CreateUriNode(new Uri(graphObject))
graph.CreateUriNode(graphPredicate),
graph.CreateUriNode(graphObject)
)
);
}
......
......
// Ignore Spelling: Foaf Dcat Dcterms Ebocore Fdp Ldp Xsd
/// ------------------
/// TAKEN FROM THE API
/// ------------------
namespace SQL2Linked.Utils;
/// <summary>
/// Contains URIs relevant to RDF (Resource Description Framework) and related schemas/ontologies.
/// </summary>
public static class RdfUris
{
// ACL
public static readonly Uri AclPrefix = new("http://www.w3.org/ns/auth/acl#");
public static readonly Uri AclAccessTo = new("http://www.w3.org/ns/auth/acl#accessTo");
public static readonly Uri AclAgentGroup = new("http://www.w3.org/ns/auth/acl#agentGroup");
public static readonly Uri AclAuthorizationClass = new("http://www.w3.org/ns/auth/acl#Authorization");
public static readonly Uri AclDefault = new("http://www.w3.org/ns/auth/acl#default");
public static readonly Uri AclMode = new("http://www.w3.org/ns/auth/acl#mode");
public static readonly Uri AclReadClass = new("http://www.w3.org/ns/auth/acl#Read");
public static readonly Uri AclWriteClass = new("http://www.w3.org/ns/auth/acl#Write");
// Coscine
public static readonly Uri CoscinePrefix = new("https://purl.org/coscine/");
public static readonly Uri CoscineApplicationProfile = new("https://purl.org/coscine/ap/");
public static readonly Uri CoscineFixedValue = new("https://purl.org/coscine/fixedValue");
public static readonly Uri CoscineMetadataExtractionVersion = new("https://purl.org/coscine/terms/metatadataextraction#version");
public static readonly Uri CoscineProjects = new("https://purl.org/coscine/projects/");
public static readonly Uri CoscineResourceTypes = new("https://purl.org/coscine/resourcetypes/");
public static readonly Uri CoscineResources = new("https://purl.org/coscine/resources/");
public static readonly Uri CoscineRoles = new("https://purl.org/coscine/roles/");
public static readonly Uri CoscineUsers = new("https://purl.org/coscine/users/");
[Obsolete($"Beware the side-effects before changing! Use {nameof(CoscineUsers)} instead.")]
public static readonly Uri CoscineUserLegacy = new("https://coscine.rwth-aachen.de/u/");
// Coscine Terms
public static readonly Uri CoscineTermsPrefix = new("https://purl.org/coscine/terms/");
public static readonly Uri CoscineTermsLinkedBody = new("https://purl.org/coscine/terms/linked#body");
public static readonly Uri CoscineTermsMetadataTrackerAgent = new("https://purl.org/coscine/terms/metadatatracker#Agent");
public static readonly Uri CoscineTermsProject = new("https://purl.org/coscine/terms/project#");
public static readonly Uri CoscineTermsProjectDeleted = new("https://purl.org/coscine/terms/project#deleted");
public static readonly Uri CoscineTermsProjectSlug = new("https://purl.org/coscine/terms/project#slug");
public static readonly Uri CoscineTermsProjectVisibility = new("https://purl.org/coscine/terms/project#visibility");
public static readonly Uri CoscineTermsResource = new("https://purl.org/coscine/terms/resource#");
public static readonly Uri CoscineTermsResourceArchived = new("https://purl.org/coscine/terms/resource#archived");
public static readonly Uri CoscineTermsResourceDeleted = new("https://purl.org/coscine/terms/resource#deleted");
public static readonly Uri CoscineTermsResourceFixedValues = new("https://purl.org/coscine/terms/resource#fixedValues");
public static readonly Uri CoscineTermsResourceVisibility = new("https://purl.org/coscine/terms/resource#visibility");
public static readonly Uri CoscineTermsTypes = new("https://purl.org/coscine/terms/types#");
public static readonly Uri CoscineTermsUserAgent = new("https://purl.org/coscine/terms/user#Agent");
public static readonly Uri CoscineTermsVisibility = new("https://purl.org/coscine/terms/visibility#");
public static readonly Uri CoscineTermsVisibilityPublic = new("https://purl.org/coscine/terms/visibility#public");
public static readonly Uri CoscineTermsVisibilityProjectMember = new("https://purl.org/coscine/terms/visibility#projectMember");
public static readonly Uri CoscineTermsRole = new("https://purl.org/coscine/terms/role#");
// CSMD
public static readonly Uri CsmdPrefix = new("http://www.purl.org/net/CSMD/4.0#");
// DCAT
public static readonly Uri DcatPrefix = new("http://www.w3.org/ns/dcat#");
public static readonly Uri DcatCatalog = new("http://www.w3.org/ns/dcat#catalog");
public static readonly Uri DcatCatalogClass = new("http://www.w3.org/ns/dcat#Catalog");
public static readonly Uri DcatDataset = new("http://www.w3.org/ns/dcat#dataset");
public static readonly Uri DcatDatasetClass = new("http://www.w3.org/ns/dcat#Dataset");
public static readonly Uri DcatDataServiceClass = new("http://www.w3.org/ns/dcat#DataService");
public static readonly Uri DcatDistribution = new("http://www.w3.org/ns/dcat#distribution");
public static readonly Uri DcatService = new("http://www.w3.org/ns/dcat#service");
// DCMI Type
public static readonly Uri DcmiTypePrefix = new("http://purl.org/dc/dcmitype/");
// DC
public static readonly Uri DcPrefix = new("http://purl.org/dc/elements/1.1/");
// DC Terms
public static readonly Uri DcTermsPrefix = new("http://purl.org/dc/terms/");
public static readonly Uri DcTermsAlternative = new("http://purl.org/dc/terms/alternative");
public static readonly Uri DcTermsConformsTo = new("http://purl.org/dc/terms/conformsTo");
public static readonly Uri DcTermsCreator = new("http://purl.org/dc/terms/creator");
public static readonly Uri DcTermsCreated = new("http://purl.org/dc/terms/created");
public static readonly Uri DcTermsDescription = new("http://purl.org/dc/terms/description");
public static readonly Uri DcTermsEndDate = new("http://purl.org/dc/terms/endDate");
public static readonly Uri DcTermsIdentifier = new("http://purl.org/dc/terms/identifier");
public static readonly Uri DcTermsIsPartOf = new("http://purl.org/dc/terms/isPartOf");
public static readonly Uri DcTermsLicense = new("http://purl.org/dc/terms/license");
public static readonly Uri DcTermsModified = new("http://purl.org/dc/terms/modified");
public static readonly Uri DcTermsRights = new("http://purl.org/dc/terms/rights");
public static readonly Uri DcTermsRightsHolder = new("http://purl.org/dc/terms/rightsHolder");
public static readonly Uri DcTermsStartDate = new("http://purl.org/dc/terms/startDate");
public static readonly Uri DcTermsSubject = new("http://purl.org/dc/terms/subject");
public static readonly Uri DcTermsTitle = new("http://purl.org/dc/terms/title");
// EBUCORE
public static readonly Uri EbocoreHashFunction = new("http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#hashFunction");
public static readonly Uri EbocoreHashType = new("http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#hashType");
public static readonly Uri EbocoreHashValue = new("http://www.ebu.ch/metadata/ontologies/ebucore/ebucore#hashValue");
// FDP
public static readonly Uri FdpMetadataService = new("http://purl.org/fdp/fdp-o#MetadataService");
public static readonly Uri FdpHasMetadata = new("http://purl.org/fdp/fdp-o#hasMetadata");
public static readonly Uri FdpIsMetadataOf = new("http://purl.org/fdp/fdp-o#isMetadataOf");
// FOAF
public static readonly Uri FoafPrefix = new("http://xmlns.com/foaf/0.1/");
public static readonly Uri FoafHomepage = new("http://xmlns.com/foaf/0.1/homepage");
public static readonly Uri FoafName = new("http://xmlns.com/foaf/0.1/name");
public static readonly Uri FoafPersonClass = new("http://xmlns.com/foaf/0.1/Person");
// Handle
public static readonly Uri HandlePrefix = new("https://hdl.handle.net/");
// LDP
public static readonly Uri LdpBasicContainer = new("http://www.w3.org/ns/ldp#BasicContainer");
public static readonly Uri LdpNonRDFSource = new("http://www.w3.org/ns/ldp#NonRDFSource");
public static readonly Uri LdpRDFSource = new("http://www.w3.org/ns/ldp#RDFSource");
public static readonly Uri LdpDescribedBy = new("http://www.w3.org/ns/ldp#describedBy");
// ORG
public static readonly Uri OrgPrefix = new("http://www.w3.org/ns/org#");
public static readonly Uri OrgMember = new("http://www.w3.org/ns/org#member");
public static readonly Uri OrgMembershipClass = new("http://www.w3.org/ns/org#Membership");
public static readonly Uri OrgOrganization = new("http://www.w3.org/ns/org#organization");
public static readonly Uri OrgOrganizationalCollaborationClass = new("http://www.w3.org/ns/org#OrganizationalCollaboration");
public static readonly Uri OrgRole = new("http://www.w3.org/ns/org#role");
public static readonly Uri OrgRoleClass = new("http://www.w3.org/ns/org#Role");
// OWL
public static readonly Uri OwlImports = new("http://www.w3.org/2002/07/owl#imports");
// PIM
public static readonly Uri PimPrefix = new("http://www.w3.org/ns/pim/space#");
public static readonly Uri PimStorageClass = new("http://www.w3.org/ns/pim/space#Storage");
// PROV
public static readonly Uri ProvEntity = new("http://www.w3.org/ns/prov#Entity");
public static readonly Uri ProvGeneratedAtTime = new("http://www.w3.org/ns/prov#generatedAtTime");
public static readonly Uri ProvWasInvalidatedBy = new("http://www.w3.org/ns/prov#wasInvalidatedBy");
public static readonly Uri ProvWasRevisionOf = new("http://www.w3.org/ns/prov#wasRevisionOf");
// RDF
public static readonly Uri RdfUrlPrefix = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#");
public static readonly Uri A = new("http://www.w3.org/1999/02/22-rdf-syntax-ns#type");
// RDFS
public static readonly Uri RdfsUrlPrefix = new("http://www.w3.org/2000/01/rdf-schema#");
public static readonly Uri SubClassOf = new("http://www.w3.org/2000/01/rdf-schema#subClassOf");
// ROR IDs
/// <summary>
/// Represents the RWTH Aachen's ROR (Research Organization Registry) ID.
/// </summary>
public static readonly Uri RwthRorId = new("https://ror.org/04xfq0f34");
// SCHEMA
public static readonly Uri SchemaPrefix = new("http://schema.org/");
public static readonly Uri SchemaFunding = new("http://schema.org/funding");
// SHACL
public static readonly Uri ShaclPrefix = new("http://www.w3.org/ns/shacl#");
public static readonly Uri ShaclDatatype = new("http://www.w3.org/ns/shacl#datatype");
public static readonly Uri ShaclPath = new("http://www.w3.org/ns/shacl#path");
public static readonly Uri ShaclProperty = new("http://www.w3.org/ns/shacl#property");
public static readonly Uri ShaclTargetClass = new("http://www.w3.org/ns/shacl#targetClass");
public static readonly Uri Class = new("http://www.w3.org/ns/shacl#class");
// Trellis
public static readonly Uri TrellisGraph = new("http://www.trellisldp.org/ns/trellis#PreferServerManaged");
// Vcard
public static readonly Uri VcardPrefix = new("http://www.w3.org/2006/vcard/ns#");
public static readonly Uri VcardHasMember = new("http://www.w3.org/2006/vcard/ns#hasMember");
public static readonly Uri VcardGroupClass = new("http://www.w3.org/2006/vcard/ns#Group");
// XSD
public static readonly Uri XsdPrefix = new("http://www.w3.org/2001/XMLSchema#");
public static readonly Uri XsdDateTime = new("http://www.w3.org/2001/XMLSchema#dateTime");
public static readonly Uri XsdBoolean = new("http://www.w3.org/2001/XMLSchema#boolean");
public static readonly Uri XsdHexBinary = new("http://www.w3.org/2001/XMLSchema#hexBinary");
}
\ No newline at end of file
namespace SQL2Linked.Utils;
/// <summary>
///
/// </summary>
public static class UriHelper
{
/// <summary>
///
/// </summary>
/// <param name="baseUri"></param>
/// <param name="relativePath"></param>
/// <returns></returns>
public static Uri? TryCombineUri(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? TryCombineUri(Uri baseUri, Guid relativePath)
{
Uri.TryCreate(baseUri, relativePath.ToString(), out Uri? result);
return result;
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment