Skip to content

Commit

Permalink
intgrate into OntologyRoute
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Jan 2, 2025
1 parent 4af19cd commit 3d43706
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 67 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import org.knora.webapi.slice.infrastructure.api.ManagementEndpoints
import org.knora.webapi.slice.infrastructure.api.ManagementRoutes
import org.knora.webapi.slice.lists.api.ListsApiModule
import org.knora.webapi.slice.lists.domain.ListsService
import org.knora.webapi.slice.ontology.api.OntologyApiModule
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityServiceLive
import org.knora.webapi.slice.ontology.domain.service.CardinalityService
Expand All @@ -66,7 +67,6 @@ import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.slice.resources.repo.service.ResourcesRepo
import org.knora.webapi.slice.resources.repo.service.ResourcesRepoLive
import org.knora.webapi.slice.search.api.SearchApiRoutes
import org.knora.webapi.slice.ontology.api.OntologyApiModule
import org.knora.webapi.slice.search.api.SearchEndpoints
import org.knora.webapi.slice.security.ScopeResolver
import org.knora.webapi.slice.security.SecurityModule
Expand Down
6 changes: 4 additions & 2 deletions webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import org.apache.pekko.actor.ActorSystem
import zio.*
import zio.ULayer
import zio.ZLayer

import org.knora.webapi.config.AppConfig
import org.knora.webapi.config.AppConfig.AppConfigurations
import org.knora.webapi.config.InstrumentationServerConfig
Expand Down Expand Up @@ -46,6 +45,7 @@ import org.knora.webapi.slice.infrastructure.api.ManagementEndpoints
import org.knora.webapi.slice.infrastructure.api.ManagementRoutes
import org.knora.webapi.slice.lists.api.ListsApiModule
import org.knora.webapi.slice.lists.domain.ListsService
import org.knora.webapi.slice.ontology.api.OntologyApiModule
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.ontology.api.service.RestCardinalityServiceLive
import org.knora.webapi.slice.ontology.domain.service.CardinalityService
Expand Down Expand Up @@ -107,6 +107,7 @@ object LayersLive {
OntologyCache &
OntologyCacheHelpers &
OntologyInferencer &
OntologyApiModule.Provided &
OntologyResponderV2 &
OntologyTriplestoreHelpers &
PermissionRestService &
Expand Down Expand Up @@ -168,8 +169,9 @@ object LayersLive {
ManagementEndpoints.layer,
ManagementRoutes.layer,
MessageRelayLive.layer,
OntologyCacheLive.layer,
OntologyApiModule.layer,
OntologyCacheHelpersLive.layer,
OntologyCacheLive.layer,
OntologyRepoLive.layer,
OntologyResponderV2.layer,
OntologyServiceLive.layer,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1040,44 +1040,6 @@ case class ChangeOntologyMetadataRequestV2(
requestingUser: User,
) extends OntologiesResponderRequestV2

/**
* Constructs instances of [[ChangeOntologyMetadataRequestV2]] based on JSON-LD requests.
*/
object ChangeOntologyMetadataRequestV2 {

/**
* Converts a JSON-LD request to a [[ChangeOntologyMetadataRequestV2]].
*
* @param jsonLDDocument the JSON-LD input.
* @param apiRequestID the UUID of the API request.
* @param requestingUser the user making the request.
* @return a [[ChangeClassLabelsOrCommentsRequestV2]] representing the input.
*/
def fromJsonLd(
jsonLDDocument: JsonLDDocument,
apiRequestID: UUID,
requestingUser: User,
): ChangeOntologyMetadataRequestV2 = {
val inputOntologyV2 = InputOntologyV2.fromJsonLD(jsonLDDocument)
val inputMetadata = inputOntologyV2.ontologyMetadata
val ontologyIri = inputMetadata.ontologyIri
val label: Option[String] = inputMetadata.label
val comment: Option[String] = inputMetadata.comment
val lastModificationDate = inputMetadata.lastModificationDate.getOrElse(
throw BadRequestException("No knora-api:lastModificationDate submitted"),
)

ChangeOntologyMetadataRequestV2(
ontologyIri = ontologyIri,
label = label,
comment = comment,
lastModificationDate = lastModificationDate,
apiRequestID = apiRequestID,
requestingUser = requestingUser,
)
}
}

/**
* Deletes the comment from an ontology. A successful response will be a [[ReadOntologyMetadataV2]].
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import org.apache.pekko.http.scaladsl.model.HttpMethods.*
import org.apache.pekko.http.scaladsl.server.Directives.*
import org.apache.pekko.http.scaladsl.server.Route
import zio.*

import org.knora.webapi.config.AppConfig
import org.knora.webapi.core
import org.knora.webapi.core.AppRouter
Expand All @@ -32,6 +31,7 @@ import org.knora.webapi.slice.common.ApiComplexV2JsonLdRequestParser
import org.knora.webapi.slice.common.api.AuthorizationRestService
import org.knora.webapi.slice.infrastructure.api.ManagementRoutes
import org.knora.webapi.slice.lists.api.ListsApiV2Routes
import org.knora.webapi.slice.ontology.api.OntologyV2RequestParser
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.resourceinfo.api.ResourceInfoRoutes
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
Expand Down Expand Up @@ -100,8 +100,9 @@ object ApiRoutes {

private type ApiRoutesRuntime =
ApiComplexV2JsonLdRequestParser & AppConfig & AuthenticationApiRoutes & AuthorizationRestService & core.State &
IriConverter & MessageRelay & ProjectService & RestCardinalityService & WebApiAuthenticator & SearchApiRoutes &
SearchResponderV2 & SipiService & StringFormatter & UserService & ValuesResponderV2 & ListsApiV2Routes
IriConverter & ListsApiV2Routes & MessageRelay & OntologyV2RequestParser & ProjectService &
RestCardinalityService & SearchApiRoutes & SearchResponderV2 & SipiService & StringFormatter & UserService &
ValuesResponderV2 & WebApiAuthenticator

/**
* All routes composed together.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import zio.*
import zio.prelude.Validation

import java.time.Instant

import dsp.constants.SalsahGui
import dsp.errors.BadRequestException
import dsp.errors.ValidationException
Expand All @@ -36,6 +35,7 @@ import org.knora.webapi.routing.RouteUtilV2
import org.knora.webapi.routing.RouteUtilV2.completeResponse
import org.knora.webapi.routing.RouteUtilV2.getStringQueryParam
import org.knora.webapi.routing.RouteUtilZ
import org.knora.webapi.slice.ontology.api.OntologyV2RequestParser
import org.knora.webapi.slice.ontology.api.service.RestCardinalityService
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.slice.security.Authenticator
Expand All @@ -45,11 +45,13 @@ import org.knora.webapi.slice.security.Authenticator
*/
final case class OntologiesRouteV2()(
private implicit val runtime: Runtime[
AppConfig & Authenticator & IriConverter & MessageRelay & RestCardinalityService & StringFormatter,
AppConfig & Authenticator & IriConverter & MessageRelay & OntologyV2RequestParser & RestCardinalityService &
StringFormatter,
],
) {

private val ontologiesBasePath: PathMatcher[Unit] = PathMatcher("v2" / "ontologies")
private val requestParser = ZIO.serviceWithZIO[OntologyV2RequestParser]

private val allLanguagesKey = "allLanguages"
private val lastModificationDateKey = "lastModificationDate"
Expand Down Expand Up @@ -151,11 +153,11 @@ final case class OntologiesRouteV2()(
entity(as[String]) { jsonRequest => requestContext =>
{
val requestTask = for {
requestDoc <- RouteUtilV2.parseJsonLd(jsonRequest)
requestingUser <- ZIO.serviceWithZIO[Authenticator](_.getUserADM(requestContext))
apiRequestId <- RouteUtilZ.randomUuid()
requestMessage <-
ZIO.attempt(ChangeOntologyMetadataRequestV2.fromJsonLd(requestDoc, apiRequestId, requestingUser))
requestParser(_.changeOntologyMetadataRequestV2(jsonRequest, apiRequestId, requestingUser))
.mapError(BadRequestException.apply)
} yield requestMessage
RouteUtilV2.runRdfRouteZ(requestTask, requestContext)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package org.knora.webapi.slice.ontology.api
import zio.*

import org.knora.webapi.slice.URModule
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import zio.*

object OntologyApiModule extends URModule[IriConverter, OntologyV2RequestParser] { self =>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,41 +1,42 @@
package org.knora.webapi.slice.ontology.api

import org.apache.jena.vocabulary.RDFS
import zio.*

import java.util.UUID
import scala.language.implicitConversions

import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex.*
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.v2.responder.ontologymessages.ChangeOntologyMetadataRequestV2
import org.knora.webapi.slice.admin.domain.model.User
import org.knora.webapi.slice.common.jena.JenaConversions.given_Conversion_String_Property
import org.knora.webapi.slice.common.jena.ModelOps
import org.knora.webapi.slice.common.jena.ModelOps.*
import org.knora.webapi.slice.common.jena.ResourceOps.*
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import org.knora.webapi.slice.common.jena.JenaConversions.given_Conversion_String_Property

import java.util.UUID
import zio.*

import java.time.Instant

final case class OntologyV2RequestParser(iriConverter: IriConverter) {

def changeOntologyMetadataRequestV2(
jsonLd: String,
apiRequestID: UUID,
apiRequestId: UUID,
requestingUser: User,
): IO[String, ChangeOntologyMetadataRequestV2] = ZIO.scoped {
for {
model <- ModelOps.fromJsonLd(jsonLd)
r <- ZIO.fromEither(model.singleRootResource)
ontologyIri: SmartIri <-
ZIO.fromOption(r.uri).orElseFail("No IRI found").flatMap(iriConverter.asSmartIri(_).mapError(_.getMessage))
label = None
comment = None
label <- ZIO.fromEither(r.objectStringOption(RDFS.label))
comment <- ZIO.fromEither(r.objectStringOption(RDFS.comment))
lastModificationDate <- ZIO.fromEither(r.objectInstant(LastModificationDate))
} yield ChangeOntologyMetadataRequestV2(
ontologyIri,
label,
comment,
lastModificationDate,
apiRequestID,
apiRequestId,
requestingUser,
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
package org.knora.webapi.slice.ontology.api
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.v2.responder.ontologymessages.ChangeOntologyMetadataRequestV2
import org.knora.webapi.slice.resourceinfo.domain.IriConverter
import zio.*
import zio.test.ZIOSpecDefault
import zio.test.assertCompletes
import zio.test.assertTrue

import java.time.Instant

import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.v2.responder.ontologymessages.ChangeOntologyMetadataRequestV2
import org.knora.webapi.slice.resourceinfo.domain.IriConverter

object OntologyV2RequestParserSpec extends ZIOSpecDefault {
private val sf = StringFormatter.getInitializedTestInstance

Expand All @@ -27,6 +27,14 @@ object OntologyV2RequestParserSpec extends ZIOSpecDefault {
| "@type" : "xsd:dateTimeStamp",
| "@value" : "2017-12-19T15:23:42.166Z"
| },
| "rdfs:label" : {
| "@language" : "en",
| "@value" : "Some Label"
| },
| "rdfs:comment" : {
| "@language" : "en",
| "@value" : "Some Comment"
| },
| "@context" : {
| "rdf" : "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
| "knora-api" : "http://api.knora.org/ontology/knora-api/v2#",
Expand All @@ -38,14 +46,15 @@ object OntologyV2RequestParserSpec extends ZIOSpecDefault {
|}
|""".stripMargin
for {
req <- parser(_.changeOntologyMetadataRequestV2(jsonLd, null, null))
uuid <- Random.nextUUID
req <- parser(_.changeOntologyMetadataRequestV2(jsonLd, uuid, null))

Check warning on line 50 in webapi/src/test/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParserSpec.scala

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

webapi/src/test/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParserSpec.scala#L50

Avoid using null.
} yield assertTrue(
req == ChangeOntologyMetadataRequestV2(
sf.toSmartIri("http://0.0.0.0:3333/ontology/0001/anything/v2"),
None,
None,
Some("Some Label"),
Some("Some Comment"),
instant,
null,
uuid,
null,

Check warning on line 58 in webapi/src/test/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParserSpec.scala

View check run for this annotation

Codacy Production / Codacy Static Code Analysis

webapi/src/test/scala/org/knora/webapi/slice/ontology/api/OntologyV2RequestParserSpec.scala#L58

Avoid using null.
),
)
Expand Down

0 comments on commit 3d43706

Please sign in to comment.