/
ResponderWithStandoffV2.scala
115 lines (100 loc) · 4.88 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
112
113
114
115
/*
* Copyright © 2015-2021 the contributors (see Contributors.md).
*
* This file is part of Knora.
*
* Knora is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Knora is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with Knora. If not, see <http://www.gnu.org/licenses/>.
*/
package org.knora.webapi.responders.v2
import akka.http.scaladsl.util.FastFuture
import akka.pattern._
import org.knora.webapi.IRI
import org.knora.webapi.exceptions.{NotFoundException, SipiException}
import org.knora.webapi.feature.FeatureFactoryConfig
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.ConstructResponseUtilV2.{MappingAndXSLTransformation, ResourceWithValueRdfData}
import org.knora.webapi.messages.util.{ConstructResponseUtilV2, ResponderData}
import org.knora.webapi.messages.v2.responder.standoffmessages.{
GetMappingRequestV2,
GetMappingResponseV2,
GetXSLTransformationRequestV2,
GetXSLTransformationResponseV2
}
import org.knora.webapi.responders.Responder
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 featureFactoryConfig the feature factory configuration.
* @param requestingUser the user making the request.
* @return the referred mappings.
*/
protected def getMappingsFromQueryResultsSeparated(
queryResultsSeparated: Map[IRI, ResourceWithValueRdfData],
featureFactoryConfig: FeatureFactoryConfig,
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 <- (responderManager ? GetMappingRequestV2(
mappingIri = mappingIri,
featureFactoryConfig = featureFactoryConfig,
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 <- (responderManager ? GetXSLTransformationRequestV2(
mapping.mapping.defaultXSLTransformation.get,
featureFactoryConfig = featureFactoryConfig,
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
}
}