/
ListsMessagesADM.scala
1371 lines (1191 loc) · 48.9 KB
/
ListsMessagesADM.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.admin.responder.listsmessages
import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport
import org.knora.webapi._
import org.knora.webapi.exceptions.BadRequestException
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.admin.responder.KnoraRequestADM
import org.knora.webapi.messages.admin.responder.KnoraResponseADM
import org.knora.webapi.messages.admin.responder.listsmessages.ListNodeCreatePayloadADM.ListChildNodeCreatePayloadADM
import org.knora.webapi.messages.admin.responder.listsmessages.ListNodeCreatePayloadADM.ListRootNodeCreatePayloadADM
import org.knora.webapi.messages.admin.responder.listsmessages.ListsErrorMessagesADM._
import org.knora.webapi.messages.admin.responder.usersmessages._
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralSequenceV2
import org.knora.webapi.messages.store.triplestoremessages.StringLiteralV2
import org.knora.webapi.messages.store.triplestoremessages.TriplestoreJsonProtocol
import spray.json._
import java.util.UUID
import org.knora.webapi.messages.admin.responder.valueObjects.Comments
/////////////// API requests
/**
* Represents an API request payload that asks the Knora API server to create a new list root node.
* At least one label and comment need to be supplied.
*
* @param id the optional custom IRI of the list node.
* @param projectIri the IRI of the project.
* @param name the optional name of the list node.
* @param labels labels of the list node.
* @param comments comments of the list node.
*/
case class ListRootNodeCreateApiRequestADM(
id: Option[IRI] = None,
projectIri: IRI,
name: Option[String] = None,
labels: Seq[StringLiteralV2],
comments: Seq[StringLiteralV2]
) extends ListADMJsonProtocol {
def toJsValue: JsValue = createListRootNodeApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to create a new list child node.
* attached to given parent node as a sublist node.
* If a specific position is given, insert the child node there. Otherwise, the newly created list node will be appended
* to the end of the list of children.
* At least one label needs to be supplied.
*
* @param id the optional custom IRI of the list node.
* @param parentNodeIri the optional IRI of the parent node.
* @param projectIri the IRI of the project.
* @param name the optional name of the list node.
* @param position the optional position of the node.
* @param labels labels of the list node.
* @param comments comments of the list node.
*/
case class ListChildNodeCreateApiRequestADM(
id: Option[IRI] = None,
parentNodeIri: IRI,
projectIri: IRI,
name: Option[String] = None,
position: Option[Int] = None,
labels: Seq[StringLiteralV2],
comments: Option[Seq[StringLiteralV2]]
) extends ListADMJsonProtocol {
def toJsValue: JsValue = createListChildNodeApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update an existing node's basic information (root or child).
*
* @param listIri the IRI of the node to change.
* @param projectIri the IRI of the project the list belongs to.
* @param hasRootNode the flag to identify a child node.
* @param position the position of the node, if not a root node.
* @param name the name of the node
* @param labels the labels.
* @param comments the comments.
*/
case class ListNodeChangeApiRequestADM(
listIri: IRI,
projectIri: IRI,
hasRootNode: Option[IRI] = None,
position: Option[Int] = None,
name: Option[String] = None,
labels: Option[Seq[StringLiteralV2]] = None,
comments: Option[Seq[StringLiteralV2]] = None
) extends ListADMJsonProtocol {
def toJsValue: JsValue = changeListInfoApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update an existing node's name (root or child).
*
* @param name the new name of the node.
*/
case class ChangeNodeNameApiRequestADM(name: String) extends ListADMJsonProtocol {
def toJsValue: JsValue = changeNodeNameApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update an existing node's labels (root or child).
*
* @param labels the new labels of the node
*/
case class ChangeNodeLabelsApiRequestADM(labels: Seq[StringLiteralV2]) extends ListADMJsonProtocol {
def toJsValue: JsValue = changeNodeLabelsApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update an existing node's comments (root or child).
*
* @param comments the new comments of the node.
*/
case class ChangeNodeCommentsApiRequestADM(comments: Seq[StringLiteralV2]) extends ListADMJsonProtocol {
def toJsValue: JsValue = changeNodeCommentsApiRequestADMFormat.write(this)
}
/**
* Represents an API request payload that asks the Knora API server to update the position of child node.
*
* @param position the new position of the node.
* @param parentIri the parent node Iri.
*/
case class ChangeNodePositionApiRequestADM(position: Int, parentIri: IRI) extends ListADMJsonProtocol {
private val stringFormatter = StringFormatter.getInstanceForConstantOntologies
if (parentIri.isEmpty) {
throw BadRequestException(s"IRI of parent node is missing.")
}
if (!stringFormatter.isKnoraListIriStr(parentIri)) {
throw BadRequestException(s"Invalid IRI is given: $parentIri.")
}
if (position < -1) {
throw BadRequestException(INVALID_POSITION)
}
def toJsValue: JsValue = changeNodePositionApiRequestADMFormat.write(this)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Messages
/**
* An abstract trait for messages that can be sent to `HierarchicalListsResponderV2`.
*/
sealed trait ListsResponderRequestADM extends KnoraRequestADM
/**
* Requests a list of all lists or the lists inside a project. A successful response will be a [[ListsGetResponseADM]]
*
* @param projectIri the IRI of the project.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class ListsGetRequestADM(
projectIri: Option[IRI] = None,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM
) extends ListsResponderRequestADM
/**
* Requests a node (root or child). A successful response will be a [[ListItemGetResponseADM]]
*
* @param iri the IRI of the node (root or child).
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class ListGetRequestADM(iri: IRI, featureFactoryConfig: FeatureFactoryConfig, requestingUser: UserADM)
extends ListsResponderRequestADM
/**
* Request basic information about a node (root or child). A successful response will be a [[NodeInfoGetResponseADM]]
*
* @param iri the IRI of the list node.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class ListNodeInfoGetRequestADM(iri: IRI, featureFactoryConfig: FeatureFactoryConfig, requestingUser: UserADM)
extends ListsResponderRequestADM
/**
* Requests the path from the root node of a list to a particular node. A successful response will be
* a [[NodePathGetResponseADM]].
*
* @param iri the IRI of the node.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class NodePathGetRequestADM(iri: IRI, featureFactoryConfig: FeatureFactoryConfig, requestingUser: UserADM)
extends ListsResponderRequestADM
/**
* Requests the creation of a new list.
*
* @param createRootNode the [[ListRootNodeCreatePayloadADM]] information used for creating the root node of the list.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user creating the new list.
* @param apiRequestID the ID of the API request.
*/
case class ListRootNodeCreateRequestADM(
createRootNode: ListRootNodeCreatePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request updating basic information of an existing node.
*
* @param listIri the IRI of the node to be updated (root or child ).
* @param changeNodeRequest the data which needs to be update.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class NodeInfoChangeRequestADM(
listIri: IRI,
changeNodeRequest: ListNodeChangePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request the creation of a new list node, root or child.
*
* @param createChildNodeRequest the new node information.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
* @param apiRequestID the ID of the API request.
*/
case class ListChildNodeCreateRequestADM(
createChildNodeRequest: ListChildNodeCreatePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request updating the name of an existing node.
*
* @param nodeIri the IRI of the node whose name should be updated.
* @param changeNodeNameRequest the payload containing the new name.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class NodeNameChangeRequestADM(
nodeIri: IRI,
changeNodeNameRequest: NodeNameChangePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request updating the labels of an existing node.
*
* @param nodeIri the IRI of the node whose name should be updated.
* @param changeNodeLabelsRequest the payload containing the new labels.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class NodeLabelsChangeRequestADM(
nodeIri: IRI,
changeNodeLabelsRequest: NodeLabelsChangePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request updating the comments of an existing node.
*
* @param nodeIri the IRI of the node whose name should be updated.
* @param changeNodeCommentsRequest the payload containing the new comments.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class NodeCommentsChangeRequestADM(
nodeIri: IRI,
changeNodeCommentsRequest: NodeCommentsChangePayloadADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Request updating the position of an existing node.
*
* @param nodeIri the IRI of the node whose position should be updated.
* @param changeNodePositionRequest the payload containing the new comments.
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user initiating the request.
* @param apiRequestID the ID of the API request.
*/
case class NodePositionChangeRequestADM(
nodeIri: IRI,
changeNodePositionRequest: ChangeNodePositionApiRequestADM,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Requests deletion of a node (root or child). A successful response will be a [[ListDeleteResponseADM]]
*
* @param nodeIri the IRI of the node (root or child).
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class ListItemDeleteRequestADM(
nodeIri: IRI,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM,
apiRequestID: UUID
) extends ListsResponderRequestADM
/**
* Requests checks if a list is unused and can be deleted. A successful response will be a [[CanDeleteListResponseADM]]
*
* @param iri the IRI of the list node (root or child).
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class CanDeleteListRequestADM(iri: IRI, featureFactoryConfig: FeatureFactoryConfig, requestingUser: UserADM)
extends ListsResponderRequestADM
/**
* Requests deletion of all list node comments. A successful response will be a [[ListNodeCommentsDeleteADM]]
*
* @param iri the IRI of the list node (root or child).
* @param featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
*/
case class ListNodeCommentsDeleteRequestADM(
iri: IRI,
featureFactoryConfig: FeatureFactoryConfig,
requestingUser: UserADM
) extends ListsResponderRequestADM
///////////////////////// Responses
/**
* Responds to deletion of list node's comments by returning a success message.
*
* @param nodeIri the IRI of the list that comments are deleted.
* @param commentsDeleted contains a boolean value if comments were deleted.
*/
case class ListNodeCommentsDeleteResponseADM(nodeIri: IRI, commentsDeleted: Boolean)
extends KnoraResponseADM
with ListADMJsonProtocol {
def toJsValue: JsValue = ListNodeCommentsDeleteResponseADMFormat.write(this)
}
/**
* Returns an information if node can be deleted (none of its nodes is used in data).
*
* @param iri the IRI of the list that is checked.
* @param canDeleteList contains a boolean value if list node can be deleted.
*/
case class CanDeleteListResponseADM(listIri: IRI, canDeleteList: Boolean)
extends KnoraResponseADM
with ListADMJsonProtocol {
def toJsValue: JsValue = canDeleteListResponseADMFormat.write(this)
}
/**
* Represents a sequence of list info nodes.
*
* @param lists a [[ListRootNodeInfoADM]] sequence.
*/
case class ListsGetResponseADM(lists: Seq[ListNodeInfoADM]) extends KnoraResponseADM with ListADMJsonProtocol {
def toJsValue: JsValue = listsGetResponseADMFormat.write(this)
}
abstract class ListItemGetResponseADM(listItem: ListItemADM) extends KnoraResponseADM with ListADMJsonProtocol
/**
* Provides completes information about the list. The basic information (rood node) and all the child nodes.
*
* @param list the complete list.
*/
case class ListGetResponseADM(list: ListADM) extends ListItemGetResponseADM(list) {
def toJsValue: JsValue = listGetResponseADMFormat.write(this)
}
/**
* Provides completes information about the node. The basic information (child node) and all its children.
*
* @param node the node.
*/
case class ListNodeGetResponseADM(node: NodeADM) extends ListItemGetResponseADM(node) {
def toJsValue: JsValue = listNodeGetResponseADMFormat.write(this)
}
/**
* Provides basic information about any node (root or child) without it's children.
*
* @param nodeinfo the basic information about a node.
*/
abstract class NodeInfoGetResponseADM(nodeinfo: ListNodeInfoADM) extends KnoraResponseADM with ListADMJsonProtocol
/**
* Provides basic information about a root node without it's children.
*
* @param listinfo the basic information about a list.
*/
case class RootNodeInfoGetResponseADM(listinfo: ListRootNodeInfoADM) extends NodeInfoGetResponseADM(listinfo) {
def toJsValue: JsValue = listInfoGetResponseADMFormat.write(this)
}
/**
* Provides basic information about a child node without it's children.
*
* @param nodeinfo the basic information about a list node.
*/
case class ChildNodeInfoGetResponseADM(nodeinfo: ListChildNodeInfoADM) extends NodeInfoGetResponseADM(nodeinfo) {
def toJsValue: JsValue = listNodeInfoGetResponseADMFormat.write(this)
}
/**
* Responds to a [[NodePathGetRequestADM]] by providing the path to a particular hierarchical list node.
*
* @param elements a list of the nodes composing the path from the list's root node up to and including the specified node.
*/
case class NodePathGetResponseADM(elements: Seq[NodePathElementADM]) extends KnoraResponseADM with ListADMJsonProtocol {
def toJsValue: JsValue = nodePathGetResponseADMFormat.write(this)
}
abstract class ListItemDeleteResponseADM extends KnoraResponseADM with ListADMJsonProtocol
/**
* Responds to deletion of a list by returning a success message.
*
* @param iri the IRI of the list that is deleted.
*/
case class ListDeleteResponseADM(iri: IRI, deleted: Boolean) extends ListItemDeleteResponseADM {
def toJsValue: JsValue = listDeleteResponseADMFormat.write(this)
}
/**
* Responds to deletion of a child node by returning its parent node together with list of its immediate children
* whose position is updated.
*
* @param node the updated parent node.
*/
case class ChildNodeDeleteResponseADM(node: ListNodeADM) extends ListItemDeleteResponseADM {
def toJsValue: JsValue = listNodeDeleteResponseADMFormat.write(this)
}
/**
* Responds to change of a child node's position by returning its parent node together with list of its children.
*
* @param node the updated parent node.
*/
case class NodePositionChangeResponseADM(node: ListNodeADM) extends KnoraResponseADM with ListADMJsonProtocol {
def toJsValue: JsValue = changeNodePositionApiResponseADMFormat.write(this)
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Components of messages
abstract class ListItemADM(info: ListNodeInfoADM, children: Seq[ListChildNodeADM])
case class ListADM(listinfo: ListRootNodeInfoADM, children: Seq[ListChildNodeADM])
extends ListItemADM(listinfo, children) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[List]].
*/
def sorted: ListADM =
ListADM(
listinfo = listinfo,
children = children.sortBy(_.position).map(_.sorted)
)
}
case class NodeADM(nodeinfo: ListChildNodeInfoADM, children: Seq[ListChildNodeADM])
extends ListItemADM(nodeinfo, children) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[List]].
*/
def sorted: NodeADM =
NodeADM(
nodeinfo = nodeinfo,
children = children.sortBy(_.position).map(_.sorted)
)
}
/**
* Represents basic information about a list node, the information which is found in the list's root or child node.
*
* @param id the IRI of the list.
* @param name the name of the list node.
* @param labels the labels of the node in all available languages.
* @param comments the comments attached to the node in all available languages.
*/
abstract class ListNodeInfoADM(
id: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2
) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListNodeInfoADM]].
*/
def sorted: ListNodeInfoADM
def getName: Option[String] = name
def getLabels: StringLiteralSequenceV2 = labels
def getComments: StringLiteralSequenceV2 = comments
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String]
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String]
}
case class ListRootNodeInfoADM(
id: IRI,
projectIri: IRI,
name: Option[String] = None,
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2
) extends ListNodeInfoADM(id, name, labels, comments) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListRootNodeInfoADM]].
*/
def sorted: ListRootNodeInfoADM =
ListRootNodeInfoADM(
id = id,
projectIri = projectIri,
name = name,
labels = labels.sortByStringValue,
comments = comments.sortByStringValue
)
/**
* unescapes the special characters in labels, comments, and name for comparison in tests.
*/
def unescape: ListRootNodeInfoADM = {
val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val unescapedLabels = stringFormatter.unescapeStringLiteralSeq(labels)
val unescapedComments = stringFormatter.unescapeStringLiteralSeq(comments)
val unescapedName: Option[String] = name match {
case None => None
case Some(value) => Some(stringFormatter.fromSparqlEncodedString(value))
}
copy(name = unescapedName, labels = unescapedLabels, comments = unescapedComments)
}
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
labels.getPreferredLanguage(userLang, fallbackLang)
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
comments.getPreferredLanguage(userLang, fallbackLang)
}
case class ListChildNodeInfoADM(
id: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2,
position: Int,
hasRootNode: IRI
) extends ListNodeInfoADM(id, name, labels, comments) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListChildNodeInfoADM]].
*/
def sorted: ListChildNodeInfoADM =
ListChildNodeInfoADM(
id = id,
name = name,
labels = labels.sortByStringValue,
comments = comments,
position = position,
hasRootNode = hasRootNode
)
/**
* unescapes the special characters in labels, comments, and name for comparison in tests.
*/
def unescape: ListChildNodeInfoADM = {
val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val unescapedLabels = stringFormatter.unescapeStringLiteralSeq(labels)
val unescapedComments = stringFormatter.unescapeStringLiteralSeq(comments)
val unescapedName: Option[String] = name match {
case None => None
case Some(value) => Some(stringFormatter.fromSparqlEncodedString(value))
}
copy(name = unescapedName, labels = unescapedLabels, comments = unescapedComments)
}
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
labels.getPreferredLanguage(userLang, fallbackLang)
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
comments.getPreferredLanguage(userLang, fallbackLang)
}
/**
* Represents a hierarchical list node.
*
* @param id the IRI of the list node.
* @param name the name of the list node.
* @param labels the label(s) of the list node.
* @param comments the comment(s) attached to the list in a specific language (if language tags are used) .
* @param children the list node's child nodes.
*/
abstract class ListNodeADM(
id: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2,
children: Seq[ListChildNodeADM]
) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListNodeADM]].
*/
def sorted: ListNodeADM
def getName: Option[String] = name
def getLabels: StringLiteralSequenceV2 = labels
def getComments: StringLiteralSequenceV2 = comments
def getChildren: Seq[ListChildNodeADM] = children
def getNodeId: IRI = id
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String]
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String]
}
/**
* Represents a hierarchical list root node.
*
* @param id the IRI of the list node.
* @param projectIri the IRI of the project the list belongs to.
* @param name the name of the list node.
* @param labels the label(s) of the list node.
* @param comments the comment(s) attached to the list in a specific language (if language tags are used) .
* @param children the list node's child nodes.
*/
case class ListRootNodeADM(
id: IRI,
projectIri: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2,
children: Seq[ListChildNodeADM]
) extends ListNodeADM(id, name, labels, comments, children) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListNodeADM]].
*/
def sorted: ListRootNodeADM =
ListRootNodeADM(
id = id,
projectIri = projectIri,
name = name,
labels = labels.sortByStringValue,
comments = comments.sortByStringValue,
children = children.sortBy(_.position).map(_.sorted)
)
/**
* unescapes the special characters in labels, comments, and name for comparison in tests.
*/
def unescape: ListRootNodeADM = {
val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val unescapedLabels = stringFormatter.unescapeStringLiteralSeq(labels)
val unescapedComments = stringFormatter.unescapeStringLiteralSeq(comments)
val unescapedName: Option[String] = name match {
case None => None
case Some(value) => Some(stringFormatter.fromSparqlEncodedString(value))
}
copy(name = unescapedName, labels = unescapedLabels, comments = unescapedComments)
}
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
labels.getPreferredLanguage(userLang, fallbackLang)
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
comments.getPreferredLanguage(userLang, fallbackLang)
}
/**
* Represents a hierarchical list child node.
*
* @param id the IRI of the list node.
* @param name the name of the list node.
* @param labels the label(s) of the list node.
* @param comments the comment(s) attached to the list in a specific language (if language tags are used) .
* @param children the list node's child nodes.
* @param position the position of the node among its siblings.
* @param hasRootNode the root node of the list.
*/
case class ListChildNodeADM(
id: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: Option[StringLiteralSequenceV2],
position: Int,
hasRootNode: IRI,
children: Seq[ListChildNodeADM]
) extends ListNodeADM(
id,
name,
labels,
comments = comments.getOrElse(StringLiteralSequenceV2(Vector.empty[StringLiteralV2])),
children
) {
/**
* Sorts the whole hierarchy.
*
* @return a sorted [[ListNodeADM]].
*/
def sorted: ListChildNodeADM =
ListChildNodeADM(
id = id,
name = name,
labels = labels.sortByStringValue,
comments = comments,
position = position,
hasRootNode = hasRootNode,
children = children.sortBy(_.position).map(_.sorted)
)
/**
* unescapes the special characters in labels, comments, and name for comparison in tests.
*/
def unescape: ListChildNodeADM = {
val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val unescapedLabels = stringFormatter.unescapeStringLiteralSeq(labels)
val unescapedComments = comments match {
case Some(value) => Some(stringFormatter.unescapeStringLiteralSeq(value))
case None => None
}
val unescapedName: Option[String] = name match {
case Some(value) => Some(stringFormatter.fromSparqlEncodedString(value))
case None => None
}
copy(name = unescapedName, labels = unescapedLabels, comments = unescapedComments)
}
/**
* Gets the label in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if label is not available in user's preferred language.
* @return the label in the preferred language.
*/
def getLabelInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
labels.getPreferredLanguage(userLang, fallbackLang)
/**
* Gets the comment in the user's preferred language.
*
* @param userLang the user's preferred language.
* @param fallbackLang language to use if comment is not available in user's preferred language.
* @return the comment in the preferred language.
*/
def getCommentInPreferredLanguage(userLang: String, fallbackLang: String): Option[String] =
comments match {
case Some(value) => value.getPreferredLanguage(userLang, fallbackLang)
case None => None
}
}
/**
* Represents an element of a node path.
*
* @param id the IRI of the node path element.
* @param name the optional name of the node path element.
* @param labels the label(s) of the node path element.
* @param comments the comment(s) of the node path element.
*/
case class NodePathElementADM(
id: IRI,
name: Option[String],
labels: StringLiteralSequenceV2,
comments: StringLiteralSequenceV2
)
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// JSON formatting
/**
* A spray-json protocol for generating Knora API V2 JSON providing data about lists.
*/
trait ListADMJsonProtocol extends SprayJsonSupport with DefaultJsonProtocol with TriplestoreJsonProtocol {
implicit object ListRootNodeInfoFormat extends JsonFormat[ListRootNodeInfoADM] {
def write(node: ListRootNodeInfoADM): JsValue =
ListNodeInfoFormat.write(node)
def read(value: JsValue): ListRootNodeInfoADM =
ListNodeInfoFormat.read(value).asInstanceOf[ListRootNodeInfoADM]
}
implicit object ListChildNodeInfoFormat extends JsonFormat[ListChildNodeInfoADM] {
def write(node: ListChildNodeInfoADM): JsValue =
ListNodeInfoFormat.write(node)
def read(value: JsValue): ListChildNodeInfoADM =
ListNodeInfoFormat.read(value).asInstanceOf[ListChildNodeInfoADM]
}
implicit object ListNodeInfoFormat extends JsonFormat[ListNodeInfoADM] {
/**
* Converts a [[ListNodeInfoADM]] to a [[JsValue]].
*
* @param nodeInfo a [[ListNodeInfoADM]].
* @return a [[JsValue]].
*/
def write(nodeInfo: ListNodeInfoADM): JsValue =
nodeInfo match {
case root: ListRootNodeInfoADM =>
if (root.name.nonEmpty) {
JsObject(
"id" -> root.id.toJson,
"projectIri" -> root.projectIri.toJson,
"name" -> root.name.toJson,
"labels" -> JsArray(root.labels.stringLiterals.map(_.toJson)),
"comments" -> JsArray(root.comments.stringLiterals.map(_.toJson)),
"isRootNode" -> true.toJson
)
} else {
JsObject(
"id" -> root.id.toJson,
"projectIri" -> root.projectIri.toJson,
"labels" -> JsArray(root.labels.stringLiterals.map(_.toJson)),
"comments" -> JsArray(root.comments.stringLiterals.map(_.toJson)),
"isRootNode" -> true.toJson
)
}
case child: ListChildNodeInfoADM =>
if (child.name.nonEmpty) {
JsObject(
"id" -> child.id.toJson,
"name" -> child.name.toJson,
"labels" -> JsArray(child.labels.stringLiterals.map(_.toJson)),
"comments" -> JsArray(child.comments.stringLiterals.map(_.toJson)),
"position" -> child.position.toJson,
"hasRootNode" -> child.hasRootNode.toJson
)
} else {
JsObject(
"id" -> child.id.toJson,
"labels" -> JsArray(child.labels.stringLiterals.map(_.toJson)),
"comments" -> JsArray(child.comments.stringLiterals.map(_.toJson)),
"position" -> child.position.toJson,
"hasRootNode" -> child.hasRootNode.toJson
)
}
}
/**
* Converts a [[JsValue]] to a [[ListNodeInfoADM]].
*
* @param value a [[JsValue]].
* @return a [[ListNodeInfoADM]].