package net.sourceforge.rtf.usecases.bookmark;

import java.io.File;
import java.text.DateFormat;
import java.util.Date;

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

public class RTFTemplateWithBookmarkImbricated extends AbstractRTFUseCase {

    
    public RTFTemplateWithBookmarkImbricated(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) {
    }
    
    /**
     * Use case of RTFTemplate with the RTF source model <b>jakarta-velocity-model.rtf</b>.
     * It generate three files into directory <b>usecases/out/jakartavelocityproject</b> : 
     * <ul>
     *  <li><b>jakarta-velocity-model.rtf.velocity.rtf</b> RTF with velocity macro.</li>
     * </ul>
     * <ul>
     *  <li><b>jakarta-velocity-model.rtf.out.rtf</b> RTF final with value of the context.</li>
     * </ul>
     * <ul>
     *  <li><b>jakarta-velocity.fields.xml</b> XML fields available.</li>
     * </ul>
     * @param args the args.
     * @throws Exception the exception.
     */
    public static void main( String[] args ) throws Exception {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplateWithBookmarkImbricated...");
        String outDirectory = baseDirectory + "usecases/out/jakartavelocityproject";
        // 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/jakartavelocityproject/bookmark-imbricated.rtf";
        RTFTemplateWithBookmarkImbricated usecase = new RTFTemplateWithBookmarkImbricated(outDirectory);
        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 + "/explanation_model.fields.xml";
        usecase.saveXmlFields(xmlFieldsAvailable ,false);
        logger.logTitle("GENERATE XML FIELDS AVAIALABLE : " + xmlFieldsAvailable);
        logger.logBody(usecase.getXMLFields());
        
        logger.logTitle("End RTFTemplateWithBookmarkImbricated...");
        
    }
}
