Refresh Your Java - Before Java Interview
We are Agile, believe in less Documentation - Only Quick notes (Java Interview Questions) of Java/J2ee Read more....
This following examples guide us how to
use FreeMarker template for sending
an email in real
time Applications.
In this example we take a scenorio in which a Sales Rep sending an email to customer after placing the Order.
import
java.io.File;
import java.io.IOException;
import freemarker.template.Configuration;
import
freemarker.template.Template;
public
class FreeMarkerConfig {
private static FreeMarkerConfig
freeMarkerConfig = null;
private static Configuration cfg = null;
private
FreeMarkerConfig(ServletContext context){
cfg = new Configuration();
cfg.setTemplateLoader(TEMPLATE_PATH,
context.getRealPath("/templates/SalesOrderEmailTemplate")));
}
public static FreeMarkerConfig
getInstance(){
if(freeMarkerConfig == null){
freeMarkerConfig = new FreeMarkerConfig(null);
return freeMarkerConfig;
}
return freeMarkerConfig;
}
public static Template
getTemplateByName(String fileName){
try{
return cfg.getTemplate(fileName);
}catch(IOException e){
throw new RuntimeException("Error in loading ftl
template: "+e.getMessage());
}
}
}
---Template Invoker - which fills the data in Email
template
public class EmailTemplateReader ()
{
public static String getMailBodyBySalesOrderEvent(Opportunity opp,
WebUser webuser) throws Exception{
Template template =
FreeMarkerConfig.getTemplateByName("SalesOrder.ftl");
Contact contact =
LeadServices.getPrimaryContact(opp.getObjid());
Site site = LeadServices.getPrimarySite(opp.getObjid());
Address address = site.getAddressByCustPrimaddr2address();
/* Create a data model */
Map root = new
HashMap();
root.put("contact",
getContactData(orderId));
root.put("webuser", getUserData(orderId));
StringWriter strOut = new
StringWriter();
/* Merge data model with template
*/
return getProcessedTemplate(root, template);
}
public static String
getProcessedTemplate(Map root, Template template){
try{
StringWriter textWriter = new StringWriter();
template.process(root,
textWriter);
return textWriter.toString();
}catch(Exception e){
throw new RuntimeException("Exception in processing
template:"+e.getMessage());
}
}
}
SalesOrder.ftl
(Template) :
${contact},
Thank you for contacting me about Java Book. Per
our discussion I have attached
a sales order for
${contact.address1} ${contact.address2} ${contact.city}
${contact.state} ${contact.zip}.
Please sign where
indicated on the last page and fax the entire sales order to me at
${faxNumber}.
Once I have the signed sales order, I will
place your order and get your stuff shipped.
Thank you for choosing YYYYOrg.
Regards,
${user.userFirstName} ${user.userLastName}
${user.userOffice}
${user.userPhone}
${user.userEmail}
This Portal is intended to put all Java/J2ee related topics at one single place for quick referance, not only Technical , but also the Project Management Related thing such as Development Process methodoogies build process, unit testing etc.,
This Portal has More than 500 Java Interview Questions (also could be Considered as Quick Notes) very neatly separated topic by topic with simple diagrams which makes you easily understandable. Importantly these are from our Realtime expericance.