Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Create resources repo #3269

Merged
merged 5 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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.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 @@
ontologyRepo: OntologyRepo,
permissionsResponder: PermissionsResponder,
ontologyService: OntologyService,
resourcesRepo: ResourcesRepo,
)(implicit val stringFormatter: StringFormatter)
extends MessageHandler
with LazyLogging
Expand All @@ -100,7 +102,7 @@
appConfig,
iriService,
messageRelay,
triplestore,
resourcesRepo,
constructResponseUtilV2,
standoffTagUtilV2,
resourceUtilV2,
Expand Down Expand Up @@ -2022,7 +2024,8 @@
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))

Check warning on line 2028 in webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/ResourcesResponderV2.scala#L2027-L2028

Added lines #L2027 - L2028 were not covered by tests
} 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.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.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 @@
)(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 @@
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)

Check warning on line 255 in webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala#L255

Added line #L255 was not covered by tests

// 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(

Check warning on line 257 in webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala#L257

Added line #L257 was not covered by tests
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 @@
}

}

/**
* 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(

Check warning on line 44 in webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala#L44

Added line #L44 was not covered by tests
dataGraphIri: InternalIri,
resource: ResourceReadyToCreate,
userIri: IRI,
projectIri: IRI,
): Task[Unit] =
triplestore.query(
Update(
sparql.v2.txt.createNewResource(

Check warning on line 52 in webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala#L50-L52

Added lines #L50 - L52 were not covered by tests
dataNamedGraph = dataGraphIri.value,
resourceToCreate = resource.sparqlTemplateResourceToCreate,
projectIri = projectIri,
creatorIri = userIri,
),
),
)

}

object ResourcesRepoLive { val layer = ZLayer.derive[ResourcesRepoLive] }

Check warning on line 63 in webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala

View check run for this annotation

Codecov / codecov/patch

webapi/src/main/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLive.scala#L63

Added line #L63 was not covered by tests
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
Loading