/
ValueUtilV1.scala
1145 lines (1011 loc) · 47 KB
/
ValueUtilV1.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 © 2021 - 2022 Swiss National Data and Service Center for the Humanities and/or DaSCH Service Platform contributors.
* SPDX-License-Identifier: Apache-2.0
*/
package org.knora.webapi.messages.util
import akka.actor.ActorRef
import akka.pattern._
import akka.util.Timeout
import org.knora.webapi._
import org.knora.webapi.exceptions.{
InconsistentRepositoryDataException,
NotImplementedException,
OntologyConstraintException
}
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.GroupedProps._
import org.knora.webapi.messages.util.rdf.VariableResultsRow
import org.knora.webapi.messages.util.standoff.StandoffTagUtilV2
import org.knora.webapi.messages.v1.responder.ontologymessages._
import org.knora.webapi.messages.v1.responder.resourcemessages.{
LiteralValueType,
LocationV1,
ResourceCreateValueObjectResponseV1,
ResourceCreateValueResponseV1
}
import org.knora.webapi.messages.v1.responder.valuemessages._
import org.knora.webapi.messages.v2.responder.standoffmessages._
import org.knora.webapi.messages.{OntologyConstants, StringFormatter}
import org.knora.webapi.settings.KnoraSettingsImpl
import scala.concurrent.{ExecutionContext, Future}
/**
* Converts data from SPARQL query results into [[ApiValueV1]] objects.
*/
class ValueUtilV1(private val settings: KnoraSettingsImpl) {
private val stringFormatter = StringFormatter.getGeneralInstance
/**
* Given a [[ValueProps]] containing details of a `knora-base:Value` object, creates a [[ApiValueV1]].
*
* @param valueProps a [[GroupedProps.ValueProps]] resulting from querying the `Value`, in which the keys are RDF predicates,
* and the values are lists of the objects of each predicate.
* @param featureFactoryConfig the feature factory configuration.
* @return a [[ApiValueV1]] representing the `Value`.
*/
def makeValueV1(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val valueTypeIri = valueProps.literalData(OntologyConstants.Rdf.Type).literals.head
valueTypeIri match {
case OntologyConstants.KnoraBase.TextValue =>
makeTextValue(valueProps, responderManager, featureFactoryConfig, userProfile)
case OntologyConstants.KnoraBase.IntValue => makeIntValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.DecimalValue => makeDecimalValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.BooleanValue => makeBooleanValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.UriValue => makeUriValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.DateValue => makeDateValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.ColorValue => makeColorValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.GeomValue => makeGeomValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.GeonameValue => makeGeonameValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.ListValue => makeListValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.IntervalValue => makeIntervalValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.TimeValue => makeTimeValue(valueProps, responderManager, userProfile)
case OntologyConstants.KnoraBase.StillImageFileValue =>
makeStillImageValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.TextFileValue =>
makeTextFileValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.AudioFileValue =>
makeAudioFileValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.MovingImageFileValue =>
makeVideoFileValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.DocumentFileValue =>
makeDocumentFileValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.ArchiveFileValue =>
makeArchiveFileValue(valueProps, projectShortcode, responderManager, userProfile)
case OntologyConstants.KnoraBase.LinkValue => makeLinkValue(valueProps, responderManager, userProfile)
}
}
def makeSipiImagePreviewGetUrlFromFilename(projectShortcode: String, filename: String): String =
s"${settings.externalSipiIIIFGetUrl}/$projectShortcode/$filename/full/!128,128/0/default.jpg"
/**
* Creates a IIIF URL for accessing an image file via Sipi.
*
* @param imageFileValueV1 the image file value representing the image.
* @return a Sipi IIIF URL.
*/
def makeSipiImageGetUrlFromFilename(imageFileValueV1: StillImageFileValueV1): String =
s"${settings.externalSipiIIIFGetUrl}/${imageFileValueV1.projectShortcode}/${imageFileValueV1.internalFilename}/full/${imageFileValueV1.dimX},${imageFileValueV1.dimY}/0/default.jpg"
/**
* Creates a URL for accessing a document file via Sipi.
*
* @param documentFileValueV1 the document file value.
* @return a Sipi URL.
*/
def makeSipiDocumentGetUrlFromFilename(documentFileValueV1: DocumentFileValueV1): String =
s"${settings.externalSipiIIIFGetUrl}/${documentFileValueV1.projectShortcode}/${documentFileValueV1.internalFilename}/file"
/**
* Creates a URL for accessing a archive file via Sipi.
*
* @param archiveFileValueV1 the archive file value.
* @return a Sipi URL.
*/
def makeSipiArchiveGetUrlFromFilename(archiveFileValueV1: ArchiveFileValueV1): String =
s"${settings.externalSipiIIIFGetUrl}/${archiveFileValueV1.projectShortcode}/${archiveFileValueV1.internalFilename}/file"
/**
* Creates a URL for accessing a text file via Sipi.
*
* @param textFileValue the text file value representing the text file.
* @return a Sipi URL.
*/
def makeSipiTextFileGetUrlFromFilename(textFileValue: TextFileValueV1): String =
s"${settings.externalSipiBaseUrl}/${textFileValue.projectShortcode}/${textFileValue.internalFilename}"
/**
* Creates a URL for accessing an audio file via Sipi.
*
* @param audioFileValue the file value representing the audio file.
* @return a Sipi URL.
*/
def makeSipiAudioFileGetUrlFromFilename(audioFileValue: AudioFileValueV1): String =
s"${settings.externalSipiIIIFGetUrl}/${audioFileValue.projectShortcode}/${audioFileValue.internalFilename}/file"
/**
* Creates a URL for accessing a video file via Sipi.
*
* @param videoFileValue the file value representing the video file.
* @return a Sipi URL.
*/
def makeSipiVideoFileGetUrlFromFilename(videoFileValue: MovingImageFileValueV1): String =
s"${settings.externalSipiIIIFGetUrl}/${videoFileValue.projectShortcode}/${videoFileValue.internalFilename}/file"
// A Map of MIME types to Knora API v1 binary format name.
private val mimeType2V1Format = new ErrorHandlingMap(
Map(
"application/octet-stream" -> "BINARY-UNKNOWN",
"image/jpeg" -> "JPEG",
"image/jp2" -> "JPEG2000",
"image/jpx" -> "JPEG2000",
"application/pdf" -> "PDF",
"application/postscript" -> "POSTSCRIPT",
"application/vnd.ms-powerpoint" -> "PPT",
"application/vnd.openxmlformats-officedocument.presentationml.presentation" -> "PPTX",
"application/rtf" -> "RTF",
"video/salsah" -> "WEBVIDEO",
"text/sgml" -> "SGML",
"image/tiff" -> "TIFF",
"application/msword" -> "WORD",
"application/vnd.openxmlformats-officedocument.wordprocessingml.document" -> "WORDX",
"application/vnd.ms-excel" -> "XLS",
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" -> "XLSX",
"application/xml" -> "XML",
"text/xml" -> "XML",
"text/csv" -> "CSV",
"text/plain" -> "TEXT",
"application/zip" -> "ZIP",
"application/x-compressed-zip" -> "ZIP",
"audio/mpeg" -> "AUDIO",
"audio/mp4" -> "AUDIO",
"audio/wav" -> "AUDIO",
"audio/x-wav" -> "AUDIO",
"audio/vnd.wave" -> "AUDIO",
"video/mp4" -> "VIDEO"
),
{ key: String =>
s"Unknown MIME type: $key"
}
)
/**
* Converts a [[FileValueV1]] (which is used internally by the Knora API server) to a [[LocationV1]] (which is
* used in certain API responses).
*
* @param fileValueV1 a [[FileValueV1]].
* @return a [[LocationV1]].
*/
def fileValueV12LocationV1(fileValueV1: FileValueV1): LocationV1 =
fileValueV1 match {
case stillImageFileValueV1: StillImageFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(stillImageFileValueV1.internalMimeType),
origname = stillImageFileValueV1.originalFilename,
nx = Some(stillImageFileValueV1.dimX),
ny = Some(stillImageFileValueV1.dimY),
path = makeSipiImageGetUrlFromFilename(stillImageFileValueV1)
)
case documentFileValueV1: DocumentFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(documentFileValueV1.internalMimeType),
origname = documentFileValueV1.originalFilename,
nx = documentFileValueV1.dimX,
ny = documentFileValueV1.dimY,
path = makeSipiDocumentGetUrlFromFilename(documentFileValueV1)
)
case archiveFileValueV1: ArchiveFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(archiveFileValueV1.internalMimeType),
origname = archiveFileValueV1.originalFilename,
path = makeSipiArchiveGetUrlFromFilename(archiveFileValueV1)
)
case textFileValue: TextFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(textFileValue.internalMimeType),
origname = textFileValue.originalFilename,
path = makeSipiTextFileGetUrlFromFilename(textFileValue)
)
case audioFileValue: AudioFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(audioFileValue.internalMimeType),
origname = audioFileValue.originalFilename,
path = makeSipiAudioFileGetUrlFromFilename(audioFileValue)
)
case videoFileValue: MovingImageFileValueV1 =>
LocationV1(
format_name = mimeType2V1Format(videoFileValue.internalMimeType),
origname = videoFileValue.originalFilename,
path = makeSipiVideoFileGetUrlFromFilename(videoFileValue)
)
case otherType => throw NotImplementedException(s"Type not yet implemented: ${otherType.valueTypeIri}")
}
/**
* Creates a URL pointing to the given resource class icon. From the resource class IRI it gets the ontology specific path, i.e. the ontology name.
* If the resource class IRI is "http://www.knora.org/ontology/knora-base#Region", the ontology name would be "knora-base".
* To the base path, the icon name is appended. In case of a region with the icon name "region.gif",
* "http://salsahapp:port/project-icons-basepath/knora-base/region.gif" is returned.
*
* This method requires the IRI segment before the last slash to be a unique identifier for all the ontologies used with Knora..
*
* @param resourceClassIri the IRI of the resource class in question.
* @param iconsSrc the name of the icon file.
*/
def makeResourceClassIconURL(resourceClassIri: IRI, iconsSrc: String): IRI = {
// get ontology name, e.g. "knora-base" from "http://www.knora.org/ontology/knora-base#Region"
// add +1 to ignore the slash
val ontologyName =
resourceClassIri.substring(resourceClassIri.lastIndexOf('/') + 1, resourceClassIri.lastIndexOf('#'))
// create URL: combine salsah-address and port, project icons base path, ontology name, icon name
settings.salsah1BaseUrl + settings.salsah1ProjectIconsBasePath + ontologyName + '/' + iconsSrc
}
/**
* Creates [[ValueProps]] from a List of [[VariableResultsRow]] representing a value object
* (the triples where the given value object is the subject in).
*
* A [[VariableResultsRow]] is expected to have the following members (SPARQL variable names):
*
* - objPred: the object predicate (e.g. http://www.knora.org/ontology/knora-base#valueHasString).
* - objObj: The string representation of the value assigned to objPred.
*
* In one given row, objPred **must** indicate the type of the given value object using rdfs:type (e.g. http://www.knora.org/ontology/knora-base#TextValue)
*
* In case the given value object contains standoff (objPred is http://www.knora.org/ontology/knora-base#valueHasStandoff),
* it has the following additional members compared those mentioned above:
*
* - predStandoff: the standoff predicate (e.g. http://www.knora.org/ontology/knora-base#standoffHasStart)
* - objStandoff: the string representation of the value assigned to predStandoff
*
* @param valueIri the IRI of the value that was queried.
* @param objRows SPARQL results.
* @return a [[ValueProps]] representing the SPARQL results.
*/
def createValueProps(valueIri: IRI, objRows: Seq[VariableResultsRow]): ValueProps = {
val groupedValueObject = groupKnoraValueObjectPredicateRows(objRows.map(_.rowMap))
ValueProps(
valueIri,
new ErrorHandlingMap(
groupedValueObject.valuesLiterals,
{ key: IRI =>
s"Predicate $key not found in value $valueIri"
}
),
groupedValueObject.standoff
)
}
/**
* Converts three lists of SPARQL query results representing all the properties of a resource into a [[GroupedPropertiesByType]].
*
* Each [[VariableResultsRow]] is expected to have the following SPARQL variables:
*
* - prop: the IRI of the resource property (e.g. http://www.knora.org/ontology/knora-base#hasComment)
* - obj: the IRI of the object that the property points to, which may be either a value object (an ordinary value or a reification) or another resource
* - objPred: the IRI of each predicate of `obj` (e.g. for its literal contents, or for its permissions)
* - objObj: the object of each `objPred`
*
* The remaining members are identical to those documented in [[createValueProps]].
*
* @param rowsWithOrdinaryValues SPARQL result rows describing properties that point to ordinary values (not link values).
* @param rowsWithLinkValues SPARQL result rows describing properties that point link values (reifications of links to resources).
* @param rowsWithLinks SPARQL result rows describing properties that point to resources.
* @return a [[GroupedPropertiesByType]] representing the SPARQL results.
*/
def createGroupedPropsByType(
rowsWithOrdinaryValues: Seq[VariableResultsRow],
rowsWithLinkValues: Seq[VariableResultsRow],
rowsWithLinks: Seq[VariableResultsRow]
): GroupedPropertiesByType =
GroupedPropertiesByType(
groupedOrdinaryValueProperties = groupKnoraPropertyRows(rowsWithOrdinaryValues),
groupedLinkValueProperties = groupKnoraPropertyRows(rowsWithLinkValues),
groupedLinkProperties = groupKnoraPropertyRows(rowsWithLinks)
)
/**
* Checks that a value type is valid for the `knora-base:objectClassConstraint` of a property.
*
* @param propertyIri the IRI of the property.
* @param valueType the IRI of the value type.
* @param propertyObjectClassConstraint the IRI of the property's `knora-base:objectClassConstraint`.
* @param responderManager a reference to the Knora API Server responder manager.
* @return A future containing Unit on success, or a failed future if the value type is not valid for the property's range.
*/
def checkValueTypeForPropertyObjectClassConstraint(
propertyIri: IRI,
valueType: IRI,
propertyObjectClassConstraint: IRI,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[Unit] =
if (propertyObjectClassConstraint == valueType) {
Future.successful(())
} else {
for {
checkSubClassResponse <- (responderManager ? CheckSubClassRequestV1(
subClassIri = valueType,
superClassIri = propertyObjectClassConstraint,
userProfile = userProfile
)).mapTo[CheckSubClassResponseV1]
_ = if (!checkSubClassResponse.isSubClass) {
throw OntologyConstraintException(
s"Property $propertyIri requires a value of type $propertyObjectClassConstraint"
)
}
} yield ()
}
/**
* Converts a [[CreateValueResponseV1]] returned by the values responder on value creation
* to the expected format for the resources responder [[ResourceCreateValueResponseV1]], which describes a value
* added to a new resource.
*
* @param resourceIri the IRI of the created resource.
* @param creatorIri the creator of the resource.
* @param propertyIri the property the valueResponse belongs to.
* @param valueResponse the value that has been attached to the resource.
* @return a [[ResourceCreateValueResponseV1]] representing the created value.
*/
def convertCreateValueResponseV1ToResourceCreateValueResponseV1(
resourceIri: IRI,
creatorIri: IRI,
propertyIri: IRI,
valueResponse: CreateValueResponseV1
): ResourceCreateValueResponseV1 = {
val basicObjectResponse = ResourceCreateValueObjectResponseV1(
textval = Map(LiteralValueType.StringValue -> valueResponse.value.toString),
resource_id = Map(LiteralValueType.StringValue -> resourceIri),
property_id = Map(LiteralValueType.StringValue -> propertyIri),
person_id = Map(LiteralValueType.StringValue -> creatorIri),
order = Map(LiteralValueType.IntegerValue -> 1) // TODO: include correct order: valueHasOrder
)
val objectResponse = valueResponse.value match {
case integerValue: IntegerValueV1 =>
basicObjectResponse.copy(
ival = Some(Map(LiteralValueType.IntegerValue -> integerValue.ival))
)
case decimalValue: DecimalValueV1 =>
basicObjectResponse.copy(
dval = Some(Map(LiteralValueType.DecimalValue -> decimalValue.dval))
)
case dateValue: DateValueV1 =>
val julianDayCountValue = DateUtilV1.dateValueV1ToJulianDayNumberValueV1(dateValue)
basicObjectResponse.copy(
dateval1 = Some(Map(LiteralValueType.StringValue -> dateValue.dateval1)),
dateval2 = Some(Map(LiteralValueType.StringValue -> dateValue.dateval2)),
dateprecision1 = Some(Map(LiteralValueType.StringValue -> julianDayCountValue.dateprecision1)),
dateprecision2 = Some(Map(LiteralValueType.StringValue -> julianDayCountValue.dateprecision2)),
calendar = Some(Map(LiteralValueType.StringValue -> julianDayCountValue.calendar))
)
case _: TextValueV1 => basicObjectResponse
case _: LinkV1 => basicObjectResponse
case _: StillImageFileValueV1 => basicObjectResponse
case _: TextFileValueV1 => basicObjectResponse
case _: DocumentFileValueV1 => basicObjectResponse
case _: ArchiveFileValueV1 => basicObjectResponse
case _: AudioFileValueV1 => basicObjectResponse
case _: MovingImageFileValueV1 => basicObjectResponse
case _: HierarchicalListValueV1 => basicObjectResponse
case _: ColorValueV1 => basicObjectResponse
case _: GeomValueV1 => basicObjectResponse
case intervalValue: IntervalValueV1 =>
basicObjectResponse.copy(
timeval1 = Some(Map(LiteralValueType.DecimalValue -> intervalValue.timeval1)),
timeval2 = Some(Map(LiteralValueType.DecimalValue -> intervalValue.timeval2))
)
case _: TimeValueV1 => basicObjectResponse
case _: GeonameValueV1 => basicObjectResponse
case _: BooleanValueV1 => basicObjectResponse
case _: UriValueV1 => basicObjectResponse
case other =>
throw new Exception(s"Resource creation response format not implemented for value type ${other.valueTypeIri}")
}
ResourceCreateValueResponseV1(
value = objectResponse,
id = valueResponse.id
)
}
/**
* Creates a tuple that can be turned into a [[ValueProps]] representing both literal values and standoff.
*
* It expects the members documented in [[createValueProps]].
*
* @param objRows a value object's predicates.
* @return a [[GroupedValueObject]] containing the values (literal or linking) and standoff nodes if given.
*/
private def groupKnoraValueObjectPredicateRows(objRows: Seq[Map[String, String]]): GroupedValueObject = {
// get rid of the value object IRI `obj` and group by predicate IRI `objPred` (e.g. `valueHasString`)
val valuesGroupedByPredicate = objRows.map(_ - "obj").groupBy(_("objPred"))
valuesGroupedByPredicate.foldLeft(
GroupedValueObject(valuesLiterals = Map.empty[String, ValueLiterals], standoff = Map.empty[IRI, Map[IRI, String]])
) { case (acc: GroupedValueObject, (objPredIri: IRI, values: Seq[Map[String, String]])) =>
if (objPredIri == OntologyConstants.KnoraBase.ValueHasStandoff) {
// standoff information
val groupedByStandoffNodeIri: Map[IRI, Seq[Map[String, String]]] = values.groupBy(_("objObj"))
val standoffNodeAssertions: Map[IRI, Map[String, String]] = groupedByStandoffNodeIri.map {
case (standoffNodeIri: IRI, values: Seq[Map[String, String]]) =>
val valuesMap: Map[String, String] = values.map {
// make a Map with the standoffPred as the key and the objStandoff as the value
value: Map[String, String] =>
Map(value("predStandoff") -> value("objStandoff"))
}
.foldLeft(Map.empty[String, String]) {
// for each standoff node, we want to have just one Map
// this foldLeft turns a Sequence of Maps into one Map (a predicate can only occur once)
case (nodeValues: Map[String, String], value: Map[String, String]) =>
nodeValues ++ value
}
standoffNodeIri -> valuesMap
}
acc.copy(
standoff = acc.standoff ++ standoffNodeAssertions
)
} else {
// non standoff value
val value: (String, ValueLiterals) = (
objPredIri,
ValueLiterals(values.map { value: Map[String, String] =>
value("objObj")
})
)
acc.copy(
valuesLiterals = acc.valuesLiterals + value
)
}
}
}
/**
* Given a list of result rows from the `get-resource-properties-and-values` SPARQL query, groups the rows first by property,
* then by property object, and finally by property object predicate. In case the results contain standoff information, the standoff nodes are grouped
* according to their blank node IRI. If the first row of results has a `linkValue` column, this is taken to mean that the property
* is a link property and that the value of `linkValue` is the IRI of the corresponding `knora-base:LinkValue`; that IRI is then
* added to the literals in the results, with the key [[OntologyConstants.KnoraBase.LinkValue]].
*
* For example, suppose we have the following rows for a property that points to Knora values.
*
* {{{
* prop obj objPred objObj
* ---------------------------------------------------------------------------------------------------------------------------------------------------
* incunabula:pagenum http://rdfh.ch/8a0b1e75/values/61cb927602 knora-base:valueHasString a1r, Titelblatt
* incunabula:pagenum http://rdfh.ch/8a0b1e75/values/61cb927602 knora-base:hasViewPermission knora-base:KnownUser
* incunabula:pagenum http://rdfh.ch/8a0b1e75/values/61cb927602 knora-base:hasViewPermission knora-base:UnknownUser
* }}}
*
* The result will be a [[GroupedProperties]] containing a [[ValueProps]] with two keys, `valueHasString` and `hasPermission`.
*
* @param rows the SPARQL query result rows to group, which are expected to contain the columns given in the description of the `createGroupedPropsByType`
* method.
* @return a [[GroupedProperties]] representing the SPARQL results.
*/
private def groupKnoraPropertyRows(rows: Seq[VariableResultsRow]): GroupedProperties = {
val gp: Map[String, ValueObjects] = rows.groupBy(_.rowMap("prop")).map {
// grouped by resource property (e.g. hasComment)
case (resProp: String, rows: Seq[VariableResultsRow]) =>
val vo = (
resProp,
rows.map(_.rowMap - "prop").groupBy(_("obj")).map {
// grouped by value object IRI
case (objIri: IRI, objRows: Seq[Map[String, String]]) =>
val groupedValueObject = groupKnoraValueObjectPredicateRows(objRows)
val vp: ValueProps = ValueProps(
valueIri = objIri,
new ErrorHandlingMap(
groupedValueObject.valuesLiterals,
{ key: IRI =>
s"Predicate $key not found for property object $objIri"
}
),
groupedValueObject.standoff
)
(objIri, vp)
}
)
(resProp, GroupedProps.ValueObjects(vo._2))
}
GroupedProps.GroupedProperties(gp)
}
/**
* Converts a [[ValueProps]] into an [[IntegerValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return an [[IntegerValueV1]].
*/
private def makeIntValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(IntegerValueV1(predicates(OntologyConstants.KnoraBase.ValueHasInteger).literals.head.toInt))
}
/**
* Converts a [[ValueProps]] into a [[DecimalValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[DecimalValueV1]].
*/
private def makeDecimalValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(DecimalValueV1(BigDecimal(predicates(OntologyConstants.KnoraBase.ValueHasDecimal).literals.head)))
}
/**
* Converts a [[ValueProps]] into a [[BooleanValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[BooleanValueV1]].
*/
private def makeBooleanValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(BooleanValueV1(predicates(OntologyConstants.KnoraBase.ValueHasBoolean).literals.head.toBoolean))
}
/**
* Converts a [[ValueProps]] into a [[UriValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[UriValueV1]].
*/
private def makeUriValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(UriValueV1(predicates(OntologyConstants.KnoraBase.ValueHasUri).literals.head))
}
/**
* Converts a [[ValueProps]] into a [[DateValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[DateValueV1]].
*/
private def makeDateValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
val julianDayNumberValueV1 = JulianDayNumberValueV1(
dateval1 = predicates(OntologyConstants.KnoraBase.ValueHasStartJDN).literals.head.toInt,
dateval2 = predicates(OntologyConstants.KnoraBase.ValueHasEndJDN).literals.head.toInt,
dateprecision1 =
KnoraPrecisionV1.lookup(predicates(OntologyConstants.KnoraBase.ValueHasStartPrecision).literals.head),
dateprecision2 =
KnoraPrecisionV1.lookup(predicates(OntologyConstants.KnoraBase.ValueHasEndPrecision).literals.head),
calendar = KnoraCalendarV1.lookup(predicates(OntologyConstants.KnoraBase.ValueHasCalendar).literals.head)
)
Future(DateUtilV1.julianDayNumberValueV1ToDateValueV1(julianDayNumberValueV1))
}
/**
* Converts a [[ValueProps]] into an [[IntervalValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return an [[IntervalValueV1]].
*/
private def makeIntervalValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
IntervalValueV1(
timeval1 = BigDecimal(predicates(OntologyConstants.KnoraBase.ValueHasIntervalStart).literals.head),
timeval2 = BigDecimal(predicates(OntologyConstants.KnoraBase.ValueHasIntervalEnd).literals.head)
)
)
}
/**
* Converts a [[ValueProps]] into a [[TimeValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[TimeValueV1]].
*/
private def makeTimeValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
val timeStampStr = predicates(OntologyConstants.KnoraBase.ValueHasTimeStamp).literals.head
Future(
TimeValueV1(
timeStamp = stringFormatter.xsdDateTimeStampToInstant(
timeStampStr,
throw InconsistentRepositoryDataException(s"Can't parse timestamp: $timeStampStr")
)
)
)
}
/**
* Creates a [[TextValueWithStandoffV1]] from the given string and the standoff nodes.
*
* @param utf8str the string representation.
* @param valueProps the properties of the TextValue with standoff.
* @param responderManager the responder manager.
* @param featureFactoryConfig the feature factory configuration.
* @param userProfile the client that is making the request.
* @return a [[TextValueWithStandoffV1]].
*/
private def makeTextValueWithStandoff(
utf8str: String,
language: Option[String] = None,
valueProps: ValueProps,
responderManager: ActorRef,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[TextValueWithStandoffV1] = {
// get the IRI of the mapping
val mappingIri = valueProps.literalData
.getOrElse(
OntologyConstants.KnoraBase.ValueHasMapping,
throw InconsistentRepositoryDataException(
s"no mapping IRI associated with standoff belonging to textValue ${valueProps.valueIri}"
)
)
.literals
.head
for {
// get the mapping and the related standoff entities
// v2 responder is used here directly, v1 responder would inernally use v2 responder anyway and do unnecessary back and forth conversions
mappingResponse: GetMappingResponseV2 <- (responderManager ? GetMappingRequestV2(
mappingIri = mappingIri,
featureFactoryConfig = featureFactoryConfig,
requestingUser = userProfile
)).mapTo[GetMappingResponseV2]
standoffTags: Seq[StandoffTagV2] <- StandoffTagUtilV2.createStandoffTagsV2FromSelectResults(
standoffAssertions = valueProps.standoff,
responderManager = responderManager,
requestingUser = userProfile
)
} yield TextValueWithStandoffV1(
utf8str = utf8str,
language = language,
standoff = standoffTags,
mappingIri = mappingIri,
mapping = mappingResponse.mapping,
resource_reference = stringFormatter.getResourceIrisFromStandoffTags(standoffTags)
)
}
/**
* Creates a [[TextValueSimpleV1]] from the given string.
*
* @param utf8str the string representation of the TextValue.
* @return a [[TextValueSimpleV1]].
*/
private def makeTextValueSimple(utf8str: String, language: Option[String] = None)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[TextValueSimpleV1] =
Future(
TextValueSimpleV1(
utf8str = utf8str,
language = language
)
)
/**
* Converts a [[ValueProps]] into a [[TextValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @param featureFactoryConfig the feature factory configuration.
* @return a [[TextValueV1]].
*/
private def makeTextValue(
valueProps: ValueProps,
responderManager: ActorRef,
featureFactoryConfig: FeatureFactoryConfig,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val valueHasString: String = valueProps.literalData
.get(OntologyConstants.KnoraBase.ValueHasString)
.map(_.literals.head)
.getOrElse(
throw InconsistentRepositoryDataException(s"Value ${valueProps.valueIri} has no knora-base:valueHasString")
)
val valueHasLanguage: Option[String] =
valueProps.literalData.get(OntologyConstants.KnoraBase.ValueHasLanguage).map(_.literals.head)
if (valueProps.standoff.nonEmpty) {
// there is standoff markup
makeTextValueWithStandoff(
utf8str = valueHasString,
language = valueHasLanguage,
valueProps = valueProps,
responderManager = responderManager,
featureFactoryConfig = featureFactoryConfig,
userProfile = userProfile
)
} else {
// there is no standoff markup
makeTextValueSimple(valueHasString, valueHasLanguage)
}
}
/**
* Converts a [[ValueProps]] into a [[ColorValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[ColorValueV1]].
*/
private def makeColorValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(ColorValueV1(predicates(OntologyConstants.KnoraBase.ValueHasColor).literals.head))
}
/**
* Converts a [[ValueProps]] into a [[GeomValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[GeomValueV1]].
*/
private def makeGeomValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(GeomValueV1(predicates(OntologyConstants.KnoraBase.ValueHasGeometry).literals.head))
}
/**
* Converts a [[ValueProps]] into a [[HierarchicalListValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[HierarchicalListValueV1]].
*/
private def makeListValue(valueProps: ValueProps, responderManager: ActorRef, userProfile: UserADM)(implicit
timeout: Timeout,
executionContext: ExecutionContext
): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(HierarchicalListValueV1(predicates(OntologyConstants.KnoraBase.ValueHasListNode).literals.head))
}
/**
* Converts a [[ValueProps]] into a [[StillImageFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[StillImageFileValueV1]].
*/
private def makeStillImageValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
StillImageFileValueV1(
internalMimeType = predicates(OntologyConstants.KnoraBase.InternalMimeType).literals.head,
internalFilename = predicates(OntologyConstants.KnoraBase.InternalFilename).literals.head,
originalFilename = predicates.get(OntologyConstants.KnoraBase.OriginalFilename).map(_.literals.head),
projectShortcode = projectShortcode,
dimX = predicates(OntologyConstants.KnoraBase.DimX).literals.head.toInt,
dimY = predicates(OntologyConstants.KnoraBase.DimY).literals.head.toInt
)
)
}
/**
* Converts a [[ValueProps]] into a [[TextFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[TextFileValueV1]].
*/
private def makeTextFileValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
TextFileValueV1(
internalMimeType = predicates(OntologyConstants.KnoraBase.InternalMimeType).literals.head,
internalFilename = predicates(OntologyConstants.KnoraBase.InternalFilename).literals.head,
originalFilename = predicates.get(OntologyConstants.KnoraBase.OriginalFilename).map(_.literals.head),
projectShortcode = projectShortcode
)
)
}
/**
* Converts a [[ValueProps]] into a [[DocumentFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[DocumentFileValueV1]].
*/
private def makeDocumentFileValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
DocumentFileValueV1(
internalMimeType = predicates(OntologyConstants.KnoraBase.InternalMimeType).literals.head,
internalFilename = predicates(OntologyConstants.KnoraBase.InternalFilename).literals.head,
originalFilename = predicates.get(OntologyConstants.KnoraBase.OriginalFilename).map(_.literals.head),
projectShortcode = projectShortcode,
pageCount = predicates.get(OntologyConstants.KnoraBase.PageCount).flatMap(_.literals.headOption.map(_.toInt)),
dimX = predicates.get(OntologyConstants.KnoraBase.DimX).flatMap(_.literals.headOption.map(_.toInt)),
dimY = predicates.get(OntologyConstants.KnoraBase.DimY).flatMap(_.literals.headOption.map(_.toInt))
)
)
}
/**
* Converts a [[ValueProps]] into a [[ArchiveFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[ArchiveFileValueV1]].
*/
private def makeArchiveFileValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
ArchiveFileValueV1(
internalMimeType = predicates(OntologyConstants.KnoraBase.InternalMimeType).literals.head,
internalFilename = predicates(OntologyConstants.KnoraBase.InternalFilename).literals.head,
originalFilename = predicates.get(OntologyConstants.KnoraBase.OriginalFilename).map(_.literals.head),
projectShortcode = projectShortcode
)
)
}
/**
* Converts a [[ValueProps]] into a [[AudioFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[AudioFileValueV1]].
*/
private def makeAudioFileValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM
)(implicit timeout: Timeout, executionContext: ExecutionContext): Future[ApiValueV1] = {
val predicates = valueProps.literalData
Future(
AudioFileValueV1(
internalMimeType = predicates(OntologyConstants.KnoraBase.InternalMimeType).literals.head,
internalFilename = predicates(OntologyConstants.KnoraBase.InternalFilename).literals.head,
originalFilename = predicates.get(OntologyConstants.KnoraBase.OriginalFilename).map(_.literals.head),
projectShortcode = projectShortcode,
duration = predicates
.get(OntologyConstants.KnoraBase.Duration)
.map(valueLiterals => BigDecimal(valueLiterals.literals.head))
)
)
}
/**
* Converts a [[ValueProps]] into a [[MovingImageFileValueV1]].
*
* @param valueProps a [[ValueProps]] representing the SPARQL query results to be converted.
* @return a [[MovingImageFileValueV1]].
*/
private def makeVideoFileValue(
valueProps: ValueProps,
projectShortcode: String,
responderManager: ActorRef,
userProfile: UserADM