package examples.report;

import java.io.IOException;

import de.vc.jfo.importer.ImportParameters;
import de.vc.jfo.renderer.OutputFormat;
import de.vc.jfo.renderer.Renderer;
import de.vc.jfo.renderer.RendererFactory;
import de.vc.jfo.report.FOReportBuilder;
import de.vc.jfo.tools.FontMapper;
import de.vc.report.DataSource;
import de.vc.report.ReportEngine;
import de.vc.report.ReportStorage;
import de.vc.report.sources.csv.CSVDataSource;
import de.vc.report.sources.csv.CSVTableModel;
import de.vc.report.storages.FileStorage;

/**
 * This example shows you how to use CSV as a data source.
 */

public class ReportCSVSource {

  private final static String RTF_FILE  = "docs-in/report/music-template.rtf";
  private final static String CSV_FILE_CD  = "docs-in/report/music-ds-cd.csv";
  private final static String CSV_FILE_TRACKS  = "docs-in/report/music-ds-tracks.csv";
  private final static String REPORT_DIR  = "docs-out/report/reports";
  private final static String IMAGE_DIR = "docs-out/report/images";


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

    // Create a data-source that delivers data for each report
    DataSource ds = createDataSource();

    // Where to put generated reports...
    ReportStorage storage = new FileStorage( REPORT_DIR, "music-", "-csv");

    // Our report-engine is not XSL-FO specific, so we need a
    // special report builder used by our report-engine.
    FOReportBuilder reportBuilder = new FOReportBuilder(
        RTF_FILE,
        getParameters()
    );

    // Or render to any other format
    OutputFormat format = OutputFormat.PDF;
    Renderer r = RendererFactory.instance().getRenderer(format);

    if (r!=null)
      reportBuilder.setOutputFormat( format, r );
    else
      System.out.println("No renderer found for "+format+". Is FOP in classpath?");


    // Now we can create the engine and run it.
    ReportEngine engine = new ReportEngine( ds, reportBuilder, storage);
    engine.run();

    // That's all. :o)
  }




  private static DataSource createDataSource() throws IOException{
    // Column-names for CD CSV
    String[] csvColumns = new String[]{
      "id","cd-artist","cd-title","cd-year","cd-label"};

    // Create CSV datasource
    CSVDataSource source = new CSVDataSource(
        CSV_FILE_CD,  // Location of CSV
        csvColumns    // Name of columns (FILL-IN names in RTF)
    );

    // Column-names for tracks of CDs
    String[] csvTableColumns = new String[]{
      "cd-id","track-title","track-length"
    };
    // Create table model for tracks of cd
    CSVTableModel cdTable = new CSVTableModel(
        CSV_FILE_TRACKS,   // Location of CSV
        csvTableColumns,   // Name of columns (FILL-IN names in RTF table)
        0,                 // Column number of reference to data source csv ("cd-id")
        "id"               // Name of referenced column in datasource
    );

    // Add table model to datasource
    source.addTableModel(cdTable);
    return source;
  }




  /**
   * Create parameters for RTF import (used by FOReportBuilder).
   * @return Import parameters.
   */
  private static ImportParameters getParameters(){
    ImportParameters params = new ImportParameters();

    // Path for images
    params.setImagePath(IMAGE_DIR);

    // If you don't set a font mapper, the font-mapping
    // in jfo-config file is used.
    // In case of any question about font-mapping please read the
    // user manual.
    FontMapper fm = new FontMapper();
    fm.addMappingRule("Times*", "Times");
    fm.addMappingRule("Arial*", "Helvetica");
    params.setFontMapper(fm);

    return params;
  }

}
