From 352f700c5cd4b050b1ba3172de2e205a79d624ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Kleinb=C3=B6lting?= Date: Fri, 22 Nov 2024 09:23:54 +0100 Subject: [PATCH] extract optics --- .../it/v2/CopyrightAndLicensesSpec.scala | 3 +- .../resourcemessages/ResourceMessagesV2.scala | 49 +++---------- .../valuemessages/ValueMessagesV2Optics.scala | 70 +++++++++++++++++++ 3 files changed, 80 insertions(+), 42 deletions(-) create mode 100644 webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2Optics.scala diff --git a/integration/src/test/scala/org/knora/webapi/it/v2/CopyrightAndLicensesSpec.scala b/integration/src/test/scala/org/knora/webapi/it/v2/CopyrightAndLicensesSpec.scala index e71f37767a..5a31a0c130 100644 --- a/integration/src/test/scala/org/knora/webapi/it/v2/CopyrightAndLicensesSpec.scala +++ b/integration/src/test/scala/org/knora/webapi/it/v2/CopyrightAndLicensesSpec.scala @@ -10,11 +10,13 @@ import org.apache.jena.rdf.model.Property import org.apache.jena.rdf.model.Resource import org.apache.jena.vocabulary.RDF import zio.* +import zio.http.Response import zio.test.* import java.net.URLEncoder import scala.jdk.CollectionConverters.IteratorHasAsScala import scala.language.implicitConversions + import org.knora.webapi.E2EZSpec import org.knora.webapi.messages.OntologyConstants import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex.HasCopyrightAttribution @@ -33,7 +35,6 @@ 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 zio.http.Response object CopyrightAndLicensesSpec extends E2EZSpec { diff --git a/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/resourcemessages/ResourceMessagesV2.scala b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/resourcemessages/ResourceMessagesV2.scala index 521e89f59d..3a9b024c81 100644 --- a/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/resourcemessages/ResourceMessagesV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/resourcemessages/ResourceMessagesV2.scala @@ -5,11 +5,7 @@ package org.knora.webapi.messages.v2.responder.resourcemessages -import monocle.Lens import monocle.Optional -import monocle.Prism -import monocle.macros.GenLens -import monocle.macros.GenPrism import java.time.Instant import java.util.UUID @@ -32,6 +28,7 @@ import org.knora.webapi.messages.util.standoff.XMLUtil import org.knora.webapi.messages.v2.responder.* import org.knora.webapi.messages.v2.responder.standoffmessages.MappingXMLtoStandoff import org.knora.webapi.messages.v2.responder.valuemessages.* +import org.knora.webapi.messages.v2.responder.valuemessages.ValueMessagesV2Optics.* import org.knora.webapi.routing.v2.AssetIngestState import org.knora.webapi.routing.v2.AssetIngestState.* import org.knora.webapi.slice.admin.api.model.Project @@ -591,10 +588,8 @@ object ReadResourceV2 { copyright: Option[CopyrightAttribution], license: Option[License], ): ReadResourceV2 => ReadResourceV2 = - setIfMissing(licenseLens)(license).andThen(setIfMissing(copyrightAttributionLens)(copyright)) - - private val copyrightAttributionLens = GenLens[FileValueV2](_.copyrightAttribution) - private val licenseLens = GenLens[FileValueV2](_.license) + setIfMissing(FileValueV2Optics.licenseLens)(license) + .andThen(setIfMissing(FileValueV2Optics.copyrightAttributionLens)(copyright)) private def ifMissingMapper[T](optional: Optional[ReadValueV2, Option[T]], newValue: Option[T]) = (smartIri: SmartIri, seq: Seq[ReadValueV2]) => @@ -608,39 +603,11 @@ object ReadResourceV2 { ) private def setIfMissing[T](opt: Optional[FileValueV2, Option[T]]): Option[T] => ReadResourceV2 => ReadResourceV2 = - value => rr => rr.copy(values = rr.values.map(ifMissingMapper(fileValueFromReadValue.andThen(opt), value)(_, _))) - - private val fileValueContentLens: Lens[ReadValueV2, ValueContentV2] = - Lens[ReadValueV2, ValueContentV2](_.valueContent)(fc => { - case rv: ReadLinkValueV2 => - fc match { - case lv: LinkValueContentV2 => rv.copy(valueContent = lv) - case _ => rv - } - case rv: ReadTextValueV2 => - fc match { - case tv: TextValueContentV2 => rv.copy(valueContent = tv) - case _ => rv - } - case ov: ReadOtherValueV2 => ov.copy(valueContent = fc) - }) - - private val fileValueContentPrism: Prism[ValueContentV2, FileValueContentV2] = - GenPrism[ValueContentV2, FileValueContentV2] - - private val fileValueLens: Lens[FileValueContentV2, FileValueV2] = - Lens[FileValueContentV2, FileValueV2](_.fileValue)(fv => { - case vc: MovingImageFileValueContentV2 => vc.copy(fileValue = fv) - case vc: StillImageFileValueContentV2 => vc.copy(fileValue = fv) - case vc: AudioFileValueContentV2 => vc.copy(fileValue = fv) - case vc: DocumentFileValueContentV2 => vc.copy(fileValue = fv) - case vc: StillImageExternalFileValueContentV2 => vc.copy(fileValue = fv) - case vc: ArchiveFileValueContentV2 => vc.copy(fileValue = fv) - case vc: TextFileValueContentV2 => vc.copy(fileValue = fv) - }) - - private val fileValueFromReadValue: Optional[ReadValueV2, FileValueV2] = - fileValueContentLens.andThen(fileValueContentPrism).andThen(fileValueLens) + value => + rr => + rr.copy(values = + rr.values.map(ifMissingMapper(ReadValueV2Optics.fileValueFromReadValue.andThen(opt), value)(_, _)), + ) private def setCopyrightAndLicenceIfMissingOnLinkedResources( copyright: Option[CopyrightAttribution], diff --git a/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2Optics.scala b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2Optics.scala new file mode 100644 index 0000000000..ce0d48cb53 --- /dev/null +++ b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2Optics.scala @@ -0,0 +1,70 @@ +/* + * 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.v2.responder.valuemessages + +import monocle.* +import monocle.macros.* + +import org.knora.webapi.slice.admin.domain.model.KnoraProject.CopyrightAttribution +import org.knora.webapi.slice.admin.domain.model.KnoraProject.License + +object ValueMessagesV2Optics { + + object FileValueV2Optics { + + val copyrightAttributionLens: Lens[FileValueV2, Option[CopyrightAttribution]] = + GenLens[FileValueV2](_.copyrightAttribution) + + val licenseLens: Lens[FileValueV2, Option[License]] = + GenLens[FileValueV2](_.license) + + } + + object FileValueContentV2Optics { + + val fileValueLens: Lens[FileValueContentV2, FileValueV2] = + Lens[FileValueContentV2, FileValueV2](_.fileValue)(fv => { + case vc: MovingImageFileValueContentV2 => vc.copy(fileValue = fv) + case vc: StillImageFileValueContentV2 => vc.copy(fileValue = fv) + case vc: AudioFileValueContentV2 => vc.copy(fileValue = fv) + case vc: DocumentFileValueContentV2 => vc.copy(fileValue = fv) + case vc: StillImageExternalFileValueContentV2 => vc.copy(fileValue = fv) + case vc: ArchiveFileValueContentV2 => vc.copy(fileValue = fv) + case vc: TextFileValueContentV2 => vc.copy(fileValue = fv) + }) + + } + + object ReadValueV2Optics { + + val fileValueContentLens: Lens[ReadValueV2, ValueContentV2] = + Lens[ReadValueV2, ValueContentV2](_.valueContent)(fc => { + case rv: ReadLinkValueV2 => + fc match { + case lv: LinkValueContentV2 => rv.copy(valueContent = lv) + case _ => rv + } + case rv: ReadTextValueV2 => + fc match { + case tv: TextValueContentV2 => rv.copy(valueContent = tv) + case _ => rv + } + case ov: ReadOtherValueV2 => ov.copy(valueContent = fc) + }) + + val fileValueFromReadValue: Optional[ReadValueV2, FileValueV2] = + ReadValueV2Optics.fileValueContentLens + .andThen(ValueContentV2Optics.fileValueContentPrism) + .andThen(FileValueContentV2Optics.fileValueLens) + + } + + object ValueContentV2Optics { + + val fileValueContentPrism: Prism[ValueContentV2, FileValueContentV2] = GenPrism[ValueContentV2, FileValueContentV2] + + } +}