Skip to content
Snippets Groups Projects
Select Git revision
  • ad8a532c1c1241d5ba82726fc983dcbd9f14f5b8
  • master default protected
2 results

README.md

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StructuralData.cs 7.12 KiB
    using Coscine.ApiClient;
    using Coscine.ApiClient.Core.Api;
    using Coscine.ApiClient.Core.Client;
    using Coscine.ApiClient.Core.Model;
    using Microsoft.Extensions.Configuration;
    using SQL2Linked.Models.ConfigurationModels;
    using VDS.RDF;
    
    namespace SQL2Linked;
    
    /// <summary>
    /// Provides an abstract base class for handling structural data transformations and migrations.
    /// </summary>
    /// <typeparam name="S">The type of data to be transformed or migrated.</typeparam>
    /// <remarks>
    /// This class is partially shared between this script and the Trellis Migrator script.
    /// </remarks>
    public abstract class StructuralData<S>
    {
        private readonly string _adminToken;
        protected readonly Configuration _apiConfiguration;
        protected readonly AdminApi _adminApi;
        protected readonly PidConfiguration _pidConfiguration; // Comes from the API Client, not from the application's own configuration
    
        /// <summary>
        /// Initializes a new instance of the <see cref="StructuralData{S}"/> class.
        /// </summary>
        public StructuralData()
        {
            // Retrieve the configuration settings for the PID ePIC API from the API Client
            var apiConfiguration = ApiConfigurationUtil.RetrieveApiConfiguration();
            _pidConfiguration = apiConfiguration.GetSection(PidConfiguration.Section).Get<PidConfiguration>() ?? new();
    
            // Ensiure that the prefix is not null or empty.
            ArgumentException.ThrowIfNullOrWhiteSpace(_pidConfiguration.Prefix, nameof(_pidConfiguration.Prefix));
    
            // Generate an admin token for the API Client
            var jwtConfiguration = ApiConfigurationUtil.RetrieveJwtConfiguration();
            _adminToken = ApiConfigurationUtil.GenerateAdminToken(jwtConfiguration);
            _apiConfiguration = new Configuration()
            {
                BasePath = "http://localhost:7206/coscine",
                ApiKeyPrefix = { { "Authorization", "Bearer" } },
                ApiKey = { { "Authorization", _adminToken } },
            };
            _adminApi = new AdminApi(_apiConfiguration);
        }
    
        /// <summary>
        /// Converts the given entries to linked data graphs.
        /// </summary>
        /// <param name="entries">The entries to convert.</param>
        /// <returns>An enumerable collection of graphs representing the linked data.</returns>
        public abstract Task<IEnumerable<IGraph>> ConvertToLinkedDataAsync(IEnumerable<S> entries);
    
        /// <summary>
        /// Retrieves all entries of type <typeparamref name="S"/>.
        /// </summary>
        /// <returns>A task that represents the asynchronous operation. The task result contains an enumerable of all entries.</returns>
        public abstract Task<IEnumerable<S>> GetAll();
    
        /// <summary>
        /// Migrates the data, optionally in a dummy mode where changes are not persisted.
        /// </summary>
        /// <param name="dummyMode">If set to <c>true</c>, the migration is simulated but not executed.</param>
        /// <returns>A task representing the asynchronous operation.</returns>
        public async Task Migrate(bool dummyMode)
        {
            var spacer = new string('-', 35);
            Console.WriteLine($"\n{spacer}\n{typeof(S).Name}\n{spacer}");