/
ValueMessagesV2.scala
3190 lines (2756 loc) · 152 KB
/
ValueMessagesV2.scala
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
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright © 2015-2018 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/>.
*/
package org.knora.webapi.messages.v2.responder.valuemessages
import java.time.Instant
import java.util.UUID
import akka.actor.ActorRef
import akka.event.LoggingAdapter
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import akka.util.Timeout
import org.knora.webapi._
import org.knora.webapi.exceptions.{AssertionException, BadRequestException, NotImplementedException, SipiException}
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectADM
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.store.sipimessages.{GetFileMetadataRequest, GetFileMetadataResponse}
import org.knora.webapi.messages.util.PermissionUtilADM.EntityPermission
import org.knora.webapi.messages.util.standoff.StandoffTagUtilV2.TextWithStandoffTagsV2
import org.knora.webapi.messages.util.standoff.{StandoffTagUtilV2, XMLUtil}
import org.knora.webapi.messages.util._
import org.knora.webapi.messages.v2.responder._
import org.knora.webapi.messages.v2.responder.resourcemessages.ReadResourceV2
import org.knora.webapi.messages.v2.responder.standoffmessages._
import org.knora.webapi.messages.{OntologyConstants, SmartIri, StringFormatter}
import org.knora.webapi.settings.KnoraSettingsImpl
import org.knora.webapi.util._
import scala.concurrent.{ExecutionContext, Future}
/**
* A tagging trait for requests handled by [[org.knora.webapi.responders.v2.ValuesResponderV2]].
*/
sealed trait ValuesResponderRequestV2 extends KnoraRequestV2
/**
* Requests the creation of a value.
*
* @param createValue a [[CreateValueV2]] representing the value to be created. A successful response will be
* a [[CreateValueResponseV2]].
* @param requestingUser the user making the request.
* @param apiRequestID the API request ID.
*/
case class CreateValueRequestV2(createValue: CreateValueV2,
requestingUser: UserADM,
apiRequestID: UUID) extends ValuesResponderRequestV2
/**
* Constructs [[CreateValueRequestV2]] instances based on JSON-LD input.
*/
object CreateValueRequestV2 extends KnoraJsonLDRequestReaderV2[CreateValueRequestV2] {
/**
* Converts JSON-LD input to a [[CreateValueRequestV2]].
*
* @param jsonLDDocument the JSON-LD input.
* @param apiRequestID the UUID of the API request.
* @param requestingUser the user making the request.
* @param responderManager a reference to the responder manager.
* @param storeManager a reference to the store manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a case class instance representing the input.
*/
override def fromJsonLD(jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: UserADM,
responderManager: ActorRef,
storeManager: ActorRef,
settings: KnoraSettingsImpl,
log: LoggingAdapter)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[CreateValueRequestV2] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
for {
// Get the IRI of the resource that the value is to be created in.
resourceIri: SmartIri <- Future(jsonLDDocument.requireIDAsKnoraDataIri)
_ = if (!resourceIri.isKnoraResourceIri) {
throw BadRequestException(s"Invalid resource IRI: <$resourceIri>")
}
// Get the resource class.
resourceClassIri: SmartIri = jsonLDDocument.requireTypeAsKnoraTypeIri
// Get the resource property and the value to be created.
createValue: CreateValueV2 <- jsonLDDocument.requireResourcePropertyValue match {
case (propertyIri: SmartIri, jsonLDObject: JsonLDObject) =>
for {
valueContent: ValueContentV2 <-
ValueContentV2.fromJsonLDObject(
jsonLDObject = jsonLDObject,
requestingUser = requestingUser,
responderManager = responderManager,
storeManager = storeManager,
settings = settings,
log = log
)
// Get and validate the custom value IRI if provided.
maybeCustomValueIri: Option[SmartIri] = jsonLDObject.maybeIDAsKnoraDataIri.map {
definedNewIri =>
stringFormatter.validateCustomValueIri(
customValueIri = definedNewIri,
projectCode = resourceIri.getProjectCode.get,
resourceID = resourceIri.getResourceID.get
)
}
// Get the custom value UUID if provided.
maybeCustomUUID: Option[UUID] = jsonLDObject.maybeUUID(OntologyConstants.KnoraApiV2Complex.ValueHasUUID)
// Get the value's creation date.
// TODO: creationDate for values is a bug, and will not be supported in future. Use valueCreationDate instead.
maybeCreationDate: Option[Instant] = jsonLDObject.maybeDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.ValueCreationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
).orElse(jsonLDObject.maybeDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.CreationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
))
maybePermissions: Option[String] = jsonLDObject.maybeStringWithValidation(OntologyConstants.KnoraApiV2Complex.HasPermissions, stringFormatter.toSparqlEncodedString)
} yield CreateValueV2(
resourceIri = resourceIri.toString,
resourceClassIri = resourceClassIri,
propertyIri = propertyIri,
valueContent = valueContent,
valueIri = maybeCustomValueIri,
valueUUID = maybeCustomUUID,
valueCreationDate = maybeCreationDate,
permissions = maybePermissions
)
}
} yield CreateValueRequestV2(
createValue = createValue,
apiRequestID = apiRequestID,
requestingUser = requestingUser
)
}
}
/**
* Represents a successful response to a [[CreateValueRequestV2]].
*
* @param valueIri the IRI of the value that was created.
* @param valueType the type of the value that was created.
* @param valueUUID the value's UUID.
* @param valueCreationDate the value's creationDate
* @param projectADM the project in which the value was created.
*/
case class CreateValueResponseV2(valueIri: IRI,
valueType: SmartIri,
valueUUID: UUID,
valueCreationDate: Instant,
projectADM: ProjectADM) extends KnoraResponseV2 with UpdateResultInProject {
override def toJsonLDDocument(targetSchema: ApiV2Schema, settings: KnoraSettingsImpl, schemaOptions: Set[SchemaOption]): JsonLDDocument = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
if (targetSchema != ApiV2Complex) {
throw AssertionException(s"CreateValueResponseV2 can only be returned in the complex schema")
}
JsonLDDocument(
body = JsonLDObject(
Map(
JsonLDConstants.ID -> JsonLDString(valueIri),
JsonLDConstants.TYPE -> JsonLDString(valueType.toOntologySchema(ApiV2Complex).toString),
OntologyConstants.KnoraApiV2Complex.ValueHasUUID -> JsonLDString(stringFormatter.base64EncodeUuid(valueUUID)),
OntologyConstants.KnoraApiV2Complex.ValueCreationDate -> JsonLDUtil.datatypeValueToJsonLDObject(
value = valueCreationDate.toString,
datatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri
)
)
),
context = JsonLDUtil.makeContext(
fixedPrefixes = Map(
OntologyConstants.KnoraApi.KnoraApiOntologyLabel -> OntologyConstants.KnoraApiV2Complex.KnoraApiV2PrefixExpansion
)
)
)
}
}
/**
* Requests an update to a value, i.e. the creation of a new version of an existing value.
*
* @param updateValue an [[UpdateValueV2]] representing the new version of the value. A successful response will be
* an [[UpdateValueResponseV2]].
* @param requestingUser the user making the request.
* @param apiRequestID the API request ID.
*/
case class UpdateValueRequestV2(updateValue: UpdateValueV2,
requestingUser: UserADM,
apiRequestID: UUID) extends ValuesResponderRequestV2
/**
* Constructs [[UpdateValueRequestV2]] instances based on JSON-LD input.
*/
object UpdateValueRequestV2 extends KnoraJsonLDRequestReaderV2[UpdateValueRequestV2] {
/**
* Converts JSON-LD input to a [[CreateValueRequestV2]].
*
* @param jsonLDDocument the JSON-LD input.
* @param apiRequestID the UUID of the API request.
* @param requestingUser the user making the request.
* @param responderManager a reference to the responder manager.
* @param storeManager a reference to the store manager.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a case class instance representing the input.
*/
override def fromJsonLD(jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: UserADM,
responderManager: ActorRef,
storeManager: ActorRef,
settings: KnoraSettingsImpl,
log: LoggingAdapter)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[UpdateValueRequestV2] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
for {
// Get the IRI of the resource that the value is to be created in.
resourceIri: SmartIri <- Future(jsonLDDocument.requireIDAsKnoraDataIri)
_ = if (!resourceIri.isKnoraResourceIri) {
throw BadRequestException(s"Invalid resource IRI: <$resourceIri>")
}
// Get the resource class.
resourceClassIri: SmartIri = jsonLDDocument.requireTypeAsKnoraTypeIri
// Get the resource property and the new value version.
updateValue: UpdateValueV2 <- jsonLDDocument.requireResourcePropertyValue match {
case (propertyIri: SmartIri, jsonLDObject: JsonLDObject) =>
val valueIri: SmartIri = jsonLDObject.requireIDAsKnoraDataIri
// Get the custom value creation date, if provided.
val maybeValueCreationDate: Option[Instant] = jsonLDObject.maybeDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.ValueCreationDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
// Get and validate the custom new value version IRI, if provided.
val maybeNewIri: Option[SmartIri] = jsonLDObject.maybeIriInObject(
OntologyConstants.KnoraApiV2Complex.NewValueVersionIri,
stringFormatter.toSmartIriWithErr
).map {
definedNewIri =>
if (definedNewIri == valueIri) {
throw BadRequestException(s"The IRI of a new value version cannot be the same as the IRI of the current version")
}
stringFormatter.validateCustomValueIri(
customValueIri = definedNewIri,
projectCode = valueIri.getProjectCode.get,
resourceID = valueIri.getResourceID.get
)
}
// Aside from the value's ID and type and the optional predicates above, does the value object just
// contain knora-api:hasPermissions?
val otherValuePredicates: Set[IRI] = jsonLDObject.value.keySet -- Set(
JsonLDConstants.ID,
JsonLDConstants.TYPE,
OntologyConstants.KnoraApiV2Complex.ValueCreationDate,
OntologyConstants.KnoraApiV2Complex.NewValueVersionIri
)
if (otherValuePredicates == Set(OntologyConstants.KnoraApiV2Complex.HasPermissions)) {
// Yes. This is a request to change the value's permissions.
val valueType: SmartIri = jsonLDObject.requireStringWithValidation(JsonLDConstants.TYPE, stringFormatter.toSmartIriWithErr)
val permissions = jsonLDObject.requireStringWithValidation(OntologyConstants.KnoraApiV2Complex.HasPermissions, stringFormatter.toSparqlEncodedString)
FastFuture.successful(
UpdateValuePermissionsV2(
resourceIri = resourceIri.toString,
resourceClassIri = resourceClassIri,
propertyIri = propertyIri,
valueIri = valueIri.toString,
valueType = valueType,
permissions = permissions,
valueCreationDate = maybeValueCreationDate,
newValueVersionIri = maybeNewIri
)
)
} else {
// No. This is a request to change the value content.
for {
valueContent: ValueContentV2 <- ValueContentV2.fromJsonLDObject(
jsonLDObject = jsonLDObject,
requestingUser = requestingUser,
responderManager = responderManager,
storeManager = storeManager,
settings = settings,
log = log
)
maybePermissions: Option[String] = jsonLDObject.maybeStringWithValidation(OntologyConstants.KnoraApiV2Complex.HasPermissions, stringFormatter.toSparqlEncodedString)
} yield UpdateValueContentV2(
resourceIri = resourceIri.toString,
resourceClassIri = resourceClassIri,
propertyIri = propertyIri,
valueIri = valueIri.toString,
valueContent = valueContent,
permissions = maybePermissions,
valueCreationDate = maybeValueCreationDate,
newValueVersionIri = maybeNewIri
)
}
}
} yield UpdateValueRequestV2(
updateValue = updateValue,
apiRequestID = apiRequestID,
requestingUser = requestingUser
)
}
}
/**
* Represents a successful response to an [[UpdateValueRequestV2]].
*
* @param valueIri the IRI of the value version that was created.
* @param valueType the type of the value that was updated.
* @param valueUUID the value's UUID.
* @param projectADM the project in which the value was updated.
*/
case class UpdateValueResponseV2(valueIri: IRI,
valueType: SmartIri,
valueUUID: UUID,
projectADM: ProjectADM) extends KnoraResponseV2 with UpdateResultInProject {
override def toJsonLDDocument(targetSchema: ApiV2Schema, settings: KnoraSettingsImpl, schemaOptions: Set[SchemaOption]): JsonLDDocument = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
if (targetSchema != ApiV2Complex) {
throw AssertionException(s"UpdateValueResponseV2 can only be returned in the complex schema")
}
JsonLDDocument(
body = JsonLDObject(
Map(
JsonLDConstants.ID -> JsonLDString(valueIri),
JsonLDConstants.TYPE -> JsonLDString(valueType.toOntologySchema(ApiV2Complex).toString),
OntologyConstants.KnoraApiV2Complex.ValueHasUUID -> JsonLDString(stringFormatter.base64EncodeUuid(valueUUID))
)
),
context = JsonLDUtil.makeContext(
fixedPrefixes = Map(
OntologyConstants.KnoraApi.KnoraApiOntologyLabel -> OntologyConstants.KnoraApiV2Complex.KnoraApiV2PrefixExpansion
)
)
)
}
}
/**
* Requests that a value is marked as deleted. A successful response will be a [[SuccessResponseV2]].
*
* @param resourceIri the IRI of the containing resource.
* @param resourceClassIri the IRI of the resource class.
* @param propertyIri the IRI of the property pointing to the value to be marked as deleted.
* @param valueIri the IRI of the value to be marked as deleted.
* @param valueTypeIri the IRI of the value class.
* @param deleteComment an optional comment explaining why the value is being marked as deleted.
* @param deleteDate an optional timestamp indicating when the value was deleted. If not supplied,
* the current time will be used.
* @param requestingUser the user making the request.
* @param apiRequestID the API request ID.
*/
case class DeleteValueRequestV2(resourceIri: IRI,
resourceClassIri: SmartIri,
propertyIri: SmartIri,
valueIri: IRI,
valueTypeIri: SmartIri,
deleteComment: Option[String] = None,
deleteDate: Option[Instant] = None,
requestingUser: UserADM,
apiRequestID: UUID) extends ValuesResponderRequestV2
object DeleteValueRequestV2 extends KnoraJsonLDRequestReaderV2[DeleteValueRequestV2] {
/**
* Converts JSON-LD input into a case class instance.
*
* @param jsonLDDocument the JSON-LD input.
* @param apiRequestID the UUID of the API request.
* @param requestingUser the user making the request.
* @param responderManager a reference to the responder manager.
* @param storeManager a reference to the store manager.
* @param settings the application settings.
* @param log a logging adapter.
* @param timeout a timeout for `ask` messages.
* @param executionContext an execution context for futures.
* @return a case class instance representing the input.
*/
override def fromJsonLD(jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: UserADM,
responderManager: ActorRef,
storeManager: ActorRef,
settings: KnoraSettingsImpl,
log: LoggingAdapter)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[DeleteValueRequestV2] = {
Future {
fromJsonLDSync(
jsonLDDocument = jsonLDDocument,
apiRequestID = apiRequestID,
requestingUser = requestingUser
)
}
}
private def fromJsonLDSync(jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: UserADM): DeleteValueRequestV2 = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
// Get the IRI of the resource that the value is to be created in.
val resourceIri: SmartIri = jsonLDDocument.requireIDAsKnoraDataIri
if (!resourceIri.isKnoraResourceIri) {
throw BadRequestException(s"Invalid resource IRI: <$resourceIri>")
}
// Get the resource class.
val resourceClassIri: SmartIri = jsonLDDocument.requireTypeAsKnoraTypeIri
// Get the resource property and the IRI and class of the value to be deleted.
jsonLDDocument.requireResourcePropertyValue match {
case (propertyIri: SmartIri, jsonLDObject: JsonLDObject) =>
val valueIri = jsonLDObject.requireIDAsKnoraDataIri
if (!valueIri.isKnoraValueIri) {
throw BadRequestException(s"Invalid value IRI: <$valueIri>")
}
val valueTypeIri: SmartIri = jsonLDObject.requireTypeAsKnoraApiV2ComplexTypeIri
val deleteComment: Option[String] = jsonLDObject.maybeStringWithValidation(OntologyConstants.KnoraApiV2Complex.DeleteComment, stringFormatter.toSparqlEncodedString)
val deleteDate: Option[Instant] = jsonLDObject.maybeDatatypeValueInObject(
key = OntologyConstants.KnoraApiV2Complex.DeleteDate,
expectedDatatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri,
validationFun = stringFormatter.xsdDateTimeStampToInstant
)
DeleteValueRequestV2(
resourceIri = resourceIri.toString,
resourceClassIri = resourceClassIri,
propertyIri = propertyIri,
valueIri = valueIri.toString,
valueTypeIri = valueTypeIri,
deleteComment = deleteComment,
deleteDate = deleteDate,
requestingUser = requestingUser,
apiRequestID = apiRequestID
)
}
}
}
/**
* Requests SPARQL for creating multiple values in a new, empty resource. The resource ''must'' be a new, empty
* resource, i.e. it must have no values. This message is used only internally by Knora, and is not part of the Knora
* v1 API. All pre-update checks must already have been performed before this message is sent. Specifically, the
* sender must ensure that:
*
* - The requesting user has permission to add values to the resource.
* - Each submitted value is consistent with the `knora-base:objectClassConstraint` of the property that is supposed
* to point to it.
* - The resource class has a suitable cardinality for each submitted value.
* - All required values are provided.
* - Redundant values are not submitted.
* - Any custom permissions in values have been validated and correctly formatted.
* - The target resources of link values and standoff links exist, if they are expected to exist.
* - The list nodes referred to by list values exist.
*
* A successful response will be a [[GenerateSparqlToCreateMultipleValuesResponseV2]].
*
* @param resourceIri the IRI of the resource in which values are to be created.
* @param values a map of property IRIs to the values to be added for each property.
* @param creationDate an xsd:dateTimeStamp that will be attached to the values.
* @param requestingUser the user that is creating the values.
*/
case class GenerateSparqlToCreateMultipleValuesRequestV2(resourceIri: IRI,
values: Map[SmartIri, Seq[GenerateSparqlForValueInNewResourceV2]],
creationDate: Instant,
requestingUser: UserADM) extends ValuesResponderRequestV2 {
lazy val flatValues: Iterable[GenerateSparqlForValueInNewResourceV2] = values.values.flatten
}
case class GenerateSparqlForValueInNewResourceV2(valueContent: ValueContentV2,
customValueIri: Option[SmartIri],
customValueUUID: Option[UUID],
customValueCreationDate: Option[Instant],
permissions: String) extends IOValueV2
/**
* Represents a response to a [[GenerateSparqlToCreateMultipleValuesRequestV2]], providing a string that can be
* included in the `INSERT DATA` clause of a SPARQL update operation to create the requested values.
*
* @param insertSparql a string containing statements that must be inserted into the INSERT clause of the SPARQL
* update that will create the values.
* @param unverifiedValues a map of property IRIs to [[UnverifiedValueV2]] objects describing
* the values that should have been created.
* @param hasStandoffLink `true` if the property `knora-base:hasStandoffLinkToValue` was automatically added.
*/
case class GenerateSparqlToCreateMultipleValuesResponseV2(insertSparql: String,
unverifiedValues: Map[SmartIri, Seq[UnverifiedValueV2]],
hasStandoffLink: Boolean)
/**
* The value of a Knora property in the context of some particular input or output operation.
* Any implementation of `IOValueV2` is an API operation-specific wrapper of a `ValueContentV2`.
*/
trait IOValueV2 {
def valueContent: ValueContentV2
}
/**
* Provides information about the deletion of a resource or value.
*
* @param deleteDate the date when the resource or value was deleted.
* @param maybeDeleteComment the reason why the resource or value was deleted.
*/
case class DeletionInfo(deleteDate: Instant,
maybeDeleteComment: Option[String]) {
def toJsonLDFields(targetSchema: ApiV2Schema): Map[IRI, JsonLDValue] = {
if (targetSchema != ApiV2Complex) {
throw AssertionException("DeletionInfo is available in JSON-LD only in the complex schema")
}
val maybeDeleteCommentStatement = maybeDeleteComment.map {
deleteComment => OntologyConstants.KnoraApiV2Complex.DeleteComment -> JsonLDString(deleteComment)
}
Map(
OntologyConstants.KnoraApiV2Complex.IsDeleted -> JsonLDBoolean(true),
OntologyConstants.KnoraApiV2Complex.DeleteDate -> JsonLDObject(
Map(
JsonLDConstants.TYPE -> JsonLDString(OntologyConstants.Xsd.DateTimeStamp),
JsonLDConstants.VALUE -> JsonLDString(deleteDate.toString)
)
)
) ++ maybeDeleteCommentStatement
}
}
/**
* Represents a Knora value as read from the triplestore.
*/
sealed trait ReadValueV2 extends IOValueV2 {
/**
* The IRI of the value.
*/
def valueIri: IRI
/**
* The user that created the value.
*/
def attachedToUser: IRI
/**
* The value's permissions.
*/
def permissions: String
/**
* The permission that the requesting user has on the value.
*/
def userPermission: EntityPermission
/**
* The date when the value was created.
*/
def valueCreationDate: Instant
/**
* The UUID shared by all the versions of this value.
*/
def valueHasUUID: UUID
/**
* The content of the value.
*/
def valueContent: ValueContentV2
/**
* The IRI of the previous version of this value. Not returned in API responses, but needed
* here for testing.
*/
def previousValueIri: Option[IRI]
/**
* If the value has been marked as deleted, information about its deletion.
*/
def deletionInfo: Option[DeletionInfo]
/**
* Converts this value to the specified ontology schema.
*
* @param targetSchema the schema that the value should be converted to.
*/
def toOntologySchema(targetSchema: ApiV2Schema): ReadValueV2
/**
* Converts this value to JSON-LD.
*
* @param targetSchema the target schema.
* @param settings the application settings.
* @return a JSON-LD representation of this value.
*/
def toJsonLD(targetSchema: ApiV2Schema, projectADM: ProjectADM, settings: KnoraSettingsImpl, schemaOptions: Set[SchemaOption]): JsonLDValue = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val valueContentAsJsonLD = valueContent.toJsonLDValue(
targetSchema = targetSchema,
projectADM = projectADM,
settings = settings,
schemaOptions = schemaOptions
)
// In the complex schema, add the value's IRI and type to the JSON-LD object that represents it.
targetSchema match {
case ApiV2Complex =>
// In the complex schema, the value must be represented as a JSON-LD object.
valueContentAsJsonLD match {
case jsonLDObject: JsonLDObject =>
// Add the value's metadata.
val valueSmartIri = valueIri.toSmartIri
val requiredMetadata = Map(
JsonLDConstants.ID -> JsonLDString(valueIri),
JsonLDConstants.TYPE -> JsonLDString(valueContent.valueType.toString),
OntologyConstants.KnoraApiV2Complex.AttachedToUser -> JsonLDUtil.iriToJsonLDObject(attachedToUser),
OntologyConstants.KnoraApiV2Complex.HasPermissions -> JsonLDString(permissions),
OntologyConstants.KnoraApiV2Complex.UserHasPermission -> JsonLDString(userPermission.toString),
OntologyConstants.KnoraApiV2Complex.ValueCreationDate -> JsonLDUtil.datatypeValueToJsonLDObject(
value = valueCreationDate.toString,
datatype = OntologyConstants.Xsd.DateTimeStamp.toSmartIri
),
OntologyConstants.KnoraApiV2Complex.ValueHasUUID -> JsonLDString(stringFormatter.base64EncodeUuid(valueHasUUID)),
OntologyConstants.KnoraApiV2Complex.ArkUrl -> JsonLDUtil.datatypeValueToJsonLDObject(
value = valueSmartIri.fromValueIriToArkUrl(valueUUID = valueHasUUID),
datatype = OntologyConstants.Xsd.Uri.toSmartIri
),
OntologyConstants.KnoraApiV2Complex.VersionArkUrl -> JsonLDUtil.datatypeValueToJsonLDObject(
value = valueSmartIri.fromValueIriToArkUrl(valueUUID = valueHasUUID, maybeTimestamp = Some(valueCreationDate)),
datatype = OntologyConstants.Xsd.Uri.toSmartIri
)
)
val valueHasCommentAsJsonLD: Option[(IRI, JsonLDValue)] = valueContent.comment.map {
definedComment => OntologyConstants.KnoraApiV2Complex.ValueHasComment -> JsonLDString(definedComment)
}
val deletionInfoAsJsonLD: Map[IRI, JsonLDValue] = deletionInfo match {
case Some(definedDeletionInfo) => definedDeletionInfo.toJsonLDFields(ApiV2Complex)
case None => Map.empty[IRI, JsonLDValue]
}
JsonLDObject(jsonLDObject.value ++ requiredMetadata ++ valueHasCommentAsJsonLD ++ deletionInfoAsJsonLD)
case other =>
throw AssertionException(s"Expected value $valueIri to be a represented as a JSON-LD object in the complex schema, but found $other")
}
case ApiV2Simple => valueContentAsJsonLD
}
}
}
/**
* A text value, or a page of standoff markup attached to a text value, as read from the triplestore.
*
* @param valueIri the IRI of the value.
* @param attachedToUser the user that created the value.
* @param permissions the permissions that the value grants to user groups.
* @param userPermission the permission that the requesting user has on the value.
* @param valueHasUUID the UUID shared by all the versions of this value.
* @param valueContent the content of the value.
* @param valueHasMaxStandoffStartIndex if this text value has standoff markup, the highest
* `knora-base:standoffTagHasEndIndex`
* used in its standoff tags.
* @param previousValueIri the IRI of the previous version of this value. Not returned in API responses, but needed
* here for testing.
* @param deletionInfo if this value has been marked as deleted, provides the date when it was
* deleted and the reason why it was deleted.
*/
case class ReadTextValueV2(valueIri: IRI,
attachedToUser: IRI,
permissions: String,
userPermission: EntityPermission,
valueCreationDate: Instant,
valueHasUUID: UUID,
valueContent: TextValueContentV2,
valueHasMaxStandoffStartIndex: Option[Int],
previousValueIri: Option[IRI],
deletionInfo: Option[DeletionInfo]) extends ReadValueV2 with KnoraReadV2[ReadTextValueV2] {
/**
* Converts this value to the specified ontology schema.
*
* @param targetSchema the target schema.
*/
override def toOntologySchema(targetSchema: ApiV2Schema): ReadTextValueV2 = {
copy(valueContent = valueContent.toOntologySchema(targetSchema))
}
override def toJsonLD(targetSchema: ApiV2Schema, projectADM: ProjectADM, settings: KnoraSettingsImpl, schemaOptions: Set[SchemaOption]): JsonLDValue = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val valueAsJsonLDValue: JsonLDValue = super.toJsonLD(
targetSchema = targetSchema,
projectADM = projectADM,
settings = settings,
schemaOptions = schemaOptions
)
// If this is the complex schema and separate standoff has been requested, and the text value has
// valueHasMaxStandoffStartIndex, add it along with textValueHasMarkup to the metadata returned with the value.
targetSchema match {
case ApiV2Complex =>
if (SchemaOptions.renderMarkupAsStandoff(targetSchema = ApiV2Complex, schemaOptions = schemaOptions)) {
valueHasMaxStandoffStartIndex match {
case Some(maxStartIndex) =>
val valueAsJsonLDObject: JsonLDObject = valueAsJsonLDValue match {
case jsonLDObject: JsonLDObject => jsonLDObject
case other => throw AssertionException(s"Expected value $valueIri to be a represented as a JSON-LD object in the complex schema, but found $other")
}
JsonLDObject(
valueAsJsonLDObject.value ++ Map(
OntologyConstants.KnoraApiV2Complex.TextValueHasMarkup -> JsonLDBoolean(true),
OntologyConstants.KnoraApiV2Complex.TextValueHasMaxStandoffStartIndex -> JsonLDInt(maxStartIndex)
)
)
case None => valueAsJsonLDValue
}
} else {
valueAsJsonLDValue
}
case ApiV2Simple => valueAsJsonLDValue
}
}
}
/**
* A link value as read from the triplestore.
*
* @param valueIri the IRI of the value.
* @param attachedToUser the user that created the value.
* @param permissions the permissions that the value grants to user groups.
* @param userPermission the permission that the requesting user has on the value.
* @param valueHasUUID the UUID shared by all the versions of this value.
* @param valueContent the content of the value.
* @param valueHasRefCount if this is a link value, its reference count. Not returned in API responses, but needed
* here for testing.
* @param previousValueIri the IRI of the previous version of this value. Not returned in API responses, but needed
* here for testing.
* @param deletionInfo if this value has been marked as deleted, provides the date when it was
* deleted and the reason why it was deleted.
*/
case class ReadLinkValueV2(valueIri: IRI,
attachedToUser: IRI,
permissions: String,
userPermission: EntityPermission,
valueCreationDate: Instant,
valueHasUUID: UUID,
valueContent: LinkValueContentV2,
valueHasRefCount: Int,
previousValueIri: Option[IRI] = None,
deletionInfo: Option[DeletionInfo]) extends ReadValueV2 with KnoraReadV2[ReadLinkValueV2] {
/**
* Converts this value to the specified ontology schema.
*
* @param targetSchema the target schema.
*/
override def toOntologySchema(targetSchema: ApiV2Schema): ReadLinkValueV2 = {
copy(valueContent = valueContent.toOntologySchema(targetSchema))
}
}
/**
* A non-text, non-link value as read from the triplestore.
*
* @param valueIri the IRI of the value.
* @param attachedToUser the user that created the value.
* @param permissions the permissions that the value grants to user groups.
* @param userPermission the permission that the requesting user has on the value.
* @param valueHasUUID the UUID shared by all the versions of this value.
* @param valueContent the content of the value.
* @param previousValueIri the IRI of the previous version of this value. Not returned in API responses, but needed
* here for testing.
* @param deletionInfo if this value has been marked as deleted, provides the date when it was
* deleted and the reason why it was deleted.
*/
case class ReadOtherValueV2(valueIri: IRI,
attachedToUser: IRI,
permissions: String,
userPermission: EntityPermission,
valueCreationDate: Instant,
valueHasUUID: UUID,
valueContent: ValueContentV2,
previousValueIri: Option[IRI],
deletionInfo: Option[DeletionInfo]) extends ReadValueV2 with KnoraReadV2[ReadOtherValueV2] {
/**
* Converts this value to the specified ontology schema.
*
* @param targetSchema the target schema.
*/
override def toOntologySchema(targetSchema: ApiV2Schema): ReadOtherValueV2 = {
copy(valueContent = valueContent.toOntologySchema(targetSchema))
}
}
/**
* Represents a Knora value to be created in an existing resource.
*
* @param resourceIri the resource the new value should be attached to.
* @param resourceClassIri the resource class that the client believes the resource belongs to.
* @param propertyIri the property of the new value. If the client wants to create a link, this must be a link value property.
* @param valueContent the content of the new value. If the client wants to create a link, this must be a [[LinkValueContentV2]].
* @param valueIri the optional custom IRI supplied for the value.
* @param valueUUID the optional custom UUID supplied for the value.
* @param valueCreationDate the optional custom creation date supplied for the value. If not supplied,
* the current time will be used.
* @param permissions the permissions to be given to the new value. If not provided, these will be taken from defaults.
*/
case class CreateValueV2(resourceIri: IRI,
resourceClassIri: SmartIri,
propertyIri: SmartIri,
valueContent: ValueContentV2,
valueIri: Option[SmartIri] = None,
valueUUID: Option[UUID] = None,
valueCreationDate: Option[Instant] = None,
permissions: Option[String] = None) extends IOValueV2
/**
* A trait for classes representing information to be updated in a value.
*/
trait UpdateValueV2 {
/**
* The IRI of the resource containing the value.
*/
val resourceIri: IRI
/**
* The external IRI of the resource class.
*/
val resourceClassIri: SmartIri
/**
* The external IRI of the property pointing to the value.
*/
val propertyIri: SmartIri
/**
* The value IRI.
*/
val valueIri: IRI
/**
* A custom value creation date.
*/
val valueCreationDate: Option[Instant]
}
/**
* A new version of a value of a Knora property to be created.
*
* @param resourceIri the resource that the current value version is attached to.
* @param resourceClassIri the resource class that the client believes the resource belongs to.
* @param propertyIri the property that the client believes points to the value. If the value is a link value,
* this must be a link value property.
* @param valueIri the IRI of the value to be updated.
* @param valueContent the content of the new version of the value.
* @param permissions the permissions to be attached to the new value version.
* @param valueCreationDate an optional custom creation date to be attached to the new value version. If not provided,
* the current time will be used.
* @param newValueVersionIri an optional IRI to be used for the new value version. If not provided, a random IRI
* will be generated.
*/
case class UpdateValueContentV2(resourceIri: IRI,
resourceClassIri: SmartIri,
propertyIri: SmartIri,
valueIri: IRI,
valueContent: ValueContentV2,
permissions: Option[String] = None,
valueCreationDate: Option[Instant] = None,
newValueVersionIri: Option[SmartIri] = None) extends IOValueV2 with UpdateValueV2
/**
* New permissions for a value.
*
* @param resourceIri the resource that the current value version is attached to.
* @param resourceClassIri the resource class that the client believes the resource belongs to.
* @param propertyIri the property that the client believes points to the value. If the value is a link value,
* this must be a link value property.
* @param valueIri the IRI of the value to be updated.
* @param valueType the IRI of the value type.
* @param permissions the permissions to be attached to the new value version.
* @param valueCreationDate an optional custom creation date to be attached to the new value version. If not provided,
* the current time will be used.
* @param newValueVersionIri an optional IRI to be used for the new value version. If not provided, a random IRI
* will be generated.
*/
case class UpdateValuePermissionsV2(resourceIri: IRI,
resourceClassIri: SmartIri,
propertyIri: SmartIri,
valueIri: IRI,
valueType: SmartIri,
permissions: String,
valueCreationDate: Option[Instant] = None,
newValueVersionIri: Option[SmartIri] = None) extends UpdateValueV2
/**
* The IRI and content of a new value or value version whose existence in the triplestore needs to be verified.
*
* @param newValueIri the IRI that was assigned to the new value.
* @param newValueUUID the UUID attached to the new value.
* @param valueContent the content of the new value (unescaped, as it would be read from the triplestore).
* @param permissions the permissions of the new value.
* @param creationDate the new value's creation date.
*/
case class UnverifiedValueV2(newValueIri: IRI,
newValueUUID: UUID,
valueContent: ValueContentV2,
permissions: String,
creationDate: Instant)
/**
* The content of the value of a Knora property.
*/
sealed trait ValueContentV2 extends KnoraContentV2[ValueContentV2] {
protected implicit def stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
/**
* The IRI of the value type.
*/
def valueType: SmartIri
/**
* The string representation of this `ValueContentV2`.
*/
def valueHasString: String
/**
* a comment on this [[ValueContentV2]], if any.
*/
def comment: Option[String]
/**
* Converts this value to the specified ontology schema.
*
* @param targetSchema the target schema.
*/
def toOntologySchema(targetSchema: OntologySchema): ValueContentV2
/**
* A representation of the `ValueContentV2` as a [[JsonLDValue]].
*
* @param targetSchema the API schema to be used.
* @param settings the configuration options.
* @return a [[JsonLDValue]] that can be used to generate JSON-LD representing this value.
*/
def toJsonLDValue(targetSchema: ApiV2Schema, projectADM: ProjectADM, settings: KnoraSettingsImpl, schemaOptions: Set[SchemaOption]): JsonLDValue
/**
* Undoes the SPARQL-escaping of strings in this [[ValueContentV2]].
*
* @return the same [[ValueContentV2]] with its strings unescaped.