Source:
<code>static PreTransactionalOperation<TransactionContext, ChargeEntity> prepareForTerminate(ChargeDao chargeDao, ChargeEventDao chargeEventDao, String chargeId, StatusFlow statusFlow ) { return context -> chargeDao.findByExternalId(chargeId).map(chargeEntity -> { ChargeStatus newStatus = statusFlow.getLockState(); if (!chargeIsInTerminatableStatus(statusFlow, chargeEntity)) { <START> if (newStatus.equals(ChargeStatus.fromString(chargeEntity.getStatus()))) { <END> throw new OperationAlreadyInProgressRuntimeException(statusFlow.getName(), chargeId); } else if (Arrays.asList(AUTHORISATION_READY, AUTHORISATION_3DS_READY).contains(ChargeStatus.fromString(chargeEntity.getStatus()))) { throw new ConflictRuntimeException(chargeEntity.getExternalId()); } logger.error("Charge is not in one of the legal states. charge_external_id={}, status={}, legal_states={}", chargeId, chargeEntity.getStatus(), getLegalStatusNames(statusFlow.getTerminatableStatuses())); throw new IllegalStateRuntimeException(chargeId); } chargeEntity.setStatus(newStatus); GatewayAccountEntity gatewayAccount = chargeEntity.getGatewayAccount(); logger.info("Card cancel request sent - charge_external_id={}, charge_status={}, account_id={}, transaction_id={}, amount={}, operation_type={}, provider={}, provider_type={}, locking_status={}", chargeEntity.getExternalId(), fromString(chargeEntity.getStatus()), gatewayAccount.getId(), chargeEntity.getGatewayTransactionId(), chargeEntity.getAmount(), OperationType.CANCELLATION.getValue(), gatewayAccount.getGatewayName(), gatewayAccount.getType(), newStatus); chargeEventDao.persistChargeEventOf(chargeEntity, Optional.empty()); return chargeEntity; }).orElseThrow(() -> new ChargeNotFoundRuntimeException(chargeId)); }</code><technical_language>i code cleaner if ChargeStatus.fromString(chargeEntity.getStatus()) extracted. chargeIsInTerminatableStatus in ChargeStatus instead of pass in chargeEntity</technical_language>

Target:
static PreTransactionalOperation<TransactionContext, ChargeEntity> prepareForTerminate(ChargeDao chargeDao, ChargeEventDao chargeEventDao, String chargeId, StatusFlow statusFlow ) { return context -> chargeDao.findByExternalId(chargeId).map(chargeEntity -> { ChargeStatus newStatus = statusFlow.getLockState(); final ChargeStatus chargeStatus = ChargeStatus.fromString(chargeEntity.getStatus()); if (!chargeIsInTerminatableStatus(statusFlow, chargeStatus)) { if (newStatus.equals(chargeStatus)) { throw new OperationAlreadyInProgressRuntimeException(statusFlow.getName(), chargeId); } else if (Arrays.asList(AUTHORISATION_READY, AUTHORISATION_3DS_READY).contains(chargeStatus)) { throw new ConflictRuntimeException(chargeEntity.getExternalId()); } logger.error("Charge is not in one of the legal states. charge_external_id={}, status={}, legal_states={}", chargeId, chargeEntity.getStatus(), getLegalStatusNames(statusFlow.getTerminatableStatuses())); throw new IllegalStateRuntimeException(chargeId); } chargeEntity.setStatus(newStatus); GatewayAccountEntity gatewayAccount = chargeEntity.getGatewayAccount(); logger.info("Card cancel request sent - charge_external_id={}, charge_status={}, account_id={}, transaction_id={}, amount={}, operation_type={}, provider={}, provider_type={}, locking_status={}", chargeEntity.getExternalId(), chargeStatus, gatewayAccount.getId(), chargeEntity.getGatewayTransactionId(), chargeEntity.getAmount(), OperationType.CANCELLATION.getValue(), gatewayAccount.getGatewayName(), gatewayAccount.getType(), newStatus); chargeEventDao.persistChargeEventOf(chargeEntity, Optional.empty()); return chargeEntity; }).orElseThrow(() -> new ChargeNotFoundRuntimeException(chargeId)); }