Skip to content

Instantly share code, notes, and snippets.

@steklopod
Created December 25, 2018 13:59
Show Gist options
  • Save steklopod/0d361d75d8f112d726cc381e22ffc936 to your computer and use it in GitHub Desktop.
Save steklopod/0d361d75d8f112d726cc381e22ffc936 to your computer and use it in GitHub Desktop.
Generic Kotlin Service
abstract class PaymentOrderCommon<AnyChildOfOrderEsbRequest : OrderEsbRequest> {
private val esbSystemId = "ESB"
private val okStatusValue = "ok"
private val absTimeoutErrorCode = "450"
private val absTimeoutErrorMessage = "5NT не ответил по таймауту"
private val defaultEsbTimeOutErrorCode = "timeOut"
private val defaultEsbTimeOutErrorMessage = "Таймаут при обращении в ESB"
private val defaultEsbErrorCode = "error"
private val defaultEsbErrorMessage = "Ошибка при обращении в ESB"
fun exchangeWithEsb(
esbRequest: EsbJmsRequest<AnyChildOfOrderEsbRequest, BaseRequestProperties>,
transferOperation: TransferOperation,
esbClientService: EsbClientService<AnyChildOfOrderEsbRequest, BaseRequestProperties, PaymentOrderEsbResponse>,
transferOperationService: TransferOperationService
): EsbJmsResponse<PaymentOrderEsbResponse> {
try {
val esbJmsResponse = esbClientService.exchange(esbRequest, PaymentOrderEsbResponse::class)
transferOperation.updateStatusAndHistory(TransferOperationStatusCode.PROCESSING, transferOperationService)
return esbJmsResponse
} catch (e: Exception) {
e.convertToOmniBusinessExceptionWithTimeOutCheck(transferOperation, transferOperationService)
}
}
fun TransferOperation.updateStatusAndHistory(
statusCode: TransferOperationStatusCode,
transferOperationService: TransferOperationService,
comment: String? = null
): TransferOperation {
return transferOperationService.updateStatusAndHistory(this, statusCode, comment)
}
fun EsbJmsResponse<PaymentOrderEsbResponse>.validateResult(
transferOperation: TransferOperation, transferOperationService: TransferOperationService
): EsbJmsResponse<PaymentOrderEsbResponse> {
validateEsbResult(this.properties, this.payload?.data, transferOperation, transferOperationService)
return this
}
private fun validateEsbResult(
esbResponseProperties: BaseResponseProperties,
esbResponseData: EsbResponseData?,
transferOperation: TransferOperation,
transferOperationService: TransferOperationService
) {
if (esbResponseProperties.resultStatus != okStatusValue) {
val comment = esbResponseData?.responseDescAbs ?: esbResponseProperties.resultDescr
transferOperation.updateStatusAndHistory(ERROR_DEBET, transferOperationService, comment)
checkForTimeOut(esbResponseProperties.resultCode)
val errorCode = esbResponseData?.responseCodeAbs ?: defaultEsbErrorCode
val errorMessage = esbResponseData?.responseDescAbs ?: defaultEsbErrorMessage
throw OmniBusinessException(systemId = esbSystemId, code = errorCode, message = errorMessage)
}
}
private fun checkForTimeOut(resultCode: String) {
if (resultCode == absTimeoutErrorCode) throw OmniBusinessException(
systemId = esbSystemId, code = defaultEsbTimeOutErrorCode, message = absTimeoutErrorMessage
)
}
private fun Exception.convertToOmniBusinessExceptionWithTimeOutCheck(
transferOperation: TransferOperation, transferOperationService: TransferOperationService
): Nothing {
var exceptionMessage = defaultEsbErrorMessage
when (this) {
is EsbClientTimeoutException -> exceptionMessage = defaultEsbTimeOutErrorMessage
}
transferOperation.updateStatusAndHistory(ERROR_DEBET, transferOperationService)
throw OmniBusinessException(systemId = esbSystemId, code = defaultEsbErrorCode, message = exceptionMessage)
}
}
////////////
@Service
class PaymentOrderService(
private val paymentOrderEsbConverter: CreatePaymentOrderEsbConverter,
private val transferOperationService: TransferOperationService,
private val esbClientService: EsbClientService<PaymentOrderEsbRequest, BaseRequestProperties, PaymentOrderEsbResponse>
) : PaymentOrder,
PaymentOrderCommon<PaymentOrderEsbRequest>() {
override fun processPaymentOrderAndGetPayload(paymentOrderContext: PaymentOrderContext, systemInfo: SystemInfo)
: PaymentOrderEsbResponse {
val transferOperation = transferOperationService.getTransferOperation(systemInfo)
.updateStatusAndHistory(ACCEPTED, transferOperationService)
val esbRequest: EsbJmsRequest<PaymentOrderEsbRequest, BaseRequestProperties> = paymentOrderEsbConverter
.convertToPaymentOrderEsbRequest(paymentOrderContext, transferOperation, paymentOrderContext.actionType)
return exchangeWithEsb(esbRequest, transferOperation, esbClientService, transferOperationService)
.validateResult(transferOperation, transferOperationService)
.payload!!
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment