package net.sourceforge.rtf.usecases.page;

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

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.model.DeveloperGroup;
import net.sourceforge.rtf.usecases.util.UseCaseLog;
import net.sourceforge.rtf.usecases.util.UseCaseUtil;

public class RTFTemplatePageBeakWithGroupWithVelocity extends AbstractRTFUseCase {

    public RTFTemplatePageBeakWithGroupWithVelocity(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 list of POJO (Developers and Dependencies)
         */
        List developersGroup = new ArrayList();
        for (int i=0; i< 10; i++) {
            Developer developer1 = new Developer("NAME_1_" + i, "EMAIL_1_" + i);
            Developer developer2 = new Developer("NAME_2_" + i, "EMAIL_2_" + i);
            Developer developer3 = new Developer("NAME_3_" + i, "EMAIL_3_" + i);
            
            DeveloperGroup developerGroup = new DeveloperGroup(developer1, developer2, developer3);
            
            developersGroup.add(developerGroup);
            
        }
        
        context.put("developersGroup", developersGroup );
        
    }
    
    /**
     * Use case of RTFTemplate with the RTF source model <b>jakarta-velocity-model.rtf</b>.
     * @param args
     */
    public static void main( String[] args ) throws Exception {
        String baseDirectory = UseCaseUtil.getBaseDirectory(args);
        UseCaseLog logger = new UseCaseLog();
        logger.logTitle("Start RTFTemplatePageBeakWithGroup...");
        String outDirectory = baseDirectory + "usecases/out/page_with_group";
        // 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/page/pagebreak_model_with_group.rtf";
        RTFTemplatePageBeakWithGroupWithVelocity usecase = new RTFTemplatePageBeakWithGroupWithVelocity(outDirectory);
        usecase.saveTransformedDocument(true); // Save RTF file with velocity macro
        /**
         * Group by iteration per 3 list of developer group.
         * After 3 iteration, RTFTemplate generate Page Break.
         */
        usecase.setGroupByPerPageBreak(3);    
        usecase.run(rtfSource);
        
        logger.logTitle("GENERATE RTF VELOCITY AND TARGET FILES INTO : " + outDirectory);
        
        // Save XML fields into file with update 
        String xmlFieldsAvailable = outDirectory + "/pagebreak_model_with_group.fields.xml";
        usecase.saveXmlFields(xmlFieldsAvailable ,false);
        logger.logTitle("GENERATE XML FIELDS AVAIALABLE : " + xmlFieldsAvailable);
        logger.logBody(usecase.getXMLFields());
        
        logger.logTitle("End RTFTemplatePageBeakWithGroup...");
        
    }

}
