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 8e59542348..521e89f59d 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 @@ -580,38 +580,35 @@ case class ReadResourceV2( object ReadResourceV2 { - private def setCopyrightAndLicenceIfMissingOnLinkedResources( + def setCopyrightAndLicenceIfMissing( copyright: Option[CopyrightAttribution], license: Option[License], ): ReadResourceV2 => ReadResourceV2 = - rr => { - val newValues: Map[SmartIri, Seq[ReadValueV2]] = rr.values.map((iri: SmartIri, seq: Seq[ReadValueV2]) => - ( - iri, - seq.map { - case lv: ReadLinkValueV2 => - linkValueFromReadValue - .andThen(nestedResourceFromLinkValueContent) - .modifyOption(setCopyrightAndLicenceIfMissingResourceValues(copyright, license))(lv) - .getOrElse(lv) - case other => other - }, - ), - ) - rr.copy(values = newValues) - } + setCopyrightAndLicenceIfMissingResourceValues(copyright, license) + .andThen(setCopyrightAndLicenceIfMissingOnLinkedResources(copyright, license)) - private val linkValueFromReadValue = Optional[ReadValueV2, LinkValueContentV2] { - case lv: ReadLinkValueV2 => Some(lv.valueContent) - case _ => None - }(lv => { - case rv: ReadLinkValueV2 => rv.copy(valueContent = lv) - case rv: ReadOtherValueV2 => rv.copy(valueContent = lv) - case rv: ReadTextValueV2 => rv - }) + private def setCopyrightAndLicenceIfMissingResourceValues( + 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) + + private def ifMissingMapper[T](optional: Optional[ReadValueV2, Option[T]], newValue: Option[T]) = + (smartIri: SmartIri, seq: Seq[ReadValueV2]) => + ( + smartIri, + seq.map { readValue => + optional.getOption(readValue).flatten match + case Some(_) => readValue + case None => optional.replace(newValue)(readValue) + }, + ) - private val nestedResourceFromLinkValueContent = - Optional[LinkValueContentV2, ReadResourceV2](_.nestedResource)(rr => lv => lv.copy(nestedResource = Some(rr))) + 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 => { @@ -642,52 +639,41 @@ object ReadResourceV2 { case vc: TextFileValueContentV2 => vc.copy(fileValue = fv) }) - private val copyrightAttributionLens: Lens[FileValueV2, Option[CopyrightAttribution]] = - GenLens[FileValueV2](_.copyrightAttribution) - - private val licenseLens: Lens[FileValueV2, Option[License]] = - GenLens[FileValueV2](_.license) - private val fileValueFromReadValue: Optional[ReadValueV2, FileValueV2] = fileValueContentLens.andThen(fileValueContentPrism).andThen(fileValueLens) - private def setIfMissing[T]( - optional: Optional[ReadValueV2, Option[T]], - ): Option[T] => ReadResourceV2 => ReadResourceV2 = - newValue => - readResource => { - val newValues: Map[SmartIri, Seq[ReadValueV2]] = - readResource.values.map((iri: SmartIri, seq: Seq[ReadValueV2]) => - ( - iri, - seq.map { readValue => - optional.getOption(readValue).flatten match { - case Some(_) => readValue - case None => optional.replace(newValue)(readValue) - } - }, - ), - ) - readResource.copy(values = newValues) - } - - private def setCopyrightAndLicenceIfMissingResourceValues( - copyright: Option[CopyrightAttribution], - license: Option[License], - ): ReadResourceV2 => ReadResourceV2 = { - val licenseOptional: Optional[ReadValueV2, Option[License]] = - fileValueFromReadValue.andThen(licenseLens) - val copyrightAttributionOptional: Optional[ReadValueV2, Option[CopyrightAttribution]] = - fileValueFromReadValue.andThen(copyrightAttributionLens) - setIfMissing(licenseOptional)(license).andThen(setIfMissing(copyrightAttributionOptional)(copyright)) - } - - def setCopyrightAndLicenceIfMissing( + private def setCopyrightAndLicenceIfMissingOnLinkedResources( copyright: Option[CopyrightAttribution], license: Option[License], ): ReadResourceV2 => ReadResourceV2 = - setCopyrightAndLicenceIfMissingResourceValues(copyright, license) - .andThen(setCopyrightAndLicenceIfMissingOnLinkedResources(copyright, license)) + rr => rr.copy(values = rr.values.map(linkValueIfMissingMapper(copyright, license)(_, _))) + + private def linkValueIfMissingMapper(copyright: Option[CopyrightAttribution], license: Option[License]) = + (iri: SmartIri, seq: Seq[ReadValueV2]) => + ( + iri, + seq.map { + case lv: ReadLinkValueV2 => + linkValueFromReadValue + .andThen(nestedResourceFromLinkValueContent) + .modifyOption(setCopyrightAndLicenceIfMissingResourceValues(copyright, license))(lv) + .getOrElse(lv) + case other => other + }, + ) + + private val linkValueFromReadValue = Optional[ReadValueV2, LinkValueContentV2] { + case lv: ReadLinkValueV2 => Some(lv.valueContent) + case _ => None + }(lv => { + case rv: ReadLinkValueV2 => rv.copy(valueContent = lv) + case rv: ReadOtherValueV2 => rv.copy(valueContent = lv) + case rv: ReadTextValueV2 => rv + }) + + private val nestedResourceFromLinkValueContent = + Optional[LinkValueContentV2, ReadResourceV2](_.nestedResource)(rr => lv => lv.copy(nestedResource = Some(rr))) + } /**