package net.sourceforge.rtf.usecases.strategies;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import net.sourceforge.rtf.RTFTemplate;
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;

/**
 * Use case of RTFTemplate, by using Reader strategy.
 * This example use RTFTemplate with Reader Strategy : 
 * NO RTF file with velocity macro is generated. 
 * RTF with velocity macro is Reader (RTFDocumentReader)
 * This example use RTF model
 * <b>usecases/models/jakartavelocityproject/jakarta-velocity-model.rtf</b>
 * RTF file target is generated into 
 * directory <b>usecases/out/reader-strategy</b>.
 * 
 * @version 1.0.0
 * @author <a href="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
 * 
 */
public class RTFTemplateReaderStrategy {

    /**
     * @param args the args.
     */
    public static void main(String[] args) {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplateReaderStrategy...");
        try {
            String outDirectory = baseDirectory + "usecases/out/cache";
            // Create out Directory
            File out = new File(outDirectory);
            out.mkdirs();            
            
            // 1. Get default RTFtemplateBuilder
            RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder();            
            
            // 2. Get RTFtemplate with default Implementation of template engine (Velocity) 
            RTFTemplate rtfTemplate = builder.newRTFTemplate();    
            
            // 3. Set the RTF model source 
            rtfTemplate.setTemplate(new File(
                    baseDirectory + "usecases/models/jakartavelocityproject/jakarta-velocity-model.rtf"));  
            
            // 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_while_merging.rtf";
            rtfTemplate
                    .merge(rtfTarget);
            
            logger.logTitle("GENERATE RTF TARGET FILE : " + rtfTarget);
            logger.logTitle("End RTFTemplateReaderStrategy...");
        } catch (Exception e) {
            logger.logTitle("Error while using RTFTemplate with Reader strategy : "
                    + e.getMessage());
        }

    }


}
