package net.sourceforge.rtf.usecases.config;

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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;

public class RTFTemplateWithSpringConfig {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplateWithSpringConfig...");
        try {
            String outDirectory = baseDirectory + "usecases/out/config";
            // Create out Directory
            File out = new File(outDirectory);
            out.mkdirs();            
            
            // Load rtftemplate-config Spring configuration
            String key = 
                "net/sourceforge/rtf/usecases/config/rtftemplate-config.xml";
            ApplicationContext applicationContext = new ClassPathXmlApplicationContext(key);
            
            // 1. Get RTFtemplateBuilder with spring config
            RTFTemplateBuilder builder = RTFTemplateBuilder.newRTFTemplateBuilder(key, applicationContext);            
            
            // 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 + "/spring_config.rtf";
            rtfTemplate
                    .merge(rtfTarget);
            
            logger.logTitle("GENERATE RTF TARGET FILE : " + rtfTarget);
            logger.logTitle("End RTFTemplateWithSpringConfig...");
        } catch (Exception e) {
            logger.logTitle("Error while using RTFTemplate with RTFTemplateWithSpringConfig : "
                    + e.getMessage());
        }

    }

}
