/
addValueVersion.scala.txt
520 lines (392 loc) · 20.5 KB
/
addValueVersion.scala.txt
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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
193
194
195
196
197
198
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
@*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*@
@import java.util.UUID
@import org.knora.webapi._
@import org.knora.webapi.exceptions.SparqlGenerationException
@import org.knora.webapi.messages.StringFormatter
@import org.knora.webapi.messages.twirl.SparqlTemplateLinkUpdate
@import org.knora.webapi.messages.v1.responder.valuemessages._
@**
* Inserts a new version of an existing value of a resource property.
*
* @param dataNamedGraph the named graph to update.
* @param triplestore the name of the triplestore being used.
* @param resourceIri the resource to update.
* @param propertyIri the IRI of the property that points from the resource to the value.
* @param currentValueIri the most recent existing version of the value.
* @param newValueIri the new value IRI.
* @param valueTypeIri the type of the new value.
* @param permissionRelevantAssertions Permission-relevant assertions, i.e. owner and project plus permissions, in
* the form of a list of tuples (predicate, object).
* @param value the value to insert.
* @param an optional comment on the new value.
* @param linkUpdates a list of [[SparqlTemplateLinkUpdate]] objects describing links and LinkValues that need to be
* updated for resource references in standoff markup.
* @param currentTime an xsd:dateTimeStamp that will be attached to the resources.
* @param requestingUser the IRI of the user making the request.
* @param stringFormatter a [[StringFormatter]].
*
* This operation will do nothing if `currentValueIri` is not in fact the most recent version, if `valueTypeIri` is
* the wrong type for the property, or if the resource or the current value are nonexistent or marked as deleted. To
* find out whether the operation succeeded, the application must query the property's version history.
*@
@(dataNamedGraph: IRI,
triplestore: String,
resourceIri: IRI,
propertyIri: IRI,
currentValueIri: IRI,
newValueIri: IRI,
valueTypeIri: IRI,
value: UpdateValueV1,
valueCreator: IRI,
valuePermissions: String,
maybeComment: Option[String],
linkUpdates: Seq[SparqlTemplateLinkUpdate],
currentTime: String,
requestingUser: IRI,
stringFormatter: StringFormatter)
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>
PREFIX owl: <http://www.w3.org/2002/07/owl#>
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#>
PREFIX knora-base: <http://www.knora.org/ontology/knora-base#>
DELETE {
GRAPH ?dataNamedGraph {
@* Detach the current version of the value from the resource. *@
?resource ?property ?currentValue .
@* Delete the resource's last modification date so we can update it. *@
?resource knora-base:lastModificationDate ?resourceLastModificationDate .
@* Delete the UUID from the current version of the value, because the new version will store it. *@
?currentValue knora-base:valueHasUUID ?currentValueUUID .
@* Delete the permissions from the current version of the value, because the new version will store them. *@
?currentValue knora-base:hasPermissions ?currentValuePermissions .
@for((linkUpdate, linkValueIndex) <- linkUpdates.zipWithIndex) {
@* Delete direct links for resource references that no longer exist. *@
@if(linkUpdate.deleteDirectLink) {
?resource <@linkUpdate.linkPropertyIri> <@linkUpdate.linkTargetIri> .
}
@* If any LinkValues need to be updated, detach them from the resource. *@
@if(linkUpdate.linkValueExists) {
?resource <@{linkUpdate.linkPropertyIri}Value> ?linkValue@linkValueIndex .
@* Delete the UUID from the current version of the link value, because the new version will store it. *@
?linkValue@linkValueIndex knora-base:valueHasUUID ?linkValueUUID@linkValueIndex .
@* Delete the permissions from the current version of the link value, because the new version will store them. *@
?linkValue@linkValueIndex knora-base:hasPermissions ?linkValuePermissions@linkValueIndex .
}
}
}
} INSERT {
GRAPH ?dataNamedGraph {
@* Construct the new value version. *@
?newValue rdf:type ?valueType ;
knora-base:isDeleted "false"^^xsd:boolean ;
knora-base:valueHasString """@value.toString""" .
@value match {
case textValueSimple: TextValueSimpleV1 => {
@if(!textValueSimple.language.isEmpty) {
<@newValueIri> knora-base:valueHasLanguage """@textValueSimple.language.get""" .
}
}
case textValueWithStandoff: TextValueWithStandoffV1 => {
@* Create a Standoff node for each StandoffTagV1. *@
?newValue knora-base:valueHasMapping <@textValueWithStandoff.mappingIri> ;
knora-base:valueHasMaxStandoffStartIndex @textValueWithStandoff.computedMaxStandoffStartIndex.get .
@if(!textValueWithStandoff.language.isEmpty) {
<@newValueIri> knora-base:valueHasLanguage """@textValueWithStandoff.language.get""" .
}
@for((createStandoff, standoffNodeIndex) <- textValueWithStandoff.prepareForSparqlInsert(newValueIri).zipWithIndex) {
?newValue knora-base:valueHasStandoff <@createStandoff.standoffTagInstanceIri> .
<@createStandoff.standoffTagInstanceIri>
@*
Check for optional standoff properties
*@
@if(createStandoff.standoffNode.endIndex.isDefined) {
knora-base:standoffTagHasEndIndex @createStandoff.standoffNode.endIndex.get ;
}
@if(createStandoff.startParentIri.isDefined) {
knora-base:standoffTagHasStartParent <@createStandoff.startParentIri.get> ;
}
@if(createStandoff.endParentIri.isDefined) {
knora-base:standoffTagHasEndParent <@createStandoff.endParentIri.get> ;
}
@if(createStandoff.standoffNode.originalXMLID.isDefined) {
knora-base:standoffTagHasOriginalXMLID """@createStandoff.standoffNode.originalXMLID.get""" ;
}
@*
Handle standoff class specific standoff properties
*@
@for(createProperty <- createStandoff.standoffNode.attributes) {
<@createProperty.standoffPropertyIri> @createProperty.rdfValue ;
}
knora-base:standoffTagHasStartIndex @createStandoff.standoffNode.startIndex ;
knora-base:standoffTagHasUUID "@{stringFormatter.base64EncodeUuid(createStandoff.standoffNode.uuid)}" ;
knora-base:standoffTagHasStart @createStandoff.standoffNode.startPosition ;
knora-base:standoffTagHasEnd @createStandoff.standoffNode.endPosition ;
rdf:type <@createStandoff.standoffNode.standoffTagClassIri> .
}
}
case intValue: IntegerValueV1 => {
?newValue knora-base:valueHasInteger @intValue.ival .
}
case decimalValue: DecimalValueV1 => {
?newValue knora-base:valueHasDecimal "@decimalValue.dval"^^xsd:decimal .
}
case booleanValue: BooleanValueV1 => {
?newValue knora-base:valueHasBoolean @booleanValue.bval .
}
case uriValue: UriValueV1 => {
?newValue knora-base:valueHasUri """@uriValue.uri"""^^xsd:anyURI .
}
case jdnValue: JulianDayNumberValueV1 => {
?newValue knora-base:valueHasStartJDN @jdnValue.dateval1 .
?newValue knora-base:valueHasEndJDN @jdnValue.dateval2 .
?newValue knora-base:valueHasStartPrecision "@jdnValue.dateprecision1" .
?newValue knora-base:valueHasEndPrecision "@jdnValue.dateprecision2" .
?newValue knora-base:valueHasCalendar "@jdnValue.calendar" .
}
case colorValue: ColorValueV1 => {
?newValue knora-base:valueHasColor """@colorValue.color""" .
}
case geometryValue: GeomValueV1 => {
?newValue knora-base:valueHasGeometry """@geometryValue.geom""" .
}
case stillImageFileValue: StillImageFileValueV1 => {
?newValue knora-base:internalFilename """@stillImageFileValue.internalFilename""" .
?newValue knora-base:internalMimeType """@stillImageFileValue.internalMimeType""" .
?newValue knora-base:dimX @stillImageFileValue.dimX .
?newValue knora-base:dimY @stillImageFileValue.dimY .
@stillImageFileValue.originalFilename match {
case Some(definedOriginalFilename) => {
?newValue knora-base:originalFilename """@definedOriginalFilename""" .
}
case None => {}
}
@stillImageFileValue.originalMimeType match {
case Some(definedOriginalMimeType) => {
?newValue knora-base:originalMimeType """@definedOriginalMimeType""" .
}
case None => {}
}
}
case textFileValue: TextFileValueV1 => {
?newValue knora-base:internalFilename """@textFileValue.internalFilename""" ;
knora-base:internalMimeType """@textFileValue.internalMimeType""" .
@textFileValue.originalFilename match {
case Some(definedOriginalFilename) => {
?newValue knora-base:originalFilename """@definedOriginalFilename""" .
}
case None => {}
}
@textFileValue.originalMimeType match {
case Some(definedOriginalMimeType) => {
?newValue knora-base:originalMimeType """@definedOriginalMimeType""" .
}
case None => {}
}
}
case documentFileValue: DocumentFileValueV1 => {
?newValue knora-base:internalFilename """@documentFileValue.internalFilename""" .
?newValue knora-base:internalMimeType """@documentFileValue.internalMimeType""" .
?newValue knora-base:pageCount @documentFileValue.pageCount .
@documentFileValue.dimX match {
case Some(definedDimX) => {
?newValue knora-base:dimX @definedDimX .
}
case None => {}
}
@documentFileValue.dimY match {
case Some(definedDimY) => {
?newValue knora-base:dimY @definedDimY .
}
case None => {}
}
@documentFileValue.originalFilename match {
case Some(definedOriginalFilename) => {
?newValue knora-base:originalFilename """@definedOriginalFilename""" .
}
case None => {}
}
@documentFileValue.originalMimeType match {
case Some(definedOriginalMimeType) => {
?newValue knora-base:originalMimeType """@definedOriginalMimeType""" .
}
case None => {}
}
}
case listValue: HierarchicalListValueV1 => {
?newValue knora-base:valueHasListNode <@listValue.hierarchicalListIri> .
}
case intervalValue: IntervalValueV1 => {
?newValue knora-base:valueHasIntervalStart "@intervalValue.timeval1"^^xsd:decimal ;
knora-base:valueHasIntervalEnd "@intervalValue.timeval2"^^xsd:decimal .
}
case timeValue: TimeValueV1 => {
?newValue knora-base:valueHasTimeStamp "@timeValue.timeStamp"^^xsd:dateTime .
}
case geonameValue: GeonameValueV1 => {
?newValue knora-base:valueHasGeonameCode """@geonameValue.geonameCode""" .
}
case other => {
@{throw SparqlGenerationException(s"Value object $other is not supported in this SPARQL template"); ()}
}
}
@* TODO: add remaining types. *@
@* Insert the value's comment, if given. *@
@maybeComment match {
case Some(comment) => {
?newValue knora-base:valueHasComment """@comment""" .
}
case None => {}
}
@* Insert the value's owner and permissions. *@
?newValue knora-base:attachedToUser <@valueCreator> ;
knora-base:hasPermissions "@valuePermissions" ;
knora-base:valueHasUUID ?currentValueUUID .
@* TODO: in case of a file value, order is not given *@
?newValue knora-base:valueHasOrder ?order .
?newValue knora-base:valueCreationDate "@currentTime"^^xsd:dateTime .
?newValue knora-base:previousValue ?currentValue .
@* Insert direct links and LinkValues for resource references. *@
@for((linkUpdate, linkValueIndex) <- linkUpdates.zipWithIndex) {
@* Insert a direct link for the resource reference if necessary. *@
@if(linkUpdate.insertDirectLink) {
?resource <@linkUpdate.linkPropertyIri> <@linkUpdate.linkTargetIri> .
}
@* Insert a LinkValue for the resource reference. *@
<@linkUpdate.newLinkValueIri> rdf:type knora-base:LinkValue ;
rdf:subject ?resource ;
rdf:predicate <@linkUpdate.linkPropertyIri> ;
rdf:object <@linkUpdate.linkTargetIri> ;
knora-base:valueHasString "@linkUpdate.linkTargetIri" ;
knora-base:valueHasRefCount @linkUpdate.newReferenceCount ;
@if(linkUpdate.newReferenceCount == 0) {
knora-base:isDeleted true ;
knora-base:deletedBy <@requestingUser> ;
knora-base:deleteDate "@currentTime"^^xsd:dateTime ;
} else {
knora-base:isDeleted false ;
}
knora-base:valueCreationDate "@currentTime"^^xsd:dateTime .
<@linkUpdate.newLinkValueIri> knora-base:attachedToUser <@linkUpdate.newLinkValueCreator> ;
knora-base:hasPermissions "@linkUpdate.newLinkValuePermissions" .
@*
If we're inserting a new version of an existing LinkValue, attach it to the previous version,
and use the previous version's UUID. Otherwise, make a random UUID.
*@
@if(linkUpdate.linkValueExists) {
<@linkUpdate.newLinkValueIri> knora-base:previousValue ?linkValue@linkValueIndex ;
knora-base:valueHasUUID ?linkValueUUID@linkValueIndex .
} else {
<@linkUpdate.newLinkValueIri> knora-base:valueHasUUID "@{stringFormatter.base64EncodeUuid(UUID.randomUUID)}" .
}
@* Attach the new LinkValue to its containing resource. *@
?resource <@{linkUpdate.linkPropertyIri}Value> <@linkUpdate.newLinkValueIri> .
}
@* Attach the value to the resource. *@
?resource ?property ?newValue .
@* Update the resource's last modification date. *@
?resource knora-base:lastModificationDate "@currentTime"^^xsd:dateTime .
}
}
@* Ensure that inference is not used in the WHERE clause of this update. *@
@if(triplestore.startsWith("graphdb")) {
USING <http://www.ontotext.com/explicit>
}
WHERE {
BIND(IRI("@dataNamedGraph") AS ?dataNamedGraph)
BIND(IRI("@resourceIri") AS ?resource)
BIND(IRI("@propertyIri") AS ?property)
BIND(IRI("@currentValueIri") AS ?currentValue)
BIND(IRI("@newValueIri") AS ?newValue)
BIND(IRI("@valueTypeIri") AS ?valueType)
?resource ?property ?currentValue .
?currentValue rdf:type ?valueType .
?property knora-base:objectClassConstraint ?valueType .
@*
Consider order if given (not given for file values)
*@
OPTIONAL {
?currentValue knora-base:valueHasOrder ?order .
}
@*
Do nothing if the resource isn't actually a resource.
*@
?resource rdf:type ?resourceClass .
?resourceClass rdfs:subClassOf* knora-base:Resource .
@* Do nothing if the resource is marked as deleted. *@
?resource knora-base:isDeleted false .
@* Don't add a new version of a deleted value. *@
?currentValue knora-base:isDeleted false ;
knora-base:valueHasUUID ?currentValueUUID ;
knora-base:hasPermissions ?currentValuePermissions .
@*
Check the state of any LinkValues to be updated for resource references.
*@
@for((linkUpdate, linkValueIndex) <- linkUpdates.zipWithIndex) {
@if(linkUpdate.insertDirectLink) {
@*
Do nothing if we were asked to insert a direct link to a target that doesn't exist, is marked as deleted,
isn't a knora-base:Resource, or belongs to the wrong OWL class for the link property.
*@
<@linkUpdate.linkTargetIri> rdf:type ?linkTargetClass@linkValueIndex .
<@linkUpdate.linkTargetIri> knora-base:isDeleted false .
?linkTargetClass@linkValueIndex rdfs:subClassOf* knora-base:Resource .
<@linkUpdate.linkPropertyIri> knora-base:objectClassConstraint ?expectedTargetClass@linkValueIndex .
?linkTargetClass@linkValueIndex rdfs:subClassOf* ?expectedTargetClass@linkValueIndex .
}
@if(linkUpdate.directLinkExists) {
@* Make sure the relevant direct link exists between the two resources. *@
?resource <@linkUpdate.linkPropertyIri> <@linkUpdate.linkTargetIri> .
} else {
@* Make sure there no such direct link. *@
MINUS {
?resource <@linkUpdate.linkPropertyIri> <@linkUpdate.linkTargetIri> .
}
}
@if(linkUpdate.linkValueExists) {
@*
Make sure a knora-base:LinkValue exists, describing the relevant direct link
between the source and target resources, and has the correct reference count.
*@
?resource <@{linkUpdate.linkPropertyIri}Value> ?linkValue@linkValueIndex .
?linkValue@linkValueIndex rdf:type knora-base:LinkValue ;
rdf:subject ?resource ;
rdf:predicate <@linkUpdate.linkPropertyIri> ;
rdf:object <@linkUpdate.linkTargetIri> ;
knora-base:valueHasRefCount @linkUpdate.currentReferenceCount ;
knora-base:isDeleted false ;
knora-base:valueHasUUID ?linkValueUUID@linkValueIndex ;
knora-base:hasPermissions ?linkValuePermissions@linkValueIndex .
} else {
@* Make sure there is no such knora-base:LinkValue. *@
MINUS {
?resource <@{linkUpdate.linkPropertyIri}Value> ?linkValue@linkValueIndex .
?linkValue@linkValueIndex rdf:type knora-base:LinkValue ;
rdf:subject ?resource ;
rdf:predicate <@linkUpdate.linkPropertyIri> ;
rdf:object <@linkUpdate.linkTargetIri> ;
knora-base:isDeleted false .
}
}
}
@* Get the resource's last modification date, if it has one, so we can update it. *@
OPTIONAL {
?resource knora-base:lastModificationDate ?resourceLastModificationDate .
}
}