Friday, February 24, 2012

How to add your sitemap file located in Amazon S3 into Google Webmaster tools

If you hosted your rails website in Heroku, you are most properly using https://github.com/kjvarga/sitemap_generator gem to dynamically generate your sitemap file.

I won’t go into the details of how to use sitemap_generator, there are many website guide you for this, just google it.

Once you got your sitemap generated and uploaded to Amazon S3 successfully, you might want to track your sitemap in Google Webmaster Tools as well.

First, add your Amazon S3 site into Google Webmaster Tools. For example, http://s3.amazonaws.com/mybucket/sitemaps


Second, verify your Amazon S3 site by copy the googleXXXXX.html file into your bucket and give the permission accordingly.


Third, go into your Amazon S3 site in Google Webmaster Tools. Navigate to Site Configuration > Sitemaps, add the sitemap located in Amazon S3 for your Rails website.


Done! That All.

Google can accept “Cross-site submissions” for your sitemap.

http://googlewebmastercentral.blogspot.com/2007/10/dealing-with-sitemap-cross-submissions.html

Wednesday, February 22, 2012

How to merge multiple reports into a single report in JasperReports


import java.io.FileOutputStream;

import java.util.ArrayList;

import java.util.HashMap;

import net.sf.jasperreports.engine.JREmptyDataSource;

import net.sf.jasperreports.engine.JRExporter;

import net.sf.jasperreports.engine.JRParameter;

import net.sf.jasperreports.engine.JRPrintPage;

import net.sf.jasperreports.engine.JasperFillManager;

import net.sf.jasperreports.engine.JasperPrint;

import net.sf.jasperreports.engine.JasperReport;

import net.sf.jasperreports.engine.export.JRPdfExporter;

import net.sf.jasperreports.engine.export.JRPdfExporterParameter;

import net.sf.jasperreports.engine.fill.JRFileVirtualizer;

import net.sf.jasperreports.engine.util.JRLoader;

public class JasperReportMerge {

ArrayList reportsList = new ArrayList();

HashMap params = new HashMap();

JasperPrint jrPrint = new JasperPrint();

public void buildReport() throws Exception {

String templateFile = "C://Testing//MergeReportTemplate.jasper";

JasperReport jrReport = (JasperReport) JRLoader.loadObject(templateFile);

JRFileVirtualizer virtualizer = null;

JasperPrint jp = null;

for(int i=0; i<5; i++) {

virtualizer = new JRFileVirtualizer(2, "C://tmp");

virtualizer.setReadOnly(true);

params.put(JRParameter.REPORT_VIRTUALIZER, virtualizer);

// Create dummy data for the each report

params.put("name", "Hello " + i);

params.put("ic", "IC " + i);

params.put("address", i + " Address");

jp = JasperFillManager.fillReport(jrReport, params, new JREmptyDataSource());

reportsList.add(jp); // adding the report into list

// Clean the virtualizer.

// If you didnt clean it, you might encounter error such as

// "The virtualizer is used by more contexts than its in-memory cache size XXX"

virtualizer.cleanup();

}

if (reportsList != null && reportsList.size() > 0) {

jrPrint = mergeReport(); // merge the report

} else {

jrPrint = JasperFillManager.fillReport(jrReport, params, new JREmptyDataSource());

}

exportReport();

}

protected JasperPrint mergeReport() throws Exception {

JasperPrint jrPrint = new JasperPrint();

JasperPrint jp = null;

jp = (JasperPrint) reportsList.get(0);

// set the report layout

jrPrint.setOrientation(jp.getOrientation());

jrPrint.setLocaleCode(jp.getLocaleCode());

jrPrint.setPageHeight(jp.getPageHeight());

jrPrint.setPageWidth(jp.getPageWidth());

jrPrint.setTimeZoneId(jp.getTimeZoneId());

jrPrint.setName(jp.getName());

// get each report and merge it 1 by 1

for(int i=0; i<reportsList.size(); i++) {

jp = (JasperPrint) reportsList.get(i);

ArrayList list = (ArrayList) jp.getPages();

// Merging the reports into 1 single report

for(int j=0; j

jrPrint.addPage((JRPrintPage) list.get(j));

}

}

return jrPrint;

}

protected void exportReport() throws Exception {

FileOutputStream fileOut = new FileOutputStream("C://Testing//MergeReportTemplate.pdf");

JRExporter exporter = new JRPdfExporter();

exporter.setParameter(JRPdfExporterParameter.FORCE_LINEBREAK_POLICY, Boolean.TRUE);

exporter.setParameter(JRPdfExporterParameter.OUTPUT_STREAM, fileOut);

exporter.setParameter(JRPdfExporterParameter.JASPER_PRINT, jrPrint);

System.out.println("Exporting...");

exporter.exportReport();

fileOut.flush();

fileOut.close();

}

public static void main(String[] args) throws Exception {

JasperReportMerge jem = new JasperReportMerge();

jem.buildReport();

System.out.println("Done");

}

}