Skip to content
Snippets Groups Projects
Select Git revision
  • master
1 result

input.java

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    MetadataController.cs 7.62 KiB
    using Coscine.ApiCommons;
    using Coscine.ApiCommons.Exceptions;
    using Coscine.Database.Models;
    using Coscine.Database.Util;
    using Coscine.Metadata;
    using Microsoft.AspNetCore.Authorization;
    using Microsoft.AspNetCore.Mvc;
    using Newtonsoft.Json.Linq;
    using System;
    using System.Linq;
    using System.Web;
    using VDS.RDF;
    using VDS.RDF.Parsing;
    using VDS.RDF.Writing;
    
    namespace Coscine.Api.Metadata.Controllers
    {
        /// <summary>
        /// This controller represents the actions which can be performed with a Metadata storage.
        /// </summary>
        [Authorize]
        public class MetadataController : Controller
        {
            private readonly Authenticator _authenticator;
            private readonly ResourceModel _resourceModel;
            private readonly RdfStoreConnector _rdfStoreConnector;
            private readonly string ApplicationProfileUrl = "https://purl.org/coscine/ap/";
    
            /// <summary>
            /// MetadataController constructor specifying an authenticator and a ResourceModel.
            /// </summary>
            public MetadataController()
            {
                _authenticator = new Authenticator(this, Program.Configuration);
                _resourceModel = new ResourceModel();
                _rdfStoreConnector = new RdfStoreConnector(Program.Configuration.GetStringAndWait("coscine/local/virtuoso/additional/url"));
            }
    
            /// <summary>
            /// This method returns all application profiles.
            /// </summary>
            /// <returns>profiles</returns>
            [HttpGet("[controller]/profiles/")]
            public IActionResult GetProfiles()
            {
                var applicationProfiles = _rdfStoreConnector.GetApplicationProfiles();
                return Json(new JArray(applicationProfiles));
            }
    
            /// <summary>
            /// This method returns the application profile for the given profileUrl.
            /// </summary>
            /// <param name="profile">Url of the application profile</param>
            /// <returns>Json with the application profile</returns>
            [HttpGet("[controller]/profiles/{profile}")]
            public IActionResult GetProfile(string profile)
            {
                string profileUrl = profile.StartsWith("http") ? HttpUtility.UrlDecode(profile) : $"{ApplicationProfileUrl}{profile}/";
    
                var graph = _rdfStoreConnector.GetGraph(profileUrl);
    
                var tripleStore = new TripleStore();
                tripleStore.Add(graph);
    
                var outStoreJson = VDS.RDF.Writing.StringWriter.Write(tripleStore, new JsonLdWriter());
    
                var json = JToken.Parse(outStoreJson);
    
                return Ok(json);
            }
    
            /// <summary>
            /// Returns the application profile with the fixed values for the given resource.
            /// </summary>
            /// <param name="profile">Url of the application profile</param>
            /// <param name="resourceId">Id of the resource</param>
            /// <returns>JSON with the application profile or StatusCode 403</returns>
            [HttpGet("[controller]/profiles/{profile}/{resourceId}")]
            public IActionResult GetApplicationProfileComplete(string profile, string resourceId)
            {
                var user = _authenticator.GetUser();
    
                var resource = _resourceModel.GetById(Guid.Parse(resourceId));
    
                if (user == null || !_resourceModel.HasAccess(user, resource, UserRoles.Owner, UserRoles.Member))
                {
                    return Forbid("User is no project member!");
                }
    
                string profileUrl = profile.StartsWith("http") ? HttpUtility.UrlDecode(profile) : $"{ApplicationProfileUrl}{profile}/";
    
                var graph = _rdfStoreConnector.GetGraph(profileUrl);
                var fixedValuesGraph = new Graph();
    
                fixedValuesGraph.LoadFromString(resource.FixedValues, new RdfJsonParser());
    
                graph.Merge(fixedValuesGraph);
    
                var tripleStore = new TripleStore();
                tripleStore.Add(graph);
    
                var outStoreJson = VDS.RDF.Writing.StringWriter.Write(tripleStore, new JsonLdWriter());
    
                var json = JToken.Parse(outStoreJson);
    
                return Ok(json);
    
            }
    
            /// <summary>
            /// This method returns a list of all vocabularies.
            /// </summary>
            /// <throws>Exception for the code that has not been implemented</throws>
            [HttpGet("[controller]/vocabularies/")]
            public IActionResult GetVocabularies()
            {
                throw new NotImplementedException();
            }
    
            /// <summary>
            /// This method returns a specific vocabulary.
            /// </summary>
            /// <param name="path">Url of the vocabulary</param>
            /// <returns>JSON with the requested vocabulary</returns>
            [HttpGet("[controller]/vocabularies/{path}")]
            public IActionResult GetVocabulary(string path)
            {
                var graph = _rdfStoreConnector.GetGraph(HttpUtility.UrlDecode(path));
    
                var de = new JArray();
                foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "de"))
                {
                    JObject obj = new JObject
                    {
                        ["value"] = kv.Key,
                        ["name"] = kv.Value
                    };
                    de.Add(obj);
                }
    
                var en = new JArray();
                foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en"))
                {
                    JObject obj = new JObject
                    {
                        ["value"] = kv.Key,
                        ["name"] = kv.Value
                    };
                    en.Add(obj);
                }
    
                JObject json = new JObject
                {
                    ["de"] = de,
                    ["en"] = en
                };
    
                return Json(json);
            }
    
            /// <summary>
            /// This method returns instances.
            /// </summary>
            /// <param name="projectId">Id of the project</param>
            /// <param name="className">class name</param>
            /// <returns>instances as Json, or throw an Exception if the user has not beed authorized</returns>
            [HttpGet("[controller]/instances/{projectId}/{className}")]
            public IActionResult GetClassInstances(Guid projectId, string className)
            {
                var user = _authenticator.GetUser();
                ProjectModel projectModel = new ProjectModel();
                if (projectModel.HasAccess(user, projectModel.GetById(projectId), UserRoles.Owner, UserRoles.Member))
                {
                    if (!Uri.TryCreate(HttpUtility.UrlDecode(className), UriKind.Absolute, out Uri uri))
                    {
                        throw new ArgumentException("ClassName is not a valid Uri.");
                    }
                    var graph = _rdfStoreConnector.GetClassGraph(uri);
    
                    var de = new JArray();
                    foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "de"))
                    {
                        JObject obj = new JObject
                        {
                            ["value"] = kv.Key,
                            ["name"] = kv.Value
                        };
                        de.Add(obj);
                    }
    
                    var en = new JArray();
                    foreach (var kv in _rdfStoreConnector.GetVocabularyLabels(graph, "en"))
                    {
                        JObject obj = new JObject
                        {
                            ["value"] = kv.Key,
                            ["name"] = kv.Value
                        };
                        en.Add(obj);
                    }
    
                    JObject json = new JObject
                    {
                        ["de"] = de,
                        ["en"] = en
                    };
    
                    return Json(json);
                }
                else
                {
                    throw new NotAuthorizedException("User is no project member!");
                }
            }
        }
    }