-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fail.kt
67 lines (54 loc) · 2.53 KB
/
Fail.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package no.nav.jantra.river.model
import com.fasterxml.jackson.databind.JsonNode
import no.nav.helse.rapids_rivers.JsonMessage
import no.nav.helse.rapids_rivers.River
import no.nav.helse.rapids_rivers.isMissingOrNull
import no.nav.jantra.river.*
import no.nav.jantra.river.InternalBehov
import no.nav.jantra.river.InternalEvent
class Fail internal constructor(val event: MessageType.Event,
val behov: MessageType.Behov? = null,
val feilmelding: String,
private val jsonMessage: JsonMessage) : Message, TxMessage {
init {
packetValidator.validate(jsonMessage)
jsonMessage.demandValue(Key.EVENT_NAME.str(),event.value)
}
companion object {
val packetValidator = River.PacketValidation {
it.demandKey(Key.EVENT_NAME.str())
it.rejectKey(Key.BEHOV.str())
it.rejectKey(Key.DATA.str())
it.demandKey(Key.FAIL.str())
it.interestedIn(Key.RIVER_ID.str())
it.interestedIn(Key.FAILED_BEHOV)
it.interestedIn(Key.EVENT_TIME)
}
fun create(event:MessageType.Event, behov: MessageType.Behov? = null,feilmelding:String, data: Map<IKey, Any> = emptyMap() ) : Fail {
return Fail(event, behov, feilmelding,
jsonMessage = JsonMessage.newMessage(event.value, data.mapKeys { it.key.str }).also {
if (behov!=null) it[Key.FAILED_BEHOV.str()] = behov.value
it[Key.FAIL.str()] = feilmelding
})
}
fun create(jsonMessage: JsonMessage) : Fail {
val behov = jsonMessage[Key.FAILED_BEHOV.str]
.takeUnless { it.isMissingOrNull()}
?.let {
InternalBehov(it.asText())
}
return Fail(InternalEvent(jsonMessage[Key.EVENT_NAME.str()].asText()), behov, jsonMessage[Key.FAIL.str].asText(), jsonMessage)
}
}
override operator fun get(key: IKey): JsonNode = jsonMessage[key.str]
override operator fun set(key: IKey, value: Any) { jsonMessage[key.str] = value }
fun createBehov(behov: MessageType.Behov,map: Map<IDataFelt, Any>): Behov {
return Behov(event, behov, JsonMessage.newMessage(event.value, mapOfNotNull(Key.BEHOV.str() to behov.value, Key.RIVER_ID.str() to riverId()) + map.mapKeys { it.key.str }))
}
override fun riverId(): String {
return jsonMessage[Key.RIVER_ID.str()].asText()
}
override fun toJsonMessage(): JsonMessage {
return jsonMessage
}
}