I am trying to replace parts of String starting with $ (I manually use parameters in String) but for some reason it fails. Here's what I have:
public void sendNotificationOfAcceptedMeeting(String candidateName, DateTime dateTime) {
Map<String,String> params = new HashMap<String,String>();
params.put("date", dateTime.toString(DATETIME_FORMAT));
params.put("candidate", candidateName);
sendEmailWithTemplate(TEMPLATE_ALL_ACCEPTED, params);
}
then:
private void sendEmailWithTemplate(String templateName, Map<String, String> params) {
EmailTemplate template = emailTemplateDao.getEmailTemplate(templateName);
String subject = applyTemplateParameters(template.getSubject(), params);
String body = applyTemplateParameters(template.getTetxBody(), params);
sendEmail(subject, body);
}
and finally:
private String applyTemplateParameters(String templateText, Map<String, String> params) {
String toReturn = templateText;
if (params != null) {
System.out.println("map size: " + params.size());
for (Entry<String, String> entry : params.entrySet()) {
System.out.println("********************");
System.out.println(toReturn + " ***BEFORE");
toReturn.replace("$" + entry.getKey(), entry.getValue());
System.out.println("$" + entry.getKey() + " ***KEY");
System.out.println(entry.getValue() + " ***VALUE");
System.out.println(toReturn + " AFTER");
}
}
return toReturn;
}
As you probably figured, the second method calls the third method with String that I want to modify and map of parameters and their values. Second method sets the vlue of template String to:
The meeting request at $date with $candidate was accepted by all interviewers.
However the sysouts in third method print:
map size: 2
********************
The meeting request at $date with $candidate was
accepted by all interviewers. ***BEFORE
$candidate ***KEY
Acceeedsasd Stetfghfghasd ***VALUE
The meeting request at $date with $candidate was
accepted by all interviewers. AFTER
********************
The meeting request at $date with $candidate was
accepted by all interviewers. ***BEFORE
$date ***KEY
27/12/2014 8:00 ***VALUE
The meeting request at $date with $candidate was
accepted by all interviewers. AFTER
which clearly shows that it doesn't replace parameters... I thought maybe $ breaks it (regex) but replace(), unlike replaceAll() takes String - not regex. I'm out of ideas, any help appreciated.