Skip to content

Commit

Permalink
refactor: Create resources repo (#3269)
Browse files Browse the repository at this point in the history
  • Loading branch information
BalduinLandolt authored Jun 6, 2024
1 parent 11e38a8 commit 351b3e0
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -121,6 +123,7 @@ object LayersTest {
ProjectRestService &
RepositoryUpdater &
ResourceUtilV2 &
ResourcesRepo &
ResourcesResponderV2 &
RestCardinalityService &
SearchApiRoutes &
Expand Down Expand Up @@ -180,6 +183,7 @@ object LayersTest {
RepositoryUpdater.layer,
ResourceInfoLayers.live,
ResourceUtilV2Live.layer,
ResourcesRepoLive.layer,
ResourcesResponderV2.layer,
RestCardinalityServiceLive.layer,
SearchApiRoutes.layer,
Expand Down
2 changes: 2 additions & 0 deletions webapi/src/main/scala/org/knora/webapi/core/LayersLive.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -166,6 +167,7 @@ object LayersLive {
RepositoryUpdater.layer,
ResourceInfoLayers.live,
ResourceUtilV2Live.layer,
ResourcesRepoLive.layer,
ResourcesResponderV2.layer,
RestCardinalityServiceLive.layer,
SearchApiRoutes.layer,
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -91,6 +92,7 @@ final case class ResourcesResponderV2(
ontologyRepo: OntologyRepo,
permissionsResponder: PermissionsResponder,
ontologyService: OntologyService,
resourcesRepo: ResourcesRepo,
)(implicit val stringFormatter: StringFormatter)
extends MessageHandler
with LazyLogging
Expand All @@ -100,7 +102,7 @@ final case class ResourcesResponderV2(
appConfig,
iriService,
messageRelay,
triplestore,
resourcesRepo,
constructResponseUtilV2,
standoffTagUtilV2,
resourceUtilV2,
Expand Down Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.*
Expand Down Expand Up @@ -1564,9 +1561,6 @@ object OntologyHelpers {
}

final case class OntologyHelpersLive(
appConfig: AppConfig,
iriService: IriService,
messageRelay: MessageRelay,
triplestore: TriplestoreService,
ontologyCache: OntologyCache,
)(implicit val stringFormatter: StringFormatter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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.
*
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
@@ -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] }
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 351b3e0

Please sign in to comment.