/
ResponderWithStandoffV2.scala
111 lines (95 loc) · 4.74 KB
/
ResponderWithStandoffV2.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
/*
* 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.v2
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import org.knora.webapi.IRI
import dsp.errors.NotFoundException
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.ConstructResponseUtilV2
import org.knora.webapi.messages.util.ConstructResponseUtilV2.MappingAndXSLTransformation
import org.knora.webapi.messages.util.ConstructResponseUtilV2.ResourceWithValueRdfData
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.v2.responder.standoffmessages.GetMappingRequestV2
import org.knora.webapi.messages.v2.responder.standoffmessages.GetMappingResponseV2
import org.knora.webapi.messages.v2.responder.standoffmessages.GetXSLTransformationRequestV2
import org.knora.webapi.messages.v2.responder.standoffmessages.GetXSLTransformationResponseV2
import org.knora.webapi.responders.Responder
import org.knora.webapi.store.iiif.errors.SipiException
import scala.concurrent.Future
/**
* An abstract class with standoff utility methods for v2 responders.
*/
abstract class ResponderWithStandoffV2(responderData: ResponderData) extends Responder(responderData) {
/**
* Gets mappings referred to in query results [[Map[IRI, ResourceWithValueRdfData]]].
*
* @param queryResultsSeparated query results referring to mappings.
*
* @param requestingUser the user making the request.
* @return the referred mappings.
*/
protected def getMappingsFromQueryResultsSeparated(
queryResultsSeparated: Map[IRI, ResourceWithValueRdfData],
requestingUser: UserADM
): Future[Map[IRI, MappingAndXSLTransformation]] = {
// collect the Iris of the mappings referred to in the resources' text values
val mappingIris: Set[IRI] = queryResultsSeparated.flatMap { case (_, assertions: ResourceWithValueRdfData) =>
ConstructResponseUtilV2.getMappingIrisFromValuePropertyAssertions(assertions.valuePropertyAssertions)
}.toSet
// get all the mappings
val mappingResponsesFuture: Vector[Future[GetMappingResponseV2]] = mappingIris.map { mappingIri: IRI =>
for {
mappingResponse: GetMappingResponseV2 <- appActor
.ask(
GetMappingRequestV2(
mappingIri = mappingIri,
requestingUser = requestingUser
)
)
.mapTo[GetMappingResponseV2]
} yield mappingResponse
}.toVector
for {
mappingResponses: Vector[GetMappingResponseV2] <- Future.sequence(mappingResponsesFuture)
// get the default XSL transformations
mappingsWithFuture: Vector[Future[(IRI, MappingAndXSLTransformation)]] =
mappingResponses.map { mapping: GetMappingResponseV2 =>
for {
// if given, get the default XSL transformation
xsltOption: Option[String] <-
if (mapping.mapping.defaultXSLTransformation.nonEmpty) {
val xslTransformationFuture = for {
xslTransformation: GetXSLTransformationResponseV2 <-
appActor
.ask(
GetXSLTransformationRequestV2(
mapping.mapping.defaultXSLTransformation.get,
requestingUser = requestingUser
)
)
.mapTo[GetXSLTransformationResponseV2]
} yield Some(xslTransformation.xslt)
xslTransformationFuture.recover {
case notFound: NotFoundException =>
throw SipiException(
s"Default XSL transformation <${mapping.mapping.defaultXSLTransformation.get}> not found for mapping <${mapping.mappingIri}>: ${notFound.message}"
)
case other => throw other
}
} else {
FastFuture.successful(None)
}
} yield mapping.mappingIri -> MappingAndXSLTransformation(
mapping = mapping.mapping,
standoffEntities = mapping.standoffEntities,
XSLTransformation = xsltOption
)
}
mappings: Vector[(IRI, MappingAndXSLTransformation)] <- Future.sequence(mappingsWithFuture)
mappingsAsMap: Map[IRI, MappingAndXSLTransformation] = mappings.toMap
} yield mappingsAsMap
}
}