package net.sourceforge.rtf.usecases.map;

import java.io.File;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

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

/**
 * Test with HashMap like context. Description :
 * 
 * @version 1.0.0
 * @author <a href="mailto:angelo.zerr@gmail.com">Angelo ZERR</a>
 * 
 */
public class RTFTemplateWithMapWithVelocity extends AbstractRTFUseCase {

    public RTFTemplateWithMapWithVelocity(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());
        // Instead defining JavaBean with getter
        // you can use HashMap
        Map projectMap = new HashMap();
        // Upper case for Name (it's the same thing like project.getName()
        projectMap.put("Name", "Jakarta Velocity"); // => in the model
                                                    // $project.Name
        projectMap.put("name2", "Jakarta Velocity (2)"); // => in the model
                                                            // $project.name2
        context.put("project", projectMap);

        // context.put("project", new Project("Jakarta Velocity"));

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

    }

    public static void main(String[] args) throws Exception {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplateWithMapWithVelocity...");
        String outDirectory = baseDirectory + "usecases/out/map";
        // 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/map/map.rtf";
        RTFTemplateWithMapWithVelocity usecase = new RTFTemplateWithMapWithVelocity(
                outDirectory);
        /**
         * Set Velocity as RTFTemplate implementation
         * 
         */
        // usecase.setRtfTemplateImpl(RTFTemplateHelper.DEFAULT_VELOCITY_RTFTEMPLATE);
        // => this line is
        // not required, because by default velocity is the default RTFTemplate
        // implementation
        usecase.saveTransformedDocument(true); // Save RTF file with velocity
        // macro
        usecase.run(rtfSource);

        logger.logTitle("GENERATE RTF VELOCITY AND TARGET FILES INTO : "
                + outDirectory);

        // Save XML fields into file with update
        String xmlFieldsAvailable = outDirectory
                + "/jakartavelocityproject.fields.xml";
        usecase.saveXmlFields(xmlFieldsAvailable, false);
        logger.logTitle("GENERATE XML FIELDS AVAIALABLE : "
                + xmlFieldsAvailable);
        logger.logBody(usecase.getXMLFields());

        logger.logTitle("End RTFTemplateWithMapWithVelocity...");

    }

}
