Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Avoid mutability and non-null assertions (TT-1643) #83

Merged
merged 4 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import org.springframework.context.annotation.Configuration

@ConfigurationProperties("kerberos")
class KerberosConfigProperties (
var realm: String,
var kdc: String
val realm: String,
val kdc: String
)

@Configuration
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
package no.nb.bikube.catalogue.collections.controller

import no.nb.bikube.catalogue.collections.exception.CollectionsException
import no.nb.bikube.catalogue.collections.exception.CollectionsItemNotFound
import no.nb.bikube.catalogue.collections.exception.CollectionsManifestationNotFound
import no.nb.bikube.catalogue.collections.exception.CollectionsTitleNotFound
import no.nb.bikube.catalogue.collections.exception.*
import no.nb.bikube.core.controller.addDefaultProperties
import no.nb.bikube.core.util.logger
import org.springframework.http.HttpStatus
Expand Down Expand Up @@ -57,4 +54,15 @@ class CollectionsControllerExceptionHandler {

return problemDetail
}

@ExceptionHandler(CollectionsObjectMissing::class)
fun handleCollectionsObjectMissingException(exception: CollectionsObjectMissing): ProblemDetail {
logger().warn("CollectionsObjectMissing occurred: ${exception.message}")

val problemDetail = ProblemDetail.forStatus(HttpStatus.INTERNAL_SERVER_ERROR)
problemDetail.detail = "Collections object missing: ${exception.message}"
problemDetail.addDefaultProperties()

return problemDetail
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package no.nb.bikube.catalogue.collections.exception

class CollectionsObjectMissing (message: String? = null): CollectionsException(message)
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ fun mapCollectionsPartsObjectToGenericItem(
date: String? = null
): Item {
return Item(
catalogueId = model.priRef!!,
catalogueId = model.priRef,
name = model.getName(),
date = parseYearOrDate(date) ?: model.getStartDate(),
materialType = materialType,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package no.nb.bikube.catalogue.collections.model

import no.nb.bikube.catalogue.collections.exception.CollectionsObjectMissing

interface CollectionsGenericObject {
val priRef: String
}

interface CollectionsGenericRecordList<T> {
val recordList: List<T>?
}

interface CollectionsGenericModel<T> {
val adlibJson: CollectionsGenericRecordList<T>

fun getObjects(): List<T>? {
return this.adlibJson.recordList
}

fun hasObjects(): Boolean {
return this.getObjects()?.isNotEmpty() ?: false
}

@Throws(CollectionsObjectMissing::class)
fun getFirstObject(): T {
return this.getObjects().takeIf { !it.isNullOrEmpty() }
?. first()
?: throw CollectionsObjectMissing()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class CollectionsLocationModel(
@JsonProperty("adlibJSON")
val adlibJson: CollectionsLocationRecordList?
)
override val adlibJson: CollectionsLocationRecordList
) : CollectionsGenericModel<CollectionsLocationObject>

data class CollectionsLocationRecordList(
val recordList: List<CollectionsLocationObject>?
)
override val recordList: List<CollectionsLocationObject>?
) : CollectionsGenericRecordList<CollectionsLocationObject>

data class CollectionsLocationObject(
@JsonProperty("@priref")
val priRef: String?,
override val priRef: String,

@JsonProperty("name")
val name: List<String>?,
Expand All @@ -24,8 +24,4 @@ data class CollectionsLocationObject(
// This one is weird for containers/location, use language=0! There we have "container" or "location". Otherwise, it's "package" or "location"
@JsonProperty("package_location")
val packageLocation: List<List<CollectionsLanguageListObject>>?
)

fun CollectionsLocationModel.getFirstObject(): CollectionsLocationObject? {
return this.adlibJson?.recordList?.firstOrNull()
}
) : CollectionsGenericObject
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class CollectionsNameModel(
@JsonProperty("adlibJSON")
val adlibJson: CollectionsNameRecordList
)
override val adlibJson: CollectionsNameRecordList
) : CollectionsGenericModel<CollectionsNameObject>

data class CollectionsNameRecordList(
val recordList: List<CollectionsNameObject>?
)
override val recordList: List<CollectionsNameObject>?
) : CollectionsGenericRecordList<CollectionsNameObject>

data class CollectionsNameObject(
@JsonProperty("@priref")
val priRef: String?,
override val priRef: String,

@JsonProperty("name")
val name: String,
)
) : CollectionsGenericObject
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class CollectionsModel(
@JsonProperty("adlibJSON")
val adlibJson: CollectionsRecordList
)
override val adlibJson: CollectionsRecordList
) : CollectionsGenericModel<CollectionsObject>

data class CollectionsRecordList(
val recordList: List<CollectionsObject>?,
override val recordList: List<CollectionsObject>?,

val diagnostic: CollectionDiagnostic? = null
)
) : CollectionsGenericRecordList<CollectionsObject>

data class CollectionsObject(
@JsonProperty("@priref")
val priRef: String,
override val priRef: String,

@JsonProperty("Title")
val titleList: List<CollectionsTitle>?,
Expand Down Expand Up @@ -82,7 +82,7 @@ data class CollectionsObject(

@JsonProperty("current_location.barcode")
val locationBarcode: String? = null
)
) : CollectionsGenericObject

