/
StandoffResponderV1.scala
155 lines (136 loc) · 5.75 KB
/
StandoffResponderV1.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
/*
* 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 java.util.UUID
import scala.concurrent.Future
import dsp.errors.NotFoundException
import org.knora.webapi._
import org.knora.webapi.messages.IriConversions._
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.admin.responder.usersmessages.UserADM
import org.knora.webapi.messages.util.ResponderData
import org.knora.webapi.messages.v1.responder.ontologymessages.ConvertOntologyClassV2ToV1
import org.knora.webapi.messages.v1.responder.ontologymessages.StandoffEntityInfoGetResponseV1
import org.knora.webapi.messages.v1.responder.standoffmessages._
import org.knora.webapi.messages.v2.responder.standoffmessages._
import org.knora.webapi.responders.Responder
import org.knora.webapi.store.iiif.errors.SipiException
/**
* Responds to requests relating to the creation of mappings from XML elements and attributes to standoff classes and properties.
*/
class StandoffResponderV1(responderData: ResponderData) extends Responder(responderData.actorDeps) {
/**
* Receives a message of type [[StandoffResponderRequestV1]], and returns an appropriate response message.
*/
def receive(msg: StandoffResponderRequestV1) = msg match {
case CreateMappingRequestV1(xml, label, projectIri, mappingName, userProfile, uuid) =>
createMappingV1(xml, label, projectIri, mappingName, userProfile, uuid)
case GetMappingRequestV1(mappingIri, userProfile) =>
getMappingV1(mappingIri, userProfile)
case GetXSLTransformationRequestV1(xsltTextReprIri, userProfile) =>
getXSLTransformation(xsltTextReprIri, userProfile)
case other => handleUnexpectedMessage(other, log, this.getClass.getName)
}
val xsltCacheName = "xsltCache"
/**
* Retrieves a `knora-base:XSLTransformation` in the triplestore and requests the corresponding XSL file from Sipi.
*
* @param xslTransformationIri The IRI of the resource representing the XSL Transformation (a [[org.knora.webapi.messages.OntologyConstants.KnoraBase.XSLTransformation]]).
*
* @param userProfile The client making the request.
* @return a [[GetXSLTransformationResponseV1]].
*/
private def getXSLTransformation(
xslTransformationIri: IRI,
userProfile: UserADM
): Future[GetXSLTransformationResponseV1] = {
val xslTransformationFuture = for {
xsltTransformation <- appActor
.ask(
GetXSLTransformationRequestV2(
xsltTextRepresentationIri = xslTransformationIri,
requestingUser = userProfile
)
)
.mapTo[GetXSLTransformationResponseV2]
} yield GetXSLTransformationResponseV1(
xslt = xsltTransformation.xslt
)
xslTransformationFuture.recover {
case notFound: NotFoundException =>
throw SipiException(s"XSL transformation $xslTransformationIri not found: ${notFound.message}")
case other => throw other
}
}
/**
* Creates a mapping between XML elements and attributes to standoff classes and properties.
* The mapping is used to convert XML documents to [[TextValueV1]] and back.
*
* @param xml the provided mapping.
*
* @param userProfile the client that made the request.
*/
private def createMappingV1(
xml: String,
label: String,
projectIri: IRI,
mappingName: String,
userProfile: UserADM,
apiRequestID: UUID
): Future[CreateMappingResponseV1] = {
implicit val stringFormatter: StringFormatter = StringFormatter.getGeneralInstance
val createMappingRequest = CreateMappingRequestV2(
metadata = CreateMappingRequestMetadataV2(
label = label,
projectIri = projectIri.toSmartIri,
mappingName = mappingName
),
xml = CreateMappingRequestXMLV2(xml),
requestingUser = userProfile,
apiRequestID = apiRequestID
)
for {
mappingResponse <- appActor.ask(createMappingRequest).mapTo[CreateMappingResponseV2]
} yield CreateMappingResponseV1(
mappingResponse.mappingIri
)
}
/**
* The name of the mapping cache.
*/
val mappingCacheName = "mappingCache"
/**
* Gets a mapping either from the cache or by making a request to the triplestore.
*
* @param mappingIri the IRI of the mapping to retrieve.
*
* @param userProfile the user making the request.
* @return a [[MappingXMLtoStandoff]].
*/
private def getMappingV1(
mappingIri: IRI,
userProfile: UserADM
): Future[GetMappingResponseV1] =
for {
mappingResponse: GetMappingResponseV2 <- appActor
.ask(
GetMappingRequestV2(
mappingIri = mappingIri,
requestingUser = userProfile
)
)
.mapTo[GetMappingResponseV2]
} yield GetMappingResponseV1(
mappingIri = mappingResponse.mappingIri,
mapping = mappingResponse.mapping,
standoffEntities = StandoffEntityInfoGetResponseV1(
standoffClassInfoMap =
ConvertOntologyClassV2ToV1.classInfoMapV2ToV1(mappingResponse.standoffEntities.standoffClassInfoMap),
standoffPropertyInfoMap =
ConvertOntologyClassV2ToV1.propertyInfoMapV2ToV1(mappingResponse.standoffEntities.standoffPropertyInfoMap)
)
)
}