Skip to content

Commit

Permalink
use traverse
Browse files Browse the repository at this point in the history
  • Loading branch information
seakayone committed Nov 27, 2024
1 parent c04a032 commit 30cabe5
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package org.knora.webapi.messages.v2.responder.valuemessages

import org.apache.jena.rdf.model.Resource
import org.apache.jena.vocabulary.XSD
import zio.IO
import zio.ZIO

Expand All @@ -27,7 +28,6 @@ import org.knora.webapi.messages.IriConversions.*
import org.knora.webapi.messages.OntologyConstants
import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex
import org.knora.webapi.messages.OntologyConstants.KnoraApiV2Complex.*
import org.knora.webapi.messages.OntologyConstants.Xsd
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.StringFormatter
import org.knora.webapi.messages.ValuesValidator
Expand Down Expand Up @@ -60,10 +60,7 @@ import org.knora.webapi.store.iiif.api.SipiService
import org.knora.webapi.util.WithAsIs

private def objectCommentOption(r: Resource): Either[String, Option[String]] =
r.objectStringOption(ValueHasComment) flatMap {
case Some(str) => Iri.toSparqlEncodedString(str).toRight(s"Invalid comment: $str").map(Some(_))
case None => Right(None)
}
r.objectStringOption(ValueHasComment, str => Iri.toSparqlEncodedString(str).toRight(s"Invalid comment: $str"))

