From f05eaae5c993eb2256b6fa3f0bcdb38123dee89d Mon Sep 17 00:00:00 2001 From: Balduin Landolt <33053745+BalduinLandolt@users.noreply.github.com> Date: Wed, 19 Jun 2024 18:06:29 +0200 Subject: [PATCH] continue --- .../repo/service/ResourcesRepoLive.scala | 21 +++++-- .../repo/service/ResourcesRepoLiveSpec.scala | 59 +++++++++++++++++-- 2 files changed, 72 insertions(+), 8 deletions(-) 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 index e701ee213d..6ee649a359 100644 --- 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 @@ -119,16 +119,21 @@ object ResourcesRepoLive { query.insertData(resourcePattern) for (newValueInfo <- resourceToCreate.newValueInfos) { - resourcePattern.andHas(iri(newValueInfo.propertyIri), Rdf.iri(newValueInfo.valueIri)) - query.insertData(valuePatternForCreateNewResource(newValueInfo)) + query.insertData(valuePatternForCreateNewResource(newValueInfo, resourcePattern, resourceToCreate.resourceIri)) } Update(query.getQueryString()) } - private def valuePatternForCreateNewResource(newValueInfo: NewValueInfo): TriplePattern = { + private def valuePatternForCreateNewResource( + newValueInfo: NewValueInfo, + resourcePattern: TriplePattern, + resourceIri: IRI, + ): TriplePattern = { import TypeSpecificValueInfo.* + resourcePattern.andHas(iri(newValueInfo.propertyIri), Rdf.iri(newValueInfo.valueIri)) + val valuePattern = Rdf .iri(newValueInfo.valueIri) @@ -147,7 +152,14 @@ object ResourcesRepoLive { newValueInfo.comment.foreach(comment => valuePattern.andHas(KnoraBaseVocab.valueHasComment, Rdf.literalOf(comment))) newValueInfo.value match - case LinkValueInfo(referredResourceIri) => ??? + case LinkValueInfo(referredResourceIri) => + val directLinkPropertyIri = newValueInfo.propertyIri.stripSuffix("Value") + resourcePattern.andHas(Rdf.iri(directLinkPropertyIri), Rdf.iri(referredResourceIri)) + valuePattern + .andHas(RDF.SUBJECT, Rdf.iri(resourceIri)) + .andHas(RDF.PREDICATE, Rdf.iri(directLinkPropertyIri)) + .andHas(RDF.OBJECT, Rdf.iri(referredResourceIri)) + .andHas(KnoraBaseVocab.valueHasRefCount, Rdf.literalOf(1)) case UnformattedTextValueInfo(valueHasLanguage) => ??? case FormattedTextValueInfo(valueHasLanguage, mappingIri, maxStandoffStartIndex, standoff) => ??? @@ -288,6 +300,7 @@ object KnoraBaseVocab { val valueHasIntervalEnd = iri(kb + "valueHasIntervalEnd") val valueHasTimeStamp = iri(kb + "valueHasTimeStamp") val valueHasGeonameCode = iri(kb + "valueHasGeonameCode") + val valueHasRefCount = iri(kb + "valueHasRefCount") val internalFilename = iri(kb + "internalFilename") val internalMimeType = iri(kb + "internalMimeType") diff --git a/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala b/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala index d4f0a69af8..9bc22696f6 100644 --- a/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala +++ b/webapi/src/test/scala/org/knora/webapi/slice/resources/repo/service/ResourcesRepoLiveSpec.scala @@ -46,6 +46,21 @@ object TestData { linkUpdates = Seq.empty, ) + val linkValueDefinition = NewValueInfo( + resourceIri = resourceIri, + propertyIri = "foo:hasLinkToValue", + valueIri = "foo:LinkValueIri", + valueTypeIri = OntologyConstants.KnoraBase.LinkValue, + valueUUID = UUID.randomUUID(), + value = TypeSpecificValueInfo.LinkValueInfo("foo:LinkTargetIri"), + valuePermissions = valuePermissions, + valueCreator = valueCreator, + creationDate = valueCreationDate, + valueHasOrder = 1, + valueHasString = "foo:LinkValueIri", + comment = None, + ) + val intValueDefinition = NewValueInfo( resourceIri = resourceIri, @@ -374,6 +389,46 @@ object ResourcesRepoLiveSpec extends ZIOSpecDefault { } val createResourceWithValueSuite = suite("Create new resource with any type of value")( + test("Create a new resource with a link value") { + val resource = resourceDefinition.copy(newValueInfos = List(linkValueDefinition)) + + val expected = Update( + s"""| + |PREFIX rdf: + |PREFIX rdfs: + |PREFIX xsd: + |PREFIX knora-base: + | + |INSERT DATA { + | GRAPH <${graphIri.value}> { + | <$resourceIri> rdf:type <$resourceClassIri> ; + | rdfs:label "$label" ; + | knora-base:isDeleted false ; + | knora-base:attachedToUser <$userIri> ; + | knora-base:attachedToProject <$projectIri> ; + | knora-base:hasPermissions "$permissions" ; + | knora-base:creationDate "$creationDate"^^xsd:dateTime ; + | ; + | . + | rdf:type ; + | knora-base:isDeleted false ; + | knora-base:valueHasString "foo:LinkValueIri" ; + | knora-base:valueHasUUID "${UuidUtil.base64Encode(linkValueDefinition.valueUUID)}" ; + | knora-base:attachedToUser <$valueCreator> ; + | knora-base:hasPermissions "$valuePermissions" ; + | knora-base:valueHasOrder 1 ; + | knora-base:valueCreationDate "$valueCreationDate"^^xsd:dateTime ; + | rdf:subject <$resourceIri> ; + | rdf:predicate ; + | rdf:object ; + | knora-base:valueHasRefCount 1 . + | } + |} + |""".stripMargin, + ) + val result = ResourcesRepoLive.createNewResourceQuery(graphIri, resource, projectIri, userIri) + assertUpdateQueriesEqual(expected, result) + }, test("Create a new resource with an integer value") { val resource = resourceDefinition.copy(newValueInfos = List(intValueDefinition)) @@ -1184,12 +1239,8 @@ object ResourcesRepoLiveSpec extends ZIOSpecDefault { ) // TODO: // - add test for other value types - // - link value // - text value (unformatted) // - text value (formatted) // - bring back the link stuff (and figure out what's the deal) - // - add test for creating a resource with multiple values - // - add test for creating a resource with multiple links - // - add test for creating a resource with comment (and other optionals) }