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 e226264b3b..ebb620db04 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,6 +5,8 @@ package org.knora.webapi.messages.v2.responder.resourcemessages +import monocle.Optional + import java.time.Instant import java.util.UUID @@ -24,10 +26,15 @@ import org.knora.webapi.messages.util.rdf.* import org.knora.webapi.messages.util.standoff.StandoffTagUtilV2 import org.knora.webapi.messages.util.standoff.XMLUtil import org.knora.webapi.messages.v2.responder.* +import org.knora.webapi.messages.v2.responder.resourcemessages.ResourceMessagesV2Optics.CreateResourceV2Optics +import org.knora.webapi.messages.v2.responder.resourcemessages.ResourceMessagesV2Optics.CreateValueInNewResourceV2Optics 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.slice.admin.api.model.Project +import org.knora.webapi.slice.admin.domain.model.KnoraProject.CopyrightAttribution +import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseText +import org.knora.webapi.slice.admin.domain.model.KnoraProject.LicenseUri import org.knora.webapi.slice.admin.domain.model.KnoraProject.ProjectIri import org.knora.webapi.slice.admin.domain.model.Permission import org.knora.webapi.slice.admin.domain.model.User @@ -626,6 +633,35 @@ case class CreateResourceV2( ) } +object CreateResourceV2 { + def replaceCopyrightAndLicenceIfMissing( + licenseText: Option[LicenseText], + licenseUri: Option[LicenseUri], + copyrightAttribution: Option[CopyrightAttribution], + cr: CreateResourceV2, + ): CreateResourceV2 = { + def createValuesWith( + pred: FileValueV2 => Boolean, + ): Optional[Seq[CreateValueInNewResourceV2], CreateValueInNewResourceV2] = + CreateValueInNewResourceV2Optics.elements(cv => + CreateValueInNewResourceV2Optics.fileValue.getOption(cv).exists(pred), + ) + + def fileValueWith(pred: FileValueV2 => Boolean): Optional[CreateResourceV2, FileValueV2] = + CreateResourceV2Optics + .values(createValuesWith(pred).getOption(_).isDefined) + .andThen(createValuesWith(pred)) + .andThen(CreateValueInNewResourceV2Optics.fileValue) + + def replaceIfEmpty[T](newValue: Option[T], opt: Optional[FileValueV2, Option[T]]) = + fileValueWith(opt.getOption(_).flatten.isEmpty).andThen(opt).replace(newValue) + + replaceIfEmpty(licenseText, FileValueV2Optics.licenseTextOption) + .andThen(replaceIfEmpty(licenseUri, FileValueV2Optics.licenseUriOption)) + .andThen(replaceIfEmpty(copyrightAttribution, FileValueV2Optics.copyrightAttributionOption))(cr) + } +} + /** * Represents a request to create a resource. * diff --git a/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2.scala b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2.scala index b291667632..ee309198e0 100644 --- a/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2.scala +++ b/webapi/src/main/scala/org/knora/webapi/messages/v2/responder/valuemessages/ValueMessagesV2.scala @@ -685,7 +685,7 @@ object ValueContentV2 { case fvc: FileValueContentV2 => { val lt = FileValueContentV2Optics.licenseTextOption.filter(_.isEmpty).replace(licenseText) val lu = FileValueContentV2Optics.licenseUriOption.filter(_.isEmpty).replace(licenseUri) - val cp = FileValueContentV2Optics.copyRightAttributionOption.filter(_.isEmpty).replace(copyrightAttribution) + val cp = FileValueContentV2Optics.copyrightAttributionOption.filter(_.isEmpty).replace(copyrightAttribution) lt.andThen(lu).andThen(cp)(fvc) } case other => other 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 index d5efa42833..8bab477d18 100644 --- 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 @@ -37,7 +37,7 @@ object ValueMessagesV2Optics { case vc: ArchiveFileValueContentV2 => vc.copy(fileValue = fv) case vc: TextFileValueContentV2 => vc.copy(fileValue = fv) }) - val copyRightAttributionOption: Lens[FileValueContentV2, Option[CopyrightAttribution]] = + val copyrightAttributionOption: Lens[FileValueContentV2, Option[CopyrightAttribution]] = fileValueV2.andThen(FileValueV2Optics.copyrightAttributionOption) val licenseTextOption: Lens[FileValueContentV2, Option[LicenseText]] = fileValueV2.andThen(FileValueV2Optics.licenseTextOption) diff --git a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala index c801b6e014..299f19ab7c 100644 --- a/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala +++ b/webapi/src/main/scala/org/knora/webapi/responders/v2/resources/CreateResourceV2Handler.scala @@ -6,7 +6,6 @@ package org.knora.webapi.responders.v2.resources import com.typesafe.scalalogging.LazyLogging -import monocle.Optional import zio.* import java.time.Instant @@ -30,11 +29,8 @@ import org.knora.webapi.messages.v2.responder.ontologymessages.EntityInfoGetResp import org.knora.webapi.messages.v2.responder.ontologymessages.OwlCardinality.* import org.knora.webapi.messages.v2.responder.ontologymessages.ReadClassInfoV2 import org.knora.webapi.messages.v2.responder.resourcemessages.* -import org.knora.webapi.messages.v2.responder.resourcemessages.ResourceMessagesV2Optics.CreateResourceV2Optics -import org.knora.webapi.messages.v2.responder.resourcemessages.ResourceMessagesV2Optics.CreateValueInNewResourceV2Optics import org.knora.webapi.messages.v2.responder.standoffmessages.* import org.knora.webapi.messages.v2.responder.valuemessages.* -import org.knora.webapi.messages.v2.responder.valuemessages.ValueMessagesV2Optics.FileValueV2Optics import org.knora.webapi.responders.IriLocker import org.knora.webapi.responders.IriService import org.knora.webapi.responders.admin.PermissionsResponder @@ -90,28 +86,6 @@ final case class CreateResourceV2Handler( def apply(createResourceRequestV2: CreateResourceRequestV2): Task[ReadResourcesSequenceV2] = triplestoreUpdate(createResourceRequestV2) - private def replaceCopyrightAttributionAndLicenseIfMissing(project: Project): CreateResourceV2 => CreateResourceV2 = { - def createValuesWith( - pred: FileValueV2 => Boolean, - ): Optional[Seq[CreateValueInNewResourceV2], CreateValueInNewResourceV2] = - CreateValueInNewResourceV2Optics.elements(cv => - CreateValueInNewResourceV2Optics.fileValue.getOption(cv).exists(pred), - ) - - def fileValueWith(pred: FileValueV2 => Boolean): Optional[CreateResourceV2, FileValueV2] = - CreateResourceV2Optics - .values(createValuesWith(pred).getOption(_).isDefined) - .andThen(createValuesWith(pred)) - .andThen(CreateValueInNewResourceV2Optics.fileValue) - - def replaceIfEmpty[T](newValue: Option[T], opt: Optional[FileValueV2, Option[T]]) = - fileValueWith(opt.getOption(_).flatten.isEmpty).andThen(opt).replace(newValue) - - replaceIfEmpty(project.licenseText, FileValueV2Optics.licenseTextOption) - .andThen(replaceIfEmpty(project.licenseUri, FileValueV2Optics.licenseUriOption)) - .andThen(replaceIfEmpty(project.copyrightAttribution, FileValueV2Optics.copyrightAttributionOption)) - } - private def triplestoreUpdate( createResourceRequestV2: CreateResourceRequestV2, ): Task[ReadResourcesSequenceV2] = @@ -198,7 +172,13 @@ final case class CreateResourceV2Handler( // Convert the resource to the internal ontology schema. internalCreateResource <- ZIO.attempt( - replaceCopyrightAttributionAndLicenseIfMissing(project)(createResourceRequestV2.createResource) + CreateResourceV2 + .replaceCopyrightAndLicenceIfMissing( + project.licenseText, + project.licenseUri, + project.copyrightAttribution, + createResourceRequestV2.createResource, + ) .toOntologySchema(InternalSchema), )