MetadataController.cs 8.77 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
using Coscine.Api.Project.Models;
using Coscine.ApiCommons;
using Coscine.ApiCommons.Exceptions;
using Coscine.ApiCommons.Factories;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using VDS.RDF.Writing;
using VDS.RDF.Parsing;
using VDS.RDF;
using Metadata;
using System.Web;
using System.IO;

namespace Coscine.Api.Project.Controllers
{
    public class MetadataController : Controller
    {
        private readonly Authenticator _authenticator;
        private readonly MetadataModel _metadataModel;
        private readonly ResourceModel _resourceModel;
        private readonly Util _util;

        public MetadataController()
        {
            _authenticator = new Authenticator(this, Program.Configuration);
            _metadataModel = new MetadataModel();
            _resourceModel = new ResourceModel();
            _util = new Util();
        }

        [Route("[controller]")]
        public IActionResult Index()
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                return NoContent();
            }));
        }

42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
        // returns the basic application profile
        [HttpGet("[controller]/resource/{projectId}/ap/{applicationProfileId}")]
        public IActionResult GetApplicationProfile(Guid projectId, string applicationProfileId)
        {
            var user = _authenticator.GetUserFromToken();

            if (_metadataModel.IsProjectMember(user, projectId))
            {
                var graph = _util.GetGraph(HttpUtility.UrlDecode(applicationProfileId));                

                var json = JToken.Parse(VDS.RDF.Writing.StringWriter.Write(graph, new RdfJsonWriter()));
                
                return Ok(json);
            }
            else
            {
                throw new NotAuthorizedException("User is no project member!");
            }

        }

        // returns the application profile with the fixed values
        [HttpGet("[controller]/resource/{resourceId}/apc/{applicationProfileId}")]
        public IActionResult GetApplicationProfileComplete(string resourceId, string applicationProfileId)
66
67
68
69
        {
            var user = _authenticator.GetUserFromToken();

            var resource = _resourceModel.GetById(Guid.Parse(resourceId));
70
            if (_metadataModel.IsProjectMember(user, resource) && applicationProfileId != null)
71
72
73
74
75
76
77
78
79
            {
                var graph = _util.GetGraph(HttpUtility.UrlDecode(applicationProfileId));
                var fixedValuesGraph = new Graph();

                fixedValuesGraph.LoadFromString(resource.FixedValues, new RdfJsonParser());

                graph.Merge(fixedValuesGraph);

                var json = JToken.Parse(VDS.RDF.Writing.StringWriter.Write(graph, new RdfJsonWriter()));
80

81
82
83
84
85
86
87
88
89
                return Ok(json);
            }
            else
            {
                throw new NotAuthorizedException("User is no project member!");
            }

        }

90
91
        [HttpGet("[controller]/project/{projectId}/aplist/")]
        public IActionResult ListAllApplicationProfiles(Guid projectId)
92
93
94
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
95
                if (_metadataModel.IsProjectMember(user, projectId))
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
                {
                    var graphUris = _util.ListGraphs();

                    return new JArray(graphUris.Select(x => x.ToString()).Where(x => x.StartsWith("https://purl.org/coscine/ap/")));
                }
                else
                {
                    throw new NotAuthorizedException("User is no project member!");
                }
            }));
        }

        [HttpGet("[controller]/resource/{resourceId}/filename/{filename}/ver/{version}")]
        public IActionResult GetMetadataForFile(string resourceId, string filename, string version)
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                var resource = _resourceModel.GetById(Guid.Parse(resourceId));
                if (_metadataModel.IsProjectMember(user, resource))
                {
                    var id = _metadataModel.GenerateId(resourceId, filename, version);
                    var graph = _util.GetGraph(id);
                    return JToken.Parse(VDS.RDF.Writing.StringWriter.Write(graph, new RdfJsonWriter()));
                }
                else
                {
                    throw new NotAuthorizedException("User is no project member!");
                }
            }));
        }

        [HttpPut("[controller]/resource/{resourceId}/filename/{filename}/ver/{version}")]
        public IActionResult StoreMetadataForFile(string resourceId, string filename, string version)
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
                var innerBlock = ObjectFactory<JToken>.DeserializeFromStream(Request.Body);
                var graphName = _metadataModel.GenerateId(resourceId, filename, version);
                var graphNameUri = new Uri(graphName);
                var json = new JObject
                {
                    [graphName] = innerBlock
                };

                var resource = _resourceModel.GetById(Guid.Parse(resourceId));
                if (_metadataModel.IsProjectMember(user, resource))
                {
                    var graph = new Graph();
                    graph.LoadFromString(json.ToString(), new RdfJsonParser());

                    var fixedValuesGraph = new Graph();
                    fixedValuesGraph.LoadFromString(resource.FixedValues, new RdfJsonParser());

                    foreach(var triple in fixedValuesGraph.Triples.Where(x => x.Predicate.ToString() == "https://purl.org/coscine/fixedValue"))
                    {
                        // Remove any existing triples
                        foreach (var triple2 in graph.GetTriplesWithSubjectPredicate(graph.CreateUriNode(graphNameUri), triple.Subject).ToList())
                        {
                            graph.Retract(triple2);
                        }
                        graph.Assert(graph.CreateUriNode(graphNameUri), triple.Subject, triple.Object);
                    }

                    // Default values is not checked or added

                    // validate the data
                    if (_util.ValidateShacl(graph, graphNameUri))
                    {
                        // store the data
                        if (_util.HasGraph(graphNameUri))
                        {
                            _util.ClearGraph(graphNameUri);
                        }
                        else
                        {
                            _util.CreateNamedGraph(graphNameUri);
                        }

                        // BaseUri must be set for the sparql query
                        graph.BaseUri = graphNameUri;
                        _util.AddGraph(graph);

                        return NoContent();
                    }
                    else
                    {
                        throw new NotAuthorizedException("Data has the wrong format!");
                    }

                }
                else
                {
                    throw new NotAuthorizedException("User is no project member!");
                }
            }));
        }

193
194
        [HttpGet("[controller]/vocabulary/{projectId}/{path}")]
        public IActionResult GetVocabulary(Guid projectId, string path)
195
196
197
        {
            return Ok(_authenticator.ValidateAndExecute((user) =>
            {
198
                if (_metadataModel.IsProjectMember(user, projectId))
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
                {
                    var graph = _util.GetGraph(HttpUtility.UrlDecode(path));

                    JArray de = new JArray();
                    foreach (var kv in _util.GetVocabularyLabels(graph, "de"))
                    {
                        JObject obj = new JObject
                        {
                            ["value"] = kv.Key,
                            ["name"] = kv.Value
                        };
                        de.Add(obj);
                    }

                    JArray en = new JArray();
                    foreach(var kv in _util.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;
                }
                else
                {
                    throw new NotAuthorizedException("User is no project member!");
                }
            }));
        }

    }
}