Skip to content

Commit

Permalink
fix typo and align method name, move replace function to companion ob…
Browse files Browse the repository at this point in the history
…ject
  • Loading branch information
seakayone committed Nov 26, 2024
1 parent f1144f6 commit 10dcc31
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.knora.webapi.messages.v2.responder.resourcemessages

import monocle.Optional

import java.time.Instant
import java.util.UUID

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

Expand Down

0 comments on commit 10dcc31

Please sign in to comment.