From 351b3e00d1bac6636ff3a8cf9f5897562a1ea135 Mon Sep 17 00:00:00 2001 From: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com> Date: Thu, 6 Jun 2024 17:01:56 +0200 Subject: [PATCH] refactor: Create resources repo (#3269) --- .../org/knora/webapi/core/LayersTest.scala | 4 ++ .../org/knora/webapi/core/LayersLive.scala | 2 + .../SparqlTemplateResourceToCreate.scala | 29 --------- .../responders/v2/ResourcesResponderV2.scala | 7 ++- .../v2/ontology/OntologyHelpers.scala | 6 -- .../resources/CreateResourceV2Handler.scala | 59 +++++++++-------- .../repo/service/ResourcesRepoLive.scala | 63 +++++++++++++++++++ .../sparql/v2/createNewResource.scala.txt | 2 +- 8 files changed, 104 insertions(+), 68 deletions(-) delete mode 100644 webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateResourceToCreate.scala create mode 100644 webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala diff --git a/integration/src/test/scala/org/knora/webapi/core/LayersTest.scala b/integration/src/test/scala/org/knora/webapi/core/LayersTest.scala index 4601070895..fa8871823d 100644 --- a/integration/src/test/scala/org/knora/webapi/core/LayersTest.scala +++ b/integration/src/test/scala/org/knora/webapi/core/LayersTest.scala @@ -55,6 +55,8 @@ import org.knora.webapi.slice.ontology.repo.service.OntologyRepoLive import org.knora.webapi.slice.ontology.repo.service.PredicateRepositoryLive import org.knora.webapi.slice.resourceinfo.ResourceInfoLayers 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.search.api.SearchEndpoints import org.knora.webapi.store.iiif.IIIFRequestMessageHandler @@ -121,6 +123,7 @@ object LayersTest { ProjectRestService & RepositoryUpdater & ResourceUtilV2 & + ResourcesRepo & ResourcesResponderV2 & RestCardinalityService & SearchApiRoutes & @@ -180,6 +183,7 @@ object LayersTest { RepositoryUpdater.layer, ResourceInfoLayers.live, ResourceUtilV2Live.layer, + ResourcesRepoLive.layer, ResourcesResponderV2.layer, RestCardinalityServiceLive.layer, SearchApiRoutes.layer, diff --git a/webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala b/webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala index f88e83e982..17debc3f5c 100644 --- a/webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala +++ b/webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala @@ -53,6 +53,7 @@ import org.knora.webapi.slice.ontology.repo.service.OntologyRepoLive import org.knora.webapi.slice.ontology.repo.service.PredicateRepositoryLive import org.knora.webapi.slice.resourceinfo.ResourceInfoLayers import org.knora.webapi.slice.resourceinfo.domain.IriConverter +import org.knora.webapi.slice.resources.repo.service.ResourcesRepoLive import org.knora.webapi.slice.search.api.SearchApiRoutes import org.knora.webapi.slice.search.api.SearchEndpoints import org.knora.webapi.store.iiif.IIIFRequestMessageHandler @@ -166,6 +167,7 @@ object LayersLive { RepositoryUpdater.layer, ResourceInfoLayers.live, ResourceUtilV2Live.layer, + ResourcesRepoLive.layer, ResourcesResponderV2.layer, RestCardinalityServiceLive.layer, SearchApiRoutes.layer, diff --git a/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateResourceToCreate.scala b/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateResourceToCreate.scala deleted file mode 100644 index 860024c109..0000000000 --- a/webapi/src/main/scala/org/knora/webapi/messages/twirl/SparqlTemplateResourceToCreate.scala +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright © 2021 - 2024 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.messages.twirl - -import java.time.Instant - -import org.knora.webapi.* - -/** - * Represents a resource to be created with its index, label, IRI, permissions, and SPARQL for creating its values - * - * @param resourceIri the IRI of the resource to be created. - * @param permissions the permissions user has for creating the new resource. - * @param sparqlForValues the SPARQL for creating the values of the resource. - * @param resourceClassIri the type of the resource to be created. - * @param resourceLabel the label of the resource. - * @param resourceCreationDate the creation date that should be attached to the resource. - */ -case class SparqlTemplateResourceToCreate( - resourceIri: IRI, - permissions: String, - sparqlForValues: String, - resourceClassIri: IRI, - resourceLabel: String, - resourceCreationDate: Instant, -) diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala index 6b2495d5db..5b45056647 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala @@ -49,6 +49,7 @@ import org.knora.webapi.slice.admin.domain.service.KnoraProjectService import org.knora.webapi.slice.admin.domain.service.ProjectService import org.knora.webapi.slice.ontology.domain.service.OntologyRepo import org.knora.webapi.slice.ontology.domain.service.OntologyService +import org.knora.webapi.slice.resources.repo.service.ResourcesRepo import org.knora.webapi.store.iiif.errors.SipiException import org.knora.webapi.store.triplestore.api.TriplestoreService import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Construct @@ -91,6 +92,7 @@ final case class ResourcesResponderV2( ontologyRepo: OntologyRepo, permissionsResponder: PermissionsResponder, ontologyService: OntologyService, + resourcesRepo: ResourcesRepo, )(implicit val stringFormatter: StringFormatter) extends MessageHandler with LazyLogging @@ -100,7 +102,7 @@ final case class ResourcesResponderV2( appConfig, iriService, messageRelay, - triplestore, + resourcesRepo, constructResponseUtilV2, standoffTagUtilV2, resourceUtilV2, @@ -2022,7 +2024,8 @@ object ResourcesResponderV2 { sf <- ZIO.service[StringFormatter] pr <- ZIO.service[PermissionsResponder] os <- ZIO.service[OntologyService] - handler <- mr.subscribe(ResourcesResponderV2(config, iriS, mr, ts, cu, su, ru, pu, kps, sr, or, pr, os)(sf)) + rr <- ZIO.service[ResourcesRepo] + handler <- mr.subscribe(ResourcesResponderV2(config, iriS, mr, ts, cu, su, ru, pu, kps, sr, or, pr, os, rr)(sf)) } yield handler } } diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala index 247f2fdc84..1c2f58d036 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/ontology/OntologyHelpers.scala @@ -15,8 +15,6 @@ import scala.util.Try import dsp.constants.SalsahGui import dsp.errors.* import org.knora.webapi.* -import org.knora.webapi.config.AppConfig -import org.knora.webapi.core.MessageRelay import org.knora.webapi.messages.IriConversions.* import org.knora.webapi.messages.OntologyConstants import org.knora.webapi.messages.SmartIri @@ -30,7 +28,6 @@ import org.knora.webapi.messages.util.rdf.VariableResultsRow import org.knora.webapi.messages.v2.responder.ontologymessages.* import org.knora.webapi.messages.v2.responder.ontologymessages.OwlCardinality.* import org.knora.webapi.messages.v2.responder.standoffmessages.StandoffDataTypeClasses -import org.knora.webapi.responders.IriService import org.knora.webapi.slice.admin.domain.model.User import org.knora.webapi.slice.admin.domain.service.KnoraProjectRepo import org.knora.webapi.slice.ontology.domain.model.Cardinality.* @@ -1564,9 +1561,6 @@ object OntologyHelpers { } final case class OntologyHelpersLive( - appConfig: AppConfig, - iriService: IriService, - messageRelay: MessageRelay, triplestore: TriplestoreService, ontologyCache: OntologyCache, )(implicit val stringFormatter: StringFormatter) diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala index 50a3ae3a86..caa8607c43 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala @@ -22,7 +22,6 @@ import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionA import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionType import org.knora.webapi.messages.admin.responder.permissionsmessages.ResourceCreateOperation import org.knora.webapi.messages.twirl.SparqlTemplateLinkUpdate -import org.knora.webapi.messages.twirl.SparqlTemplateResourceToCreate import org.knora.webapi.messages.twirl.queries.sparql import org.knora.webapi.messages.util.* import org.knora.webapi.messages.util.PermissionUtilADM.AGreaterThanB @@ -49,15 +48,15 @@ import org.knora.webapi.slice.ontology.domain.model.Cardinality.ZeroOrOne import org.knora.webapi.slice.ontology.domain.service.OntologyRepo import org.knora.webapi.slice.ontology.domain.service.OntologyService import org.knora.webapi.slice.ontology.domain.service.OntologyServiceLive -import org.knora.webapi.store.triplestore.api.TriplestoreService -import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Update +import org.knora.webapi.slice.resources.repo.service.ResourceReadyToCreate +import org.knora.webapi.slice.resources.repo.service.ResourcesRepo import org.knora.webapi.util.ZioHelper final case class CreateResourceV2Handler( appConfig: AppConfig, iriService: IriService, messageRelay: MessageRelay, - triplestore: TriplestoreService, + resourcesRepo: ResourcesRepo, constructResponseUtilV2: ConstructResponseUtilV2, standoffTagUtilV2: StandoffTagUtilV2, resourceUtilV2: ResourceUtilV2, @@ -70,20 +69,6 @@ final case class CreateResourceV2Handler( )(implicit val stringFormatter: StringFormatter) extends LazyLogging { - /** - * Represents a resource that is ready to be created and whose contents can be verified afterwards. - * - * @param sparqlTemplateResourceToCreate a [[SparqlTemplateResourceToCreate]] describing SPARQL for creating - * the resource. - * @param values the resource's values for verification. - * @param hasStandoffLink `true` if the property `knora-base:hasStandoffLinkToValue` was automatically added. - */ - private case class ResourceReadyToCreate( - sparqlTemplateResourceToCreate: SparqlTemplateResourceToCreate, - values: Map[SmartIri, Seq[UnverifiedValueV2]], - hasStandoffLink: Boolean, - ) - /** * Creates a new resource. * @@ -267,19 +252,14 @@ final case class CreateResourceV2Handler( requestingUser = createResourceRequestV2.requestingUser, ) - // Get the IRI of the named graph in which the resource will be created. - dataNamedGraph = - ProjectService.projectDataNamedGraphV2(createResourceRequestV2.createResource.projectADM).value + dataNamedGraph = ProjectService.projectDataNamedGraphV2(createResourceRequestV2.createResource.projectADM) - // Generate SPARQL for creating the resource. - sparqlUpdate = sparql.v2.txt.createNewResource( - dataNamedGraph = dataNamedGraph, - resourceToCreate = resourceReadyToCreate.sparqlTemplateResourceToCreate, - projectIri = createResourceRequestV2.createResource.projectADM.id, - creatorIri = createResourceRequestV2.requestingUser.id, - ) - // Do the update. - _ <- triplestore.query(Update(sparqlUpdate)) + _ <- resourcesRepo.createNewResource( + dataGraphIri = dataNamedGraph, + resource = resourceReadyToCreate, + projectIri = createResourceRequestV2.createResource.projectADM.id, + userIri = createResourceRequestV2.requestingUser.id, + ) // Verify that the resource was created. previewOfCreatedResource <- verifyResource( @@ -1047,3 +1027,22 @@ final case class CreateResourceV2Handler( } } + +/** + * Represents a resource to be created with its index, label, IRI, permissions, and SPARQL for creating its values + * + * @param resourceIri the IRI of the resource to be created. + * @param permissions the permissions user has for creating the new resource. + * @param sparqlForValues the SPARQL for creating the values of the resource. + * @param resourceClassIri the type of the resource to be created. + * @param resourceLabel the label of the resource. + * @param resourceCreationDate the creation date that should be attached to the resource. + */ +case class SparqlTemplateResourceToCreate( + resourceIri: IRI, + permissions: String, + sparqlForValues: String, + resourceClassIri: IRI, + resourceLabel: String, + resourceCreationDate: Instant, +) diff --git a/webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala b/webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala new file mode 100644 index 0000000000..30eca11ce0 --- /dev/null +++ b/webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala @@ -0,0 +1,63 @@ +/* + * Copyright © 2021 - 2024 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.slice.resources.repo.service + +import zio.* + +import dsp.constants.SalsahGui.IRI +import org.knora.webapi.messages.SmartIri +import org.knora.webapi.messages.twirl.queries.sparql +import org.knora.webapi.messages.v2.responder.valuemessages.UnverifiedValueV2 +import org.knora.webapi.responders.v2.resources.SparqlTemplateResourceToCreate +import org.knora.webapi.slice.resourceinfo.domain.InternalIri +import org.knora.webapi.store.triplestore.api.TriplestoreService +import org.knora.webapi.store.triplestore.api.TriplestoreService.Queries.Update + +/** + * Represents a resource that is ready to be created and whose contents can be verified afterwards. + * + * @param sparqlTemplateResourceToCreate a [[SparqlTemplateResourceToCreate]] describing SPARQL for creating + * the resource. + * @param values the resource's values for verification. + * @param hasStandoffLink `true` if the property `knora-base:hasStandoffLinkToValue` was automatically added. + */ +case class ResourceReadyToCreate( + sparqlTemplateResourceToCreate: SparqlTemplateResourceToCreate, + values: Map[SmartIri, Seq[UnverifiedValueV2]], + hasStandoffLink: Boolean, +) + +trait ResourcesRepo { + def createNewResource( + dataGraphIri: InternalIri, + resource: ResourceReadyToCreate, + userIri: IRI, + projectIri: IRI, + ): Task[Unit] +} + +final case class ResourcesRepoLive(triplestore: TriplestoreService) extends ResourcesRepo { + + def createNewResource( + dataGraphIri: InternalIri, + resource: ResourceReadyToCreate, + userIri: IRI, + projectIri: IRI, + ): Task[Unit] = + triplestore.query( + Update( + sparql.v2.txt.createNewResource( + dataNamedGraph = dataGraphIri.value, + resourceToCreate = resource.sparqlTemplateResourceToCreate, + projectIri = projectIri, + creatorIri = userIri, + ), + ), + ) + +} + +object ResourcesRepoLive { val layer = ZLayer.derive[ResourcesRepoLive] } diff --git a/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt b/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt index 647db7fcf2..30451bf404 100644 --- a/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt +++ b/webapi/src/main/twirl/org/knora/webapi/messages/twirl/queries/sparql/v2/createNewResource.scala.txt @@ -6,7 +6,7 @@ @import java.time.Instant @import org.knora.webapi.IRI @import org.knora.webapi.messages.v2.responder.valuemessages._ -@import org.knora.webapi.messages.twirl.SparqlTemplateResourceToCreate +@import org.knora.webapi.responders.v2.resources.SparqlTemplateResourceToCreate @** * Creates new resources.