data class CollectionsTitle(
val title: String?
Expand Down Expand Up @@ -159,7 +159,7 @@ data class CollectionsAssociationGeo(

data class CollectionsPartsReference(
@JsonProperty("priref")
val priRef: String?,
val priRef: String,

@JsonProperty("group:Dating")
val dateStart: List<CollectionsDating>?,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,6 @@ import no.nb.bikube.core.util.DateUtils.Companion.parseYearOrDate
import java.time.LocalDate

// CollectionsModel
fun CollectionsModel.getObjects(): List<CollectionsObject>? {
return this.adlibJson.recordList
}

fun CollectionsModel.getFirstObject(): CollectionsObject? {
return this.getObjects()?.first()
}

fun CollectionsModel.getFirstId(): String? {
return this.getObjects()?.first()?.priRef
}
Expand All @@ -27,10 +19,6 @@ fun CollectionsModel.getError(): String? {
return this.adlibJson.diagnostic?.error?.message
}

fun CollectionsModel.isEmpty(): Boolean {
return this.getObjects()?.isEmpty() ?: true
}

// CollectionsObject
fun CollectionsObject.getUrn(): String? {
return this.urn?.firstOrNull() ?: this.alternativeNumberList?.find { it.type == "URN" }?.value
Expand Down Expand Up @@ -148,21 +136,3 @@ fun CollectionsPartOfReference.getFirstPartOf(): CollectionsPartOfReference? {
fun CollectionsPartOfReference.getDate(): LocalDate? {
return this.datingList?.first()?.dateFrom?.let { parseYearOrDate(it) }
}

// CollectionsNameModel
fun CollectionsNameModel.getObjects(): List<CollectionsNameObject>? {
return this.adlibJson.recordList
}

fun CollectionsNameModel.getFirstObject(): CollectionsNameObject? {
return this.getObjects()?.first()
}

//CollectionsTermModel
fun CollectionsTermModel.getObjects(): List<CollectionsTermObject>? {
return this.adlibJson.recordList
}

fun CollectionsTermModel.getFirstObject(): CollectionsTermObject? {
return this.getObjects()?.first()
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@ import com.fasterxml.jackson.annotation.JsonProperty

data class CollectionsTermModel(
@JsonProperty("adlibJSON")
val adlibJson: CollectionsTermRecordList
)
override val adlibJson: CollectionsTermRecordList
) : CollectionsGenericModel<CollectionsTermObject>

data class CollectionsTermRecordList(
val recordList: List<CollectionsTermObject>?
)
override val recordList: List<CollectionsTermObject>?
) : CollectionsGenericRecordList<CollectionsTermObject>

data class CollectionsTermObject(
@JsonProperty("@priref")
val priRef: String,
override val priRef: String,

@JsonProperty("term")
val term: String,
)
) : CollectionsGenericObject
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,9 @@ import no.nb.bikube.catalogue.collections.exception.CollectionsItemNotFound
import no.nb.bikube.catalogue.collections.model.CollectionsLocationObject
import no.nb.bikube.catalogue.collections.model.dto.CollectionsLocationDto
import no.nb.bikube.catalogue.collections.model.dto.createContainerDto
import no.nb.bikube.catalogue.collections.model.getFirstObject
import no.nb.bikube.catalogue.collections.repository.CollectionsRepository
import org.springframework.stereotype.Service
import reactor.core.publisher.Mono
import reactor.core.publisher.SynchronousSink

@Service
class CollectionsLocationService (
Expand All @@ -22,8 +20,8 @@ class CollectionsLocationService (
): Mono<CollectionsLocationObject> {
return collectionsRepository.searchLocationAndContainers(barcode)
.flatMap {
if (it.adlibJson?.recordList?.isNotEmpty() == true) {
Mono.just(it.getFirstObject()!!)
if (it.hasObjects()) {
Mono.just(it.getFirstObject())
} else {
createLocationRecord(barcode, username)
}
Expand All @@ -37,11 +35,11 @@ class CollectionsLocationService (
val dto: CollectionsLocationDto = createContainerDto(barcode, username, null)
val encodedBody = Json.encodeToString(dto)
return collectionsRepository.createLocationRecord(encodedBody)
.handle { collectionsModel, sink: SynchronousSink<List<CollectionsLocationObject>> ->
collectionsModel.adlibJson?.recordList
?. let { sink.next(collectionsModel.adlibJson.recordList) }
?: sink.error(CollectionsItemNotFound("New container not found"))
.handle { collectionsLocationModel, sink ->
if (collectionsLocationModel.hasObjects())
sink.next(collectionsLocationModel.getFirstObject())
else
sink.error(CollectionsItemNotFound("New container not found"))
}
.map { it.first() }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ data class ItemInputDto(
val username: String,
val digital: Boolean? = false,
val urn: String? = null,
var name: String? = null,
val name: String? = null,
val containerId: String? = null
)
Loading