package net.sourceforge.rtf.usecases.page;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
import net.sourceforge.rtf.template.IContext;
import net.sourceforge.rtf.usecases.AbstractRTFUseCase;
import net.sourceforge.rtf.usecases.model.Dependency;
import net.sourceforge.rtf.usecases.model.Developer;
import net.sourceforge.rtf.usecases.model.Project;
import net.sourceforge.rtf.usecases.util.UseCaseLog;
import net.sourceforge.rtf.usecases.util.UseCaseUtil;

public class RTFTemplatePageBreakWithFreemarker extends AbstractRTFUseCase {

    public RTFTemplatePageBreakWithFreemarker(String outDirectory) {
        super(outDirectory);
    }
    
    protected void putDefaultFormat(RTFTemplate rtfTemplate) {
        /*
         * Set any common formatting objects on the engine - these will be passed through to each template it creates
         */
        rtfTemplate.setDefaultFormat(Date.class, DateFormat.getDateInstance());
    }
    
    protected void putContext(IContext context) {
        /*
         * Context of simply POJO
         */
        context.put("date", new Date());
        
        Project project = new Project("Jakarta Velocity");
        
        try {
            final InputStream in = new FileInputStream(new File("usecases/maven-button-1.png"));
            project.setLogo(in);
        }
        catch (Exception e) {
            
        }

        context.put("project", project);
        context.put("header_developer_name", "Name");
        context.put("header_developer_email", "Email");
        context.put("header_developer_roles", "Roles");
        /*
         * Context of list of POJO (Developers and Dependencies)
         */
        List developers = new ArrayList();
        Developer developer = new Developer("Will Glass-Husain", "wglass@apache.org");
        developer.addRole("Java Developer");
        developer.addRole("Release Manager");
        developers.add(developer);
        developer = new Developer("Geir Magnusson Jr.", "geirm@apache.org");
        developer.addRole("Java Developer");
        developer.addRole("Release Manager");
        developers.add(developer);
        developer = new Developer("Daniel L. Rall", "dlr@finemaltcoding.comg");
        developer.addRole("Java Developer");
        developer.addRole("Release Manager");
        developers.add(developer);
        context.put("developers", developers );
        
        List dependencies = new ArrayList();
        Dependency dependency = new Dependency("commons-collection", "jar", "1.0", "http://jakarta.apache.org/commons/collection/");
        dependencies.add(dependency);
        dependency = new Dependency("logkit", "jar", "1.0.1", "http://logkit");
        dependencies.add(dependency);
        dependency = new Dependency("oro", "jar", "2.0.8", "http://jakarta.apache.org/oro/");
        dependencies.add(dependency);
        
        context.put("dependencies", dependencies );
    }
    
    /**
     * Use case of RTFTemplate with the RTF source model <b>jakarta-velocity-model.rtf</b>.
     * @param args
     */
    public static void main( String[] args ) throws Exception {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplatePageBreak...");
        String outDirectory = baseDirectory + "usecases/out/pagebreak";
        // Create out Directory
        File out = new File(outDirectory);
        out.mkdir();
        
        // Generate RTF file by using RTF model jakarta-velocity-model.rtf
        String rtfSource = baseDirectory + "usecases/models/page/pagebreak_model.rtf";
        RTFTemplatePageBreakWithFreemarker usecase = new RTFTemplatePageBreakWithFreemarker(outDirectory);
        /**
         * Set Freemarker as RTFTemplate implementation
         */        
        usecase.setRtfTemplateImpl(RTFTemplateBuilder.DEFAULT_FREEMARKER_RTFTEMPLATE);          
        usecase.saveTransformedDocument(true); // Save RTF file with freemarker macro
        /**
         * Group by iteration per 1 list of developer group.
         * After 3 iteration, RTFTemplate generate Page Break.
         */
        usecase.setGroupByPerPageBreak(1);  
        usecase.run(rtfSource);
        
        logger.logTitle("GENERATE RTF VELOCITY AND TARGET FILES INTO : " + outDirectory);
        
        // Save XML fields into file with update 
        String xmlFieldsAvailable = outDirectory + "/pagebreak.fields.xml";
        usecase.saveXmlFields(xmlFieldsAvailable ,false);
        logger.logTitle("GENERATE XML FIELDS AVAIALABLE : " + xmlFieldsAvailable);
        logger.logBody(usecase.getXMLFields());
        
        logger.logTitle("End RTFTemplatePageBreak...");
        
    }

}
