Pages

Friday, 4 October 2013

Example of mail merge generating multiple files.

Example of mail merge (Dependency Example) generating different files using RTFTemplate library.


  • Add required JARS in class path.
  •  Required JARS:  
       
     rtftemplate-1.0.1-b14.jar  
     commons-collections-3.2.1.jar  
     spring-beans-3.2.2.RELEASE.jar  
     spring-context-3.2.2.RELEASE.jar  
     spring-context-support-3.2.2.RELEASE.jar  
     spring-core-3.2.2.RELEASE.jar  
     spring-expression-3.2.2.RELEASE.jar  
     spring-jdbc-3.2.2.RELEASE.jar  
     spring-orm-3.2.2.RELEASE.jar  
     spring-tx-3.2.2.RELEASE.jar  
     xml-apis-1.0.b2.jar  
     commons-logging-1.1.1.jar  
     freemarker.jar  
     commons-digester-2.1.jar  
     org.apache.commons.beanutils.jar  
     velocity-1.7.jar  
     commons-lang.jar  
    
package com.rtf.impl;
public class Dependency {
private String artifactID;
private String type;
private String version;
private String url;
public Dependency(String artifactID, String type, String version, String url) {
this.artifactID = artifactID;
this.type = type;
this.version = version;
this.url = url;
}
public String getArtifactID() {
return artifactID;
}
public void setArtifactID(String artifactID) {
this.artifactID = artifactID;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getVersion() {
return version;
}
public void setVersion(String version) {
this.version = version;
}
}
view raw Dependency hosted with ❤ by GitHub
package com.rtf.impl;
import java.util.ArrayList;
import java.util.Collection;
public class Developer {
private String name;
private String email;
private Collection<Role> roles;
private Manager manager;
public Developer(String name, String email) {
this.name = name;
this.email = email;
roles = new ArrayList<Role>();
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Collection<Role> getRoles() {
return roles;
}
public void setRoles(Collection<Role> roles) {
this.roles = roles;
}
public void addRole(Role role) {
roles.add(role);
}
public void addRole(String roleName) {
addRole(new Role(roleName));
}
public Manager getManager() {
return manager;
}
public void setManager(Manager manager) {
this.manager = manager;
}
}
view raw Developer hosted with ❤ by GitHub
package com.rtf.impl;
public class DeveloperGroup {
private Developer developer1;
private Developer developer2;
private Developer developer3;
public DeveloperGroup(Developer developer1, Developer developer2,
Developer developer3) {
this.developer1 = developer1;
this.developer2 = developer2;
this.developer3 = developer3;
}
public Developer getDeveloper1() {
return developer1;
}
public void setDeveloper1(Developer developer1) {
this.developer1 = developer1;
}
public Developer getDeveloper2() {
return developer2;
}
public void setDeveloper2(Developer developer2) {
this.developer2 = developer2;
}
public Developer getDeveloper3() {
return developer3;
}
public void setDeveloper3(Developer developer3) {
this.developer3 = developer3;
}
}
view raw DeveloperGroup hosted with ❤ by GitHub
package com.rtf.impl;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
/**
* This class is to generate a multiple file with there different addresses
* using RTF Template.
*
* @author Suraj Bhambhani
*
*/
public class GenerateMultipleRTFFiles {
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
// Source of file to be generated.
String rtfSource = "src/RTF_Websites.rtf";
// 1. Get default RTFtemplateBuilder
RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();
// 2. Get RTFtemplate with default Implementation of template engine
RTFTemplate rtfTemplate = builder.newRTFTemplate();
// 3. Set the RTF model source
rtfTemplate.setTemplate(new File(rtfSource));
// List of Projects
List<Project> projects = new ArrayList<Project>();
// Project with one website, website2 exists.
Project project1 = new Project("Be Open Source", "Suraj Bhambhani",
"OutOfMemory", "", "www.scart.com");
// Project with two website.
Project project2 = new Project("Be Open Source", "Suraj Bhambhani",
"OutOfMemory", "www.liveinSF.com", "www.liveinNJ.com");
// Project with one website, website1 exists.
Project project3 = new Project("Be Open Source", "Suraj Bhambhani",
"OutOfMemory", "www.workwithme.com", "");
// Project with one website, website1 exists.
Project project4 = new Project("Be Open Source", "Suraj Bhambhani",
"OutOfMemory", "www.workinSF.com", "www.workinNJ.com");
projects.add(project1);
projects.add(project2);
projects.add(project3);
projects.add(project4);
rtfTemplate.put("header_developer_name", "Name");
rtfTemplate.put("header_developer_email", "Email");
rtfTemplate.put("header_developer_roles", "Roles");
// Dependencies
List<Dependency> dependencies = new ArrayList<Dependency>();
Dependency dependency = new Dependency("commons-collection", "jar",
"1.0", "http://jakarta.apache.org/commons/collection/");
dependencies.add(dependency);
rtfTemplate.put("dependencies", dependencies);
// List of developers.
List<Developer> developers = new ArrayList<Developer>();
Developer developer;
developer = new Developer("Will Glass-Husain", "wglass@apache.org");
developer.addRole("Java Developer");
developer.addRole("Manager");
rtfTemplate.put("developers", developers);
developers.add(developer);
developer = new Developer("Ma-Husain", "wglass@apache.org");
developer.addRole("Java Developer");
developer.addRole("Manager");
developers.add(developer);
// Iterating over projects and generating files accordingly.
for (Project projectIterate : projects) {
// When both wesite exists.
if (!projectIterate.getWebsite1().equals("")
&& !projectIterate.getWebsite2().equals("")) {
withTwoWebsites(rtfTemplate, projectIterate);
withTwoWebsites(rtfTemplate, projectIterate);
}
// When Website2 exists and website1 not.
if (projectIterate.getWebsite1().equals("")
&& !projectIterate.getWebsite2().equals("")) {
withWebsite2(rtfTemplate, projectIterate);
}
// When Website1 exists and website2 not.
if (!projectIterate.getWebsite1().equals("")
&& projectIterate.getWebsite2().equals("")) {
withWebsite1(rtfTemplate, projectIterate);
}
System.out.println(projectIterate.getWebsite1());
System.out.println(projectIterate.getWebsite2());
}
}
private static void withWebsite1(RTFTemplate rtfTemplate,
Project projectIterate) throws Exception {
String name;
name = projectIterate.getWebsite1();
generateFile(rtfTemplate, projectIterate, name);
}
private static void withWebsite2(RTFTemplate rtfTemplate,
Project projectIterate) throws Exception {
String name;
projectIterate.setWebsite1(projectIterate.getWebsite2());
name = projectIterate.getWebsite2();
generateFile(rtfTemplate, projectIterate, name);
}
private static void generateFile(RTFTemplate rtfTemplate,
Project projectIterate, String name) throws Exception {
// 4. Put the context.
rtfTemplate.put("project", projectIterate);
String rtfTarget = "Project_" + name + ".rtf";
// 5. Merge the RTF source model and the context
rtfTemplate.merge(rtfTarget);
}
private static void withTwoWebsites(RTFTemplate rtfTemplate,
Project projectIterate) throws Exception {
String name;
boolean websiteCheck = true;
int websitBreak = 0;
// When two website exist.
while (true) {
name = projectIterate.getWebsite1();
if (websiteCheck == false) {
projectIterate.setWebsite1(projectIterate.getWebsite2());
name = projectIterate.getWebsite2();
}
generateFile(rtfTemplate, projectIterate, name);
websitBreak++;
websiteCheck = false;
if (websitBreak == 2)
break;
}
}
}
package com.rtf.impl;
import java.util.ArrayList;
import java.util.List;
public class Manager {
private List<Project> projects;
public Manager(String name) {
this.name = name;
this.projects = new ArrayList<Project>();
}
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void addProject(Project project) {
projects.add(project);
}
public List<Project> getProjects() {
return projects;
}
}
view raw Manager hosted with ❤ by GitHub
package com.rtf.impl;
import java.io.InputStream;
import java.util.List;
public class Project {
private String name;
private InputStream logo = null;
private String clientName;
private String error;
public String getError() {
return error;
}
public void setError(String error) {
this.error = error;
}
private String website1;
private String website2;
public String getWebsite2() {
return website2;
}
public void setWebsite2(String website1) {
this.website2 = website1;
}
public String getWebsite1() {
return website1;
}
public void setWebsite1(String website) {
this.website1 = website;
}
public String getClientName() {
return clientName;
}
public void setClientName(String clientName) {
this.clientName = clientName;
}
public Project(String name, String clientName, String error,
String website, String website1) {
this.name = name;
this.clientName = clientName;
this.error = error;
this.website1 = website;
this.website2 = website1;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setLogo(InputStream logo) {
this.logo = logo;
}
public InputStream getLogo() {
return logo;
}
private List<Project> group;
public List<Project> getGroup() {
return group;
}
public void addProject(Project project) {
group.add(project);
}
}
view raw Project hosted with ❤ by GitHub
package com.rtf.impl;
public class Role {
private String name;
public Role(String name) {
this.name = name;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
view raw Role hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- **********************************************************************
********************* RTFTEMPLTE IMPLEMENTATION *********************
********************************************************************** -->
<!-- Defautlt RTFTemplate implementation with freemarker template engine -->
<bean id="ftlRTFTemplate"
class="net.sourceforge.rtf.RTFTemplate"
singleton="false" >
<property name="parser" ref="defaultRTFParser" />
<property name="transformer" ref="ftlTransformer" />
<property name="templateEngine" ref="ftl" />
</bean>
<!-- Defautlt RTFTemplate implementation with velocity template engine -->
<bean id="vmRTFTemplate"
class="net.sourceforge.rtf.RTFTemplate"
singleton="false" >
<property name="parser" ref="defaultRTFParser" />
<property name="transformer" ref="vmTransformer" />
<property name="templateEngine" ref="vm" />
</bean>
<!-- **********************************************************************
********************* RTFDOCUMENT PARSER *********************
********************************************************************** -->
<!-- Defautlt RTFDocument Parser -->
<bean id="defaultRTFParser"
class="net.sourceforge.rtf.handler.RTFDocumentHandler"
singleton="true" >
</bean>
<!-- **********************************************************************
********************* FREEMARKER TEMPLATE ENGINE *********************
********************************************************************** -->
<!-- Freemarker template engine -->
<bean id="ftl"
class="net.sourceforge.rtf.template.freemarker.FreemarkerTemplateEngineImpl"
singleton="false" >
<property name="freemarkerConfiguration" ref="ftlConfiguration" />
</bean>
<!-- Freemarker Configuration -->
<bean id="ftlConfiguration"
class="freemarker.template.Configuration"
singleton="true" >
</bean>
<!-- Freemarker RTF Document Transformer -->
<bean id="ftlTransformer"
class="net.sourceforge.rtf.template.freemarker.RTFFreemarkerTransformerImpl"
singleton="true" >
</bean>
<!-- **********************************************************************
********************* VELOCITY TEMPLATE ENGINE *********************
********************************************************************** -->
<bean id="vm"
class="net.sourceforge.rtf.template.velocity.VelocityTemplateEngineImpl"
singleton="false" >
<property name="velocityEngine" ref="vmEngine" />
</bean>
<!-- VelocityEngine Configuration -->
<bean id="vmEngine"
class="org.apache.velocity.app.VelocityEngine"
singleton="true" >
</bean>
<!-- Velocity RTF Document Transformer -->
<bean id="vmTransformer"
class="net.sourceforge.rtf.template.velocity.RTFVelocityTransformerImpl"
singleton="true" >
</bean>
</beans>


References:-
http://sourceforge.net/projects/rtftemplate/

http://rtftemplate.sourceforge.net/

Thursday, 3 October 2013

Example of mail Merging

Example of mail Merging (Address Example) using RTFTemplate library.


  • Add required JARS in class path.
  •  Required JAR:  
       
     commons-collections-3.2.1.jar  
     commons-digester-2.1.jar  
     commons-lang.jar  
     commons-logging-1.1.1.jar  
     freemarker.jar  
     org.apache.commons.beanutils.jar  
     rtftemplate-1.0.1-b14.jar  
     spring-beans-3.2.2.RELEASE.jar  
     spring-context-3.2.2.RELEASE.jar  
     spring-context-support-3.2.2.RELEASE.jar  
     spring-core-3.2.2.RELEASE.jar  
     spring-expression-3.2.2.RELEASE.jar  
     spring-tx-3.2.2.RELEASE.jar  
     velocity-1.7.jar  
     xml-apis-1.0.b2.jar  
    
  • Download the RTF sample and Template file in rtf format.
  • Add the required classes.

package com.sb.rtf;
public class Address {
private String typeOfAddress;
private String address;
private int pinCode;
public int getPinCode() {
return pinCode;
}
public void setPinCode(int pinCode) {
this.pinCode = pinCode;
}
public String getTypeOfAddress() {
return typeOfAddress;
}
public void setTypeOfAddress(String typeOfAddress) {
this.typeOfAddress = typeOfAddress;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public Address(String typeOfAddress, String address, int i) {
super();
this.typeOfAddress = typeOfAddress;
this.address = address;
this.pinCode = i;
}
}
view raw Address hosted with ❤ by GitHub
package com.sb.rtf;
import java.util.List;
public class ContractPartner {
private final String businessPartnerName;
private final List<Address> businessPartnerAddreses;
private final String businessPartnerWebsite;
private final List<Partner> partners;
private final List<Role> roles;
private final String contractWith;
public String getBusinessPartnerName() {
return businessPartnerName;
}
public List<Address> getBusinessPartnerAddreses() {
return businessPartnerAddreses;
}
public String getBusinessPartnerWebsite() {
return businessPartnerWebsite;
}
public ContractPartner(String businessPartnerName,
List<Address> businessPartnerAddreses,
String businessPartnerWebsite, List<Partner> partners,
String contractWith,List<Role> roles) {
super();
this.businessPartnerName = businessPartnerName;
this.businessPartnerAddreses = businessPartnerAddreses;
this.businessPartnerWebsite = businessPartnerWebsite;
this.partners = partners;
this.contractWith = contractWith;
this.roles = roles;
}
public List<Partner> getPartners() {
return partners;
}
public String getContractWith() {
return contractWith;
}
public List<Role> getRoles() {
return roles;
}
}
view raw ContractPartner hosted with ❤ by GitHub
package com.sb.rtf;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class ContractPartnerProvider {
@SuppressWarnings("unchecked")
public static List<ContractPartner> getBusinessPartner()
throws SQLException {
List<ContractPartner> contractPartner = new ArrayList<ContractPartner>();
List<Address> adresses = new ArrayList<Address>();
Address address = new Address("Work",
"540, Kalbadevi Rd, Wagh wadi, Kalbadevi Mumbai 022 6622 5454",
400096);
adresses.add(address);
address = new Address("Home",
"640 , Wagh wadi, Kalbadevi Mumbai 022 6622 5454", 401090);
adresses.add(address);
List<Role> roles =new ArrayList<Role>();
roles.add(new Role("Editor"));
roles.add(new Role("writer"));
roles.add(new Role("Author"));
ContractPartner bp = new ContractPartner("Suraj Bhambhani", adresses,
"www.sbit.com",getPartner(), "SB IT Solutions",roles);
contractPartner.add(bp);
return contractPartner;
}
public static List<Partner> getPartner() {
List<Partner> partners = new ArrayList<Partner>();
Partner partner = new Partner("Susen", "susen@gmail.com", "Instructor");
partners.add(partner);
partner = new Partner("Saush", "saushh@gmail.com", "Super Instructor");
partners.add(partner);
return partners;
}
}
package com.sb.rtf;
public class Partner {
private String partnerName;
private String partnerEmail;
private String partnerRole;
public String getPartnerName() {
return partnerName;
}
public void setPartnerName(String partnerName) {
this.partnerName = partnerName;
}
public String getPartnerEmail() {
return partnerEmail;
}
public void setPartnerEmail(String partnerEmail) {
this.partnerEmail = partnerEmail;
}
public String getPartnerRole() {
return partnerRole;
}
public void setPartnerRole(String partnerRole) {
this.partnerRole = partnerRole;
}
public Partner(String partnerName, String partnerEmail, String partnerRole) {
super();
this.partnerName = partnerName;
this.partnerEmail = partnerEmail;
this.partnerRole = partnerRole;
}
}
view raw Partner hosted with ❤ by GitHub
package com.sb.rtf;
public class Role {
private final String role;
public Role(String role) {
this.role = role;
}
public String getRole() {
return role;
}
}
view raw Role hosted with ❤ by GitHub
package com.sb.rtf;
import java.io.File;
import java.io.FileNotFoundException;
import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.UnsupportedRTFTemplate;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
public class RTFHandler {
public static String getRTFTemplateSource(String rtfSource) {
return rtfSource;
}
public static RTFTemplate getRTFTemplate(String rtfSource)
throws UnsupportedRTFTemplate, FileNotFoundException {
// 1. Get default RTFtemplateBuilder
RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();
// 2. Get RTFtemplate with default Implementation of template engine
RTFTemplate rtfTemplate = builder.newRTFTemplate();
// 3. Set the RTF model source
rtfTemplate.setTemplate(new File(rtfSource));
return rtfTemplate;
}
public static void putList(RTFTemplate rtfTemplate,String source,Object object) {
rtfTemplate.put(source,object );
}
public static void putData(RTFTemplate rtfTemplate,String source,String destination) {
rtfTemplate.put(source, destination);
}
public static void generateFiles(RTFTemplate rtfTemplate,
String name) throws Exception {
// 4. Put the context.
String rtfTarget = "Contract_" + name + ".rtf";
// 5. Merge the RTF source model and the context
rtfTemplate.merge(rtfTarget);
}
public static void generateFile(RTFTemplate rtfTemplate, String string) throws Exception {
String rtfTarget = "Project_.rtf";
// 5. Merge the RTF source model and the context
rtfTemplate.merge(rtfTarget);
}
}
view raw RTFHandler hosted with ❤ by GitHub
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<!-- **********************************************************************
********************* RTFTEMPLTE IMPLEMENTATION *********************
********************************************************************** -->
<!-- Defautlt RTFTemplate implementation with freemarker template engine -->
<bean id="ftlRTFTemplate"
class="net.sourceforge.rtf.RTFTemplate"
singleton="false" >
<property name="parser" ref="defaultRTFParser" />
<property name="transformer" ref="ftlTransformer" />
<property name="templateEngine" ref="ftl" />
</bean>
<!-- Defautlt RTFTemplate implementation with velocity template engine -->
<bean id="vmRTFTemplate"
class="net.sourceforge.rtf.RTFTemplate"
singleton="false" >
<property name="parser" ref="defaultRTFParser" />
<property name="transformer" ref="vmTransformer" />
<property name="templateEngine" ref="vm" />
</bean>
<!-- **********************************************************************
********************* RTFDOCUMENT PARSER *********************
********************************************************************** -->
<!-- Defautlt RTFDocument Parser -->
<bean id="defaultRTFParser"
class="net.sourceforge.rtf.handler.RTFDocumentHandler"
singleton="true" >
</bean>
<!-- **********************************************************************
********************* FREEMARKER TEMPLATE ENGINE *********************
********************************************************************** -->
<!-- Freemarker template engine -->
<bean id="ftl"
class="net.sourceforge.rtf.template.freemarker.FreemarkerTemplateEngineImpl"
singleton="false" >
<property name="freemarkerConfiguration" ref="ftlConfiguration" />
</bean>
<!-- Freemarker Configuration -->
<bean id="ftlConfiguration"
class="freemarker.template.Configuration"
singleton="true" >
</bean>
<!-- Freemarker RTF Document Transformer -->
<bean id="ftlTransformer"
class="net.sourceforge.rtf.template.freemarker.RTFFreemarkerTransformerImpl"
singleton="true" >
</bean>
<!-- **********************************************************************
********************* VELOCITY TEMPLATE ENGINE *********************
********************************************************************** -->
<bean id="vm"
class="net.sourceforge.rtf.template.velocity.VelocityTemplateEngineImpl"
singleton="false" >
<property name="velocityEngine" ref="vmEngine" />
</bean>
<!-- VelocityEngine Configuration -->
<bean id="vmEngine"
class="org.apache.velocity.app.VelocityEngine"
singleton="true" >
</bean>
<!-- Velocity RTF Document Transformer -->
<bean id="vmTransformer"
class="net.sourceforge.rtf.template.velocity.RTFVelocityTransformerImpl"
singleton="true" >
</bean>
</beans>
package com.sb.rtf;
import java.util.List;
import net.sourceforge.rtf.RTFTemplate;
/**
* This class is to generate a sample contract using RTF Template.
* @author Suraj Bhambhani
*
*/
public class SampleContractGenerator {
public static void main(String[] args) throws Exception {
// Source of file to be generated.
String rtfSource = "ContractTemplate.rtf";
// Generate template.
RTFTemplate rtfTemplate = RTFHandler.getRTFTemplate(rtfSource);
// Business Partner
List<ContractPartner> contractPartners = ContractPartnerProvider
.getBusinessPartner();
// Generate file.
generateFile(rtfTemplate, contractPartners);
// Desktop.getDesktop().open(new File("Contract_Example.rtf"));
}
private static void generateFile(RTFTemplate rtfTemplate,
List<ContractPartner> contractPartners) throws Exception {
RTFHandler.putList(rtfTemplate, "businessPartner",
(ContractPartner) contractPartners.get(0));
RTFHandler.generateFiles(rtfTemplate, "SB IT Solution");
}
}
References:-
http://sourceforge.net/projects/rtftemplate/

http://rtftemplate.sourceforge.net/