Skip to content

Commit

Permalink
refactor: Replace reused datastructures with specific ones for create…
Browse files Browse the repository at this point in the history
…NewResource.scala.txt template (#3277)
  • Loading branch information
BalduinLandolt authored Jun 15, 2024
1 parent 8b74e1a commit 43b342e
Show file tree
Hide file tree
Showing 4 changed files with 377 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import java.util.UUID

import org.knora.webapi.IRI
import org.knora.webapi.messages.SmartIri
import org.knora.webapi.messages.v2.responder.valuemessages.ValueContentV2
import org.knora.webapi.messages.util.CalendarNameV2
import org.knora.webapi.messages.util.DatePrecisionV2

/**
* Contains instructions that can be given to a SPARQL template for updating direct links and `knora-base:LinkValue`
Expand Down Expand Up @@ -64,11 +65,91 @@ final case class NewLinkValueInfo(
final case class NewValueInfo(
resourceIri: IRI,
propertyIri: IRI,
value: ValueContentV2,
newValueIri: IRI,
newValueUUID: UUID,
valueCreator: IRI,
valueIri: IRI,
valueTypeIri: IRI,
valueUUID: UUID,
value: TypeSpecificValueInfo,
valuePermissions: String,
valueCreator: IRI,
creationDate: Instant,
valueHasOrder: Int,
valueHasString: String,
comment: Option[String],
)

final case class StandoffAttribute(
propertyIri: IRI,
value: String,
)

final case class StandoffTagInfo(
standoffTagClassIri: IRI,
standoffTagInstanceIri: IRI,
startParentIri: Option[IRI],
endParentIri: Option[IRI],
uuid: UUID,
originalXMLID: Option[String],
startIndex: Int,
endIndex: Option[Int],
startPosition: Int,
endPosition: Int,
attributes: Seq[StandoffAttribute],
)

enum TypeSpecificValueInfo {
case LinkValueInfo(referredResourceIri: IRI)
case UnformattedTextValueInfo(valueHasLanguage: Option[String])
case FormattedTextValueInfo(
valueHasLanguage: Option[String],
mappingIri: IRI,
maxStandoffStartIndex: Int,
standoff: Seq[StandoffTagInfo],
)
case IntegerValueInfo(valueHasInteger: Int)
case DecimalValueInfo(valueHasDecimal: BigDecimal)
case BooleanValueInfo(valueHasBoolean: Boolean)
case UriValueInfo(valueHasUri: String)
case DateValueInfo(
valueHasStartJDN: Int,
valueHasEndJDN: Int,
valueHasStartPrecision: DatePrecisionV2,
valueHasEndPrecision: DatePrecisionV2,
valueHasCalendar: CalendarNameV2,
)
case ColorValueInfo(valueHasColor: String)
case GeomValueInfo(valueHasGeometry: String)
case StillImageFileValueInfo(
internalFilename: String,
internalMimeType: String,
originalFilename: Option[String],
originalMimeType: Option[String],
dimX: Int,
dimY: Int,
)
case StillImageExternalFileValueInfo(
internalFilename: String,
internalMimeType: String,
originalFilename: Option[String],
originalMimeType: Option[String],
externalUrl: String,
)
case DocumentFileValueInfo(
internalFilename: String,
internalMimeType: String,
originalFilename: Option[String],
originalMimeType: Option[String],
dimX: Option[Int],
dimY: Option[Int],
pageCount: Option[Int],
)
case OtherFileValueInfo(
internalFilename: String,
internalMimeType: String,
originalFilename: Option[String],
originalMimeType: Option[String],
)
case HierarchicalListValueInfo(valueHasListNode: IRI)
case IntervalValueInfo(valueHasIntervalStart: BigDecimal, valueHasIntervalEnd: BigDecimal)
case TimeValueInfo(valueHasTimeStamp: Instant)
case GeonameValueInfo(valueHasGeonameCode: String)
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,16 @@ import org.knora.webapi.*
import org.knora.webapi.config.AppConfig
import org.knora.webapi.core.MessageRelay
import org.knora.webapi.messages.*
import org.knora.webapi.messages.IriConversions.*
import org.knora.webapi.messages.admin.responder.permissionsmessages.DefaultObjectAccessPermissionsStringForResourceClassGetADM
import org.knora.webapi.messages.admin.responder.permissionsmessages.DefaultObjectAccessPermissionsStringResponseADM
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionADM
import org.knora.webapi.messages.admin.responder.permissionsmessages.PermissionType
import org.knora.webapi.messages.admin.responder.permissionsmessages.ResourceCreateOperation
import org.knora.webapi.messages.twirl.NewLinkValueInfo
import org.knora.webapi.messages.twirl.NewValueInfo
import org.knora.webapi.messages.twirl.StandoffAttribute
import org.knora.webapi.messages.twirl.StandoffTagInfo
import org.knora.webapi.messages.twirl.TypeSpecificValueInfo.*
import org.knora.webapi.messages.util.*
import org.knora.webapi.messages.util.PermissionUtilADM.AGreaterThanB
import org.knora.webapi.messages.util.PermissionUtilADM.PermissionComparisonResult
Expand Down Expand Up @@ -449,16 +451,167 @@ final case class CreateResourceV2Handler(
// Make a creation date for the value. If a custom creation date is given for a value, consider that otherwise
// use resource creation date for the value.
valueCreationDate: Instant = valueToCreate.customValueCreationDate.getOrElse(creationDate)

valueInfo <-
valueToCreate.valueContent match
case DateValueContentV2(
_,
valueHasStartJDN,
valueHasEndJDN,
valueHasStartPrecision,
valueHasEndPrecision,
valueHasCalendar,
_,
) =>
ZIO.succeed(
DateValueInfo(
valueHasStartJDN = valueHasStartJDN,
valueHasEndJDN = valueHasEndJDN,
valueHasStartPrecision = valueHasStartPrecision,
valueHasEndPrecision = valueHasEndPrecision,
valueHasCalendar = valueHasCalendar,
),
)
case TextValueContentV2(_, _, valueHasLanguage, _, None, _, _, _) =>
ZIO.succeed(UnformattedTextValueInfo(valueHasLanguage))
case tv @ TextValueContentV2(_, _, valueHasLanguage, _, Some(mappingIri), _, _, _) =>
val standoffInfo = tv
.prepareForSparqlInsert(newValueIri)
.map(standoffTag =>
StandoffTagInfo(
standoffTagClassIri = standoffTag.standoffNode.standoffTagClassIri.toString(),
standoffTagInstanceIri = standoffTag.standoffTagInstanceIri,
startParentIri = standoffTag.startParentIri,
endParentIri = standoffTag.endParentIri,
uuid = standoffTag.standoffNode.uuid,
originalXMLID = standoffTag.standoffNode.originalXMLID,
startIndex = standoffTag.standoffNode.startIndex,
endIndex = standoffTag.standoffNode.endIndex,
startPosition = standoffTag.standoffNode.startPosition,
endPosition = standoffTag.standoffNode.endPosition,
attributes = standoffTag.standoffNode.attributes
.map(attr => StandoffAttribute(attr.standoffPropertyIri.toString(), attr.rdfValue)),
),
)
ZIO
.fromOption(tv.computedMaxStandoffStartIndex)
.orElseFail(StandoffInternalException("Max standoff start index not computed"))
.map(standoffStartIndex =>
FormattedTextValueInfo(valueHasLanguage, mappingIri, standoffStartIndex, standoffInfo),
)
case IntegerValueContentV2(_, valueHasInteger, _) =>
ZIO.succeed(IntegerValueInfo(valueHasInteger))
case DecimalValueContentV2(_, valueHasDecimal, _) =>
ZIO.succeed(DecimalValueInfo(valueHasDecimal))
case BooleanValueContentV2(_, valueHasBoolean, _) =>
ZIO.succeed(BooleanValueInfo(valueHasBoolean))
case GeomValueContentV2(_, valueHasGeometry, _) =>
ZIO.succeed(GeomValueInfo(valueHasGeometry))
case IntervalValueContentV2(_, valueHasIntervalStart, valueHasIntervalEnd, _) =>
ZIO.succeed(IntervalValueInfo(valueHasIntervalStart, valueHasIntervalEnd))
case TimeValueContentV2(_, valueHasTimeStamp, _) =>
ZIO.succeed(TimeValueInfo(valueHasTimeStamp))
case HierarchicalListValueContentV2(_, valueHasListNode, listNodeLabel, _) =>
ZIO.succeed(HierarchicalListValueInfo(valueHasListNode))
case ColorValueContentV2(_, valueHasColor, _) =>
ZIO.succeed(ColorValueInfo(valueHasColor))
case UriValueContentV2(_, valueHasUri, _) =>
ZIO.succeed(UriValueInfo(valueHasUri))
case GeonameValueContentV2(_, valueHasGeonameCode, _) =>
ZIO.succeed(GeonameValueInfo(valueHasGeonameCode))
case StillImageFileValueContentV2(_, fileValue, dimX, dimY, _) =>
ZIO.succeed(
StillImageFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
dimX = dimX,
dimY = dimY,
),
)
case StillImageExternalFileValueContentV2(_, fileValue, externalUrl, _) =>
ZIO.succeed(
StillImageExternalFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
externalUrl = externalUrl.value.toString(),
),
)
case DocumentFileValueContentV2(_, fileValue, pageCount, dimX, dimY, _) =>
ZIO.succeed(
DocumentFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
dimX = dimX,
dimY = dimY,
pageCount = pageCount,
),
)
case ArchiveFileValueContentV2(_, fileValue, _) =>
ZIO.succeed(
OtherFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
),
)
case TextFileValueContentV2(_, fileValue, _) =>
ZIO.succeed(
OtherFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
),
)
case AudioFileValueContentV2(_, fileValue, _) =>
ZIO.succeed(
OtherFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
),
)
case MovingImageFileValueContentV2(_, fileValue, _) =>
ZIO.succeed(
OtherFileValueInfo(
internalFilename = fileValue.internalFilename,
internalMimeType = fileValue.internalMimeType,
originalFilename = fileValue.originalFilename,
originalMimeType = fileValue.originalMimeType,
),
)
case LinkValueContentV2(
_,
referredResourceIri,
referredResourceExists,
isIncomingLink,
nestedResource,
_,
) =>
ZIO.succeed(LinkValueInfo(referredResourceIri))
case _: DeletedValueContentV2 => ZIO.fail(BadRequestException("Deleted values cannot be created"))

} yield NewValueInfo(
resourceIri = resourceIri,
propertyIri = propertyIri.toIri,
value = valueToCreate.valueContent,
newValueIri = newValueIri,
newValueUUID = newValueUUID,
value = valueInfo,
valueIri = newValueIri,
valueTypeIri = valueToCreate.valueContent.valueType.toString(),
valueUUID = newValueUUID,
valueCreator = requestingUser.id,
valuePermissions = valueToCreate.permissions,
creationDate = valueCreationDate,
valueHasOrder = valueHasOrder,
valueHasString = valueToCreate.valueContent.valueHasString,
comment = valueToCreate.valueContent.comment,
)
}
} yield ResourceReadyToCreate(
Expand Down
Loading

0 comments on commit 43b342e

Please sign in to comment.