package com.holmsted.gerrit.processors.perperson;

import com.holmsted.gerrit.Commit;
import com.holmsted.gerrit.OutputRules;
import com.holmsted.gerrit.processors.CommitDataProcessor;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;
import org.apache.velocity.context.Context;
import org.apache.velocity.runtime.RuntimeConstants;
import org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader;

import java.io.File;
import java.io.IOError;
import java.io.IOException;
import java.io.StringWriter;
import java.util.List;

import javax.annotation.Nonnull;

import file.FileWriter;

class PerPersonHtmlFormatter implements CommitDataProcessor.OutputFormatter<PerPersonData> {
    private static final String DEFAULT_OUTPUT_DIR = "out";
    private static final String TEMPLATES_RES_PATH = "templates";
    private static final String VM_PERSON_PROFILE = TEMPLATES_RES_PATH + File.separator + "person_profile.vm";
    private OutputRules outputRules;

    public PerPersonHtmlFormatter(@Nonnull OutputRules outputRules) {
        this.outputRules = outputRules;
    }

    @Override
    public void format(@Nonnull PerPersonData data) {
        File outputDir = new File(DEFAULT_OUTPUT_DIR);
        if (!outputDir.exists() && !outputDir.mkdirs()) {
            throw new IOError(new IOException("Cannot create output directory " + outputDir.getAbsolutePath()));
        }
        VelocityEngine velocity = new VelocityEngine();
        velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
        velocity.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
        velocity.init();

        List<IdentityRecord> orderedList = data.toOrderedList(new AlphabeticalOrderComparator());

        for (IdentityRecord record : orderedList) {
            String outputFilename = getOutputFilenameForIdentity(record.identity);
            System.out.println("Creating " + outputFilename);

            Context context = new VelocityContext();
            context.put("outputRules", outputRules);
            context.put("identity", record.identity);
            context.put("record", record);
            context.put("Gerrit", GerritUtils.class);

            StringWriter writer = new StringWriter();
            velocity.mergeTemplate(VM_PERSON_PROFILE, "UTF-8", context, writer);

            FileWriter.writeFile(outputDir.getPath() + File.separator + outputFilename, writer.toString());
        }

        System.out.println("Output written to " + outputDir.getAbsolutePath());
    }

    private static String getOutputFilenameForIdentity(@Nonnull Commit.Identity identity) {
        return identity.username + ".html";
    }
}

@@ -975,142 +975,583 @@
 ate 
-OutputRules outputRules;%0A%0A    public PerPersonHtmlFormatter(@Nonnull OutputRules outputRules) %7B%0A        this.outputRules = outputRules
+static final String VM_INDEX = TEMPLATES_RES_PATH + File.separator + %22index.vm%22;%0A%0A    private OutputRules outputRules;%0A    private VelocityEngine velocity = new VelocityEngine();%0A    private File outputDir;%0A%0A    public PerPersonHtmlFormatter(@Nonnull OutputRules outputRules) %7B%0A        this.outputRules = outputRules;%0A        velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, %22classpath%22);%0A        velocity.setProperty(%22classpath.resource.loader.class%22, ClasspathResourceLoader.class.getName());%0A        velocity.init();%0A%0A        outputDir = new File(DEFAULT_OUTPUT_DIR)
 ;%0A  
@@ -1627,63 +1627,8 @@
 ) %7B%0A
-        File outputDir = new File(DEFAULT_OUTPUT_DIR);%0A
     
@@ -1804,24 +1804,25 @@
       %7D%0A
+%0A
         
 Velocity
@@ -1817,64 +1817,107 @@
     
-VelocityEngine velocity = new VelocityEngine(
+List%3CIdentityRecord%3E orderedList = data.toOrderedList(new AlphabeticalOrderComparator()
 );%0A
+%0A
         
 velo
@@ -1916,85 +1916,52 @@
     
-velocity.setProperty(RuntimeConstants.RESOURCE_LOADER, %22classpath%22
+createPerPersonFiles(orderedList
 );%0A
+%0A
         
 velo
@@ -1960,101 +1960,75 @@
     
-velocity.setProperty(%22classpath.resource.loader.class%22, ClasspathResourceLoader.class.getName
+System.out.println(%22Output written to %22 + outputDir.getAbsolutePath
 ());
@@ -2036,128 +2036,93 @@
     
+%7D%0A%0A
     
-velocity.init();%0A%0A        List%3CIdentityRecord%3E orderedList = data.toOrderedList(new AlphabeticalOrderComparator());%0A
+private void createPerPersonFiles(@Nonnull List%3CIdentityRecord%3E orderedList) %7B
 %0A   
@@ -2570,32 +2570,179 @@
 ls.class);%0A%0A    
+        writeTemplate(context, outputFilename);%0A        %7D%0A    %7D%0A%0A    private void writeTemplate(@Nonnull Context context, String outputFilename) %7B%0A
         StringWr
@@ -2771,28 +2771,24 @@
 ngWriter();%0A
-    
         velo
@@ -2852,28 +2852,24 @@
 riter);%0A
-%0A   
         
  FileWri
@@ -2860,17 +2860,16 @@
         
- 
 FileWrit
@@ -2960,99 +2960,8 @@
 ));%0A
-        %7D%0A%0A        System.out.println(%22Output written to %22 + outputDir.getAbsolutePath());%0A
     

Refactor Html formatter to support multiple pages