/**
* Represents a successful response to a create value Request.
Expand Down Expand Up @@ -869,25 +866,19 @@ object DateValueContentV2 {
)
}

def from(r: Resource): Either[String, DateValueContentV2] = {
def objectEraOption(resource: Resource, property: String) = for {
eraStr <- resource.objectStringOption(property)
era <- eraStr match
case Some(e) => DateEraV2.fromString(e).map(Some(_))
case None => Right(None)
} yield era
def from(r: Resource): Either[String, DateValueContentV2] =
for {
startYear <- r.objectInt(DateValueHasStartYear)
startMonth <- r.objectIntOption(DateValueHasStartMonth)
startDay <- r.objectIntOption(DateValueHasStartDay)
startEra <- objectEraOption(r, DateValueHasStartEra)
startEra <- r.objectStringOption(DateValueHasStartEra, DateEraV2.fromString)

endYear <- r.objectInt(DateValueHasEndYear)
endMonth <- r.objectIntOption(DateValueHasEndMonth)
endDay <- r.objectIntOption(DateValueHasEndDay)
endEra <- objectEraOption(r, DateValueHasEndEra)
endEra <- r.objectStringOption(DateValueHasEndEra, DateEraV2.fromString)

calendarName <- r.objectString(DateValueHasCalendar).flatMap(CalendarNameV2.fromString)
calendarName <- r.objectString(DateValueHasCalendar, CalendarNameV2.fromString)

// validate the combination of start/end dates and calendarName
_ <- if (startMonth.isEmpty && startDay.isDefined) Left(s"Start day defined, missing start month") else Right(())
Expand All @@ -912,7 +903,6 @@ object DateValueContentV2 {
calendarName,
comment,
)
}
}

/**
Expand Down Expand Up @@ -2154,9 +2144,9 @@ object StillImageFileValueContentV2 {
def from(r: Resource, fileInfo: FileInfo): Either[String, StillImageFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = fileInfo.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
fileValue = FileValueV2(
fileInfo.filename,
meta.internalMimeType,
Expand All @@ -2175,30 +2165,6 @@ object StillImageFileValueContentV2 {
)
}

def getCopyrightAttribution(resource: Resource): Either[String, Option[CopyrightAttribution]] = for {
str <- resource.objectStringOption(HasCopyrightAttribution)
copyrightAttribution <- str match {
case Some(str) => CopyrightAttribution.from(str).map(Some(_))
case None => Right(None)
}
} yield copyrightAttribution

def getLicenseText(resource: Resource): Either[String, Option[LicenseText]] = for {
str <- resource.objectStringOption(HasLicenseText)
licenseText <- str match {
case Some(str) => LicenseText.from(str).map(Some(_))
case None => Right(None)
}
} yield licenseText

def getLicenseUri(resource: Resource): Either[String, Option[LicenseUri]] = for {
str <- resource.objectDataTypeOption(HasLicenseUri, Xsd.Uri)
licenseUri <- str match {
case Some(str) => LicenseUri.from(str).map(Some(_))
case None => Right(None)
}
} yield licenseUri

/**
* Represents the external image file metadata.
*
Expand Down Expand Up @@ -2279,9 +2245,9 @@ object StillImageExternalFileValueContentV2 {
externalUrlStr <- r.objectString(StillImageFileValueHasExternalUrl)
iifUrl <- IiifImageRequestUrl.from(externalUrlStr)
comment <- objectCommentOption(r)
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
fileValue = FileValueV2(
"internalFilename",
"internalMimeType",
Expand Down Expand Up @@ -2430,9 +2396,9 @@ object DocumentFileValueContentV2 {
def from(r: Resource, info: FileInfo): Either[String, DocumentFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = info.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
fileValue = FileValueV2(
info.filename,
meta.internalMimeType,
Expand All @@ -2452,9 +2418,9 @@ object ArchiveFileValueContentV2 {
def from(r: Resource, info: FileInfo): Either[String, ArchiveFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = info.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
fileValue = FileValueV2(
info.filename,
meta.internalMimeType,
Expand Down Expand Up @@ -2533,9 +2499,9 @@ object TextFileValueContentV2 {
def from(r: Resource, info: FileInfo): Either[String, TextFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = info.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
fileValue = FileValueV2(
info.filename,
meta.internalMimeType,
Expand Down Expand Up @@ -2614,9 +2580,9 @@ object AudioFileValueContentV2 {
def from(r: Resource, info: FileInfo): Either[String, AudioFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = info.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
} yield AudioFileValueContentV2(
ApiV2Complex,
FileValueV2(
Expand Down Expand Up @@ -2700,9 +2666,9 @@ object MovingImageFileValueContentV2 {
def from(r: Resource, info: FileInfo): Either[String, MovingImageFileValueContentV2] = for {
comment <- objectCommentOption(r)
meta = info.metadata
copyrightAttribution <- getCopyrightAttribution(r)
licenseText <- getLicenseText(r)
licenseUri <- getLicenseUri(r)
copyrightAttribution <- r.objectStringOption(HasCopyrightAttribution, CopyrightAttribution.from)
licenseText <- r.objectStringOption(HasLicenseText, LicenseText.from)
licenseUri <- r.objectDataTypeOption(HasLicenseUri, XSD.anyURI.toString, LicenseUri.from)
} yield MovingImageFileValueContentV2(
ApiV2Complex,
FileValueV2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@

package org.knora.webapi.slice.common.jena

import cats.instances.option.*
import cats.syntax.traverse.*
import org.apache.jena.rdf.model.Property
import org.apache.jena.rdf.model.Resource
import org.apache.jena.rdf.model.Statement
Expand Down Expand Up @@ -39,8 +41,14 @@ object ResourceOps {
def objectInt(p: Property): Either[String, Int] = statement(p).flatMap(_.objectAsInt)
def objectIntOption(p: Property): Either[String, Option[Int]] = fromStatement(p, _.objectAsInt)

def objectString(p: Property): Either[String, String] = statement(p).flatMap(_.objectAsString)
def objectStringOption(p: Property): Either[String, Option[String]] = fromStatement(p, _.objectAsString)
def objectString(p: Property): Either[String, String] =
statement(p).flatMap(_.objectAsString)
def objectString[A](p: Property, mapper: String => Either[String, A]): Either[String, A] =
objectString(p).flatMap(mapper)
def objectStringOption(p: Property): Either[String, Option[String]] =
fromStatement(p, _.objectAsString)
def objectStringOption[A](p: Property, mapper: String => Either[String, A]): Either[String, Option[A]] =
objectStringOption(p).flatMap(_.traverse(mapper))

def objectUri(p: Property): Either[String, String] = statement(p).flatMap(stmt => stmt.objectAsUri)
def objectUriOption(p: Property): Either[String, Option[String]] = fromStatement(p, _.objectAsUri)
Expand All @@ -52,6 +60,8 @@ object ResourceOps {
statement(p).flatMap(stmt => stmt.objectAsDataType(dt))
def objectDataTypeOption(p: Property, dt: String): Either[String, Option[String]] =
fromStatement(p, _.objectAsDataType(dt))
def objectDataTypeOption[A](p: Property, dt: String, f: String => Either[String, A]): Either[String, Option[A]] =
objectDataTypeOption(p, dt).flatMap(_.traverse(f))

def rdfsType: Option[String] = Option(res.getPropertyResourceValue(RDF.`type`)).flatMap(_.uri)
def uri: Option[String] = Option(res.getURI)
Expand Down

0 comments on commit 30cabe5

Please sign in to comment.