package net.sourceforge.rtf.usecases.cache;

import java.io.File;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sourceforge.rtf.RTFTemplate;
import net.sourceforge.rtf.document.RTFDocument;
import net.sourceforge.rtf.helper.RTFTemplateBuilder;
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 RTFTemplateWithCache {

    private static Map transformedDocumentMap = new HashMap();
    
    /**
     * @param args
     */
    public static void main(String[] args) throws Exception {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplateWithCache...");
        try {
            String outDirectory = baseDirectory + "usecases/out/cache";
            // Create out Directory
            File out = new File(outDirectory);
            out.mkdirs();            
            
            String rtfSource = baseDirectory + "usecases/models/jakartavelocityproject/jakarta-velocity-model.rtf";
            /**
             * Execute transformed RTFDocument
             * and put it the instance of transformed RTFDocument
             * into the Map (into cache)
             */
            executeTransformedDocument(rtfSource);
            
            // 1. Get default RTFtemplateBuilder
            RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            
            
            // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
            RTFTemplate rtfTemplate = builder.newRTFTemplate();   
            
            // 3. Get the transformed document
            // and NOT the rtf source
            RTFDocument transformedDocument =  (RTFDocument)transformedDocumentMap.get(rtfSource); 
            rtfTemplate.setTransformedDocument(transformedDocument);
            
            // 4. Put the context           
            rtfTemplate.put("header_developer_name", "Name");
            rtfTemplate.put("header_developer_email", "Email");
            rtfTemplate.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);
            rtfTemplate.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);

            rtfTemplate.put("dependencies", dependencies);
            rtfTemplate.put("project", new Project("Jakarta Velocity project"));
            
            // 5. Merge the RTF sourec model and the context
            String rtfTarget = outDirectory + "/cache.rtf";
            rtfTemplate
                    .merge(rtfTarget);
            
            logger.logTitle("GENERATE RTF TARGET FILE : " + rtfTarget);
            logger.logTitle("End RTFTemplateWithCache...");
        } catch (Exception e) {
            logger.logTitle("Error while using RTFTemplate with Cache : "
                    + e.getMessage());
        }
    }
    
    private static void executeTransformedDocument(String rtfSourceFile) throws Exception {
        // 1. Get default RTFtemplateBuilder
        RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            
        
        // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
        RTFTemplate rtfTemplate = builder.newRTFTemplate();   
        
        // 3 Load XML fields available and set it to the RTFTemplate
        InputStream xmlFieldsStream = RTFTemplateWithCache.class.getResourceAsStream("jakartavelocityproject.fields.xml");
        rtfTemplate.setXmlFields(xmlFieldsStream);
        
        // 3. Set the RTF model source 
        rtfTemplate.setTemplate(new File(rtfSourceFile));  
        
        // 4. Transform to get transformed RTFDocument
        RTFDocument transformedDocument = rtfTemplate.transform();
        
        // 5. Cache the transformed document into map
        transformedDocumentMap.put(rtfSourceFile, transformedDocument);
    }

}
