In a payment application a day before and at the same time with some payments some messages(email) needs to be sent.
I have a DTO (called EscrowPayment
, projected from some entity) from which I generate and send two similar `messages.
I look for advice specifically on the following issues with the code I ended up:
- Copy-pasted code.
- Type suffix in identifiers. eg:
nominalAmountStr
- Static utility methods
formatInteger
etc (Used application wide). - String constants in code. But because they are parameterized by position and not by name; if I extract them to a configuration file as they are,
I wouldn't know in which order I should be giving parameters to
String.format
.
Here is the code; it is sanitized and translated, but issues are clearly identifiable:
public class EscrowPaymentMessageServiceImpl {
private JavaMailSender mailSender;
// .....
@Override
public void escrowPaymentWillBeDone(EscrowPayment escrowPayment) throws BusinessLayerException {
try {
String nominalAmountStr = formatInteger(new BigDecimal(escrowPayment.getNominal()));
String paymentDateStr = dateFormat(escrowPayment.getPaymentDate(), "dd/MM/yyyy");
String interestRateStr = format(escrowPayment.getInterestRate(), "0.00###");
String paymentAmountStr = format(escrowPayment.getPaymentAmount());
String messageText = String
.format("BLAH BLAH some security with ID %s of nominal value EUR %s "
+ "at date %s BLAH your account with number 1234567890 will be debited to pay "
+ "EUR %s coupon payment for %%%s interest.",
escrowPayment.getSecurityId(), nominalStr, paymentDateStr,
interestRateStr, paymentAmountStr);
sendMessage(messageText);
} catch (MessagingException e) {
getLog().error("An error occured while sending the email:", e);
throw new BusinessLayerException("EscrowPaymentMessage.MessageCouldNotBeSent", e);
}
}
@Override
public void escrowPaymentIsDone(EscrowPayment escrowPayment) throws BusinessLayerException {
try {
String nominalAmountStr = formatInteger(new BigDecimal(escrowPayment.getNominal()));
String paymentDateStr = dateFormat(escrowPayment.getPaymentDate(), "dd/MM/yyyy");
String paymentAmountStr = format(escrowPayment.getPaymentAmount());
String messageText = String.format("BLAH BLAH at date %s the security with ID %s of nominal value EUR %s "
+ "BLAH your account with number 1234567890 has been debited to pay EUR %s." ,
paymentDateStr, escrowPayment.getSecurityId(), nominalStr, paymentAmountStr);
sendMessage(messageText);
} catch (MessagingException e) {
getLog().error("An error occured while sending the email:", e);
throw new BusinessLayerException("EscrowPaymentMessage.MessageCouldNotBeSent", e);
}
}
private void sendMessage(messageText) throws MessagingException {
//....
}
}