Archive for August 2013

Apache Velocity Template to generate CSV (or any other format) file

This post is simple one to generate CSV file from some Source using Velocity Template and to verify the new design for the Blog (Metro Blue). The code (just a running stuff and nothing fancy) provided is simple and doesn't need any explanation and is dedicated to a dear friend Sachin Shah.






Java Class
package com.gognamunish.test;

import org.apache.velocity.app.Velocity;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;

import java.io.File;
import java.io.FileWriter;
import java.io.StringWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CSVFeedGeneration {

  static DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss");

  static DataFeed dataFeed;
 static {
  // prepare some static data
  dataFeed = new DataFeed();
  List<Map<String, Object>> dataList = new ArrayList<Map<String, Object>>();
  Map<String, Object> data = new HashMap<String, Object>();
  data.put("TransactionId", 1);
  data.put("Amount", new Integer(2312));
  data.put("Currency", "SGD");
  data.put("ExecutionDate", dateFormat.format(new Date()));
  dataList.add(data);
  data = new HashMap<String, Object>();
  data.put("TransactionId", 2);
  data.put("Amount", new Integer(1212));
  data.put("Currency", "INR");
  data.put("ExecutionDate", dateFormat.format(new Date()));
  data.put("Status", "Active");
  dataList.add(data);

   dataFeed.getFeedData().add(dataList);

  }

  public static void main(String[] args) throws Exception {
  VelocityContext context = new VelocityContext();
  FileWriter writer = new FileWriter(new File("feed.csv"));
  
  // inject the objects to be used in template
  context.put("feed", dataFeed);
  context.put("DELIMETER", ",");
  context.put("NEWLINE", "\n");

  Velocity.init();
  Template template = Velocity.getTemplate("csvfeed.vm");
  template.merge(context, writer);
  writer.flush();
  writer.close();

  }

}
Template File (csvfeed.vm)
TransactionId${DELIMETER}Amount${DELIMETER}ExecutionDate ${DELIMETER}Currency${DELIMETER}Status$NEWLINE
#foreach( $rowSet in $feed.feedData)
#foreach( $row in $rowSet )
$!row.get("TransactionId")$DELIMETER$!row.get("Amount") $DELIMETER$!row.get("ExecutionDate")$DELIMETER $!row.get("Currency")$DELIMETER$!row.get("Status")$NEWLINE
#end
#end
Output File (feed.csv)
TransactionId,Amount,ExecutionDate,Currency,Status
1,2312,2013-08-18T14:29:35,SGD,
2,1212,2013-08-18T14:29:35,INR,Active
The main advantage of this approach is that you can change the data format any time without changing or touching any Java code.
POM Dependency
<dependency>
   <groupId>org.apache.velocity</groupId>
   <artifactId>velocity</artifactId>
   <version>1.7</version>
  </dependency>
In case you are interested in exploring more, reach out here http://velocity.apache.org/engine/devel/index.html
Sunday, August 18, 2013
Posted by Munish Gogna

Popular Post

Labels

enums (1) java (2) JAX-RS (1) JPA (1) mysql (1) request 2 (1) RESTful (1) sphinx (1) tomcat (1) web service (2) ws (2)