Skip to content

Commit

Permalink
extract optics
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 22, 2024
1 parent 6abca09 commit 352f700
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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]) =>
Expand All @@ -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],
Expand Down
Original file line number Diff line number Diff line change
@@ -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]

}
}

0 comments on commit 352f700

Please sign in to comment.