/
OntologyResponderV1.scala
719 lines (658 loc) · 40.9 KB
/
OntologyResponderV1.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
/*
* 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.responders.v1
import akka.pattern._
import scala.concurrent.Future
import dsp.constants.SalsahGui
import dsp.errors.InconsistentRepositoryDataException
import dsp.errors.NotFoundException
import org.knora.webapi._
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectADM
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectsGetRequestADM
import org.knora.webapi.messages.admin.responder.projectsmessages.ProjectsGetResponseADM
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.util.ValueUtilV1
import org.knora.webapi.messages.v1.responder.ontologymessages._
import org.knora.webapi.messages.v1.responder.resourcemessages.SalsahGuiConversions
import org.knora.webapi.messages.v2.responder.SuccessResponseV2
import org.knora.webapi.messages.v2.responder.ontologymessages.OwlCardinality.KnoraCardinalityInfo
import org.knora.webapi.messages.v2.responder.ontologymessages._
import org.knora.webapi.responders.Responder
/**
* Handles requests for information about ontology entities.
*
* All ontology data is loaded and cached when the application starts. To refresh the cache, you currently have to restart
* the application.
*/
class OntologyResponderV1(responderData: ResponderData) extends Responder(responderData.actorDeps) {
private val valueUtilV1 = new ValueUtilV1(responderData.appConfig)
/**
* Receives a message extending [[OntologyResponderRequestV1]], and returns an appropriate response message.
*/
def receive(msg: OntologyResponderRequestV1) = msg match {
case LoadOntologiesRequestV1(userProfile) => loadOntologies(userProfile)
case EntityInfoGetRequestV1(resourceIris, propertyIris, userProfile) =>
getEntityInfoResponseV1(resourceIris, propertyIris, userProfile)
case ResourceTypeGetRequestV1(resourceTypeIri, userProfile) =>
getResourceTypeResponseV1(resourceTypeIri, userProfile)
case checkSubClassRequest: CheckSubClassRequestV1 => checkSubClass(checkSubClassRequest)
case subClassesGetRequest: SubClassesGetRequestV1 => getSubClasses(subClassesGetRequest)
case NamedGraphsGetRequestV1(projectIris, userProfile) =>
getNamedGraphs(projectIris, userProfile)
case ResourceTypesForNamedGraphGetRequestV1(namedGraphIri, userProfile) =>
getResourceTypesForNamedGraph(namedGraphIri, userProfile)
case PropertyTypesForNamedGraphGetRequestV1(namedGraphIri, userProfile) =>
getPropertyTypesForNamedGraph(namedGraphIri, userProfile)
case PropertyTypesForResourceTypeGetRequestV1(restypeId, userProfile) =>
getPropertyTypesForResourceType(restypeId, userProfile)
case StandoffEntityInfoGetRequestV1(standoffClassIris, standoffPropertyIris, userProfile) =>
getStandoffEntityInfoResponseV1(standoffClassIris, standoffPropertyIris, userProfile)
case StandoffClassesWithDataTypeGetRequestV1(userProfile) => getStandoffStandoffClassesWithDataTypeV1(userProfile)
case StandoffAllPropertiesGetRequestV1(userProfile) => getAllStandoffPropertyEntities(userProfile)
case NamedGraphEntityInfoRequestV1(namedGraphIri, userProfile) =>
getNamedGraphEntityInfoV1ForNamedGraph(namedGraphIri, userProfile)
case other => handleUnexpectedMessage(other, log, this.getClass.getName)
}
/**
* Loads and caches all ontology information.
*
* @param userProfile the profile of the user making the request.
* @return a [[LoadOntologiesResponse]].
*/
private def loadOntologies(
userProfile: UserADM
): Future[LoadOntologiesResponse] =
for {
// forward the request to the v2 ontologies responder
_: SuccessResponseV2 <- appActor
.ask(
LoadOntologiesRequestV2(
requestingUser = userProfile
)
)
.mapTo[SuccessResponseV2]
} yield LoadOntologiesResponse()
/**
* Given a list of resource IRIs and a list of property IRIs (ontology entities), returns an [[EntityInfoGetResponseV1]] describing both resource and property entities.
*
* @param resourceClassIris the IRIs of the resource entities to be queried.
* @param propertyIris the IRIs of the property entities to be queried.
* @param userProfile the profile of the user making the request.
* @return an [[EntityInfoGetResponseV1]].
*/
private def getEntityInfoResponseV1(
resourceClassIris: Set[IRI] = Set.empty[IRI],
propertyIris: Set[IRI] = Set.empty[IRI],
userProfile: UserADM
): Future[EntityInfoGetResponseV1] =
for {
response: EntityInfoGetResponseV2 <- appActor
.ask(
EntityInfoGetRequestV2(
resourceClassIris.map(_.toSmartIri),
propertyIris.map(_.toSmartIri),
userProfile
)
)
.mapTo[EntityInfoGetResponseV2]
} yield EntityInfoGetResponseV1(
resourceClassInfoMap = ConvertOntologyClassV2ToV1.classInfoMapV2ToV1(response.classInfoMap),
propertyInfoMap = ConvertOntologyClassV2ToV1.propertyInfoMapV2ToV1(response.propertyInfoMap)
)
/**
* Given a list of standoff class IRIs and a list of property IRIs (ontology entities), returns an [[StandoffEntityInfoGetResponseV1]] describing both resource and property entities.
*
* @param standoffClassIris the IRIs of the resource entities to be queried.
* @param standoffPropertyIris the IRIs of the property entities to be queried.
* @param userProfile the profile of the user making the request.
* @return an [[EntityInfoGetResponseV1]].
*/
private def getStandoffEntityInfoResponseV1(
standoffClassIris: Set[IRI] = Set.empty[IRI],
standoffPropertyIris: Set[IRI] = Set.empty[IRI],
userProfile: UserADM
): Future[StandoffEntityInfoGetResponseV1] =
for {
response: StandoffEntityInfoGetResponseV2 <- appActor
.ask(
StandoffEntityInfoGetRequestV2(
standoffClassIris.map(_.toSmartIri),
standoffPropertyIris.map(_.toSmartIri),
userProfile
)
)
.mapTo[StandoffEntityInfoGetResponseV2]
} yield StandoffEntityInfoGetResponseV1(
standoffClassInfoMap = ConvertOntologyClassV2ToV1.classInfoMapV2ToV1(response.standoffClassInfoMap),
standoffPropertyInfoMap = ConvertOntologyClassV2ToV1.propertyInfoMapV2ToV1(response.standoffPropertyInfoMap)
)
/**
* Gets information about all standoff classes that are a subclass of a data type standoff class.
*
* @param userProfile the profile of the user making the request.
* @return a [[StandoffClassesWithDataTypeGetResponseV1]]
*/
private def getStandoffStandoffClassesWithDataTypeV1(
userProfile: UserADM
): Future[StandoffClassesWithDataTypeGetResponseV1] =
for {
response: StandoffClassesWithDataTypeGetResponseV2 <- appActor
.ask(
StandoffClassesWithDataTypeGetRequestV2(
userProfile
)
)
.mapTo[StandoffClassesWithDataTypeGetResponseV2]
} yield StandoffClassesWithDataTypeGetResponseV1(
standoffClassInfoMap = ConvertOntologyClassV2ToV1.classInfoMapV2ToV1(response.standoffClassInfoMap)
)
/**
* Gets all standoff property entities.
*
* @param userProfile the profile of the user making the request.
* @return a [[StandoffAllPropertiesGetResponseV1]].
*/
private def getAllStandoffPropertyEntities(userProfile: UserADM): Future[StandoffAllPropertiesGetResponseV1] =
for {
response: StandoffAllPropertyEntitiesGetResponseV2 <- appActor
.ask(
StandoffAllPropertyEntitiesGetRequestV2(
userProfile
)
)
.mapTo[StandoffAllPropertyEntitiesGetResponseV2]
} yield StandoffAllPropertiesGetResponseV1(
standoffAllPropertiesInfoMap =
ConvertOntologyClassV2ToV1.propertyInfoMapV2ToV1(response.standoffAllPropertiesEntityInfoMap)
)
/**
* Given the IRI of a resource type, returns a [[ResourceTypeResponseV1]] describing the resource type and its possible
* properties.
*
* @param resourceTypeIri the IRI of the resource type to be queried.
* @param userProfile the profile of the user making the request.
* @return a [[ResourceTypeResponseV1]].
*/
private def getResourceTypeResponseV1(
resourceTypeIri: String,
userProfile: UserADM
): Future[ResourceTypeResponseV1] = {
for {
// Get all information about the resource type, including its property cardinalities.
resourceClassInfoResponse: EntityInfoGetResponseV1 <- getEntityInfoResponseV1(
resourceClassIris = Set(resourceTypeIri),
userProfile = userProfile
)
resourceClassInfo: ClassInfoV1 =
resourceClassInfoResponse.resourceClassInfoMap
.getOrElse(resourceTypeIri, throw NotFoundException(s"Resource class $resourceTypeIri not found"))
// Get all information about those properties.
propertyInfo: EntityInfoGetResponseV1 <- getEntityInfoResponseV1(
propertyIris = resourceClassInfo.knoraResourceCardinalities.keySet,
userProfile = userProfile
)
// Build the property definitions.
propertyDefinitions: Vector[PropertyDefinitionV1] = resourceClassInfo.knoraResourceCardinalities.filterNot {
// filter out the properties that point to LinkValue objects
case (propertyIri, _) =>
resourceClassInfo.linkValueProperties(
propertyIri
) || propertyIri == OntologyConstants.KnoraBase.HasStandoffLinkTo
}.map {
case (
propertyIri: IRI,
cardinalityInfo: KnoraCardinalityInfo
) =>
propertyInfo.propertyInfoMap.get(propertyIri) match {
case Some(entityInfo: PropertyInfoV1) =>
if (entityInfo.isLinkProp) {
// It is a linking prop: its valuetype_id is knora-base:LinkValue.
// It is restricted to the resource class that is given for knora-base:objectClassConstraint
// for the given property which goes in the attributes that will be read by the GUI.
PropertyDefinitionV1(
id = propertyIri,
name = propertyIri,
label = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Label,
preferredLangs = Some(
userProfile.lang,
responderData.appConfig.fallbackLanguage
)
),
description = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Comment,
preferredLangs = Some(
userProfile.lang,
responderData.appConfig.fallbackLanguage
)
),
vocabulary = entityInfo.ontologyIri,
occurrence = cardinalityInfo.cardinality.value,
valuetype_id =
OntologyConstants.KnoraBase.LinkValue,
attributes = valueUtilV1.makeAttributeString(
entityInfo
.getPredicateStringObjectsWithoutLang(
SalsahGui.GuiAttribute
) + valueUtilV1
.makeAttributeRestype(
entityInfo
.getPredicateObject(
OntologyConstants.KnoraBase.ObjectClassConstraint
)
.getOrElse(
throw InconsistentRepositoryDataException(
s"Property $propertyIri has no knora-base:objectClassConstraint"
)
)
)
),
gui_name = entityInfo
.getPredicateObject(
SalsahGui.GuiElementProp
)
.map(iri =>
SalsahGuiConversions.iri2SalsahGuiElement(iri)
),
guiorder = cardinalityInfo.guiOrder
)
} else {
PropertyDefinitionV1(
id = propertyIri,
name = propertyIri,
label = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Label,
preferredLangs = Some(
userProfile.lang,
responderData.appConfig.fallbackLanguage
)
),
description = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Comment,
preferredLangs = Some(
userProfile.lang,
responderData.appConfig.fallbackLanguage
)
),
vocabulary = entityInfo.ontologyIri,
occurrence = cardinalityInfo.cardinality.value,
valuetype_id = entityInfo
.getPredicateObject(
OntologyConstants.KnoraBase.ObjectClassConstraint
)
.getOrElse(
throw InconsistentRepositoryDataException(
s"Property $propertyIri has no knora-base:objectClassConstraint"
)
),
attributes = valueUtilV1.makeAttributeString(
entityInfo.getPredicateStringObjectsWithoutLang(
SalsahGui.GuiAttribute
)
),
gui_name = entityInfo
.getPredicateObject(
SalsahGui.GuiElementProp
)
.map(iri =>
SalsahGuiConversions.iri2SalsahGuiElement(iri)
),
guiorder = cardinalityInfo.guiOrder
)
}
case None =>
throw new InconsistentRepositoryDataException(
s"Resource type $resourceTypeIri is defined as having property $propertyIri, which doesn't exist"
)
}
}.toVector
.sortBy(_.guiorder)
// Build the API response.
resourceTypeResponse = ResourceTypeResponseV1(
restype_info = ResTypeInfoV1(
name = resourceTypeIri,
label = resourceClassInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Label,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
description = resourceClassInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Comment,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
iconsrc =
resourceClassInfo.getPredicateObject(OntologyConstants.KnoraBase.ResourceIcon),
properties = propertyDefinitions
)
)
} yield resourceTypeResponse
}
/**
* Checks whether a certain Knora resource or value class is a subclass of another class.
*
* @param checkSubClassRequest a [[CheckSubClassRequestV1]]
* @return a [[CheckSubClassResponseV1]].
*/
private def checkSubClass(checkSubClassRequest: CheckSubClassRequestV1): Future[CheckSubClassResponseV1] =
for {
response: CheckSubClassResponseV2 <- appActor
.ask(
CheckSubClassRequestV2(
subClassIri = checkSubClassRequest.subClassIri.toSmartIri,
superClassIri = checkSubClassRequest.superClassIri.toSmartIri,
checkSubClassRequest.userProfile
)
)
.mapTo[CheckSubClassResponseV2]
} yield CheckSubClassResponseV1(response.isSubClass)
/**
* Gets the IRIs of the subclasses of a resource class.
*
* @param getSubClassesRequest a [[SubClassesGetRequestV1]].
* @return a [[SubClassesGetResponseV1]].
*/
private def getSubClasses(getSubClassesRequest: SubClassesGetRequestV1): Future[SubClassesGetResponseV1] =
for {
response: SubClassesGetResponseV2 <- appActor
.ask(
SubClassesGetRequestV2(
getSubClassesRequest.resourceClassIri.toSmartIri,
getSubClassesRequest.userADM
)
)
.mapTo[SubClassesGetResponseV2]
subClasses = response.subClasses.map { subClassInfoV2 =>
SubClassInfoV1(
id = subClassInfoV2.id.toString,
label = subClassInfoV2.label
)
}
} yield SubClassesGetResponseV1(subClasses)
/**
* Returns information about ontology named graphs as a [[NamedGraphsResponseV1]].
*
* @param projectIris the IRIs of the projects whose named graphs should be returned.
*
* @param userProfile the profile of the user making the request.
* @return a [[NamedGraphsResponseV1]].
*/
private def getNamedGraphs(
projectIris: Set[IRI] = Set.empty[IRI],
userProfile: UserADM
): Future[NamedGraphsResponseV1] =
for {
projectsResponse <- appActor
.ask(
ProjectsGetRequestADM(
requestingUser = userProfile
)
)
.mapTo[ProjectsGetResponseADM]
readOntologyMetadataV2 <- appActor
.ask(
OntologyMetadataGetByProjectRequestV2(
projectIris = projectIris.map(_.toSmartIri),
requestingUser = userProfile
)
)
.mapTo[ReadOntologyMetadataV2]
projectsMap: Map[IRI, ProjectADM] = projectsResponse.projects.map { project =>
project.id -> project
}.toMap
namedGraphs: Seq[NamedGraphV1] = readOntologyMetadataV2.ontologies.toVector
.map(_.toOntologySchema(InternalSchema))
.filter { ontologyMetadata =>
// In V1, the only built-in ontology we show is knora-base.
val ontologyLabel = ontologyMetadata.ontologyIri.getOntologyName
ontologyLabel == OntologyConstants.KnoraBase.KnoraBaseOntologyLabel || !OntologyConstants.BuiltInOntologyLabels
.contains(ontologyLabel)
}
.map { ontologyMetadata =>
val project = projectsMap(ontologyMetadata.projectIri.get.toString)
NamedGraphV1(
id = ontologyMetadata.ontologyIri.toString,
shortname = project.shortname,
longname = project.longname
.getOrElse(
throw InconsistentRepositoryDataException(
s"Project ${project.id} has no longname"
)
),
description = project.description.headOption
.getOrElse(
throw InconsistentRepositoryDataException(
s"Project ${project.id} has no description"
)
)
.toString,
project_id = project.id,
uri = ontologyMetadata.ontologyIri.toString,
active = project.status
)
}
response = NamedGraphsResponseV1(
vocabularies = namedGraphs
)
} yield response
/**
* Gets the [[NamedGraphEntityInfoV1]] for a named graph
*
* @param namedGraphIri the IRI of the named graph to query
* @param userProfile the profile of the user making the request.
* @return a [[NamedGraphEntityInfoV1]].
*/
def getNamedGraphEntityInfoV1ForNamedGraph(namedGraphIri: IRI, userProfile: UserADM): Future[NamedGraphEntityInfoV1] =
for {
response: OntologyKnoraEntitiesIriInfoV2 <- appActor
.ask(
OntologyKnoraEntityIrisGetRequestV2(
namedGraphIri.toSmartIri,
userProfile
)
)
.mapTo[OntologyKnoraEntitiesIriInfoV2]
classIrisForV1 = response.classIris.map(_.toString) -- OntologyConstants.KnoraBase.AbstractResourceClasses
propertyIrisForV1 = response.propertyIris.map(_.toString) - OntologyConstants.KnoraBase.ResourceProperty
} yield NamedGraphEntityInfoV1(
namedGraphIri = response.ontologyIri.toString,
resourceClasses = classIrisForV1,
propertyIris = propertyIrisForV1
)
/**
* Gets all the resource classes and their properties for a named graph.
*
* @param namedGraphIriOption the IRI of the named graph or None if all the named graphs should be queried.
*
* @param userProfile the profile of the user making the request.
* @return [[ResourceTypesForNamedGraphResponseV1]].
*/
private def getResourceTypesForNamedGraph(
namedGraphIriOption: Option[IRI],
userProfile: UserADM
): Future[ResourceTypesForNamedGraphResponseV1] = {
// get the resource types for a named graph
def getResourceTypes(namedGraphIri: IRI): Future[Seq[ResourceTypeV1]] =
for {
// get NamedGraphEntityInfoV1 for the given named graph
namedGraphEntityInfo: NamedGraphEntityInfoV1 <- getNamedGraphEntityInfoV1ForNamedGraph(
namedGraphIri,
userProfile
)
// get resinfo for each resource class in namedGraphEntityInfo
resInfosForNamedGraphFuture: Set[Future[(String, ResourceTypeResponseV1)]] =
namedGraphEntityInfo.resourceClasses.map { resClassIri =>
for {
resInfo <- getResourceTypeResponseV1(resClassIri, userProfile)
} yield (resClassIri, resInfo)
}
resInfosForNamedGraph: Set[(IRI, ResourceTypeResponseV1)] <- Future.sequence(resInfosForNamedGraphFuture)
resourceTypes: Vector[ResourceTypeV1] = resInfosForNamedGraph.map { case (resClassIri, resInfo) =>
val properties = resInfo.restype_info.properties.map { prop =>
PropertyTypeV1(
id = prop.id,
label = prop.label.getOrElse(
throw InconsistentRepositoryDataException(
s"No label given for ${prop.id}"
)
)
)
}.toVector
ResourceTypeV1(
id = resClassIri,
label = resInfo.restype_info.label.getOrElse(
throw InconsistentRepositoryDataException(
s"No label given for $resClassIri"
)
),
properties = properties
)
}.toVector
} yield resourceTypes
// get resource types for named graph depending on given IRI-Option
namedGraphIriOption match {
case Some(namedGraphIri) => // get the resource types for the given named graph
for {
resourceTypes <- getResourceTypes(namedGraphIri)
} yield ResourceTypesForNamedGraphResponseV1(resourcetypes = resourceTypes)
case None => // map over all named graphs and collect the resource types
for {
projectNamedGraphsResponse: NamedGraphsResponseV1 <- getNamedGraphs(
userProfile = userProfile
)
projectNamedGraphIris: Seq[IRI] = projectNamedGraphsResponse.vocabularies.map(_.uri)
resourceTypesPerProject: Seq[Future[Seq[ResourceTypeV1]]] =
projectNamedGraphIris.map(iri => getResourceTypes(iri))
resourceTypes: Seq[Seq[ResourceTypeV1]] <- Future.sequence(resourceTypesPerProject)
} yield ResourceTypesForNamedGraphResponseV1(resourcetypes = resourceTypes.flatten)
}
}
/**
* Gets the property types defined in the given named graph. If there is no named graph defined, get property types for all existing named graphs.
*
* @param namedGraphIriOption the IRI of the named graph or None if all the named graphs should be queried.
*
* @param userProfile the profile of the user making the request.
* @return a [[PropertyTypesForNamedGraphResponseV1]].
*/
private def getPropertyTypesForNamedGraph(
namedGraphIriOption: Option[IRI],
userProfile: UserADM
): Future[PropertyTypesForNamedGraphResponseV1] = {
def getPropertiesForNamedGraph(
namedGraphIri: IRI,
userProfile: UserADM
): Future[Seq[PropertyDefinitionInNamedGraphV1]] =
for {
namedGraphEntityInfo <- getNamedGraphEntityInfoV1ForNamedGraph(namedGraphIri, userProfile)
propertyIris: Set[IRI] = namedGraphEntityInfo.propertyIris
entities: EntityInfoGetResponseV1 <- getEntityInfoResponseV1(
propertyIris = propertyIris,
userProfile = userProfile
)
propertyInfoMap: Map[IRI, PropertyInfoV1] =
entities.propertyInfoMap.filterNot { case (_, propertyEntityInfo) =>
propertyEntityInfo.isLinkValueProp
}
propertyDefinitions: Vector[PropertyDefinitionInNamedGraphV1] =
propertyInfoMap.map { case (propertyIri: IRI, entityInfo: PropertyInfoV1) =>
if (entityInfo.isLinkProp) {
// It is a linking prop: its valuetype_id is knora-base:LinkValue.
// It is restricted to the resource class that is given for knora-base:objectClassConstraint
// for the given property which goes in the attributes that will be read by the GUI.
PropertyDefinitionInNamedGraphV1(
id = propertyIri,
name = propertyIri,
label = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Label,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
description = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Comment,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
vocabulary = entityInfo.ontologyIri,
valuetype_id = OntologyConstants.KnoraBase.LinkValue,
attributes = valueUtilV1.makeAttributeString(
entityInfo
.getPredicateStringObjectsWithoutLang(SalsahGui.GuiAttribute) + valueUtilV1
.makeAttributeRestype(
entityInfo
.getPredicateObject(OntologyConstants.KnoraBase.ObjectClassConstraint)
.getOrElse(
throw InconsistentRepositoryDataException(
s"Property $propertyIri has no knora-base:objectClassConstraint"
)
)
)
),
gui_name = entityInfo
.getPredicateObject(SalsahGui.GuiElementProp)
.map(iri => SalsahGuiConversions.iri2SalsahGuiElement(iri))
)
} else {
PropertyDefinitionInNamedGraphV1(
id = propertyIri,
name = propertyIri,
label = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Label,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
description = entityInfo.getPredicateObject(
predicateIri = OntologyConstants.Rdfs.Comment,
preferredLangs = Some(userProfile.lang, responderData.appConfig.fallbackLanguage)
),
vocabulary = entityInfo.ontologyIri,
valuetype_id = entityInfo
.getPredicateObject(OntologyConstants.KnoraBase.ObjectClassConstraint)
.getOrElse(
throw InconsistentRepositoryDataException(
s"Property $propertyIri has no knora-base:objectClassConstraint"
)
),
attributes = valueUtilV1.makeAttributeString(
entityInfo.getPredicateStringObjectsWithoutLang(SalsahGui.GuiAttribute)
),
gui_name = entityInfo
.getPredicateObject(SalsahGui.GuiElementProp)
.map(iri => SalsahGuiConversions.iri2SalsahGuiElement(iri))
)
}
}.toVector
} yield propertyDefinitions
namedGraphIriOption match {
case Some(namedGraphIri) => // get all the property types for the given named graph
for {
propertyTypes <- getPropertiesForNamedGraph(namedGraphIri, userProfile)
} yield PropertyTypesForNamedGraphResponseV1(properties = propertyTypes)
case None => // get the property types for all named graphs (collect them by mapping over all named graphs)
for {
projectNamedGraphsResponse: NamedGraphsResponseV1 <- getNamedGraphs(
userProfile = userProfile
)
projectNamedGraphIris: Seq[IRI] = projectNamedGraphsResponse.vocabularies.map(_.uri)
propertyTypesPerProject: Seq[Future[Seq[PropertyDefinitionInNamedGraphV1]]] =
projectNamedGraphIris.map(iri => getPropertiesForNamedGraph(iri, userProfile))
propertyTypes: Seq[Seq[PropertyDefinitionInNamedGraphV1]] <- Future.sequence(propertyTypesPerProject)
} yield PropertyTypesForNamedGraphResponseV1(properties = propertyTypes.flatten)
}
}
/**
* Gets the property types defined for the given resource class.
*
* @param resourceClassIri the IRI of the resource class to query for.
* @param userProfile the profile of the user making the request.
* @return a [[PropertyTypesForResourceTypeResponseV1]].
*/
private def getPropertyTypesForResourceType(
resourceClassIri: IRI,
userProfile: UserADM
): Future[PropertyTypesForResourceTypeResponseV1] =
for {
resInfo: ResourceTypeResponseV1 <- getResourceTypeResponseV1(resourceClassIri, userProfile)
propertyTypes = resInfo.restype_info.properties.toVector
} yield PropertyTypesForResourceTypeResponseV1(properties = propertyTypes)
}