package examples.report;

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.ReportData;
import de.vc.report.ReportEngine;
import de.vc.report.ReportStorage;
import de.vc.report.sources.ListDataSource;
import de.vc.report.sources.ListTableModel;
import de.vc.report.sources.MapTableModel;
import de.vc.report.storages.FileStorage;

/**
 * This example shows you how to create reports from a RTF template using
 * the report-engine of jFO and a java-datasource.
 */
public class ReportJavaSource {

  private final static String RTF_FILE  = "docs-in/report/music-template.rtf";
  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-", "-java");
    storage = new WatermarkReportStorage(storage);

    // 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();

  }


  private static DataSource createDataSource(){

    // Here we use a data source that stores all report data in a list.
    // Note, that if you have a lot of report-data the memory usage can
    // be very high. To avoid this you can create an own DataSource and
    // implement the deliverData-Methode.
    // Create data source
    ListDataSource source = new ListDataSource();

    // CD 1
    ReportData cd1Data = new ReportData();
    cd1Data.setValue("cd-artist", "Kraftwerk");
    cd1Data.setValue("cd-title", "Mensche-Maschine");
    cd1Data.setValue("cd-year", "1978");
    cd1Data.setValue("cd-label", "Klingklang");

    // CD 1 : Track-Table, using a list-model
    ListTableModel cd1Table = new ListTableModel(new String[]{"track-title","track-length"});
    cd1Table.addRow(new String[]{"Die Roboter","6:17"});
    cd1Table.addRow(new String[]{"Spacelab","5:56"});
    cd1Table.addRow(new String[]{"Metropolis","5:59"});
    cd1Table.addRow(new String[]{"Neonlicht","9:05"});
    cd1Table.addRow(new String[]{"Die Mensch-Maschine","6:25"});

    // Add table model to data of CD1
    cd1Data.addTableModel(cd1Table);


    // CD 2
    ReportData cd2Data = new ReportData();
    cd2Data.setValue("cd-artist", "Enigma");
    cd2Data.setValue("cd-title", "The screen behind the mirror");
    cd2Data.setValue("cd-year", "2000");
    cd2Data.setValue("cd-label", "Virgin");

    // CD 2 : Track-Table, using a map-model
    MapTableModel cd2Table = new MapTableModel();
    cd2Table.setCell("track-title","The Gate");
    cd2Table.setCell("track-length","2:03");
    cd2Table.commitRow();
    cd2Table.setCell("track-title","Push the limits");
    cd2Table.setCell("track-length","6:27");
    cd2Table.commitRow();
    cd2Table.setCell("track-title","Gravity of love");
    cd2Table.setCell("track-length","4:01");
    cd2Table.commitRow();
    cd2Table.setCell("track-title", "...more...");
    cd2Table.setCell("track-length","..:..");
    cd2Table.commitRow();

    // Add table model to data of CD2
    cd2Data.addTableModel(cd2Table);

    // Add data to datasource
    source.add(cd1Data);
    source.add(cd2Data);

    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;
  }

}
