Skip to main content
Sign in
Snippets Groups Projects
Select Git revision
  • 42ec34db0e04c876d8b9327f4239f77c0fc0f37e
  • 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

StructuralData.cs

  • Hanna Führ's avatar
    Hanna Führ authored and Benedikt Heinrichs committed
    42ec34db
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    StructuralData.cs 3.49 KiB
    using Coscine.Configuration;
    using Coscine.Database.Models;
    using Coscine.Metadata;
    using VDS.RDF;
    using VDS.RDF.Query;
    
    namespace SQL2Linked
    {
        public abstract class StructuralData<S, T> where S : class where T : DatabaseModel<S>, new()
        {
            public T Model { get; init; }
            public ConsulConfiguration Configuration { get; init; }
            public RdfStoreConnector RdfStoreConnector { get; init; }
            public static string Prefix { get; set; }
            public readonly SparqlRemoteEndpoint QueryEndpoint;
    
            public StructuralData()
            {
                Configuration = new ConsulConfiguration();
                RdfStoreConnector = new RdfStoreConnector(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url"));
                Model = new T();
                Prefix = Configuration.GetStringAndWait("coscine/global/epic/prefix");
                QueryEndpoint = new SparqlRemoteEndpoint(new Uri(Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url")));
            }
    
            public abstract IEnumerable<IGraph> ConvertToLinkedData(IEnumerable<S> entries);
    
            public void Migrate(bool dummyMode)
            {
                var spacer = new string('-', 35);
                Console.WriteLine($"\n{spacer}\n{typeof(T).Name}\n{spacer}");
                var graphs = ConvertToLinkedData(Model.GetAll());
                if (!dummyMode)
                {
                    StoreGraphs(graphs);
                }
            }
    
            private void StoreGraphs(IEnumerable<IGraph> graphs)
            {
                foreach (var graph in graphs)
                {
                    Console.WriteLine($" ({graph.BaseUri})");
                    var exists = RdfStoreConnector.HasGraph(graph.BaseUri);
                    if (exists)
                    {
                        Console.WriteLine($" - Graph {graph.BaseUri} exists");
    
                        // Clear the existing graph from the store
                        RdfStoreConnector.ClearGraph(graph.BaseUri);
                        Console.WriteLine($" - Cleared Graph {graph.BaseUri}");
                    }
                    // Add the new graph to the store
                    RdfStoreConnector.AddGraph(graph);
                    Console.WriteLine($" - Graph {graph.BaseUri} added successfully");
                    Console.WriteLine();
                }
            }
            public void AssertToGraphUriNode(IGraph graph, string graphSubject, string graphPredicate, string graphObject)
            {
                graph.Assert(
                    new Triple(
                        graph.CreateUriNode(new Uri(graphSubject)),
                        graph.CreateUriNode(new Uri(graphPredicate)),
                        graph.CreateUriNode(new Uri(graphObject))
                    )
                );
            }
            public void AssertToGraphLiteralNode(IGraph graph, string graphSubject, string graphPredicate, string graphObject, Uri? objectType = null)
            {
                graph.Assert(
                    new Triple(
                        graph.CreateUriNode(new Uri(graphSubject)),
                        graph.CreateUriNode(new Uri(graphPredicate)),
                        graph.CreateLiteralNode(graphObject, objectType)
                    )
                );
            }
    
            public void AssertToGraphBlankAndUriNode(IGraph graph, IBlankNode graphSubject, string graphPredicate, string graphObject)
            {
                graph.Assert(
                    new Triple(
                        graphSubject,
                        graph.CreateUriNode(new Uri(graphPredicate)),
                        graph.CreateUriNode(new Uri(graphObject))
                    )
                );
            }
        }
    }